function setFocus(aField) {
document.forms[0][aField].focus();
}

function isAnEmailAddress(aTextField) {
// 1+@3+ [or x@x.x] is as close as we will test

if (document.forms[0][aTextField].value.length<5) {
return false;
}
else if (document.forms[0][aTextField].value.indexOf("@") < 1) {
return false;
}
else if (document.forms[0][aTextField].value.length - document.forms[0][aTextField].value.indexOf("@") < 4) {
return false;
}
else { return true; }
}

function isEmpty(aTextField) {
if ((document.forms[0][aTextField].value.length==0) || (document.forms[0][aTextField].value==null)) {
return true;
}
else { return false; }
}

function validate() {

// check that the name field is valued
if (isEmpty("Company-name")) {
	alert("Please fill in the Company-name field.");
	setFocus("Company-name");
	return false;
}
// check that the Contact Name field is valued
if (isEmpty("Contact-name")) {
	alert("Please fill in the Contact Name field.");
	setFocus("Contact-name");
	return false;
}


// check that the Postcode field is valued
if (isEmpty("Postcode")) {
	alert("Please fill in the Postcode field.");
	setFocus("Postcode");
	return false;
}


// check that the email field is valued
if (isEmpty("email")) {
	alert("Please complete the email field with valid email address.");
	setFocus("email");
	return false;
}
// Check that the email address is valid
if (!isAnEmailAddress("email")) {
	alert("The entered email address is invalid. Please check and try again");
	setFocus("email");
	return false;
}


// check that the Tel field is valued
if (isEmpty("Telephone")) {
	alert("Please fill in the Telephone field, we may need to call you.");
	setFocus("Telephone");
	return false;
}


// if  everthing is ok submit the form
return true;
}
