<!-- 
//******************** SUPPORT FUNCTIONS ********************/

function isAlphabetic(val){
	return (/^[A-Z]+$/i.test(val));}


function isAlphaNumeric(val){
	return (!/[^A-Z\d]/i.test(val) && /[A-Z]/i.test(val) && /[\d]/.test(val));}


function isDate(theField, pastOrFuture){
	var theDate = theField.value;
	// remove leading spaces
	while (theDate.indexOf(" ") == 0){
		theDate = theDate.substring(1);}
	// remove trailing spaces
	while (theDate.lastIndexOf(" ") == theDate.length-1){
		theDate = theDate.substring(0, theDate.length-1);}
	// check delimiters
	var delim1 = theDate.indexOf("/");
	if (delim1 == -1) delim1 = theDate.indexOf("-");
	if (delim1 == -1) delim1 = theDate.indexOf(" ");
	var delim2 = theDate.lastIndexOf("/");
	if (delim2 == -1) delim2 = theDate.lastIndexOf("-");
	if (delim2 == -1) delim2 = theDate.lastIndexOf(" ");
	if (delim1 == -1 || delim2 == -1 || delim1 == delim2) return false;
	
	var theDay = theDate.substring(0,delim1).toUpperCase();
	var theMonth = theDate.substring(delim1+1, delim2).toUpperCase();
	var theYear = theDate.substring(delim2+1);
	
	if (theDay.substring(theDay.length-2) == "ST" || theDay.substring(theDay.length-2) == "ND" || theDay.substring(theDay.length-2) == "RD" || theDay.substring(theDay.length-2) == "TH"){
		theDay = theDay.substring(0, theDay.length-2);}
	
	if (theDay <= 0 || theDay > 31 || isNaN(theDay)) return false;
	if ((theYear.length != 2 && theYear.length != 4) || isNaN(theYear)) return false;
	
	if (theYear < 70) theYear = "20" + theYear;
	else if (theYear < 100) theYear = "19" + theYear;

	var theMon = 0;
	var textMonth;
	if (theMonth == "JAN" || theMonth == "JANUARY" || theMonth == "01" || theMonth == "1") {
		theMon = 1;
		textMonth = "01";}
	else if (theMonth == "FEB"  || theMonth == "FEBRUARY"|| theMonth == "02" || theMonth == "2") {
		theMon = 2;
		textMonth = "02";}
	else if (theMonth == "MAR" || theMonth == "MARCH" || theMonth == "03" || theMonth == "3") {
		theMon = 3;
		textMonth = "03";}
	else if (theMonth == "APR" || theMonth == "APRIL" || theMonth == "04" || theMonth == "4") {
		theMon = 4;
		textMonth = "04";}
	else if (theMonth == "MAY" || theMonth == "05" || theMonth == "5") {
		theMon = 5;
		textMonth = "05";}
	else if (theMonth == "JUN" || theMonth == "JUNE" || theMonth == "06" || theMonth == "6") {
		theMon = 6;
		textMonth = "06";}
	else if (theMonth == "JUL" || theMonth == "JULY" || theMonth == "07" || theMonth == "7") {
		theMon = 7;
		textMonth = "07";}
	else if (theMonth == "AUG" || theMonth == "AUGUST" || theMonth == "08" || theMonth == "8") {
		theMon = 8;
		textMonth = "08";}
	else if (theMonth == "SEP" || theMonth == "SEPT" || theMonth == "SEPTEMBER" || theMonth == "09" || theMonth == "9") {
		theMon = 9;
		textMonth = "09";}
	else if (theMonth == "OCT" || theMonth == "OCTOBER" || theMonth == "10") {
		theMon = 10;
		textMonth = "10";}
	else if (theMonth == "NOV" || theMonth == "NOVEMBER" || theMonth == "11") {
		theMon = 11;
		textMonth = "11";}
	else if (theMonth == "DEC" || theMonth == "DECEMBER" || theMonth == "12") {
		theMon = 12;
		textMonth = "12";}
	if (theMon == 0) return false;
   
	if (theMon == 1 || theMon == 3 || theMon == 5 || theMon == 7 || theMon == 8 || theMon == 10 || theMon==12) {
		if (theDay > 31) return false;}
	else if (theMon == 2) {
		if ((parseInt(theYear/4) != parseFloat(theYear/4) || (parseInt(theYear/100) == parseFloat(theYear/100) && parseInt(theYear/400) != parseFloat(theYear/400))) && theDay > 28) return false; 
		else if (theDay > 29)	return false;}
	else if (theDay > 30) return false;

	var todayDate = new Date();
	if (pastOrFuture.toUpperCase() == "P"){
		theDate = new Date(theYear, theMon-1, theDay, 23, 59, 59); 
		if (theDate > todayDate) return false}
	if (pastOrFuture.toUpperCase() == "F"){
		theDate = new Date(theYear, theMon-1, theDay, 0, 0, 0); 
		if (theDate < todayDate) return false}
      
	if (theDay < 10 && theDay.substring(0,1) != 0) theDay = "0" + theDay;
	theField.value = theDay + "/" + textMonth + "/" + theYear; 
	return true;}


