function trimXS(strXS){
	while(strXS.charAt(0)==(" ")){strXS=strXS.substring(1)}
  while(strXS.charAt(strXS.length-1)==" "){strXS=strXS.substring(0,strXS.length-1)}
  return strXS;
}

function resetStyles() {
	document.Form1.Sender_Name.className    ="xprequired";
	document.Form1.sender_address.className ="xprequired";
	document.Form1.Postcode.className       ="xpinput";
	document.Form1.Telephone.className      ="xpinput";
}

function checkForm() {
/* captcha */
	if(document.Form1.mstp.value != ""){
		location.href="contact.asp?rp=1";
	}
	else{

/* define regular expression test patterns */
//		testEmail (OLD)           = /^(\w+@\w+(\.\w+)+)|(\w\.\w+@\w+(\.\w+)+)|(\w+@\w+\W\w+(\.\w+)+)|(\w+@\w+\W\w+\W\w+(\.\w+)+)$/;

		testEmail =   /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w][^_]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;

		testPostcodeUK      = /^([A-PR-UWYZ0-9][A-HK-Y0-9][ABCDEFGHJKSTUW0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLNP-UW-Z]{2}|GIR 0AA)$/;
		testPostcodeForeign = /^[X][A-Z][A-Z]\ [0-9A-Z -]+$/;
		testTelephone       = /[A-Za-z_.@'!"£$%^&*()-]/; // "

/* form element names */
		optName       = document.Form1.Sender_Name;
		optEmail      = document.Form1.sender_address;
		optAddress    = document.Form1.Address;
		optPostcode   = document.Form1.Postcode;
		optTelephone  = document.Form1.Telephone;
		optComments   = document.Form1.Comments;
//	optTransactionDate   = document.Form1.transaction_date;
//	optTransactionReference   = document.Form1.transaction_reference;
//	optProductDescription   = document.Form1.product_description;

/* reset warning styles */
		resetStyles();

/* form element values */
		optName.value                 = trimXS(optName.value);      strName      = optName.value;
		optEmail.value                = trimXS(optEmail.value);     strEmail     = optEmail.value;
		optAddress.value              = trimXS(optAddress.value); // not validated
		optPostcode.value             = trimXS(optPostcode.value);  strPostcode  = optPostcode.value.toUpperCase();
		optTelephone.value            = trimXS(optTelephone.value); strTelephone = optTelephone.value;
		optComments.value             = trimXS(optComments.value); // not validated
//	optTransactionDate.value      = trimXS(optTransactionDate.value); // not validated
//	optTransactionReference.value = trimXS(optTransactionReference.value); // not validated
//	optProductDescription.value   = trimXS(optProductDescription.value); // not validated

/* check whether to use UK or foreign test pattern */
		var pcValid = testPostcodeUK;
		var pcTest  = strPostcode.charAt(0);
		if (pcTest=="X") { pcValid = testPostcodeForeign }

/* replace hyphens in telephone numbers with white space */
		while (strTelephone.search("-") > -1) { strTelephone = strTelephone.replace("-"," ") }
		document.Form1.Telephone.value = strTelephone;

/* define alert messages */
		var msg = "", Flag = 0, FlagName = 0, FlagPostcode = 0, FlagTelephone = 0, FlagEmail = 0;
		var msgFill      = "\n- - - - - - - - - - - - - - - - - - - - - - - - - -\n";

		var msgName      = "Please enter your Name\n";
		var msgEmailA    = "Please enter your Email Address\n";
		var msgEmailB    = "Sorry, but \"" + strEmail + "\" is an invalid Email Address\n";
		var msgPostcode  = "Sorry, but \"" + strPostcode + "\" is an invalid Postcode,\nor you have omitted the central space\n";
		var msgTelephone = "Sorry, but \"" + strTelephone + "\" is an invalid telephone number\n";

/* step through each validation check and assemble Alert Message */
		if (strName == "") {
			FlagName = 1;
			Flag = 1;
			msg = msg + msgFill + msgName;
			optName.className="badValue";
		}

		if (strEmail == "") {
			FlagEmail = 1;
			Flag = 2;
			if(FlagName) { Flag = 1 }
			msg = msg + msgFill + msgEmailA;
			optEmail.className="badValue";
		}

		if ((strEmail != "") && (! testEmail.test(strEmail))) {
			FlagEmail = 1;
			Flag = 2;
			if(FlagName) { Flag = 1 }
			msg = msg + msgFill + msgEmailB;
			optEmail.className="badValue";
		}

		if ((strPostcode != "") && (! pcValid.test(strPostcode))) {
			FlagPostcode = 1;
			Flag = 3;
			if(FlagEmail) { Flag = 2 }
			if(FlagName) { Flag = 1 }
			msg = msg + msgFill + msgPostcode;
			optPostcode.className="badValue";
		}

		if ((strTelephone != "") && (testTelephone.test(strTelephone))) {
			FlagTelephone = 1;
			Flag = 4;
			if(FlagPostcode) { Flag = 3 }
			if(FlagEmail) { Flag = 2 }
			if(FlagName) { Flag = 1 }
			msg = msg + msgFill + msgTelephone;
			optTelephone.className="badValue";
		}

/* determine which box to set focus on and display Alert */	
		if (Flag >=1 ) {
			if (Flag == 1) {optName.focus();}
			if (Flag == 2) {optEmail.focus();}
			if (Flag == 3) {optPostcode.focus();}
			if (Flag == 4) {optTelephone.focus();}

			alert(msg);
		}

//	else { alert("Form Good"); }
		else { document.Form1.submit(); }
	}
}
