/*
	Password Strength Checker v 1.0
	Author:  Nitin Menon
	Email: 	 nitin@bigbuzz.com
	Date:	 11/3/2006 2:08 PM
	
	This script was  created with concepts from Steve Moitozo's "Javascript Password Strength Meter" 
	which can be found at http://www.geekwisdom.com/dyn/passwdmeter,
	and from the Google Password checker at https://www.google.com/accounts/NewAccount
	
	To use this file the globals.js file has be set up with the addEvent() in it.
	
	Calling function snippet: 
		
		var initPW = function(){ initPasswordStrength('Password'); }
		addEvent( window, 'load', initPW );
*/

ratingMsgs = new Array();

ratingMsgs[0] = "Too Short";
ratingMsgs[1] = "Weak";
ratingMsgs[2] = "Fair";
ratingMsgs[3] = "Good";
ratingMsgs[4] = "Strong";

barColors = new Array();

barColors[0] = "#dddddd";
barColors[1] = "#aa0033";
barColors[2] = "#ffcc33";
barColors[3] = "#6699cc";
barColors[4] = "#008000";

ratingMsgColors = new Array();

ratingMsgColors[0] = "#676767";
ratingMsgColors[1] = "#aa0033";
ratingMsgColors[2] = "#f5ac00";
ratingMsgColors[3] = "#6699cc";
ratingMsgColors[4] = "#008000";

function drawBar(rating,id) {
	var posbar = $('posBar_' + id);
	var passwdRating = $('pwstrth_' + id);
	var barLength = $('passwdBar_' + id).width;
	
	posbar.style.width = barLength / 4 * rating + "px";
	posbar.style.background = barColors[rating];
	passwdRating.innerHTML = "<span style='color:" + ratingMsgColors[rating] + "'>" + ratingMsgs[rating] + "</span>";
}

function PasswordStrength(element){
	var score = 0;
	var strength = "";
	var passwd = element.value;
	
	// length is less than 6
	if (passwd.length < 6){
		score = score + 0; 
		
	// length between 6 and 9	
	}else if (passwd.length >= 6 && passwd.length <= 9){
		score = score + 9; 
	
	// length greater than 9	
	}else if (passwd.length > 9){
		score = score + 15;
	}
		
	// at least one lower case letter	
	if (passwd.match(/[a-z]/)) score = score + 2;

	// at least one upper case letter
	if (passwd.match(/[A-Z]/)) score = score + 2;
		
	// at least one number	
	if (passwd.match(/\d+/)) score = score + 2; 
		
	// at least three numbers	
	if (passwd.match(/^[0-9]{3,}/)) score = score + 4; 
		
	// at least one special character	
	if (passwd.match(/[!@#$%^&*?_~]/)) score = score + 3; 
		
	// at least two special chars	
	if (passwd.match(/^[!@#$%^&*?_~]{2,}/)) score = score + 5;
	
	// both upper and lower case
	if (passwd.match(/^(?=.*[a-z])(?=.*[A-Z])/)) score = score + 4;
	
	 // both letters and numbers and greater than or equal to 6 characters
	if (passwd.match(/^((?=.*[a-zA-Z])(?=.*[0-9])).{6,}/)) score = score + 4;

	// letters, numbers, and special characters
	if (passwd.match(/^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[!@#$%^&*?_~])/)) score = score + 5; 
	
	if(score < 9){
		drawBar(0,element.id);
	}else if (score > 8 && score <= 15){
		drawBar(1,element.id);
	}else if (score > 15 && score <= 25){
		drawBar(2,element.id);
	}else if (score > 25 && score <= 35){
		drawBar(3,element.id);
	}else{
		drawBar(4,element.id);
	}
}

function initPasswordStrength(id){ 
	var element = $(id);

	if(!element){ 
		alert('An element with the id=' + id + ' could not be found. Please check your code.');
		return false;
	}else{
		// Add onkeyup event to this element
		addEvent( element, 'keyup', function(){ PasswordStrength(this); } ); 

		// Insert strength meter after the element 
		var meter = ' <br/> <table width=235" cellpadding="0" cellspacing="0"> \
								<tr> \
									<td style="padding-bottom:5px;"> \
										<small> \
											Password Strength: \
											<span id="pwstrth_' + id + '" style="font-weight:bold;"></span> \
										</small> \
									</td> \
								</tr> \
								<tr> \
									<td id="passwdBar_' + id + '" bgcolor="#e0e0e0" height="4" width="235"> \
										<table id="posBar_' + id + '" cellpadding="0" cellspacing="0"><tr><td height="4"></td></tr></div> \
									</td> \
								</tr>\
							</table>';		  
							
		var span = document.createElement("span"); 

		var parent = element.parentNode; 
		parent.insertBefore(span,element.nextSibling); 

		span.innerHTML = meter;
	}
}
