// JavaScript Document

$(document).ready(function() {

	$("input.Strength[type=password]").keyup(function() {
				  

		switch(parseInt($.testPasswd($(this).attr('value')))) {
			
			case 0:
				message = '<div rel="strength" class="passwd_short">Mot de passe trop court</div>';
			break;
			
			case 1:
				message = '<div rel="strength" class="passwd_weak">Trop faible</div>';
			break;
	
			case 2:
				message = '<div rel="strength" class="passwd_medium">Mot de passe moyen</div>';
			break;
	
			case 3:
				message = '<div rel="strength" class="passwd_strong">Bon mot de passe</div>';
			break;
		}

		if($(this).next().attr('rel') == 'strength') {
			$(this).next().remove();	
		}
		
		$(this).after(message);
	});
});


(function($) {
	
	$.extend({
				
		checkRepetition: function(pLen,str) {
		var res = "";
		 for (var i=0; i<str.length ; i++ ) 
		 {
			 var repeated=true;
			 
			 for (var 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;
		},

		testPasswd: function(password){

			
				var score = 0; 

				//password < 4
				if (password.length < 4 ) { return 0; }
					
				//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 symbols
				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;}
								
				//verifying 0 < score < 100
				if ( score < 0 ) { score = 0; } 
				if ( score > 100 ){  score = 100;} 
				
				if (score < 34 ){ return 1; } 
				if (score < 68 ){ return 2; }
								
				return 3;
		}
	});	  
}) (jQuery);
