// JavaScript Document

function checkform(){

	if(document.mailus.name.value==""){
		alert("You need to fill in your name.");
		return false;
	}
	
	if(document.mailus.email.value==""){
		alert("You need to fill in your email address.");
		return false;
	}
	
	var email = document.mailus.email;
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email.value)) {
		alert('Please provide a valid email address');
		email.focus
		return false;
	}
	
	if(document.mailus.message.value==""){
		alert("You need to fill in a message.");
		return false;
	}
	
	
	return true;
}

function overImage(image){
	$("#"+image).fadeTo("fast", 0.65);
}

function outImage(image){
	$("#"+image).fadeTo("fast", 1);
}

function checkNewAccount(){
	
	if(document.newaccount.username.value==""){
		alert("You need specify a username.");
		return false;
	}
	
	if(document.newaccount.username.value.length<5){
		alert("Username must be at least 5 characters.");
		return false;
	}
	
	if(document.newaccount.firstname.value==""){
		alert("You need to fill in your first name.");
		return false;
	}
	
	if(document.newaccount.lastname.value==""){
		alert("You need to fill in your last name.");
		return false;
	}
	
	if(document.newaccount.email.value==""){
		alert("You need to fill in your email address.");
		return false;
	}
	
	var email = document.newaccount.email;
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email.value)) {
		alert('E-mail address not valid');
		return false;
	}
	
	if(document.newaccount.phone.value==""){
		alert("You need to fill in your phone number.");
		return false;
	}
	
	if(document.newaccount.password1.value==""){
		alert("You need to create a password.");
		return false;
	}
	
	if(document.newaccount.password1.value != document.newaccount.password2.value){
		alert("Passwords do not match.");
		return false;
	}

}

function testPassword(passwd)
{
		var intScore   = 0
		var strVerdict = "weak"
		var strLog     = ""
		var barColour = "#e80033"
		
		// PASSWORD LENGTH
		if (passwd.length<5)                         // length 4 or less
		{
			intScore = (intScore+3)
			strLog   = strLog + "3 points for length (" + passwd.length + ")\n"
		}
		else if (passwd.length>4 && passwd.length<8) // length between 5 and 7
		{
			intScore = (intScore+6)
			strLog   = strLog + "6 points for length (" + passwd.length + ")\n"
		}
		else if (passwd.length>7 && passwd.length<16)// length between 8 and 15
		{
			intScore = (intScore+12)
			strLog   = strLog + "12 points for length (" + passwd.length + ")\n"
		}
		else if (passwd.length>15)                    // length 16 or more
		{
			intScore = (intScore+18)
			strLog   = strLog + "18 point for length (" + passwd.length + ")\n"
		}
		
		
		// LETTERS (Not exactly implemented as dictacted above because of my limited understanding of Regex)
		if (passwd.match(/[a-z]/))                              // [verified] at least one lower case letter
		{
			intScore = (intScore+1)
			strLog   = strLog + "1 point for at least one lower case char\n"
		}
		
		if (passwd.match(/[A-Z]/))                              // [verified] at least one upper case letter
		{
			intScore = (intScore+5)
			strLog   = strLog + "5 points for at least one upper case char\n"
		}
		
		// NUMBERS
		if (passwd.match(/\d+/))                                 // [verified] at least one number
		{
			intScore = (intScore+5)
			strLog   = strLog + "5 points for at least one number\n"
		}
		
		if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/))             // [verified] at least three numbers
		{
			intScore = (intScore+5)
			strLog   = strLog + "5 points for at least three numbers\n"
		}
		
		
		// SPECIAL CHAR
		if (passwd.match(/.[!,@,#,$,%,^,&,*,?,_,~]/))            // [verified] at least one special character
		{
			intScore = (intScore+5)
			strLog   = strLog + "5 points for at least one special char\n"
		}
		
									 // [verified] at least two special characters
		if (passwd.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/))
		{
			intScore = (intScore+5)
			strLog   = strLog + "5 points for at least two special chars\n"
		}
	
		
		// COMBOS
		if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))        // [verified] both upper and lower case
		{
			intScore = (intScore+2)
			strLog   = strLog + "2 combo points for upper and lower letters\n"
		}

		if (passwd.match(/([a-zA-Z])/) && passwd.match(/([0-9])/)) // [verified] both letters and numbers
		{
			intScore = (intScore+2)
			strLog   = strLog + "2 combo points for letters and numbers\n"
		}
 
									// [verified] letters, numbers, and special characters
		if (passwd.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/))
		{
			intScore = (intScore+2)
			strLog   = strLog + "2 combo points for letters, numbers and special chars\n"
		}
		
		if(intScore < 16)
		{
		   strVerdict = "very weak"
		   barColour = "#e80033"
		}
		else if (intScore > 15 && intScore < 25)
		{
		   strVerdict = "weak"
		   barColour = "#e80033"
		}
		else if (intScore > 24 && intScore < 35)
		{
		   strVerdict = "good"
		   barColour = "#ffcc33"
		}
		else if (intScore > 34 && intScore < 45)
		{
		   strVerdict = "strong"
		   barColour = "#6699cd"
		}
		else
		{
		   strVerdict = "uber strong"
		   barColour = "#0e9732"
		}
			
		document.getElementById("loadingZone").style.display = "block";
		document.newaccount.score.value = (intScore)
		document.getElementById("progressBar").style.width = (intScore*2) + "%"; 
		document.getElementById("infoLoading").innerHTML = (strVerdict)
		document.getElementById("progressBar").style.background = barColour;

	
}