/* jQuery Password Strength Plugin (pstrength) - A jQuery plugin to provide accessibility functions
 * Author: Tane Piper (digitalspaghetti@gmail.com) 
 * Website: http://digitalspaghetti.me.uk
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
 * 
 * === Changelog ===
 * Version 1.2 (In Development)
 * Added more options for colors and common words
 * Added common words checked to see if words like 'password' or 'qwerty' are being entered
 * Added minimum characters required for password
 * Re-worked scoring system to give better results
 * 
 * Version 1.1 (20/08/2007)
 * Changed code to be more jQuery-like
 * 
 * Version 1.0 (20/07/2007)
 * Initial version.
 */
(function($){
	$.extend($.fn, {
		pstrength : function(o) {
			var o = $.extend({
				verdects:	["Very Weak","Weak","Medium","Strong","Very Strong"],
				colors: 	["#f00","#c06", "#f60","#3c0","#3f0"],
				scores: 	[10,15,30,37],
				common:		["password","sex","god","123456","123","liverpool","letmein","qwerty","monkey"],
				minchar:	4
			},o);		
			return this.each(function(){
				var e = $(this).attr('id');
				$(this).after('<div id="' + e + '_text"></div>');
				$(this).after('<div id="' + e + '_bar" style="border: 1px solid white; font-size: 1px; height: 2px; width: 0px;"></div>');
				$(this).keyup(function(){				
					$.fn.runPassword($(this).val(), e, o);
				});
			});
		},
		runPassword : function (p, f, o){
			// Check password
			nPerc = $.fn.checkPassword(p, o);
			// Get controls
	    	var ctlBar = "#" + f + "_bar"; 
	    	var ctlText = "#" + f + "_text";		
			// Color and text
			if (nPerc == -200) {
				strColor = '#f00';
				strText = 'Unsafe Password Word';
				$(ctlBar).css({width: "0%"});
			}		
			else if (nPerc < 0 && nPerc > -199) {
				strColor = '#ccc';
				strText = 'Too Short';
				$(ctlBar).css({width: "1%"});
			}
			else if(nPerc <= o.scores[0])
			{
		   		strColor = o.colors[0];
				strText = o.verdects[0];
				$(ctlBar).css({width: "1%"});
			}
			else if (nPerc > o.scores[0] && nPerc <= o.scores[1])
			{
		   		strColor = o.colors[1];
				strText = o.verdects[1];
				$(ctlBar).css({width: "25%"});
			}
			else if (nPerc > o.scores[1] && nPerc <= o.scores[2])
			{
			   	strColor = o.colors[2];
				strText = o.verdects[2];
				$(ctlBar).css({width: "50%"});
			}
			else if (nPerc > o.scores[2] && nPerc <= o.scores[3])
			{
			   	strColor = o.colors[3];
				strText = o.verdects[3];
				$(ctlBar).css({width: "75%"});
			}
			else
			{
			   	strColor = o.colors[4];
				strText = o.verdects[4];
				$(ctlBar).css({width: "99%"});
			}
			$(ctlBar).css({backgroundColor: strColor});
			$(ctlText).html("<span style='color: " + strColor + ";'>" + strText + "</span>");
		},
		checkPassword : function(p, o)
		{
			var intScore = 0;
			var strVerdict = o.verdects[0];	
			// PASSWORD LENGTH
			if (p.length < o.minchar)                         // length 3 or less
			{
				intScore = (intScore - 100)
			}
			else if (p.length >= o.minchar && p.length <= (o.minchar + 2)) // length between 4 and 6
			{
				intScore = (intScore + 6)
			}
			else if (p.length >= (o.minchar + 3) && p.length <= (o.minchar + 4))// length between 6 and 15
			{
				intScore = (intScore + 12)
			}
			else if (p.length >= (o.minchar + 5))                    // length 16 or more
			{
				intScore = (intScore + 18)
			}
			// LETTERS (Not exactly implemented as dictacted above because of my limited understanding of Regex)
			if (p.match(/[a-z]/))                              // [verified] at least one lower case letter
			{
				intScore = (intScore + 1)
			}
			if (p.match(/[A-Z]/))                              // [verified] at least one upper case letter
			{
				intScore = (intScore + 5)
			}
			// NUMBERS
			if (p.match(/\d+/))                                 // [verified] at least one number
			{
				intScore = (intScore + 5)
			}
			if (p.match(/(.*[0-9].*[0-9].*[0-9])/))             // [verified] at least three numbers
			{
				intScore = (intScore + 5)
			}
			// SPECIAL CHAR
			if (p.match(/.[!,@,#,$,%,^,&,*,?,_,~]/))            // [verified] at least one special character
			{
				intScore = (intScore + 5)
			}
			// [verified] at least two special characters
			if (p.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/))
			{
				intScore = (intScore + 5)
			}
			// COMBOS
			if (p.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))        // [verified] both upper and lower case
			{
				intScore = (intScore + 2)
			}
			if (p.match(/([a-zA-Z])/) && p.match(/([0-9])/)) // [verified] both letters and numbers
			{
				intScore = (intScore + 2)
			}
		 	// [verified] letters, numbers, and special characters
			if (p.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/))
			{
				intScore = (intScore + 3)
			}
			for (var i=0; i < o.common.length; i++) {
				if (p.toLowerCase() == o.common[i]) {
					intScore = -200;
				}
			}
			return intScore;
		}
	});
})(jQuery);
