jQuery(document).ready(function(){
    jQuery("#newpw").keyup(function () {
        var pwvalue = jQuery("#newpw").val();
        var pwstrength = getPasswordStrength(pwvalue);
        jQuery("#pwstrength").html("&nbsp;عالی");
        jQuery("#pwstrengthpos").css("background-color","#33CC00");
        if (pwstrength<75) {
            jQuery("#pwstrength").html("&nbsp;متوسط");
            jQuery("#pwstrengthpos").css("background-color","#ff6600");
        }
        if (pwstrength<30) {
            jQuery("#pwstrength").html("&nbsp;نامناسب");
            jQuery("#pwstrengthpos").css("background-color","#cc0000");
        }
        jQuery("#pwstrengthpos").css("width",pwstrength);
        jQuery("#pwstrengthneg").css("width",100-pwstrength);
    });
});

function getPasswordStrength(pw){
	score = 0;
	score += pw.length * 4;
	score += ( checkRep(1,pw).length - pw.length ) * 1;
	score += ( checkRep(2,pw).length - pw.length ) * 1;
	score += ( checkRep(3,pw).length - pw.length ) * 1;
	score += ( checkRep(4,pw).length - pw.length ) * 1;
	if (pw.match(/(.*[0-9].*[0-9].*[0-9])/))  score += 5;
	if (pw.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5;
	if (pw.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  score += 10;
	if (pw.match(/([a-zA-Z])/) && pw.match(/([0-9])/))  score += 10;
	if (pw.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && pw.match(/([0-9])/))  score += 10;
	if (pw.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && pw.match(/([a-zA-Z])/))  score += 10;
	if (pw.match(/^\w+$/) || pw.match(/^\d+$/) )  score -= 10;
	if (score < 0) return 0;
	if (score > 100) return 100;
  return (score);
}

function showStrengthBar() {
    document.write('<table align="center"><tr><td>میزان امنیت پسورد:&nbsp;</td><td width="102"><div id="pwstrengthpos" style="position:relative;float:left;width:0px;background-color:#33CC00;border:1px solid #000;border-right:0px;">&nbsp;</div><div id="pwstrengthneg" style="position:relative;float:right;width:100px;background-color:#efefef;border:1px solid #000;border-left:0px;">&nbsp;</div></td><td><div id="pwstrength">&nbsp;نامناسب</div></td></tr></table>');
}

function checkRep(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;
}
