// Contact Us web form data validation

function isValidEmail(strEmail){
	// This function returns true if the email is valid and false if not
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;

   // Search email text for regular expression matches
    if (strEmail.search(validRegExp) == -1) {return false;} 
    return true; 
}

function validate_required(field,alerttxt){
	with (field){
		if (value==null||value==""){
			alert(alerttxt);return false;
		}
		else {return true;}
	}
}

function validate_contact_us(thisform){
	with (thisform){		
		
		if (validate_required(forename,"Please provide your forename.")==false)
			{forename.focus();return false;}

		if (validate_required(surname,"Please provide your surname.")==false)
			{surname.focus();return false;}

		if (validate_required(organisation,"Please provide the name of your organisation.")==false)
			{organisation.focus();return false;}

		//Validate e-mail if e-mail is the preferred contact method OR if an e-mail has been entered
		if ((Contact_method[0].checked)||(submittedemail.value!="")){	
				if (validate_required(submittedemail,"Please provide your e-mail address.")==false)
					{submittedemail.focus();return false;}

				if (!isValidEmail(submittedemail.value))
					{alert("Sorry but the e-mail address provided isn't valid.\n\nPlease check it and try again.");
					submittedemail.focus();return false;}

				if (submittedemail.value!=email2.value)
					{alert("Sorry but the e-mail confirmation doesn't match the first one given.\n\nPlease check it and try again.");
					email2.focus();return false;}
		}

		//Validate phone Nos if telephone is the preferred contact method
		if (Contact_method[1].checked){
				
				if ((daytel.value==null||daytel.value=="")&&(mobiletel.value==null||mobiletel.value=="")){
					alert("We'll need at least one phone number (day time number or mobile) so that we can contact you at a convenient time to discuss your enquiry.");
					daytel.focus();return false
				}
				else{
					if (validate_required(calltime,"Please gives us the time when it would be most convenient for us to call you.")==false)
						{calltime.focus();return false;}
				}
		}

		// Prompt if no phone or e-mail recorded
		if ((daytel.value==null||daytel.value=="")&&(mobiletel.value==null||mobiletel.value=="")&&(submittedemail.value=="")){
				alert("Please provide either a phone number or an e-mail address so that we can get back to you.");
				daytel.focus();return false
		}

		if (validate_required(enquiry,"Hey - you forgot your enquiry!")==false)
			{enquiry.focus();return false;}

	}
}

function clearForm(){
  var r=confirm("Are you sure you want to remove all information from this form?\n\nClick 'OK' for yes or 'Cancel' for no.")
  if (r==true){
	document.forms[0].reset()
  }
  else{
    alert("You've chosen not to clear the form so please continue\nand click 'Submit enquiry' when ready.")
  }
}
