//formcheck
//Added [0] instead of [1] to fix errors

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("name")) {
	alert("Could we have your name please.");
	setFocus("name");
	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("tel")) {
	alert("Please fill in the Telephone field, we may need to call you.");
	setFocus("tel");
	return false;
}

// check that the address field is valued
if (isEmpty("address")) {
	alert("Please fill in the Address field.");
	setFocus("address");
	return false;
}

// check that the postcode field is valued
if (isEmpty("postcode")) {
	alert("Please fill in the Postcode field.");
	setFocus("postcode");
	return false;
}

// if  everthing is ok submit the form
return true;
}