/* JavaScript Document */
/* Copyright Spook Creative 2010 */
/* Author: Michael Ford */

function validateContactForm(){
	// SET UP ERROR MESSAGE
	errorMsg = "The following error(s) exist, please rectify and try again.<br/>";
	// INITIALISE ERROR COUNTER
	hasErrors = 0;
	// CREATE REG EX EXPRESSION
	var alphaNumeric = /[^\sa-zA-Z0-9!@$%&()'",.?_\-]/;
	var email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	var number = /^\d+$/;
	//
	var setFocus = 0;
	//
	// FUNCTION TO VALIDATE USERNAME ENTERED
	if(document.contact_form.name.value == "") {
			hasErrors ++;
			errorMsg += "&#8226;&nbsp;<strong>Username</strong> - Please enter a Username<br/>";
			document.getElementById("name").className = "err";
			if(setFocus == 0){
				document.contact_form.name.focus();
				setFocus = 1;
			}
	} else {
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.contact_form.name.value)){
					hasErrors ++;
					errorMsg += "&#8226;&nbsp;<strong>Username</strong> - Invalid characters entered, letters and numbers only.<br/>";	
					document.getElementById("name").className = "err";
					/*document.contact_form.name.value = "";*/
					if(setFocus == 0){
						document.contact_form.name.focus();
						setFocus = 1;
					}					
			} else {
				document.getElementById("name").className = "inputClear";
			}
	} 
	//
	// FUNCTION TO VALIDATE ADDRESS ENTERED
	if(document.contact_form.address.value == "") {
			hasErrors ++;
			errorMsg += "&#8226;&nbsp;<strong>Address</strong> - Please enter an Address<br/>";
			document.getElementById("address").className = "err";
			if(setFocus == 0){
				document.contact_form.address.focus();
				setFocus = 1;
			}
	} else {
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.contact_form.address.value)){
					hasErrors ++;
					errorMsg += "&#8226;&nbsp;<strong>Address</strong> - Invalid characters entered, letters and numbers only.<br/>";	
					document.getElementById("address").className = "err";
					/*document.contact_form.address.value = "";*/
					if(setFocus == 0){
						document.contact_form.address.focus();
						setFocus = 1;
					}					
			} else {
				document.getElementById("address").className = "inputClear";
			}
	}
	//
	// FUNCTION TO VALIDATE SUBURB ENTERED
	if(document.contact_form.suburb.value == "") {
			hasErrors ++;
			errorMsg += "&#8226;&nbsp;<strong>Suburb</strong> - Please enter a Suburb<br/>";
			document.getElementById("suburb").className = "err";
			if(setFocus == 0){
				document.contact_form.suburb.focus();
				setFocus = 1;
			}
	} else {
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.contact_form.suburb.value)){
					hasErrors ++;
					errorMsg += "&#8226;&nbsp;<strong>Suburb</strong> - Invalid characters entered, letters and numbers only.<br/>";	
					document.getElementById("suburb").className = "err";
					/*document.contact_form.suburb.value = "";*/
					if(setFocus == 0){
						document.contact_form.suburb.focus();
						setFocus = 1;
					}					
			} else {
				document.getElementById("suburb").className = "inputClear";
			}
	}
	//
	// FUNCTION TO VALIDATE CITY ENTERED
	if(document.contact_form.city.value == "") {
			hasErrors ++;
			errorMsg += "&#8226;&nbsp;<strong>City</strong> - Please enter a City<br/>";
			document.getElementById("city").className = "err";
			if(setFocus == 0){
				document.contact_form.city.focus();
				setFocus = 1;
			}
	} else {
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.contact_form.city.value)){
					hasErrors ++;
					errorMsg += "&#8226;&nbsp;<strong>City</strong> - Invalid characters entered, letters and numbers only.<br/>";	
					document.getElementById("city").className = "err";
					/*document.contact_form.city.value = "";*/
					if(setFocus == 0){
						document.contact_form.city.focus();
						setFocus = 1;
					}					
			} else {
				document.getElementById("city").className = "inputClear";
			}
	}
	//
	// FUNCTION TO VALIDATE PHONE ENTERED
	if (document.contact_form.phone.value != "") {
			if (!number.test(document.contact_form.phone.value)){
				hasErrors ++;
				errorMsg += "&#8226;&nbsp;<strong>Phone</strong> - Invalid characters entered, numbers only.<br/>";	
				document.getElementById("phone").className = "err";
				/*document.contact_form.phone.value = "";*/
				if(setFocus == 0){
					document.contact_form.phone.focus();
					setFocus = 1;
				}					
			} else {
				document.getElementById("phone").className = "inputClear";
			}
	}
	//
	// FUNCTION TO VALIDATE FAX ENTERED
	if (document.contact_form.fax.value != "") {
			if (!number.test(document.contact_form.fax.value)){
				hasErrors ++;
				errorMsg += "&#8226;&nbsp;<strong>Fax</strong> - Invalid characters entered, numbers only.<br/>";	
				document.getElementById("fax").className = "err";
				/*document.contact_form.fax.value = "";*/
				if(setFocus == 0){
					document.contact_form.fax.focus();
					setFocus = 1;
				}					
			} else {
				document.getElementById("fax").className = "inputClear";
			}
	}
	//
	// FUNCTION TO VALIDATE E-MAIL ENTERED
	if (document.contact_form.email.value == "") { 
			hasErrors ++;
			errorMsg += "&#8226;&nbsp;<strong>Email</strong> - Please enter an Email Address.<br/>";	
			document.getElementById("email").className = "err";
			if(setFocus == 0){
				document.contact_form.email.focus();
				setFocus = 1;
			}
	} else {
			if (!email.test(document.contact_form.email.value)){
				hasErrors ++;
				errorMsg += "&#8226;&nbsp;<strong>Email</strong> - Invalid Email Address.<br/>";	
				document.getElementById("email").className = "err";
				/*document.contact_form.fax.value = "";*/
				if(setFocus == 0){
					document.contact_form.email.focus();
					setFocus = 1;
				}	
			} else {
				document.getElementById("email").className = "inputClear";
			}
	}
	//
	// FUNCTION TO VALIDATE ENQUIRY ENTERED
	if(document.contact_form.enquiries.value == "") {
			hasErrors ++;
			errorMsg += "&#8226;&nbsp;<strong>Enquiries</strong> - Please enter your Enquiry<br/>";
			document.getElementById("enquiries").className = "err";
			if(setFocus == 0){
				document.contact_form.enquiries.focus();
				setFocus = 1;
			}
	} else {
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.contact_form.enquiries.value)){
					hasErrors ++;
					errorMsg += "&#8226;&nbsp;<strong>Enquiries</strong> - Invalid characters entered, letters and numbers only.<br/>";	
					document.getElementById("enquiries").className = "err";
					/*document.contact_form.city.value = "";*/
					if(setFocus == 0){
						document.contact_form.enquiries.focus();
						setFocus = 1;
					}					
			} else {
				document.getElementById("enquiries").className = "inputClear";
			}
	}
	//
	// ALL ENTRIES VALIDATED SO SUBMIT FORM
	if(hasErrors < 1) {
		//alert("Submit Form");
		document.contact_form.submit();
	} else {
		//alert(errorMsg);
		document.getElementById("err").className = "err";
		document.getElementById("err").innerHTML = errorMsg;
		return false;
	}
	//
}

function resetContactForm(){
	document.contact_form.name.value = "";
	document.contact_form.name.select();
	document.getElementById("name").className = "inputClear";
	document.contact_form.address.value = "";
	document.getElementById("address").className = "inputClear";
	document.contact_form.suburb.value = "";
	document.getElementById("suburb").className = "inputClear";
	document.contact_form.city.value = "";
	document.getElementById("city").className = "inputClear";
	document.contact_form.phone.value = "";
	document.getElementById("phone").className = "inputClear";
	document.contact_form.fax.value = "";
	document.getElementById("fax").className = "inputClear";
	document.contact_form.email.value = "";
	document.getElementById("email").className = "inputClear";
	document.contact_form.enquiries.value = "";
	document.getElementById("enquiries").className = "inputClear";
	document.getElementById("err").innerHTML = "";
	document.getElementById("err").className = "errClear";
}
