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 getCheckedSelectOptions(select) {
  var arr = new Array();
  for (var i=0,j=0; i<select.length; i++)
    if (select.options[i].selected)
      arr[j++] = i;
  if (arr.length > 0)
    return arr;
  else
    return -1;
}

function resetStyles(){
	optFacilityName.className = "";
	optManagerName.className  = "";
	optEmail.className        = "";
	optAddress.className      = "";
//	optCounty.className     = "";
	optPostcode.className     = "";
}

function checkForm(lokation){
	document.Form1.Page_Name.value = lokation;

/* define regular expression test patterns */
//	testEmail      = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w][^_]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/; // allowed two hyphens only
//	testEmail      = /^(\w+@\w+(\.\w+)+)|(\w\.\w+@\w+(\.\w+)+)|(\w+@\w+\W\w+(\.\w+)+)|(\w+@\w+\W\w+\W\w+(\.\w+)+)|(\w+@\w+\W\w+\W\w+\W\w+(\.\w+)+)$/; // allowed three hyphens

	testEmail      = /^\+?[a-z0-9](([-+.]|[_]+)?[a-z0-9]+)*@([a-z0-9]+(\.|\-))+[a-z]{2,6}$/; // from http://regexlib.com/Search.aspx?k=email

	testPostcodeUK = /^([A-PR-UWYZ0-9][A-HK-Y0-9][ABCDEFGHJKSTUW0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLNP-UW-Z]{2}|GIR 0AA)$/;

/* form element names */
	optFacilityName = document.Form1.Facility_Name;
	optManagerName  = document.Form1.Manager_Name;
	optEmail        = document.Form1.Email_Address;
	optAddress      = document.Form1.Postal_Address;
	optCounty       = document.Form1.County_Name;
	optPostcode     = document.Form1.Post_Code;

/* reset warning styles */
	resetStyles();

/* form element values */
	optFacilityName.value = trimXS(optFacilityName.value); strFacilityName = optFacilityName.value;
	optManagerName.value  = trimXS(optManagerName.value);  strManagerName  = optManagerName.value;
	optEmail.value        = trimXS(optEmail.value);        strEmail        = optEmail.value;
	optAddress.value      = trimXS(optAddress.value);      strAddress      = optAddress.value;
	strCounty             = optCounty.value; // no trimXS or alert string value required
	optPostcode.value     = trimXS(optPostcode.value);     strPostcode     = optPostcode.value.toUpperCase();

/* define alert messages */
	var msg = "", Flag = 0, FlagFacilityName = 0, FlagManagerName = 0, FlagEmail = 0, FlagAddress = 0, FlagCounty = 0, FlagPostcode = 0;
	var msgFill         = "- - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n";

	var msgFacilityName = "Please enter the Facility Name\n";
	var msgManagerName  = "Please enter the Manager's Name\n";
	var msgEmailA       = "Please enter your Email Address\n";
	var msgEmailB       = "Sorry, but \"" + strEmail + "\" is an invalid Email Address\nPlease check your entry and resubmit\n";
	var msgAddress      = "Please enter your Postal Address\n";
	var msgCounty       = "Please select a County\n";
	var msgPostcodeA    = "Please enter your Postcode\n";
	var msgPostcodeB    = "Sorry, but \"" + strPostcode + "\" is either an invalid Postcode,\nor you have omitted the central space\n";
	
/* step through each validation check and assemble Alert Message */
	if (strFacilityName == "") {
		FlagFacilityName = 1;
		Flag = 1;		
		msg = msg + msgFill + msgFacilityName;
		optFacilityName.className="badValue";
	}
	if (strManagerName == "") {
		FlagManagerName = 1;
		Flag = 2;
		if(FlagFacilityName) { Flag = 1 }
		msg = msg + msgFill + msgManagerName; 
		optManagerName.className="badValue";
	}
	if (strEmail == "") {
		FlagEmail = 1;
		Flag = 3
		if(FlagManagerName) { Flag = 2 }
		if(FlagFacilityName) { Flag = 1 }
		msg = msg + msgFill + msgEmailA;
		optEmail.className="badValue";
	}
	if ((strEmail != "") && (! testEmail.test(strEmail))) {
		FlagEmail = 1;
		Flag = 3
		if(FlagManagerName) { Flag = 2 }
		if(FlagFacilityName) { Flag = 1 }
		msg = msg + msgFill + msgEmailB;
		optEmail.className="badValue";
	}
	if (strAddress == "") {
		FlagAddress = 1;
		Flag = 4
		if(FlagEmail) { Flag = 3 }
		if(FlagManagerName) { Flag = 2 }
		if(FlagFacilityName) { Flag = 1 }
		msg = msg + msgFill + msgAddress;
		optAddress.className="badValueTextArea";
	}
	if ((getCheckedSelectOptions(optCounty) == -1) || (getCheckedSelectOptions(optCounty) == 0)) {
		FlagCounty = 1;
		Flag = 5
		if(FlagAddress) { Flag = 4 }
		if(FlagEmail) { Flag = 3 }
		if(FlagManagerName) { Flag = 2 }
		if(FlagFacilityName) { Flag = 1 }
		msg = msg + msgFill + msgCounty;
//		optCounty.className="badValue";
	}
	if (strPostcode == "") {
		FlagPostcode = 1;
		Flag = 6
		if(FlagCounty) { Flag = 5 }
		if(FlagAddress) { Flag = 4 }
		if(FlagEmail) { Flag = 3 }
		if(FlagManagerName) { Flag = 2 }
		if(FlagFacilityName) { Flag = 1 }
		msg = msg + msgFill + msgPostcodeA;
		optPostcode.className="badValue";
	}
	if ((strPostcode != "") && (! testPostcodeUK.test(strPostcode))) {
		FlagPostcode = 1;
		Flag = 6
		if(FlagCounty) { Flag = 5 }
		if(FlagAddress) { Flag = 4 }
		if(FlagEmail) { Flag = 3 }
		if(FlagManagerName) { Flag = 2 }
		if(FlagFacilityName) { Flag = 1 }
		msg = msg + msgFill + msgPostcodeB;
		optPostcode.className="badValue";
	}

/* determine which box to set focus on and display Alert */	
	if (Flag >=1 ) {
		if (Flag == 1) {optFacilityName.focus();}
		if (Flag == 2) {optManagerName.focus();}
		if (Flag == 3) {optEmail.focus();}
		if (Flag == 4) {optAddress.focus();}
		if (Flag == 5) {optCounty.focus();}
		if (Flag == 6) {optPostcode.focus();}

		alert(msg);
	}

	else {  submitForm(); }
}
