/************** OPTIONAL EDIT BELOW THIS LINE  ***********************/
/************** User specified settings *******************/
// Validation settings
var minLength = 8;             // Minimum length of password
var maxLength = 12;            // Maximum length of password
var noSpecialChars = true;     // Sets if special characters (punctuation etc.) can be in password
var isPasswordRequired = true;  // Sets if the password is a required field
var showTip = true;             // Show a tip to users if their password is not perfect

// Custom strings for personalisation or i18n
var strRequired = "Required";     // Displays when nothing is entered & password is required
var strTooShort = "Password too short";   // Displays when password is less than minLength 
var strTooLong = "Password too long";      // Displays when password is too long
var strSpecialChars = "Special characters not allowed";     // Displays when user enters special chars
var strWeak = "Password is weak!";       // Displays when password is weak strength
var strMedium = "Password could be better";   // Displays when password is medium strength
var strStrong = "Perfect password!";          // Displays when password is perfect

// UI settings
var BackgroundColor = "#FFFFFF";     // Background color of validator 
var TextColor = "#FF0000";           // Text color of validator 
var TextFontFamily = "Verdana, Arial, Helvetica, sans-serif"; // Font Family
var TextSize = "9";               // Text font size
var TextBold = true;              // Is text bold?


/*************** End of user specified settings **********/
/*************** DO NOT EDIT BELOW THIS LINE ****************/


var tip = 'Tips on creating the right password\\n1.Should be between '+minLength+' and '+maxLength+' characters \\n2.Should not be a word from the common dictionary. These passwords are easiest to guess!\\n3.Should have atleast one uppercase letter, one lowercase letter and one digit.';

/************** Create the validator **************/
function createPasswordValidator(elementToValidate)
	{	
		// Initialise display
		var validatorStyle = '<style type="text/css"> .pwdvalid { color:'+TextColor+'; font-family:'+TextFontFamily+'; font-size:'+TextSize+';';
		if(TextBold)
			validatorStyle += 'font-weight: bold;';
		validatorStyle +='}</style>';
		document.write(validatorStyle);
		
		// Get the element to validate
		var elm;
		if(!(elm = document.getElementById(elementToValidate)))
		{
			alert('Password Validator could not find your password field identified by id='+elementToValidate);
			return;
		}
		
		// Create visual output
		var output = '<div id="_pwdvalid'+elementToValidate+'" class="pwdvalid">&nbsp;</div>';
		document.write(output);
		
		// Register event handlers
		// Use quirksmode idea for flexible registration by copying existing events
		// onKeyUp
		var oldEventCode = (elm.onkeyup) ? elm.onkeyup : function () {};
		elm.onkeyup = function () {oldEventCode(); validatePassword(elm.id)};
		// onmouseout
		oldEventCode = (elm.onmouseout) ? elm.onmouseout : function () {};
		elm.onmouseout = function() {oldEventCode(); validatePassword(elm.id)};		
	}
	
function validatePassword(elementToValidate) 
	{
		var elm;
		if(!(elm = document.getElementById(elementToValidate)))
		{
			return;
		}
		var passwordDiv = document.getElementById("_pwdvalid"+elementToValidate);
		var passwordString = elm.value;
		if(passwordString.length == 0)
		{
			passwordDiv.innerHTML = strRequired;
			return;
		}
		if(passwordString.length < minLength)
		{
			passwordDiv.innerHTML = strTooShort;
			return;
		}
		if(passwordString.length > maxLength)
		{
			passwordDiv.innerHTML = strTooLong;
			return;
		}
		// Match special characters
		if(passwordString.match(/\W/))
		{
			passwordDiv.innerHTML = strSpecialChars;
			elm.value='';
			return;
		}			
		var strength = 0;
		// Match upper case characters
		if(passwordString.match(/[a-z]/))
		{
			strength++;
		}
		// Match lower case characters
		if(passwordString.match(/[A-Z]/))
		{
			strength++;
		}
		// Match digits
		if(passwordString.match(/\d/))
		{
			strength++;
		}		
		switch(strength)
		{
			case 1: passwordDiv.innerHTML = strWeak;
					displayTip(passwordDiv);
					break;
			case 2: passwordDiv.innerHTML = strMedium;
					displayTip(passwordDiv);
					break;
			case 3: passwordDiv.innerHTML = strStrong;
					break;
		}				
	}
		
	function displayTip(div)
	{		
		// Show tip
		if(showTip)		
			div.innerHTML += '&nbsp;'+'<a href="javascript:alert(\''+tip+'\');" class="bodytextred2">Tip</a>';
	}

							
							 
		
	