function checkCR(evt) {
var evt  = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
	if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
document.onkeypress = checkCR;

function verify(form) {
//Check to see that all required fields are filled in
var themessage = "Please complete the following fields: ";
if (!doesExist(form.Surname.value)) {
themessage = themessage + " -  Surname";
}
if (!doesExist(form.FirstName.value)) {
themessage = themessage + " -  First name(s) in full";
}
if (!doesExist(form.Address1.value)) {
themessage = themessage + " -  Address 1";
}
if (!doesExist(form.Address2.value)) {
themessage = themessage + " -  Address 2";
}
if (!doesExist(form.PostCode.value)) {
themessage = themessage + " -  Postcode";
}
if (!doesExist(form.Email.value)) {
themessage = themessage + " -  Email";
} else { // Check email format
	checkEmail(form);
	if (emailok == false) {
		themessage = themessage +  " - Email address is invalid";
	}
}
if (!doesExist(form.HomeTel.value) && !doesExist(form.MobileTel.value)) {
themessage = themessage + " -  Telephone Number";
}
if (form.Occupation.selectedIndex==0) {
themessage = themessage + " -  Occupation";
}
if (!doesExist(form.DOB.value)) {
	themessage = themessage + " -  Date of birth";
} else { // Check DOB format
	if (!isValidDate(form.DOB.value)) {
		themessage = themessage +  " - Date of birth is invalid";
	}	
}
if (!doesExist(form.Hear.value)) {
themessage = themessage + " -  How did you hear about us?";
}
var termsCheck = form.AgreeTerms.value;
if (termsCheck == "No") {
if (themessage == "Please complete the following fields: ") {
	themessage = "Please indicate that you want me to email your new client pack including the client questionnaire.";}
else {themessage = themessage + ". Also, please indicate that you want me to email your new client pack including the client questionnaire.";}
}

//alert if fields are empty and cancel form submit
if (themessage == "Please complete the following fields: ") {
	return true;
}
else {
alert(themessage);
return false;
   }
}

function doesExist(inputValue) {
	var aCharExists=0
	if (inputValue) {
		for (var i=0; i<inputValue.length; i++) {
			if (inputValue.charAt(i) != " ") {			
				aCharExists = 1
			}
		}
	}
	if (!aCharExists) {
		return false			
	}
	else {
		return true
	}
}

function checkEmail(form) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(form.Email.value)){
emailok=true;
}
else {
emailok=false;
}
}

function isValidDate(dateStr) {
var datePat = /^(\d{1,2})(\/)(\d{1,2})\2(\d{4})$/; // AW stopped -
var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
return false;
}
month = matchArray[3]; // parse date into variables
day = matchArray[1]; // AW switched 3 and 1
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
return false;
}
if (day < 1 || day > 31) {
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
return false;
   }
}
return true;  // date is valid
}

function checkTerms() {
	if (form.AgreeTerms.checked){
	form.AgreeTerms.value="Yes";
	}
	else {
	form.AgreeTerms.value="No";
	}
}

function goSubmit() {    if (verify(document.form)) { document.form.submit(); }}
