// CONTACT US FORM

function checkContact(theform){
 
	var total = theform.length;
	var div = document.getElementById("errorMessage");
	var errors = 0;
	var errorInclude = new Array();
	var errorMessage = new Array(4);
	errorMessage[0]="<li>You must complete the 'First Name' field</li>";
	errorMessage[1]="<li>You must complete the 'Last Name' field</li>";
	errorMessage[2]="<li>You must complete the 'Email' field</li>";
	errorMessage[3]="<li>You must complete the 'Confirm Email' field</li>";
	errorMessage[4]="";
	errorMessage[5]="";
	errorMessage[6]="<li>You must complete the 'Enquiry' field</li>";
	errorMessage[7]="<li>You must complete either the 'Telephone' or the 'Mobile field</li>";
	errorMessage[8]="<li>Your 'Email' address must be valid and match 'Confirm Email'</li>";

		
	for(i = 0; i < (total - 1); i++){
		theform[i].style.backgroundColor = "";
		theform[i].style.border = "1px solid";
		//alert(theform[i].name + " " +i);
		if(theform[i].value == "" && i < 7 && i!=4 && i!=5){
			theform[i].style.backgroundColor = "#ff7777";
			theform[i].style.border = "2px solid #bd2424";
			errorInclude = errorInclude.concat(errorMessage[i]);
			errors = 1;
		}
	}
		if(theform[4].value == "" && theform[5].value == ""){
			theform[4].style.backgroundColor = "#ff7777";
			theform[4].style.border = "2px solid #bd2424";
			theform[5].style.backgroundColor = "#ff7777";
			theform[5].style.border = "2px solid #bd2424";
			errorInclude = errorInclude.concat(errorMessage[7]);
			errors = 1;
		}
		if(theform[2].value != "" && theform[2].value != ""){
			var checkEmail = "@.";
			var checkStr = theform[2].value;
			var EmailValid = false;
			var EmailAt = false;
			var EmailPeriod = false;
			var EmailMatch = false;
			for (i = 0;  i < checkStr.length;  i++)	{
				ch = checkStr.charAt(i);
				for (j = 0;  j < checkEmail.length;  j++)	{
					if (ch == checkEmail.charAt(j) && ch == "@") { EmailAt = true; }
					if (theform[2].value == theform[3].value) { EmailMatch = true; }
					if (ch == checkEmail.charAt(j) && ch == ".") { EmailPeriod = true; }
					if (EmailAt && EmailPeriod) {	break; }
					if (j == checkEmail.length) {	break; }
				}
				// if both the @ and . were in the string
				if (EmailAt && EmailPeriod && EmailMatch)	{
					EmailValid = true
					break;
				}
			}
			if (!EmailValid) {
				theform[2].style.backgroundColor = "#ff7777";
				theform[2].style.border = "2px solid #bd2424";
				theform[3].style.backgroundColor = "#ff7777";
				theform[3].style.border = "2px solid #bd2424";
				errorInclude = errorInclude.concat(errorMessage[8]);
				errors = 1;
			}
		}
		
	//alert(errors);
	if(errors == 1){
		var totalE = errorInclude.length;
		div.innerHTML = "<h4>Errors</h4><ul>" + errorInclude.join("") +"</ul>";
		div.style.backgroundColor = "#ff7777";
		div.style.border = "2px solid #bd2424";
		div.style.color = "#000";
		div.style.margin = "0px 0px 10px 0px";
		div.style.padding = "0px 10px 20px 20px";
		//alert(errors);
		return (false);
	}
}