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 chkEventFrm(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.fname.value) || (x.fname.value=="Full Name")) {
		message += "The Name field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if (!chkEmail(x.email.value) || (x.email.value=="Email Address")) {
		errors = true;
		message += "Please enter a valid email address\n";
		errors = true;
	}
	if(!validChar(x.company.value)) {
		message += "The Name field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if(!validNum(x.tel.value)) {
		message += "The tel field can only contain a valid number\n";
		errors = true;
	}
	if(!validChar(x.attend.value=="x")) {
		message += "Please slected whether to expect to attend or not\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;
}

function chkPartyFrm(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((x.guests.value=="") || (x.guests.value=="Guest names")) {
		message += "Please enter the names of the guests.\n";
		errors = true;
	}
	if(x.room.value=="x") {
		message += "Please select your room type\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;
}

function chkEnqForm(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=="Full Name")) {
		message += "The Name field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if (!chkEmail(x.email.value) || (x.email.value=="Email Address")) {
		errors = true;
		message += "Please enter a valid email address\n";
		errors = true;
	}
	if(!validChar(x.company.value)) {
		message += "The Name field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if(!validNum(x.tel.value)) {
		message += "The tel field can only contain a valid number\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;
}

function chkCompRegRSVP(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=="Full Name")) {
		message += "The Name field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if (!chkEmail(x.email.value) || (x.email.value=="Email Address")) {
		errors = true;
		message += "Please enter a valid email address\n";
		errors = true;
	}
	if(!validNum(x.tel.value)) {
		message += "The tel field can only contain a valid number\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;
}

function chkShenzhenFrm(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 can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if(!validChar(x.company.value) || (x.company.value=="")) {
		message += "The Company field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if (!chkEmail(x.email.value) || (x.email.value=="")) {
		errors = true;
		message += "Please enter a valid email address\n";
		errors = true;
	}
	if(!validNum(x.tel.value)) {
		message += "The tel field can only contain a valid number\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;
}
