var shopFolder = "/";
var iUKId = 42, iUSId = 43;

var oCountryLocalInfo = new Array;

/* ------------------------------------------------------------------------------------------------- */

function isForename(fld) {
	var pattern = /^[A-z\xC0-\xFF \.'-]{0,20}$/;
	return isFieldType(pattern, "Forename", fld, true);
}

function isSurname(fld) {
	var pattern = /^[A-z\xC0-\xFF '-]{0,20}$/;
	return isFieldType(pattern, "Surname", 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,30}$/;
	return isFieldType(pattern, "Address 1", fld, true);
}

function isAddress2(fld) {
	var pattern = /^[A-z0-9\xC0-\xFF ',\.-]{0,30}$/;
	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 = new RegExp(oCountryLocalInfo['county_regex']);
	return isFieldType(pattern, oCountryLocalInfo['county_label'], fld, oCountryLocalInfo['county_required']);
}

function isPostCode(fld) {
	var pattern = new RegExp(oCountryLocalInfo['post_code_regex']);
	return isFieldType(pattern, oCountryLocalInfo['post_code_label'], fld, oCountryLocalInfo['post_code_required']);
}

function isPhoneNumber(fld) {
	var pattern =  new RegExp(oCountryLocalInfo['phone_regex']);
	return isFieldType(pattern, "Phone", fld, false);
}

function isEmail(fld) {
	var pattern = /^[A-z0-9_-]+(\.[A-z0-9_-]+)*@[A-z0-9_-]+(\.[A-z_-]+)+$/;
	return isFieldType(pattern, "Email", fld, true);
}

function isUserName(fld) {
	var pattern = /^[A-z0-9]{5,10}$/;
	return isFieldType(pattern, "User Name", fld, true);
}

function isPassword(fld) {
	var pattern = /^[A-z0-9]{6,20}$/;
	return isFieldType(pattern, "Password", fld, true);
}

function isOldPassword(fld) {
  var pattern = /^[A-z0-9]{8,20}$/;
  return isFieldType(pattern, "Old Password", fld, true);
}

function isSecurityQuestionID(fldQ,fldA) {
	if (fldQ.value > 0) {
		return isSecurityAnswer(fldA);
	} else {
		return "Select a Security Question\n";
	}
}

function isSecurityAnswer(fld) {
	var pattern = /^[A-z' -]{0,30}$/;
	return isFieldType(pattern, "Security Answer", fld, true);
}

function isCustomerSearch(fld) {
	var pattern = /^[A-z\xC0-\xFF \%'-]{0,100}$/;
	return isFieldType(pattern, "Search", fld, true);
}

/* ------------------------------------------------------------------------------------------------- */

function isProductCode(fld) {
	var pattern = /^[A-Z0-9]{0,10}$/;
	return isFieldType(pattern, 'Code', fld, false);
}

function isProductName(fld) {
	var pattern = /^[A-z0-9\.&' -]{0,50}$/;
	return isFieldType(pattern, 'Product', fld, true);
}

function isDescription(fld) {
	var pattern = /^[^\:]*$/;
	return isFieldType(pattern, 'Description', fld, false);
}

function isImageFile(fld) {
	var pattern = /^[A-z0-9_\.]{1,45}\.(gif|jpg|jpeg|png)$/;
	return isFieldType(pattern, 'Image File', fld, false);
}

function isAwardImageFile(fld) {
	var pattern = /^[A-z0-9_\.]{1,45}\.(gif|jpg|jpeg|png)$/;
	return isFieldType(pattern, 'Award Image File', fld, true);
}

function isThumbFile(fld) {
	var pattern = /^[A-z0-9_\.]{1,45}\.(gif|jpg|jpeg|png)$/;
	return isFieldType(pattern, 'Thumbnail File', fld, false);
}

function isVATRateId(fld) {
	if (fld.value > 0) {
		return "";
	} else {
		return "Select a VAT Rate\n";
	}
}

function isDownloadFile(fld) {
	var pattern = /^[A-z0-9_-]+\.(wav|mp3|aif|au|mid|ra|zip)$/;
	return isFieldType(pattern, "Download File '" + fld.value + "'", fld, false);
}

function isPDFFile(fld) {
	var pattern = /^[A-z0-9_-]+\.(pdf)$/;
	return isFieldType(pattern, "PDF File '" + fld.value + "'", fld, false);
}

function isProductFormatDescription(fld) {
	var pattern = /^[^\:]*$/;
	return isFieldType(pattern, "Product Format Description '" + fld.value + "'", fld, false);
}

function isDemoFile(fld) {
	var pattern = /^[A-z0-9_-]+\.(wav|mp3|aif|au|mid|ra)$/;
	return isFieldType(pattern, "Demo File", fld, false);
}

function isDemoUpload(fld) {
	var pattern = /^[A-z0-9_\\\/:-]+\.(wav|mp3|aif|au|mid|ra)$/;
	return isFieldType(pattern, "Demo Upload", fld, false);
}

function isFreeSamplesFile(fld) {
	var pattern = /^[A-z0-9_-]+\.(wav|mp3|aif|au|mid|ra|zip)$/;
	return isFieldType(pattern, "Free Samples File", fld, false);
}

/* ------------------------------------------------------------------------------------------------- */

function isCountry(fld) {
	var pattern = /^[A-z\xC0-\xFF \(\)\,\.'-]{0,50}$/;
	return isFieldType(pattern, 'Country', fld, true);
}

function isCountryCode(fld) {
	var pattern = /^[A-Z]{2}$/;
	return isFieldType(pattern, 'Country Code', fld, true);
}

function isPhoneCode(fld) {
	var pattern = /^[0-9 ]{0,10}$/;
	return isFieldType(pattern, 'Phone Code', fld, false);
}

/* ------------------------------------------------------------------------------------------------- */

function isDeliveryZone(fld) {
	var pattern = /^[A-z ]{0,30}$/;
	return isFieldType(pattern, 'Delivery Zone', fld, true);
}

function isDeliveryOption(fld) {
	var pattern = /^[A-z ]{0,100}$/;
	return isFieldType(pattern, 'Delivery Option', fld, true);
}

/* ------------------------------------------------------------------------------------------------- */

function isReviewTitle(fld) {
	var pattern = /^[A-z0-9\.' -]{0,100}$/;
	return isFieldType(pattern, 'Title', fld, true);
}

function isReviewName(fld) {
	var pattern = /^[A-z0-9' -]{0,50}$/;
	return isFieldType(pattern, 'Name', fld, true);
}

function isOrganisation(fld) {
	var pattern = /^[A-z0-9'\.\/"<>=#: _-]{0,100}$/;
	return isFieldType(pattern, 'Organisation', fld, false);
}

function isDate(fld) {
	var pattern = /^[0-3]?[0-9]\/[0-1]?[0-9]\/[0-9]{1,4}$/;
	var errMsg = isFieldType(pattern, 'Date', fld, true);
	if (errMsg.length == 0) {
		var date_array = fld.value.split('/');
		var day = date_array[0];
		var month = date_array[1] - 1;
		var year = date_array[2];
		var source_date = new Date(year,month,day);
		if(year != source_date.getFullYear() || month != source_date.getMonth() || day != source_date.getDate())
		{
		 errMsg = "Date is invalid\n";
		}
	}
    return errMsg;
}

function isReviewDescription(fld) {
	var pattern = /^[^:]*$/;
	return isFieldType(pattern, 'Description', fld, false);
}

/* ------------------------------------------------------------------------------------------------- */

function isTestimonialTitle(fld) {
	var pattern = /^[A-z0-9\.' -]{0,100}$/;
	return isFieldType(pattern, 'Title', fld, true);
}

function isTestimonialName(fld) {
	var pattern = /^[A-z0-9'\.\/"<>=#: _-]{0,100}$/;
	return isFieldType(pattern, 'Name', fld, true);
}

function isOccupation(fld) {
	var pattern = /^[A-z0-9' -]{0,50}$/;
	return isFieldType(pattern, 'Occupation', fld, false);
}

function isTestimonialDescription(fld) {
	var pattern = /^[^:]*$/;
	return isFieldType(pattern, 'Description', fld, false);
}

/* ------------------------------------------------------------------------------------------------- */

function isType(fld) {
	var pattern = /^[A-z -]{0,20}$/;
	return isFieldType(pattern, 'Type', fld, true);
}

function isStyle(fld) {
	var pattern = /^[A-z -]{0,20}$/;
	return isFieldType(pattern, 'Style', fld, true);
}

function isSeries(fld) {
	var pattern = /^[A-z -]{0,20}$/;
	return isFieldType(pattern, 'Series', fld, true);
}

function isFormat(fld) {
	var pattern = /^[A-z \/-]{0,20}$/;
	return isFieldType(pattern, 'Format', fld, true);
}

function isVatRateName(fld) {
	var pattern = /^[A-z -]{0,20}$/;
	return isFieldType(pattern, 'Rate Name', fld, true);
}

function isGenreDescription(fld) {
	var pattern = /^[^\:]*$/;
	return isFieldType(pattern, 'Description', fld, false);
}

function isSeriesDescription(fld) {
	var pattern = /^[^\:]*$/;
	return isFieldType(pattern, 'Description', fld, false);
}

function isSeriesImageFile(fld) {
	var pattern = /^[A-z0-9_\.]{1,45}\.(gif|jpg|jpeg|png)$/;
	return isFieldType(pattern, 'Image File', fld, false);
}

function isIconFile(fld, required) {
	var pattern = /^[A-z0-9_\.]{1,45}\.(gif|jpg|jpeg|png)$/;
	return isFieldType(pattern, 'Icon File', fld, required);
}

/* ------------------------------------------------------------------------------------------------- */

function isEmailName(fld) {
	var pattern = /^[A-z' -]{0,50}$/;
	return isFieldType(pattern, 'Name', fld, true);
}

function isEmailSubject(fld) {
	var pattern = /^[A-z' -]{0,50}$/;
	return isFieldType(pattern, 'Subject', fld, true);
}

function isEmailMessage(fld) {
	var pattern = /(\n|\r|%0a|%0d)(content-type:|to:|cc:|bcc:|from:)/i;
	if (fld.value.length == 0) {
		return "Message is required\n";
	} else {
		if (pattern.test(fld.value)==false) {
			return "";
		} else {
			return "Invalid characters in Message\n";			
		}
	}
}

/* ------------------------------------------------------------------------------------------------- */

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 fixPrice(fld) {
	fld.value=parseFloat(fld.value.replace(/[^0-9\.]/g, "")).toFixed(2);
	if (isNaN(fld.value)) { fld.value = "0.00"; }
}

function fixWeight(fld) {
	fld.value=parseFloat(fld.value.replace(/[^0-9\.]/g, "")).toFixed(3);
	if (isNaN(fld.value)) { fld.value = ""; }
}

function fixInt(fld) {
	fld.value=parseInt(fld.value.replace(/[^0-9]/g, ""));
	if (isNaN(fld.value)) { fld.value = "0"; }
}

function fixVatPercent(fld) {
	fld.value=parseFloat(fld.value.replace(/[^0-9\.]/g, ""));
	if (isNaN(fld.value)) { fld.value = ""; }
}

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 loadCounties(fieldName) {
	try {
		var oRequest = getXMLHttpRequest();
	} catch (oError) {
		throw new Error("Can't get XMLHttpRequest");
	}
	try {
		oRequest.open('GET', shopFolder + 'phpscripts/uk_counties.php', false);
		oRequest.send(null);
	} catch (oError) {
		throw new Error("Can't get XML data");
	}
	if (oRequest.status == 200) {
		if (oRequest.responseXML.xml == "") {
			throw new Error("No XML data");
		} else {
			 var xmlDoc = oRequest.responseXML;
			 var oCounties = xmlDoc.getElementsByTagName('county');
		}
	} else {
		throw new Error("There was a problem with the request.");
	}
	var oSelect = document.createElement("select");
	oSelect.name = fieldName + "[county]"
	oSelect.id = "county"
	addOption(oSelect, "", true);
	for (var i=0; i < oCounties.length; i++) {
		addOption(oSelect, oCounties[i].firstChild.data);
	}
	return oSelect;
}

function addOption(oSelect, sOption, bSelected) {
	var oCountyOption = document.createElement("option");
	oCountyOption.value = sOption;
	oCountyOption.appendChild(document.createTextNode(sOption));
	if (arguments.length == 3) {
		if (bSelected) {
			oCountyOption.selected = true;
		}
	}
	oSelect.appendChild(oCountyOption);
}

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 loadCountryLocalInfo(iCountryId) {
	try {
		var oRequest = getXMLHttpRequest();
	} catch (oError) {
		throw new Error("Can't get XMLHttpRequest");
	}
	try {
		oRequest.open('GET', shopFolder + 'phpscripts/country_local_info.php?cid=' + iCountryId, false);
		oRequest.send(null);
	} catch (oError) {
		throw new Error("Can't get XML data");
	}
	if (oRequest.status == 200) {
		if (oRequest.responseXML.xml == "") {
			throw new Error("No XML data");
		} else {
			 var xmlDoc = oRequest.responseXML;
			 var oArrCountryInfo = xmlDoc.getElementsByTagName("country_info");
			 var oCountryInfo = oArrCountryInfo[0];
			 oCountryLocalInfo['country'] = oCountryInfo.childNodes[0].firstChild.nodeValue;
			 oCountryLocalInfo['country_code'] = oCountryInfo.childNodes[1].firstChild.nodeValue;
			 oCountryLocalInfo['phone_code'] = (oCountryInfo.childNodes[2].firstChild != null) ? oCountryInfo.childNodes[2].firstChild.nodeValue : '';
			 oCountryLocalInfo['delivery_zone_id'] = oCountryInfo.childNodes[3].firstChild.nodeValue;
			 oCountryLocalInfo['county_label'] = oCountryInfo.childNodes[4].firstChild.nodeValue;
			 oCountryLocalInfo['post_code_label'] = oCountryInfo.childNodes[5].firstChild.nodeValue;
			 oCountryLocalInfo['phone_regex'] = oCountryInfo.childNodes[6].firstChild.nodeValue;
			 oCountryLocalInfo['county_regex'] = oCountryInfo.childNodes[7].firstChild.nodeValue;
			 oCountryLocalInfo['post_code_regex'] = oCountryInfo.childNodes[8].firstChild.nodeValue;
			 oCountryLocalInfo['county_exists'] = Boolean(parseInt(oCountryInfo.childNodes[9].firstChild.nodeValue));
			 oCountryLocalInfo['county_required'] = Boolean(parseInt(oCountryInfo.childNodes[10].firstChild.nodeValue));
			 oCountryLocalInfo['post_code_before_town'] = Boolean(parseInt(oCountryInfo.childNodes[11].firstChild.nodeValue));
			 oCountryLocalInfo['post_code_required'] = Boolean(parseInt(oCountryInfo.childNodes[12].firstChild.nodeValue));
		}
	} else {
		throw new Error("There was a problem with the request.");
	}
}