// JavaScript Document
function submitForm() {
	
		/*alert("working");*/
		
		// SET UP ERROR MESSAGE
		errorMsg = "The following error(s) exist, please rectify and try again.\r\n";
		
		// INITIALISE ERROR COUNTER
		hasErrors = 0;
		
		// CREATE REG EX EXPRESSIONS
		var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		var tre = /^\d{7,}$/;
		var validYear = /^\d{4,}$/;
		var validCode = /^\d{5,}$/;
		var alphaNumeric = /[^\sa-zA-Z0-9!@$%&()'",.?_]/;
		
		//NAME FIELD
		if(document.contactForm.name.value == "") {
				hasErrors ++;
				errorMsg += "Name: Please enter a contact name.\r\n";
				document.contactForm.name.focus();
				document.contactForm.name.select();
		} else {
				// Check for number and type of characters entered
				if (alphaNumeric.test(document.contactForm.name.value)){
						hasErrors ++;
						errorMsg += "Name: Invalid characters entered, letters and numbers only.\r\n";		
						document.contactForm.name.value = "";
						document.contactForm.name.focus();
						document.contactForm.name.select();
				}
		}
		
		//E-MAIL
		if (document.contactForm.email.value == "") {
				hasErrors ++;
				errorMsg += "Email: Please enter an email address.\r\n";
				document.contactForm.email.focus();
				document.contactForm.email.select();
		} else {
				if (!re.test(document.contactForm.email.value)){
						hasErrors ++;
						errorMsg += "Email: Please enter a VALID email address.\r\n";
						document.contactForm.email.value="";
						document.contactForm.email.focus();
						document.contactForm.email.select();
				}
		}
		
		// FUNCTION TO VALIDATE TELEPHONE NUMBER ENTERED
		if (!document.contactForm.telephone.value == ""){
				if (!tre.test(document.contactForm.telephone.value)){
						hasErrors ++;
						errorMsg += "Phone: Not a recognised phone number, please enter a minimum of 7 NUMBERS only.\r\n";
						document.contactForm.telephone.value="";
						document.contactForm.telephone.focus();
						document.contactForm.telephone.select();
				}
		}
		
		// ALL ENTRIES VALIDATED SO SUBMIT FORM
		if(hasErrors < 1) {
				//alert("Submit Form");
				document.contactForm.submit();
		} else {
				alert(errorMsg);	
				return false;
		}
}
