function randomPassword(length) {
	chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	pass = "";
	for(x=0;x<length;x++) {
		i = Math.floor(Math.random() * 62);
		pass += chars.charAt(i);
	}
	return pass;
}

function CompareString(str1, str2) {
		if (str1 != str2) {
			alert("The passwords entered do not match");
			return false;
		}
		return true;
	}

function PasswordStrength(strValue) {
	t = MM_findObj("PWS_Text");
	c1 = MM_findObj("PWS_Cell1");
	c2 = MM_findObj("PWS_Cell2");
	c3 = MM_findObj("PWS_Cell3");
	
	intScore = testPassword(strValue);

	if ((intScore > 3) && (intScore < 25)) {
		t.innerHTML = "Weak";
		c1.bgColor = "#FF0000";
		c2.bgColor = "#CCCCCC";
		c3.bgColor = "#CCCCCC";
	}
	else if ((intScore >= 25) && (intScore < 32)) {
		t.innerHTML = "Medium";
		c1.bgColor = "#FFFF00";
		c2.bgColor = "#FFFF00";
		c3.bgColor = "#CCCCCC";
	}
	else if (intScore >= 32) {
		t.innerHTML = "Strong";
		c1.bgColor = "#00FF00";
		c2.bgColor = "#00FF00";
		c3.bgColor = "#00FF00";
	}
	else {
		t.innerHTML = "Not rated";
		c1.bgColor = "#CCCCCC";
		c2.bgColor = "#CCCCCC";
		c3.bgColor = "#CCCCCC";
	}
}

function EnforcePasswordStrength(objPassword,objConfirmPassword) {
	intScore = testPassword(objPassword.value);
	if (intScore < 25) {
		alert('Your password does not meet the minimum complexity requirements!\nPasswords should be at least 7 characters in length and contain characters from at least 3 of these groups:\n  * Lowercase letters (a-z)\n  * Uppercase letters (A-Z)\n  * Numbers (0-9)\n  * Symbols (%,&,*,$, etc)');
		objPassword.value = '';
		objConfirmPassword.value = '';
		objPassword.focus();
		return false;
	}
	return true;
}


/* ************************************************************

Password Strength Factors and Weightings

password length:
level 0 (3 point): less than 4 characters
level 1 (6 points): between 5 and 7 characters
level 2 (12 points): between 8 and 15 characters
level 3 (18 points): 16 or more characters

letters:
level 0 (0 points): no letters
level 1 (5 points): all letters are lower case
level 2 (7 points): letters are mixed case

numbers:
level 0 (0 points): no numbers exist
level 1 (5 points): one number exists
level 1 (7 points): 3 or more numbers exists

special characters:
level 0 (0 points): no special characters
level 1 (5 points): one special character exists
level 2 (10 points): more than one special character exists

combinatons:
level 0 (1 points): letters and numbers exist
level 1 (1 points): mixed case letters
level 1 (2 points): letters, numbers and special characters 
					exist
level 1 (2 points): mixed case letters, numbers and special 
					characters exist

************************************************************ */
function testPassword(passwd)
{
		var intScore   = 0
		var strVerdict = "weak"
		var strLog     = ""
		
		// PASSWORD LENGTH
		if (passwd.length<5)                         // length 4 or less
		{
			intScore = (intScore+3)
			strLog   = strLog + "3 points for length (" + passwd.length + ")\n"
		}
		else if (passwd.length>4 && passwd.length<8) // length between 5 and 7
		{
			intScore = (intScore+6)
			strLog   = strLog + "6 points for length (" + passwd.length + ")\n"
		}
		else if (passwd.length>7 && passwd.length<16)// length between 8 and 15
		{
			intScore = (intScore+12)
			strLog   = strLog + "12 points for length (" + passwd.length + ")\n"
		}
		else if (passwd.length>15)                    // length 16 or more
		{
			intScore = (intScore+18)
			strLog   = strLog + "18 point for length (" + passwd.length + ")\n"
		}
		
		
		// LETTERS (Not exactly implemented as dictacted above because of my limited understanding of Regex)
		if (passwd.match(/[a-z]/))                              // [verified] at least one lower case letter
		{
			intScore = (intScore+1)
			strLog   = strLog + "1 point for at least one lower case char\n"
		}
		
		if (passwd.match(/[A-Z]/))                              // [verified] at least one upper case letter
		{
			intScore = (intScore+5)
			strLog   = strLog + "5 points for at least one upper case char\n"
		}
		
		// NUMBERS
		if (passwd.match(/\d+/))                                 // [verified] at least one number
		{
			intScore = (intScore+5)
			strLog   = strLog + "5 points for at least one number\n"
		}
		
		if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/))             // [verified] at least three numbers
		{
			intScore = (intScore+5)
			strLog   = strLog + "5 points for at least three numbers\n"
		}
		
		
		// SPECIAL CHAR
		if (passwd.match(/.[!,@,#,$,%,^,&,*,?,_,~]/))            // [verified] at least one special character
		{
			intScore = (intScore+5)
			strLog   = strLog + "5 points for at least one special char\n"
		}
		
																 // [verified] at least two special characters
		if (passwd.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/))
		{
			intScore = (intScore+5)
			strLog   = strLog + "5 points for at least two special chars\n"
		}
	
		
		// COMBOS
		if (passwd.match(/([a-z].+[A-Z])|([A-Z].+[a-z])/))        // [verified] both upper and lower case
		{
			intScore = (intScore+2)
			strLog   = strLog + "2 combo points for upper and lower letters\n"
		}

		if (passwd.match(/(\d.+\D)|(\D.+\d)/))                    // [FAILED] both letters and numbers, almost works because an additional character is required
		{
			intScore = (intScore+2)
			strLog   = strLog + "2 combo points for letters and numbers\n"
		}
 
																  // [verified] letters, numbers, and special characters
		if (passwd.match(/([a-zA-Z0-9].+[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].+[a-zA-Z0-9])/))
		{
			intScore = (intScore+2)
			strLog   = strLog + "2 combo points for letters, numbers and special chars\n"
		}
	
	
		if(intScore < 16)
		{
		   strVerdict = "very weak"
		}
		else if (intScore > 15 && intScore < 25)
		{
		   strVerdict = "weak"
		}
		else if (intScore > 24 && intScore < 35)
		{
		   strVerdict = "mediocre"
		}
		else if (intScore > 34 && intScore < 45)
		{
		   strVerdict = "strong"
		}
		else
		{
		   strVerdict = "stronger"
		}
	
	return intScore;
	
}