/**
 * password_strength_plugin.js
 * Copyright (c) 2009 myPocket technologies (www.mypocket-technologies.com)
 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * View the GNU General Public License .
 * @author Darren Mason (djmason9@gmail.com)
 * @date 3/13/2009
 * @projectDescription Password Strength Meter is a jQuery plug-in provide you smart algorithm to detect a password strength. Based on Firas Kassem orginal plugin - http://phiras.wordpress.com/2007/04/08/password-strength-meter-a-jquery-plugin/
 * @version 1.0.1
 * 
 * @requires jquery.js (tested with 1.3.2)
 * @param shortPass:	"shortPass",	//optional
 * @param badPass:		"badPass",		//optional
 * @param goodPass:		"goodPass",		//optional
 * @param strongPass:	"strongPass",	//optional
 * @param baseStyle:	"testresult",	//optional
 * @param userid:		"",				//required override
 * @param messageloc:	1				//before == 0 or after == 1
 * 
*/
(function($){ 
	$.fn.shortPass = 'Too short';
	$.fn.badPass = 'Weak';
	$.fn.goodPass = 'Good';
	$.fn.strongPass = 'Strong';
	$.fn.samePassword = 'Username and Password identical.';
	$.fn.resultStyle = "";
	
	 $.fn.passStrength = function(options) {  
	  
		 var defaults = {
				shortPassMsg: 	"Too short",	//optional
				badPassMsg:		"Weak",		//optional
				goodPassMsg:	"Good",		//optional
				strongPassMsg:	"strongPass",	//optional
				samePassMsg:	"Username and Password identical",	//optional
				shortPass: 		"shortPass",	//optional
				badPass:		"badPass",		//optional
				goodPass:		"goodPass",		//optional
				strongPass:		"strongPass",	//optional
				baseStyle:		"testresult",	//optional
				userid:			"",				//required override
				messageloc:		""				//required override
			}; 
		 	var opts = $.extend(defaults, options);  
		      
		 	return this.each(function() { 
		 		 var obj = $(this);
		 		
		 		$(obj).unbind().keyup(function()
		 		{
					var username = $(opts.userid).val();
					username = username.substring(0, username.indexOf("@")).toLowerCase();
					var results = $.fn.teststrength($(this).val(),username,opts);
					$(opts.messageloc + " > span").remove();
					$(opts.messageloc).removeClass(opts.baseStyle);
					$(opts.messageloc).removeClass(opts.shortPass);
					$(opts.messageloc).removeClass(opts.badPass);
					$(opts.messageloc).removeClass(opts.goodPass);
					$(opts.messageloc).removeClass(opts.strongPass);
					$(opts.messageloc).addClass(opts.baseStyle);
					$(opts.messageloc).addClass($(this).resultStyle);
					$(opts.messageloc).append("" + results + "");
		 		 });
		 		 
		 		//FUNCTIONS
		 		$.fn.teststrength = function(password,username,option){
		 			 	var score = 0;
		 			 	var categories = 0;
		 			    
		 			    //password < 5
		 			    if (password.length < 10 ) { this.resultStyle =  option.shortPass;return option.shortPassMsg; }
		 			    if (password.match(/[0-9]/)){ categories++; }
		 			    if (password.match(/[a-z]/)){ categories++; }
		 			    if (password.match(/[A-Z]/)){ categories++; }
		 			    if (password.match(/[!,@,#,$,%,^,&,*,?,_,~]/)){ categories++; }
		 			    
		 			    if (categories < 3) { this.resultStyle =  option.badPass;return option.badPassMsg; }
		 			    //password == user name
		 			    if (password.toLowerCase().indexOf(username.toLowerCase()) != -1) { this.resultStyle = option.badPass;return option.samePassMsg; }
		 			    
		 			    //password length
		 			    score += password.length * 4;
		 			    score += ( $.fn.checkRepetition(1,password).length - password.length ) * 1;
		 			    score += ( $.fn.checkRepetition(2,password).length - password.length ) * 1;
		 			    score += ( $.fn.checkRepetition(3,password).length - password.length ) * 1;
		 			    score += ( $.fn.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 < 68 ){ this.resultStyle = option.goodPass;return option.goodPassMsg;}
		 			    
		 			    this.resultStyle= option.strongPass;
		 			    return option.strongPassMsg;
		 			    
		 		};
		  
		  });  
	 };  
})(jQuery); 
$.fn.checkRepetition = function(pLen,str) {
 	var res = "";
     for (var i=0; i