var iUKId = 76;

function isForename(fld) {
	var pattern = /^[A-z\xC0-\xFF \.'-]{0,20}$/;
	return isFieldType(pattern, "First Name", fld, true);
}

function isSurname(fld) {
	var pattern = /^[A-z\xC0-\xFF '-]{0,20}$/;
	return isFieldType(pattern, "Last Name", fld, true);
}

function isTitle(fld) {
	var pattern = /^[A-z\. ]{0,8}$/;
	return isFieldType(pattern, "Title", fld, false);
}

function isAddress1(fld) {
	var pattern = /^[A-z0-9\xC0-\xFF ',\.-]{0,40}$/;
	return isFieldType(pattern, "Address 1", fld, true);
}

function isAddress2(fld) {
	var pattern = /^[A-z0-9\xC0-\xFF ',\.-]{0,40}$/;
	return isFieldType(pattern, "Address 2", fld, false);
}

function isTown(fld) {
	var pattern = /^[A-z\xC0-\xFF '\.\(\)-]{0,30}$/;
	return isFieldType(pattern, "Town", fld, true);
}

function isCounty(fld) {
	var pattern = /^[A-z\xC0-\xFF '\.\(\)-]{0,30}$/;
	return isFieldType(pattern, "County", fld, false);
}

function isPostCode(fld, countryId) {
	if (countryId == iUKId) {
		var pattern = /^[A-Z]{1,2}[1-9][A-Z0-9]{0,1} [0-9][A-Z]{2}$/;
		return isFieldType(pattern, "Post Code", fld, true);
	} else {
		var pattern = /^[A-z0-9 \/\.\(\)-]{0,30}$/;
		return isFieldType(pattern, "Post Code", fld, true);
	}
}

function isPhoneNumber(fld) {
	var pattern = /^[A-z0-9 \+\.\(\)-]{0,50}$/;
	return isFieldType(pattern, "Phone Number", fld, true);
}

function isEmail(fld) {
	var pattern = /^[A-z0-9_-]+(\.[A-z0-9_-]+)*@[A-z0-9_-]+(\.[A-z_-]+)+$/;
	return isFieldType(pattern, "Email", fld, true);
}

function isPassword(fld) {
	var pattern = /^[A-z0-9]{6,12}$/;
	return isFieldType(pattern, "Password", fld, true);
}

function isFoodRequirements(fld) {
	var pattern = /^.*$/;
	return isFieldType(pattern, "Dietary Requirements", fld, true);
}

function isRequirements(fld) {
	var pattern = /^.*$/;
	return isFieldType(pattern, "Other Requirements", fld, true);
}

function isAttendees(fld, required) {
	var pattern = /^.*$/;
	return isFieldType(pattern, "List of those attending", fld, required);
}

/* ------------------------------------------------------------------------------------------------- */

function isFieldType(re, lbl, fld, rqd) {
	var isValid = "";
	if (fld.value.length == 0) {
		if (rqd) {
			isValid = lbl + " is required\n";
		}
	} else {
		if (re.test(fld.value)==false) {
			isValid = lbl + " is not valid\n";			
		}
	}
	return isValid;
}

/* ------------------------------------------------------------------------------------------------- */

function passwordsMatch(pw1, pw2) {
	if (pw1.value == pw2.value) {
		return "";
	} else {
		return "Passwords don't match";
	}
}

/* ------------------------------------------------------------------------------------------------- */

function fixUKPostCode(fld) {
	if ((fld.value.length > 0) && (oCountryIDField.value == iUKId)) {
		var postCode = fld.value.toUpperCase().replace(/^\s+|\s+$/g,"");
		var pattern = /^[A-Z]{1,2}[1-9][A-Z0-9]{0,1} [0-9][A-Z]{2}$/;
		if (pattern.test(postCode)) {
			fld.value = postCode;
		} else {
			var pattern = /^[A-Z]{1,2}[1-9][A-Z0-9]?[0-9][A-Z]{2}$/;
			if (pattern.test(postCode)==false) {
				alert('Not a valid UK post code format');
				setTimeout("oPostCodeField.focus(); oPostCodeField.select();",1);
			} else {
				var pattern = /[0-9][A-Z]{2}/;
				fld.value = postCode.replace(pattern, " $&");
			}
		}
	}
}

function fixUSPhoneNumber(fld) {
	if (fld.value.length > 0) {
		var phone = fld.value;
		var re = /[^0-9]/g;
		phone = phone.replace(re, "");
		if (phone.length == 10) {
			var newPhone = '(' + phone.substring(0,3) + ') ' + phone.substring(3,6) + '-' + phone.substring(6,10);
			fld.value = newPhone;
		} else {
			alert('Not a valid US phone number');
			setTimeout("oPhoneNumberField.focus(); oPhoneNumberField.select();",1);
		}
	}
}

function getQuantity(sQuantity){
	var iQuantity = numberValue(sQuantity);
	if (iQuantity==0) {
		return 1;
	} else {
		return iQuantity;
	}
}

function numberValue(sValue) {
	var iValue = parseInt(sValue);
	if (isNaN(iValue)) {
		return 0;
	} else {
		return iValue;
	}
}


/* ------------------------------------------------------------------------------------------------- */

function getXMLHttpRequest() {
	if (window.XMLHttpRequest){
		// If IE7+, Mozilla, Safari, Opera, etc: Use native object
		return new XMLHttpRequest();
	} else {
		if (window.ActiveXObject){
			// ...otherwise, use the ActiveX control for IE5.x and IE6
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
			var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", 
									"MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", 
									"Microsoft.XMLHTTP"];
			for (var i=0; i < arrSignatures.length; i++) {
				try {
					return new ActiveXObject(arrSignatures[i]);
				} catch (oError) {
					//ignore
				}
			}
		}
	}
	throw new Error("MSXML not installed on your system");
}

function formatCurrency(nAmount) {
	return '£' + parseFloat(nAmount).toFixed(2);
}

function checkDuplicateEmail(email, customerId) {
	var message = "";
	try {
		var oRequest = getXMLHttpRequest(); // getXMLHttpRequest is in basic.js
	} catch (oError) {
		return message;
	}
	try {
		oRequest.open('GET', "/xml/xml_duplicate_email.php?email=" + email + "&id=" + customerId, false);
		oRequest.send(null);
	} catch (oError) {
		return message;
	}
	
	if (oRequest.status == 200) {
		if (oRequest.responseXML.xml != "") {
			 var xmlDoc = oRequest.responseXML;
			 var oDuplicates = xmlDoc.getElementsByTagName("Duplicates");
			 if (oDuplicates[0].hasChildNodes()) {
				 var iDuplicates = oDuplicates[0].firstChild.nodeValue;
			 	 if (iDuplicates > 0) {
					 message = "That email address is already in use. ";
					 message += "If you have previously registered it then ";
					 message += "please log in rather than creating a new account.";
				 }
			 }
		}
	}
	return message;
}

function checkEmailExists(email) {
	var message = "";
	try {
		var oRequest = getXMLHttpRequest(); // getXMLHttpRequest is in basic.js
	} catch (oError) {
		return message;
	}
	try {
		oRequest.open('GET', "/xml/xml_duplicate_email.php?email=" + email + "&id=0", false);
		oRequest.send(null);
	} catch (oError) {
		return message;
	}
	
	if (oRequest.status == 200) {
		if (oRequest.responseXML.xml != "") {
			 var xmlDoc = oRequest.responseXML;
			 var oDuplicates = xmlDoc.getElementsByTagName("Duplicates");
			 if (oDuplicates[0].hasChildNodes()) {
				 var iDuplicates = oDuplicates[0].firstChild.nodeValue;
			 	 if (iDuplicates == 0) {
					 message = "I'm sorry but that email does not match a customer on our system.";
				 }
			 }
		}
	}
	return message;
}



