// passwordtest strength meter
// This jQuery plugin is written by firas kassem [2007.04.05]
// Firas Kassem  phiras.wordpress.com || phiras at gmail {dot} com
// for more information : http://phiras.wordpress.com/2007/04/08/passwordtest-strength-meter-a-jquery-plugin/

var shortPass = '<div class=messageStackError>too short</div>'
var badPass = '<div class=messageStackError>weak password <A HREF="http://en.wikipedia.org/wiki/Password_strength" TARGET="_blank"><IMG SRC="images/icons/help.gif"></A></div>'
var goodPass = '<div class=messageStackSuccess>good password <A HREF="http://en.wikipedia.org/wiki/Password_strength" TARGET="_blank"><IMG SRC="images/icons/help.gif"></A></div>'
var strongPass = '<div class=messageStackSuccess>strong password <A HREF="http://en.wikipedia.org/wiki/Password_strength" TARGET="_blank"><IMG SRC="images/icons/help.gif"></A></div>'


function passwordStrength(passwordtest)
{
    score = 0 
    
    //passwordtest < 4
    if (passwordtest.length < 5 ) { return shortPass }
    
    
    
    //passwordtest length
    score += passwordtest.length * 4
    score += ( checkRepetition(1,passwordtest).length - passwordtest.length ) * 1
    score += ( checkRepetition(2,passwordtest).length - passwordtest.length ) * 1
    score += ( checkRepetition(3,passwordtest).length - passwordtest.length ) * 1
    score += ( checkRepetition(4,passwordtest).length - passwordtest.length ) * 1

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


// 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
}