function isEmail(val){
	var i;
	// check for only 1 '@' in the address and split into username and domain
	var arrEmail = val.split('@');
	if (arrEmail.length != 2){
		return false;}
	var strUser = arrEmail[0];
	var strDomain = arrEmail[1];
	// check for invalid characters
	for (i=0; i<val.length; i++){
		if (val.charCodeAt(i) > 127){
			return false;}}
	// split username at '.' and then test each section for invalid characters or quoted
	var arrUser = strUser.split(".");
	for (i=0; i<arrUser.length; i++){
		if (arrUser[i].search(/^([^\s\\\(\)<>,;:\"\[\]]|(\"[^\"]+\"))+$/) == -1){
			return false;}}
	// check if domain is an IP address '[10.123.34.5]' and if so check it's valid
	var arrIPdom = strDomain.match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
	if (arrIPdom != null){
		var int255 = 0;
		var int0 = 0;
		// exclude '0.0.0.0' and '255.255.255.255'
		for (i=1; i<arrIPdom.length; i++){
			if (arrIPdom[i] > 255){
				return false;}
			if (arrIPdom[i] == 255){
				int255 += 1;}
			if (arrIPdom[i] == 0){
				int0 += 1;}}
		if (int255 == 4 || int0 == 4){
			return false;}
		return true;}
	// if standard domain split at '.' and check at least 2 parts and no invalid characters
	var arrDom = strDomain.split(".");
	if (arrDom.length < 2){
		return false;}
	for (i=0; i<arrDom.length; i++){
		if (arrDom[i].search(/^[^\s\\\(\)<>,;:\"\[\]]+$/) == -1){
			return false;}}
	return true;}


function isEmpty(val){
	return (val == null || val.length == 0 || isWhitespace(val));}


function isFloat(val){
	return (/^[\d]+\.?[\d]+$/.test(val));}


function isNumeric(val){
	return (/^[\d]+$/.test(val));}


function isWhitespace(val){
	return (/^[\s]+$/.test(val));}


//********** strip characters

function stripInitialWhitespace(val){
	var i = 0;
	while (i < val.length && isWhitespace(val.charAt(i)))
		i++;
	return val.substring (i, val.length);}


function stripInitialZeroes(val){
	var i = 0;
	while (i < val.length && val.charAt(i) == "0")
		i++;
	return val.substring (i, val.length);}


function stripCharsInBag(val, bag){
	var returnString = "";
	for (var i=0; i<val.length; i++){
		var c = val.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;}
	return returnString;}


function stripCharsNotInBag(val, bag){
	var returnString = "";
	for (var i=0; i<val.length; i++){
		var c = val.charAt(i);
		if (bag.indexOf(c) != -1) returnString += c;}
	return returnString;}


//********** re-format

function reformat(val){
	var arg;
	var valPos = 0;
	var resultString = "";
	
	for (var i=1; i<reformat.arguments.length; i++){
		arg = reformat.arguments[i];
		if (i%2 == 1) resultString += arg;
		else {
			resultString += val.substring(valPos, valPos + arg);
			valPos += arg;}}
	return resultString;}


function reformatUSZIPCode(val){
	if (val.length == 5) return val;
	else return reformat(val, "", 5, "-", 4);}


//********** warning messages

function warnEmpty(theField, theName){
	theField.style.border = '#FF0000 1px solid';
	theField.style.background = '#ffbbbb';	
	theField.value = "";
	theField.focus();
	alert("You did not enter a value into the " + theName + " field.\nThis is a required field.");
	return false;}


function warnInvalid(theField, strMessage, emptyField){	
	theField.style.border = '#FF0000 1px solid';
	theField.style.background = '#ffbbbb';		
	if (warnInvalid.arguments.length < 3) emptyField = false;
	if (emptyField == true)	theField.value = "";
	theField.focus();
	theField.select();
	alert(strMessage);
	return false;}


function warnLength(theField, theName, minLen, maxLen, exactLen, emptyField){
	if (warnLength.arguments.length < 6) emptyField = false;
	if (emptyField == true)	theField.value = "";
	theField.focus();
	if (exactLen != ""){
		alert("The " + theName + " field should be exactly " + exactLen + " characters in length.");}
	if (minLen != ""){
		alert("The " + theName + " field should be at least " + minLen + " characters in length.");}
	if (maxLen != ""){
		alert("The " + theName + " field should be no more than " + maxLen + " characters in length.");}
	return false;}


function warnNumeric(theField, theName, emptyField){
	if (warnNumeric.arguments.length < 3) emptyField = false;
	if (emptyField == true) theField.value = "";
	theField.focus();
	theField.select();
	alert("The " + theName + " field must be numeric.");
	return false;}


function warnValue(theField, theName, minVal, maxVal, emptyField){
	if (warnValue.arguments.length < 5) emptyField = false;
	if (emptyField == true)	theField.value = "";
	theField.focus();
	if (minVal != ""){
		alert("The smallest allowable value for the " + theName + " field is " + minVal + ".");}
	if (maxVal != ""){
		alert("The largest allowable value for the " + theName + " field is " + maxVal + ".");}
	return false;}

//******************** END SUPPORT FUNCTIONS ********************/


//******************** PUBLIC FUNCTIONS ********************/

//********** validate single field

function checkCheckbox(theField, theName){
	if(theField.length){
		for (var i=0; i<theField.length; i++){
			if (theField[i].checked) return true;}
		theField[0].focus();}
	else {
		if (theField.checked) return true;
		theField.focus();}
	alert("You did not select a value from the " + theName + " field.\nThis is a required field.");
	return false;}


function checkDate(theField, theName, pastOrFuture, emptyOK){
	if (emptyOK == true && isEmpty(theField.value)){
		theField.value = "";
		return true;}
	if (emptyOK == false && isEmpty(theField.value)) return warnEmpty(theField, theName);
	if (pastOrFuture == "") pastOrFuture = "A";
	if (!isDate(theField, pastOrFuture)){
		return warnInvalid(theField, "The " + theName + " field must contain a valid date.");}	
	return true;}


function checkEmail(theField, theName, emptyOK){
	
	theField.style.border = '#000000 1px solid';
	theField.style.background = '#ffffff';	
		
	if (emptyOK == true && isEmpty(theField.value)){
		theField.value = "";
		return true;}
	if (emptyOK == false && isEmpty(theField.value)) return warnEmpty(theField, theName);
	if (!isEmail(theField.value)){
		return warnInvalid(theField, "The " + theName + " field appears to contain an invalid email address.");}
	return true;}


function checkLength(theField, theName, minLen, maxLen, exactLen, emptyOK){
	if (emptyOK == true && isEmpty(theField.value)){
		theField.value = "";
		return true;}
	if (emptyOK == false && isEmpty(theField.value)) return warnEmpty(theField, theName);
	if (exactLen != ""){
		if (theField.value.length != exactLen){
			return warnLength(theField, theName, "", "", exactLen);}}
	if (minLen != ""){
		if (theField.value.length < minLen){
			return warnLength(theField, theName, minLen, "", "");}}
	if (maxLen != ""){
		if (theField.value.length > maxLen){
			return warnLength(theField, theName, "", maxLen, "");}}
	return true;}


function checkNumeric(theField, theName, minVal, maxVal, floatOK, emptyOK){
	theField.style.border = '#000000 1px solid';
	theField.style.background = '#ffffff';	
	if (emptyOK == true && isEmpty(theField.value)){
		theField.value = "";
		return true;}
	if (emptyOK == false && isEmpty(theField.value)) return warnEmpty(theField, theName);
	if (isNaN(theField.value)){
		return warnNumeric(theField, theName);}
	if (floatOK == false){
		if (!isNumeric(stripCharsInBag(theField.value,"-."))){
			return warnNumeric(theField, theName);}}			
	if (minVal != ""){
		if (parseFloat(theField.value) < minVal){
			return warnValue(theField, theName, minVal, "");}}
	if (maxVal != ""){
		if (parseFloat(theField.value) > maxVal){
			return warnValue(theField, theName, "", maxVal);}}
	return true;}


function checkPassword(theField, theName){
	if (!checkLength(theField, theName, 5, 12, "", false)) return false;
	if (!isAlphaNumeric(theField.value)){
		return warnInvalid(theField, "The " + theName + " field must be alphanumeric.\n(Contain only numbers and letters).");}
	return true;}


function checkPostcode(theField, theName, emptyOK){

	theField.style.border = '#000000 1px solid';
	theField.style.background = '#ffffff';		
	
	if (emptyOK == true && isEmpty(theField.value)){
		theField.value = "";
		return true;}
	if (emptyOK == false && isEmpty(theField.value)) return warnEmpty(theField, theName);
	var arrPostcode = theField.value.split(' ');
	if (arrPostcode.length != 2){
		return warnInvalid(theField, "The " + theName + " entered is invalid. It must be U.K. postcode (e.g. SN5 8RS)");}
	var strOutcode = arrPostcode[0];
	var strIncode = arrPostcode[1];
	if (!isAlphaNumeric(strOutcode) || !isAlphaNumeric(strIncode)){
		return warnInvalid(theField, "The " + theName + " entered is invalid.");}
	theField.value = theField.value.toUpperCase();
	return true;}


function checkPostcodeSection(theField, theName, emptyOK){
	if (emptyOK == true && isEmpty(theField.value)){
		theField.value = "";
		return true;}
	if (emptyOK == false && isEmpty(theField.value)) return warnEmpty(theField, theName);
	theField.value = stripCharsInBag(theField.value," ");
	if (!isAlphaNumeric(theField.value)){
		return warnInvalid(theField, "The " + theName + " entered is invalid.");}
	theField.value = theField.value.toUpperCase();
	return true;}


function checkSelect(theField, theName){	

	theField.style.background = '#ffffff';		

	if (theField.options[theField.selectedIndex].value == "" || theField.options[theField.selectedIndex].value.toLowerCase() == "null"){
		theField.style.background = '#ffbbbb';	
		alert("You did not select a value from the " + theName + " field.\nThis is a required field.");
		theField.focus();
		return false;}
	return true;}


function checkString(theField, theName, emptyOK){
	
	theField.style.border = '#000000 1px solid';
	theField.style.background = '#ffffff';		

	if (emptyOK == true && isEmpty(theField.value)){
		theField.value = "";
		return true;}
	if (emptyOK == false && isEmpty(theField.value)) return warnEmpty(theField, theName);
	return true;}


function checkTelephoneNumber(theField, theName, emptyOK){
	theField.style.border = '#000000 1px solid';
	theField.style.background = '#ffffff';		
	if (emptyOK == true && isEmpty(theField.value)){
		theField.value = "";
		return true;}
	if (emptyOK == false && isEmpty(theField.value)) return warnEmpty(theField, theName);
	if (!isNumeric(stripCharsInBag(theField.value,"() +-"))){
		return warnInvalid(theField, "The " + theName + " field contains invalid characters.\nThis field can only contain numbers and the following characters - () +-");}
	return true;}


function checkUSZIPCode (theField, theName, emptyOK){
	theField.style.border = '#000000 1px solid';
	theField.style.background = '#ffffff';		
	if (emptyOK == true && isEmpty(theField.value)){
		theField.value = "";
		return true;}
	if (emptyOK == false && isEmpty(theField.value)) return warnEmpty(theField, theName);
	var ZIPCode = stripCharsInBag(theField.value, "-");
	if (!(isNumeric(ZIPCode) && (ZIPCode.length == 5 || ZIPCode.length == 9)))
		return warnInvalid(theField, "The " + theName + " field must be a 5 or 9 digit U.S. ZIP Code (e.g. 94043).");
	else {
		theField.value = reformatUSZIPCode(ZIPCode);
		return true;}}


//********** compare two fields

function checkCompareValues(fromField, toField, fromName, toName, equalOK, valType){
	var fromVal = fromField.value;
	var toVal = toField.value;
	
	if (valType == "numeric"){
		fromVal = parseFloat(fromVal);
		toVal = parseFloat(toVal);
		
		if (equalOK == true){
			if (toVal < fromVal){
				alert("The " + fromName + " field must be less than or equal to the " + toName + " field.");
				fromField.focus();
				return false;}
			return true;}
		else {
			if (toVal <= fromVal){
				alert("The " + fromName + " field must be less than the " + toName + " field.");
				fromField.focus();
				return false;}
			return true;}}
	if (valType == "date"){
		var fromDateArray = fromVal.split("/");
		var toDateArray = toVal.split("/");
	
		var fromDate = new Date(fromDateArray[2], parseInt(fromDateArray[1]) - 1, fromDateArray[0]);
		var toDate = new Date(toDateArray[2], parseInt(toDateArray[1]) - 1, toDateArray[0]);
	
		if (equalOK == true){
			if (toDate < fromDate){
				alert("The " + fromName + " field must be less than or equal to the " + toName + " field.");
				fromField.focus();
				return false;}
			return true;}
		else {
			if (toDate <= fromDate){
				alert("The " + fromName + " field must be less than the " + toName + " field.")
				fromField.focus();
				return false;}
			return true;}}}


function checkIdenticalValues(theField1, theName1, theField2, theName2, emptyField){
	if (theField1.value != theField2.value){
		if (emptyField == true){
			theField1.value = "";
			theField2.value = "";}
		theField1.focus();
		theField1.select();
		alert("The " + theName1 + " and " + theName2 + " fields must contain identical values.");
		return false;}
	return true;}


function checkEitherOrValues(theField1, theName1, theField2, theName2){
	if (isEmpty(theField1.value) && isEmpty(theField2.value)){
		alert("You must enter a value for either the " + theName1 + " field or the " + theName2 +" field.")
		theField1.focus();
		return false;}
	return true;}


function checkOptionalValues(theField1, theField2, theName1, theName2){
	if (isEmpty(theField1.value) && !(isEmpty(theField2.value))){
		alert("The " + theName1 + " field must also be entered if the " + theName2 + " field is filled in.");
		theField1.focus();
		return false;}
	if (isEmpty(theField2.value) && !(isEmpty(theField1.value))){
		alert("The " + theName2 + " field must also be entered if the " + theName1 + " field is filled in.");
		theField1.focus();
		return false;}
	return true;}


function checkOptionalValuesCheckbox(checkField, valField, checkName, valName){
	if (checkField.checked && isEmpty(valField.value)){
		alert("You must enter a value into the " + valName + " field when the " + checkName + " field has been selected.");
		valField.focus();
		return false;}
	else if(!checkField.checked && !isEmpty(valField.value)){
		alert("You have entered a value into the " + valName + " field, but haven't selected the " + checkName + " field.");
		checkField.focus();
		return false;}	
	return true;}


function checkOptionalValuesSelect(selField, valField, selName, valName){
	if (selField.options[selField.selectedIndex].value != "" && selField.options[selField.selectedIndex].value.toLowerCase() != "null" && isEmpty(valField.value)){
		alert("You must enter a value into the " + valName + " field when the " + selName + " field has been selected.");
		valField.focus();
		return false;}
	else if((selField.options[selField.selectedIndex].value == "" || selField.options[selField.selectedIndex].value.toLowerCase() == "null") && !isEmpty(valField.value)){
		alert("You have entered a value into the " + valName + " field, but haven't selected a value from the " + selName + " field.");
		selField.focus();
		return false;}	
	return true;}

//********** textarea counter

function textCounter(theField, maxLen, theCountField){
	if (theField.value.length > maxLen){
		theField.value = theField.value.substring(0, maxLen);
		theCountField.value = 0;
		alert("You have exceeded the maximum character limit for this field.");}
	else {
		theCountField.value = maxLen - theField.value.length;}}


//******************** END PUBLIC FUNCTIONS ********************/
// -->

