// Email functions to support contact-us.php

function emailCheck (emailStr) {
var checkTLD=1;
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=emailStr.match(emailPat);

if (matchArray==null) {
alert('Please check the email address you entered: it doesn\'t look like it is in the right format.');
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert('Please check the email address you entered for invalid characters.');
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert('Please check the email address you entered for invalid characters.');
return false;
   }
}

if (user.match(userPat)==null) {
alert('Please check the email address you entered: it doesn\'t look like it is in the right format.');
return false;
}

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {


for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert('Please check the email address you entered: it doesn\'t look like it is in the right format.');
return false;
   }
}
return true;
}

var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert('Please check the email address you entered: it doesn\'t look like it is in the right format.');
return false;
   }
}

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert('Please check the email address you entered: it doesn\'t look like it is in the right format.');
return false;
}

if (len<2) {
alert('Please check the email address you entered: it doesn\'t look like it is in the right format.');
return false;
}

return true;
}


function validateForm()
{
	if (Enquiry.Name.value == '')
	{
		alert('Please enter your name, so we know who we \'re contacting.');
		Enquiry.Name.focus();
		event.returnValue=false;
		return false;
	}
	if (Enquiry.Name.value.length <3)
	{
		alert('We\'re sorry but the name you entered appears to be too short to be correct.');
		Enquiry.Name.focus();
		event.returnValue=false;
		return false;
	}
	if (Enquiry.Message.value == '')
	{
		alert('Please enter a message: you don\'t need to write much, we\'ll get back in touch with your shortly.');
		Enquiry.Message.focus();
		event.returnValue=false;
		return false;
	}
	if (Enquiry.Message.value.length <5)
	{
		alert('We\'re sorry but the message you entered appears to be too short to be correct.');
		Enquiry.Message.focus();
		event.returnValue=false;
		return false;
	}
	if ( (Enquiry.Email.value == '') && (Enquiry.Phone.value == '') )
	{
		alert('Please enter a phone number and/or email address: otherwise we won\'t be able to contact you.');
		Enquiry.Phone.focus();
		event.returnValue=false;
		return false;
	}

	if (Enquiry.Email.value != '')
	{
		if (Enquiry.Email.value.length <8)
		{
			alert('We\'re sorry but the email address you entered appears to be too short to be correct.');
			Enquiry.Email.focus();
			event.returnValue=false;
			return false;
		}
		if (!emailCheck(Enquiry.Email.value) )
		{
			Enquiry.Email.focus();
			event.returnValue=false;
			return false;
		}
	}

	if (Enquiry.Email.value == '')
	{
		if (Enquiry.Phone.value.length <6)
		{
			alert('We\'re sorry but the phone number you entered appears to be too short to be correct.');
			Enquiry.Phone.focus();
			event.returnValue=false;
			return false;
		}
	}
		
	if (Enquiry.Validation.value=='')
	{
		alert('Please answer the validation question.');
		Enquiry.Validation.focus();
		event.returnValue=false;
		return false;
	}
	if (Enquiry.Validation.value.length != 3)
	{
		alert('We\'re sorry but the validation answer you gave was the wrong length.');
		Enquiry.Validation.focus();
		event.returnValue=false;
		return false;
	}
}	// validate()

