 jQuery.fn.passwordStrength = function( options ){
return this.each(function(){
var that = this;that.opts = {};
that.opts = jQuery.extend({}, jQuery.fn.passwordStrength.defaults, options);

that.div = jQuery(that.opts.targetDiv);
that.defaultClass = that.div.attr('class');

that.percents = (that.opts.classes.length) ? 100 / that.opts.classes.length : 100;

v = jQuery(this)
.keyup(function(){
if( typeof el == "undefined" )
this.el = jQuery(this);
var s = getPasswordStrength (this.value);
var p = this.percents;
var t = Math.floor( s / p );
if( 100 <= s )
t = this.opts.classes.length - 1;

this.div
.removeAttr('class')
.addClass( this.defaultClass )
.addClass( this.opts.classes[ t ] );
jQuery('#statues').removeAttr('class').html(this.opts.status[ t ]);
jQuery('#statues').addClass( this.opts.classes[ t ] );
if(this.value.length==0){
	this.div.removeClass(this.opts.classes[ t ])
	.addClass("is0")
	.html("");
	jQuery('#statues').html('');

}

})
});
}
function getPasswordStrength(password){
	//blank password
	   score = 0
	   if (password.length == 0 ) {return 0}
	   if (password.length < 1 ) {return 4}
	    //password < 4
	    if (password.length < 4 ) { return 14 }

	    //password == username

	    //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;
}



jQuery.fn.passwordStrength.defaults = {
classes : Array('is10','is20','is30','is40','is50','is60','is70','is80','is90','is100'),
status : Array(),
targetDiv : '#passwordStrengthDiv',
cache : {}
}
jQuery(document).ready(function(){
jQuery('input[name="password"]').passwordStrength();

});



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
}


