(function($) {
 
	$.passwordMeter = {
		tolerance: 35, // normally to have hard password, you should have 45 here
		status:null,
		lastStatus: 0,
 
		init: function(div) {
			$.passwordMeter.lastStatus = 0;
 
			var statusID = div.attr('id')+'_status';
			div.html('<div id="'+statusID+'"></div>');
 
			var status = $('#'+statusID);
			$.passwordMeter.status = status;
 
			status.html('');
			status.css('width', '0');
			status.css('height', div.css('height'));
		},
		
		passwordCheck: function() {
			var score = $.passwordMeter.testPassword(this.value);
			score = parseInt('' + (score / $.passwordMeter.tolerance * 100));
			if (score > 100) score = 100;
			if (score != $.passwordMeter.lastStatus) {
				$.passwordMeter.status.css('width', score+'%');
                var str = '';
                if (score < 25) {
                    str = 'Mauvais';
                } else if (score < 50) {
                    str = 'Moyen';
                } else if (score < 75) {
                    str = 'Bon';
                } else {
                    str = 'Excellent';
                }
                $.passwordMeter.status.html(str);
				$.passwordMeter.lastStatus = score;
			}
		},
		
		testPassword: function(passwd){ 
			// Based on this post : http://www.geekwisdom.com/dyn/passwdmeter
			
			var intScore   = 0
			if (passwd == undefined) return 0;
            if (passwd == '') return 0;
			
			// PASSWORD LENGTH
			if (passwd.length<5) intScore = (intScore+3);
			else if (passwd.length>4 && passwd.length<8) intScore = (intScore+6);
			else if (passwd.length>7 && passwd.length<16) intScore = (intScore+12);
			else if (passwd.length>15) intScore = (intScore+18);
			
			// LETTERS 
			if (passwd.match(/[a-z]/)) intScore = (intScore+1);
			if (passwd.match(/[A-Z]/)) intScore = (intScore+5);
			
			// NUMBERS
			if (passwd.match(/\d+/)) intScore = (intScore+5);
			if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/)) intScore = (intScore+5);
			
			// SPECIAL CHAR
			if (passwd.match(/.[!,@,#,$,%,^,&,*,?,_,~]/)) intScore = (intScore+5);
			if (passwd.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) intScore = (intScore+5);
		
			// COMBOS
			if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) intScore = (intScore+2);
			if (passwd.match(/([a-zA-Z])/) && passwd.match(/([0-9])/)) intScore = (intScore+2);
			if (passwd.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/)) intScore = (intScore+2);
		
			return intScore;
		}
 
	};
 
	$.fn.passwordMeter = function(div) {
		$.passwordMeter.init(div);
		this.keyup($.passwordMeter.passwordCheck);
	};
 
})(jQuery);
