document.getElementById('divErrors').style.display='none';
document.getElementById('divSuccess').style.display='block';

var previousInnerHTML = new String();

	function validateEmail(email) {
		invalidChars = " /:,;"

		for (i=0; i<invalidChars.length; i++) {// does it contain any invalid characters?
			badChar = invalidChars.charAt(i)
			if (email.indexOf(badChar,0) > -1) {
				return false
			}
		}
		atPos = email.indexOf("@",1)  // there must be one "@" symbol
		if (atPos == -1) {
			return false
		}
		if (email.indexOf("@",atPos+1) != -1) {  // and only one "@" symbol
			return false
		}
		periodPos = email.indexOf(".",atPos)
		if (periodPos == -1) {  // and at least one "." after the "@"
			return false
		}
		if (periodPos+3 > email.length) {  // must be at least 2 characters after the "."
			return false
		}
		return true;
	}



function AddErrorMsg(msg)
{
	previousInnerHTML = previousInnerHTML.concat("<li>" + msg + "</li>");    
} 



 // ----------------------------------------------------------------------


	function validateContactForm(theForm) {
	previousInnerHTML='';
		var selTitle = theForm.title.options[theForm.title.selectedIndex].value;		
		if(selTitle == "Please select") {
			AddErrorMsg("Please select your Title");
			theForm.title.focus();
		}
		
		if(theForm.fname.value == "") {
			AddErrorMsg("Please enter your First Name");
			theForm.fname.focus();
		}

		if(theForm.lname.value == "") {
			AddErrorMsg("Please enter your Last Name");
			theForm.lname.focus();
		}

		if(theForm.email.value == "") {
			AddErrorMsg("Please enter your Email Address");
			theForm.email.focus();

		}
		
		if((!validateEmail(theForm.email.value)) && (theForm.email.value != "")) {
			AddErrorMsg("Please enter a valid Email Address");
			theForm.email.focus();

		}	

		if(theForm.phone.value == "") {
			AddErrorMsg("Please enter your Phone Number");
			theForm.phone.focus();

		}
		
		/*if(theForm.mobile.value == "") {
			AddErrorMsg("Please enter your Mobile Phone Number");
			theForm.mobile.focus();

		}*/	
		
		if(theForm.address.value == "") {
			AddErrorMsg("Please enter your Address");
			theForm.address.focus();

		}

		if(theForm.suburb.value == "") {
			AddErrorMsg("Please enter your Suburb");
			theForm.suburb.focus();

		}

		if(theForm.pcode.value == "") {
			AddErrorMsg("Please enter your Post Code");
			theForm.pcode.focus();

		}

		if (!theForm.wearer[0].checked && !theForm.wearer[1].checked) {			
			AddErrorMsg("Please answer 'Are you a hearing instrument wearer' ");
		}


		if(theForm.comments.value == "") {
			AddErrorMsg("Please enter your Comments");
			theForm.comments.focus();

		}

		if (previousInnerHTML!='')
		{
			document.getElementById('divErrors').innerHTML = '<p>Please correct the following errors&#8230;</p> <ul>' + previousInnerHTML + '</ul>' ;
			document.getElementById('divErrors').style.display='block';

			return false;
		}
		else
		{
			return true;
		}

	}