/* ----------------------------------------------
 * PASSWORD STRENGTH METER
 * ----------------------------------------------
 * By Dieter Verjans <dieter@inventis.be>
 */
 
var fireOn = window.ie ? 'load' : 'domready';
window.addEvent(fireOn, function(){
	
	if($('pass'))
	{
		$('pass').addEvent('keyup', function(){
			var pass = $('pass').value;
			var strength = chechStrength(pass);
			$('pp_strength').innerHTML= "<span style='width:"+(strength*2)+"px;'>" + strength + "</span>";
		});
	}
	
	var requested = false;
	
	if($('username'))
	{
		$('username').addEvent('keydown', function(e){
			var key = e.key;
			if(key.test('[a-z0-9]', 'i'))
				return true;
			else
				return false;
		});
		
		$('username').addEvent('keyup', function(){
			var value = $('username').value;
			if(value.length == 0)
			{
				$('pp_help').innerHTML = "<span class='red'>Gelieve een gebruikersnaam op te geven</span>";
			}
			
			if(value.length > 0 )
			{
				if(!requested){
					var req = new Request.JSON({  
						method: 'post',
						url: '/nl/community/ajax-check-username/',  
						data: { 'username' : value },  
						onRequest: function() { 						
							$('pp_help').innerHTML = '<img src="/design/images/icons/ajax-loader-community-small.gif" alt="laden" /> Controleren beschikbaarheid gebruikersnaam';
						},   
						onComplete: function(response) {
							$('pp_help').innerHTML = '<span class="' + response['class'] + '">' + response['content']  +'</span>';
						}  
					}).send();
				}
			}
		});
	}

});

var chechStrength = function(p) 
{
	var intScore = 0;
			
	if (p.length == 0) return (intScore);

	// PASSWORD LENGTH
	intScore += p.length;
	
	if(p.length > 0 && p.length <= 4) {                    // length 4 or less
		intScore += p.length;
	}
	else if (p.length >= 5 && p.length <= 7) {	// length between 5 and 7
		intScore += 6;
	}
	else if (p.length >= 8 && p.length <= 15) {	// length between 8 and 15
		intScore += 12;
	}
	else if (p.length >= 16) {               // length 16 or more
		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 += 1;
	}
	if (p.match(/[A-Z]/)) {              // [verified] at least one upper case letter
		intScore += 5;
	}
	// NUMBERS
	if (p.match(/\d/)) {             	// [verified] at least one number
		intScore += 5;
	}
	if (p.match(/.*\d.*\d.*\d/)) {            // [verified] at least three numbers
		intScore += 5;
	}
	
	// SPECIAL CHAR
	if (p.match(/[!,@,#,$,%,^,&,*,?,_,~]/)) {           // [verified] at least one special character
		intScore += 5;
	}
	// [verified] at least two special characters
	if (p.match(/.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~]/)) {
		intScore += 5;
	}
	
	// COMBOS
	if (p.match(/(?=.*[a-z])(?=.*[A-Z])/)) {        // [verified] both upper and lower case
		intScore += 2;
	}
	if (p.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/)) { // [verified] both letters and numbers
		intScore += 2;
	}
	// [verified] letters, numbers, and special characters
	if (p.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!,@,#,$,%,^,&,*,?,_,~])/)) {
		intScore += 2;
	}
	
	// return score (max = 100)
	return Math.round(intScore * 2);

}
