//
//	Password Strength Meter
//	
//	This jQuery plugin is written by Firas Kassem [2007.04.05] and modified by Amin Rajaee [2009.07.26]
//	Firas Kassem  phiras.wordpress.com || phiras at gmail {dot} com
//	Amin Rajaee  rajaee at gmail (dot) com
//
//	for more information : http://phiras.wordpress.com/2007/04/08/password-strength-meter-a-jquery-plugin/
//
$(document).ready(function() {

	//
	//	Check a password's strength as the user types it into the input field
	//
	$(new_password_id).keyup( function() {
	
		password_score = getPasswordScore( $(new_password_id).val(), $('#username').val() );
		
		password_text  = getPasswordScoreText( password_score );
		
		$('#password_strength').html( password_text );
		
		if( password_score < 34 ) {
		
			$('#password_strength').css( "background-color", NEW_PWD.weak.bg_color );
			$('#password_strength').css( "color", NEW_PWD.weak.txt_color );
		
		} else if( password_score < 68 ) {
		
			$('#password_strength').css( "background-color", NEW_PWD.medium.bg_color );
			$('#password_strength').css( "color", NEW_PWD.medium.txt_color );
		
		} else {
		
			$('#password_strength').css( "background-color", NEW_PWD.strong.bg_color );
			$('#password_strength').css( "color", NEW_PWD.strong.txt_color );
		
		}
	   
	});
	
	
	//
	//	Check if the confirmation password matches the new password 
	//	to avoid having to have the user submit the form and fix 
	//	errors.
	//
	$(confirm_password_id).keyup( function(){
	
		if( $(new_password_id).val() == $(confirm_password_id).val() ) {
		
			//	MATCH
			$('#password_confirm_match').html( CONFIRM_PWD.same.verdict );
			$('#password_confirm_match').css( "background-color", CONFIRM_PWD.same.bg_color );
			$('#password_confirm_match').css( "color", CONFIRM_PWD.same.txt_color );
		
		} else {
		
			//	NO MATCH
			$('#password_confirm_match').html( CONFIRM_PWD.diff.verdict );
			$('#password_confirm_match').css( "background-color", CONFIRM_PWD.diff.bg_color );
			$('#password_confirm_match').css( "color", CONFIRM_PWD.diff.txt_color );
		
		}
	
	});

});

//
//	Check a password's strength and return an integer between 0-100
//
function getPasswordScore( password, username ) {

	//	Initialize score at zero
	score = 0 
	
	//	password < 4
	if (password.length < 4 ) return -20;
	
	if( username > '' ) {
		
		//	password == username
		if( password.toLowerCase()==username.toLowerCase() ) return -10;
		
		//	password contains username
		if( password.match( username ) ) return -10;
	
	}
	
	//	password has invalid characters
	if( !password.match(/(^[a-zA-Z0-9!,@,#,$,%,^,&,*,?,_,~]*$)/) ) return -5;
	
	//	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 numbers or chars
	if( password.match(/^\w+$/) || password.match(/^\d+$/) )  score -= 10 ;
	
	//
	//	Our Rules:
	//		- Between 4 and 32 characters
	//		- at least one UPPER CASE and LOWER CASE letter
	//		- at least one number
	//
	if( password.match(/(.*[0-9])/) && password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/) ) {
	
		//	Meets our criteria! give a score of 100
		return 100;
	
	}
	
	//	Return Numeric Score
	if( score > 100 ) return 100
	return (score)
 
}


//
//	Return a text string based on a password score
//
function getPasswordScoreText( score ) {

	if( score == -5 )	return NEW_PWD.invalid_characters.verdict;

	if( score == -10 )	return NEW_PWD.same.verdict;
	
	if( score == -20 ) 	return NEW_PWD.short.verdict;
	
	if( score < 34 )	return NEW_PWD.weak.verdict;
	
	if( score < 68 )	return NEW_PWD.medium.verdict;
	
	return NEW_PWD.strong.verdict;

}


//
//	Check for character repitition
//
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

}