/**
 * password_strength_plugin.js
*/

(function($){
	$.fn.shortPass = bp_err_password_short;
	$.fn.badPass = bp_err_password_low;
	$.fn.goodPass = bp_err_password_good;
	$.fn.strongPass = bp_err_password_excellent;
	$.fn.samePassword = '';
	$.fn.resultStyle = "";

	 $.fn.passStrength = function(options) {

		 var defaults = {
				shortPass: 		"#edabab",	//optional
				badPass:		"#edabab",		//optional
				goodPass:		"#ede3ab",		//optional
				strongPass:		"#d3edab",	//optional
				baseStyle:		"testresult",	//optional
				holder:			"",				//optional
				messageloc:		1				//before == 0 or after == 1
			};
		 	var opts = $.extend(defaults, options);

		 	return this.each(function() {
		 		 var obj = $(this);

		 		$(obj).unbind().keyup(function()
		 		{
					var results = $.fn.teststrength($(this).val(),opts);

					if(opts.messageloc === 1)
					{
						if($(this).resultStyle == opts.badPass || $(this).resultStyle == opts.shortPass)
							$(opts.holder).text(results).css("color", "#ff0000");
						else if($(this).resultStyle == opts.goodPass)
							$(opts.holder).text(results).css("color", "#FF8F00");
						else
							$(opts.holder).text(results).css("color", "#00AA00");
							
						$(this).css("background", $(this).resultStyle);
						//$(this).prev("." + opts.baseStyle).remove();
						//$(this).addClass($(this).resultStyle);
						//$(this).prev("." + opts.baseStyle).addClass($(this).resultStyle);
						//$(this).addClass($(this).resultStyle);
						//$(opts.holder).next("." + opts.baseStyle).remove();
						//$(opts.holder).prepend("<span class=\""+opts.baseStyle+"\"><span></span></span>");
						//$(opts.holder).next("." + opts.baseStyle).addClass($(this).resultStyle).find("span").text(results);
					}
					/*else
					{
						$(this).prev("." + opts.baseStyle).remove();
						$(this).before("<span class=\""+opts.baseStyle+"\"><span></span></span>");
						$(this).prev("." + opts.baseStyle).addClass($(this).resultStyle).find("span").text(results);
					}*/
		 		 });

		 		$.fn.teststrength = function(password,opt){
		 			 	var score = 0;

		 			    //password < 6
		 			    if (password.length < 6 ) { this.resultStyle =  opt.shortPass;return $(this).shortPass; }

		 			    //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 < 34 ){ this.resultStyle = opt.badPass; return $(this).badPass;}
		 			    if (score < 68 ){ this.resultStyle = opt.goodPass;return $(this).goodPass;}

		 				this.resultStyle = opt.strongPass;
		 			    return $(this).strongPass;
		 		};

		  });
	 };
})(jQuery);

$.fn.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;
	};
