// JavaScript Document
function getObj(name)
{
 if (document.getElementById)
 {
   return document.getElementById(name);
 }
 else if (document.all)
 {
   return document.all[name];
 }
 else if (document.layers)
 {
   return document.layers[name];
 }
 else return false;
}

function validChar(fVal) {
 var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@., "
 var ok = "yes";
 var temp;
 for (var i=0; i<fVal.length; i++) {
   temp = "" + fVal.substring(i, i+1);
   if (valid.indexOf(temp) == "-1") ok = "no";
 }
 if (ok == "no") {
   return false;
 }
 else return true;
}

function validNum(fVal) {
 var valid = "0123456789 ()."
 var ok = "yes";
 var temp;
 for (var i=0; i<fVal.length; i++) {
   temp = "" + fVal.substring(i, i+1);
   if (valid.indexOf(temp) == "-1") ok = "no";
 }
 if (ok == "no") {
   return false;
 }
 else return true;
}

function chkEmail(email) {
	atPos = email.indexOf("@");
	stopPos = email.lastIndexOf(".");
	
	if (email == "") {
		return false;
	}
	
	if (atPos == -1 || stopPos == -1) {
		return false;
	}
	
	if (stopPos < atPos) {
		return false;
	}
	
	if (stopPos - atPos == 1) {
		return false;
	}
	return true;
}

function chkEnquiry(frmName) {
	var x = getObj(frmName);
	var errors = false;
	var message = "Sorry, you have filled out one or more of the form fields incorrectly.\n";
	
	if((!validChar(x.name.value)) || (x.name.value == "")) {
		message += "The name field must not be blank and can only contain the characters Aa-Zz, 0-9, ',' and '.'\n";
		errors = true;
	}
	if((!validNum(x.tel.value)) && (!x.tel.value == "")) {
		message += "The telephone field must be a number and not blank\n";
		errors = true;
	}
	if((!validChar(x.msg.value)) || (x.msg.value == "")){
		message += "You need to type in your message and can only contain the characters Aa-Zz, 0-9, ',' and '.'\n";
		errors = true;
	}
	if((!chkEmail(x.email.value)) || (x.email.value == "")){
		message += "You have entered a blank or invalid email address\n";
		errors = true;
	}
	if (errors) {
		message += "Please change these details and click on the submit button again.";
		errors = false;
		alert(message);
		return false;
	}
	else return true;
}