/* 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.1 (20/08/2007)
 * Changed code to be more jQuery-like
 * 
 * Version 1.0 (20/07/2007)
 * Initial version.
 */
(function($){
	$.fn.pstrength = function(o) {
		if($(this[0]).attr("type") == "password" && this.length > 1){
			var o = $.extend({
				vImg: '/images/qiangdu1.gif',
				nImg: '/images/qiangdu2.gif'
			}, o);
			var strImg = new Array();
			for(var i = 1;i < this.length;i++) {
				strImg.push( {Img: this[i], Score: Number($(this[i]).attr("score"))} );
			}
			$(this[0]).keyup(function(){
				$.fn.runPassword($(this).val(), strImg, o);
			});
		}
	}
	$.fn.runPassword = function (p, f, o){
			// Check password
			nPerc = $.fn.checkPassword(p);
			
			for(var i = 0;i < f.length;i++){
				if (f[i].Score <= nPerc) {
					$(f[i].Img).attr("src", o.vImg);
				} else {
					$(f[i].Img).attr("src", o.nImg);
				}
			}
		}
		$.fn.checkPassword = function(p)
		{
			var intScore = 0;
			// PASSWORD LENGTH
			if (p.length<5)                         // length 4 or less
			{
				intScore = (intScore + 3)
			}
			else if (p.length>4 && p.length<8) // length between 5 and 7
			{
				intScore = (intScore+6)
			}
			else if (p.length>7 && p.length<16)// length between 8 and 15
			{
				intScore = (intScore+12)
			}
			else if (p.length>15)                    // 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+2)
			}
			return intScore;
		}
	
})(jQuery);