// Atwork Password Meter v.1.0
// 25.09.08
// The script is a combination of password stregthers from
// 	  Matthew R. Miller - 2007 www.codeandcoffee.com
// and Firas Kassem  phiras.wordpress.com || phiras at gmail {dot} com


// Settings
var minLength = 6

function passwordStrength(password,username)
{
    score = 0 
    
    //password  too short?
	if (password.length < minLength ) { return password.length } //too short
    
    //password == username
    if (password.toLowerCase()==username.toLowerCase()) return 10 //badPass
    
    //password length
    score += password.length * 4
    score += ( checkRepetition(1,password).length - password.length ) * 1
    score += ( checkRepetition(2,password).length - password.length ) * 1
    score += ( checkRepetition(3,password).length - password.length ) * 1
    score += ( checkRepetition(4,password).length - password.length ) * 1

    //password has 3 numbers
    if (password.match(/(.*[0-9].*[0-9].*[0-9])/))  score += 5 
    
    //password has 2 sybols
    if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5 
    
    //password has Upper and Lower chars
    if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  score += 10 
    
    //password has number and chars
    if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))  score += 15 
    //
    //password has number and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/))  score += 15 
    
    //password has char and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/))  score += 15 
    
    //password is just a nubers or chars
    if (password.match(/^\w+$/) || password.match(/^\d+$/) )  score -= 10 
    
    //verifing 0 < score < 100
    if ( score < 0 )  score = 0 
    if ( score > 100 )  score = 100 

	return score
}


// checkRepetition(1,'aaaaaaabcbc')   = 'abcbc'
// checkRepetition(2,'aaaaaaabcbc')   = 'aabc'
// checkRepetition(2,'aaaaaaabcdbcd') = 'aabcd'

function checkRepetition(pLen,str) {
    res = ""
    for ( i=0; i<str.length ; i++ ) {
        repeated=true
        for (j=0;j < pLen && (j+i+pLen) < str.length;j++)
            repeated=repeated && (str.charAt(j+i)==str.charAt(j+i+pLen))
        if (j<pLen) repeated=false
        if (repeated) {
            i+=pLen-1
            repeated=false
        }
        else {
            res+=str.charAt(i)
        }
    }
    return res
}
 
// Runs password through check and then updates GUI 
function runPassword() 
{
	// Get controls
	var strLogin = document.getElementById("login2").value;
	var strPassword = document.getElementById("pass2").value;
	
	var ctlBar = document.getElementById("pass2_bar"); 
	var ctlText = document.getElementById("pass2_text");
	if (!ctlBar || !ctlText)
		return;


	// Check password
	nPerc = passwordStrength(strPassword,strLogin);

	if (nPerc < minLength) {
 		strText = langTrans("register.pass.tooshort"); //"Too Short";
 		strColor = "gray";
 	} else if (nPerc < 34) {
 		strText = langTrans("register.pass.notsecure"); //"Not Secure";
 		strColor = "#FF6633";
 	} else if (nPerc < 68) {
 		strText = langTrans("register.pass.goodpass"); //"Good Pass";
 		strColor = "#3399cc";
	} else if (nPerc > 68) {
 		strText = langTrans("register.pass.strongpass"); //"Strong Pass";
 		strColor = "#009900";
 	}

	ctlBar.style.backgroundColor = strColor;
	ctlBar.style.width = nPerc + "%";
	ctlText.innerHTML = "<span style='color: " + strColor + ";'>" + strText + "</span>";
}
