/*
	Eddy's example from web
	Tidied and optimised by Martin Latter
*/

var m_strUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var m_strLowerCase = "abcdefghijklmnopqrstuvwxyz";
var m_strNumber = "0123456789";
var m_strCharacters = "!@#$%^&*?_~";

function checkPassword(strPassword) {

	var nScore = 0;
	var nNumberCount;
	
	if (strPassword.length < 5) {
		nScore += 5;
	}
	else if (strPassword.length > 4 && strPassword.length < 8) {
		nScore += 10;
	}
	else if (strPassword.length > 7) {
		nScore += 25;
	}

	var nUpperCount = countContain(strPassword, m_strUpperCase);
	var nLowerCount = countContain(strPassword, m_strLowerCase);
	var nLowerUpperCount = nUpperCount + nLowerCount;

	if (nUpperCount == 0 && nLowerCount != 0) { 
		nScore += 10; 
	}
	else if (nUpperCount != 0 && nLowerCount != 0) { 
		nScore += 20; 
	}

	nNumberCount = countContain(strPassword, m_strNumber);
	
	if (nNumberCount == 1) {
		nScore += 10;
	}

	if (nNumberCount >= 3) {
		nScore += 20;
	}

	var nCharacterCount = countContain(strPassword, m_strCharacters);
	
	if (nCharacterCount == 1) {
		nScore += 10;
	}	

	if (nCharacterCount > 1) {
		nScore += 25;
	}

	if (nNumberCount != 0 && nLowerUpperCount != 0) {
		nScore += 2;
	}

	if (nNumberCount != 0 && nLowerUpperCount != 0 && nCharacterCount != 0) {
		nScore += 3;
	}

	if (nNumberCount != 0 && nUpperCount != 0 && nLowerCount != 0 && nCharacterCount != 0) {
		nScore += 5;
	}

	return nScore;
}

function runPassword(strPassword, strFieldID) {

	var nScore = checkPassword(strPassword);
	var strField = document.getElementById(strFieldID);
    var ctlBar = document.getElementById(strFieldID + "_bar");
    ctlBar.style.visibility = "visible";
    var ctlText = document.getElementById(strFieldID + "_text");
 
	var strText = "";
 	var strColor = "";

	if (strField.value.length < 1) {
		ctlBar.style.background = "none";
		ctlText.innerHTML = "";
		return;
	}
 
    if (!ctlBar || !ctlText) {
		return;
	}

    ctlBar.style.width = (nScore * 0.44) + "%";  // adjust numeral to make bar smaller

 	/*if (nScore >= 90) {
 		strText = "Very Secure";
 		strColor = "#0ca908";
 	}
 	else if (nScore >= 80) {
 		strText = "Secure";
 		strColor = "#7ff67c";
	}*/

 	if (nScore >= 70) {
 		strText = "Very Strong";
 		strColor = "#fff";
	}
 	else if (nScore >= 60) {
 		strText = "Strong";
 		strColor = "#fff";
	}
 	else if (nScore >= 50) {
 		strText = "Average";
 		strColor = "#e3cb00";
	}
 	else if (nScore >= 25) {
 		strText = "Weak";
 		strColor = "#e7d61a";
	}
 	else {
 		strText = "Very Weak";
 		strColor = "#e7d61a";
	}

	ctlBar.style.backgroundColor = strColor;
	ctlText.innerHTML = "<span style='color: " + strColor + ";'>" + strText + " - " + nScore + "</span>";
}

function countContain(strPassword, strCheck) { 
	var nCount = 0;
	var strPassLen = strPassword.length;
	var i = 0;

	for (; i < strPassLen; i++) {
		if (strCheck.indexOf(strPassword.charAt(i)) > -1) { 
	        	nCount++;
		} 
	} 

	return nCount; 
} 

