// Validate that all mandatory fields are filled in and are correctly filled in
function validateAll()
{
	var error = "";
	var d = document.contact;
	var ok = true;
	if(d.firstName.value == ""){
		alert("First Name must be filled in.");
		ok=false;
	}else if(isAlpha(d.firstName.value,'First Name',d.firstName) == false){
		d.firstName.value = "";
		ok=false;
	}
	if(d.lastName.value == ""){
		alert("Last Name must be filled in.");
		ok=false;
	}else if(isAlpha(d.lastName.value,'Last Name',d.lastName) == false){
		d.lastName.value = "";
		ok=false;
	}
	if(d.phone.value == ""){
		alert("Phone must be filled in.");
		ok=false;
	}else if(d.phone.value.length < 8 || isNumeric(d.phone.value,'Phone Number',d.phone) == false){
		alert("Phone number must be at least 8 chars. long and numeric!");
		d.phone.value = "";
		ok=false;
	}
	if(d.email.value == ""){
		alert("Email must be filled in.");
		ok=false;
	}else if(echeck(d.email.value) == false){
		d.email.value = "";
		ok=false;
	}
	if(d.suburb.value == ""){
		alert("Suburb must be filled in.");
		ok=false;
	}else if(isAlpha(d.suburb.value,'Suburb of Building',d.suburb) == false){
		d.suburb.value = "";
		ok=false;
	}
	if(d.service.value == ""){
		alert("Please select a service.");
		d.service.value = "";
		ok=false;
	}
	
	if(!ok)
		alert("Please fix the necessary fields. Your enquiry has not been sent.");
	return ok;
}






//function validateAll()
//{
//	var error = "";
//	var d = document.contact;
//	var ok = true;
//	if(d.firstName.value == "" || isAlpha(d.firstName.value,'First Name',d.firstName) == false){
//		d.firstName.value = "";
//		ok=false};
//	if(d.lastName.value == "" || isAlpha(d.lastName.value,'Last Name',d.lastName) == false){
//		d.firstName.value = "";
//		ok=false};
//	if(d.phone.value == "" || d.phone.value.length < 8 || isNumeric(d.phone.value,'Phone Number',d.phone) == false){
//		d.firstName.value = "";
//		ok=false};
//	if(d.email.value == "" || echeck(d.email.value) == false){
//		d.firstName.value = "";
//		ok=false};
//	if(d.suburb.value == "" || isAlpha(d.suburb.value,'Suburb of Building',d.suburb) == false){
//		d.firstName.value = "";
//		ok=false};
//	if(d.service.value == ""){
//		alert("Please select a service.");
//		d.firstName.value = "";
//		ok=false};
//	
//	if(!ok)
//		alert("Please fix the necessary fields. Your enquiry has not been sent.");
//	return ok;
//}


// Validate string has alpha only
function isAlpha(test,ctrlName,ctrl)
{
	validChar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ. ';
	test = test.toUpperCase();
	for(i=0;i<test.length;i++)
	{
		if (validChar.indexOf(test.charAt(i)) < 0)
		{
			alert(ctrlName+" value must be alpha only.");
			//ctrl.value = "";
			//ctrl.focus(true);
			return false;
		}
	}
}

// Validate string is alphanumeric includes / and -
function isAlphaNumeric(test,ctrlName,ctrl)
{
	validChar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789/-';
	test = test.toUpperCase();
	for(i=0;i<test.length;i++)
	{
		if (validChar.indexOf(test.charAt(i)) < 0)
		{
			alert(ctrlName+" contains invalid characters.");
			//ctrl.value = "";
			//ctrl.focus(true);
			return false;
		}
	}
}

// validate string is numeric
function isNumeric(test,ctrlName,ctrl)
{
	validChar = '0123456789';
	for(i=0;i<test.length;i++)
	{
		if (validChar.indexOf(test.charAt(i)) < 0)
		{
			//alert(ctrlName+" value must be numeric only.");
			//ctrl.value = "";
			//ctrl.focus(true);
			return false;
		}
	}
}


function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail Address.")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail Address.")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail Address.")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail Address.")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail Address.")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail Address.")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail Address.")
		    return false
		 }

 		 return true					
	}






function ValidateForm(){
	var d = document.contact;
	
	if ((d.email.value==null)||(d.email.value=="")){
		alert("Please Enter your Email Address.")
		d.focus()
		return false
	}
	return true
 }

