// Password strength meter v2.0
// Matthew R. Miller - 2007
// www.codeandcoffee.com
// Based off of code from:
//  http://www.intelligent-web.co.uk
//  http://www.geekwisdom.com/dyn/passwdmeter

/*
	Password Strength Algorithm:
	
		
*/


// Settings
// -- Toggle to true or false, if you want to change what is checked in the password
var m_strUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var m_strLowerCase = "abcdefghijklmnopqrstuvwxyz";
var m_strNumber = "0123456789";
var m_strCharacters = "!@#$%^&*?_~,./<>?-=_+()[]{};:'`|\\\""

// Check password
function checkPassword(strPassword)
{
	// Reset combination count
	var nScore = 0;
	
	// special chars
	var nCharacterCount = countContain(strPassword, m_strCharacters);

	// Letters
	var nUpperCount = countContain(strPassword, m_strUpperCase);
	var nLowerCount = countContain(strPassword, m_strLowerCase);
	var nLowerUpperCount = nUpperCount + nLowerCount;

	// numbers
	var nNumberCount = countContain(strPassword, m_strNumber);


	// weak but acceptable
	if ( strPassword.length >= 6 && nLowerUpperCount >= 1 && nNumberCount >= 1 & nCharacterCount >= 1 ) {
		nScore += 10;
	}
	if ( strPassword.length >= 6 && nLowerUpperCount >= 1 && nNumberCount >= 1 && ( nUpperCount > 0 && nLowerCount > 0 ) && nCharacterCount >= 1 )  {
		nScore += 15;
	}
	// is acceptable and has either a special char or a mix of upper and lower case
	if ( strPassword.length >= 6 && nNumberCount >= 2 && ( nUpperCount > 0 && nLowerCount > 0 ) && nCharacterCount >= 1 ) {
		nScore += 20;
	}
	if ( strPassword.length >= 8 && nNumberCount >= 2 && ( nUpperCount > 0 && nLowerCount > 0 ) && nCharacterCount >= 2 ) {
		nScore += 25;
	}

	return nScore;
}
 
// Runs password through check and then updates GUI 
function runPassword(strPassword, strFieldID) 
{
	// Check password
	var nScore = checkPassword(strPassword);

	 // Get controls
    	var ctlBar = document.getElementById(strFieldID + "_bar"); 
    	var ctlText = document.getElementById(strFieldID + "_text");
    	if (!ctlBar || !ctlText)
    		return;
    	
	//document.pw_form.change_pass.disabled=false;


 	// Color and text
	// -- Very Secure
 	if (nScore >= 70)
 	{
 		var strText = "Very Secure";
 		var strColor = "#0ca908";
 	}
	// -- Strong
 	else if (nScore >= 45)
 	{
 		var strText = "Strong";
 		var strColor = "#5a74e3";
	}
	// -- Average
 	else if (nScore >= 25)
 	{
 		var strText = "Fair";
 		var strColor = "#FFC000";
	}
	// -- Weak
 	else if (nScore >= 10)
 	{
 		var strText = "Weak";
 		var strColor = "#ff0000";
	}
	// -- Very Weak
 	else if (nScore == 0 )
 	{
 		var strText = "Unacceptable";
 		var strColor = "#7d61a";

	//	document.pw_form.change_pass.disabled=true;
 	}

	// Set new width
	//ctlBar.style.width = nScore + "%";

	//ctlBar.style.backgroundColor = strColor;
	ctlText.innerHTML = "<span style='color: " + strColor + ";'>" + strText + ", score: " + nScore + "</span>";

	//    alert( "runPassword: " + strPassword + " " + strFieldID + " " + nScore);

}
 
 
// Runs password through check and then updates GUI 
function chkPassword(oldPwd, newPwd, strFieldID) 
{
	 // Get controls
    	var ctlBar = document.getElementById(strFieldID + "_bar"); 
    	var ctlText = document.getElementById(strFieldID + "_text");
    	if (!ctlBar || !ctlText)
    		return;

	if( newPwd.length == 0 || (oldPwd.substring( 0, newPwd.length) != newPwd))
	{
 		var strText = "Mismatch";
 		var strColor = "red";
 	}
 	else 
 	{
 		var strText = "Match";
 		var strColor = "#0ca908";
	}
	ctlText.innerHTML = "<span style='color: " + strColor + ";'>" + strText + "</span>";
}


// Checks a string for a list of characters
function countContain(strPassword, strCheck)
{ 
	// Declare variables
	var nCount = 0;
	
	for (i = 0; i < strPassword.length; i++) 
	{
		if (strCheck.indexOf(strPassword.charAt(i)) > -1) 
		{ 
	        	nCount++;
		} 
	} 
 
	return nCount; 
} 
