var oCustomerForm;
var oPostCodeField, oPostCodeLabel;
var oCountryIDField;
var oCountyDiv, oCountyField, oCountyList, oCountyLabel;
var oPhoneNumberField;
var bNewCustomer;

window.onload = function() {
	setFormFields();
}

function setFormFields() {
	oCustomerForm = document.forms["customer_form"];
	if (oCustomerForm.elements["customer[password]"] == undefined) {
		bNewCustomer = false;
	} else {
		bNewCustomer = true;
	}
	oCustomerForm.onsubmit = function() { return validateCustomer(this, bNewCustomer); }
	oPostCodeField = oCustomerForm.elements["customer[post_code]"];
	oPostCodeLabel = document.getElementById("post_code_label");
	oPostCodeField.onblur = function() { fixUKPostCode(this); }
	oCountryIDField = oCustomerForm.elements["customer[country_id]"];
	oCountryIDField.onchange = function() { countryChanged(); }
	oCountyDiv = document.getElementById("county_div");
	oCountyLabel = document.getElementById("county_label");
	oCountyField = oCountyDiv.firstChild;
	oPhoneNumberField = oCustomerForm.elements["customer[phone]"];
	
	setCountryOptions();
}

function countryChanged() {
	setCountryOptions();
}

function setCountryOptions() {
	var iCountryId = parseInt(oCountryIDField.value)
	if (iCountryId == 0) {
		for (var i=3; i<oCustomerForm.elements.length; i++) {
			oCustomerForm.elements[i].disabled = true;
			oCustomerForm.elements[i].style.backgroundColor = "#DDDDDD";
		}
	} else {
		for (var i=3; i<oCustomerForm.elements.length; i++) {
			oCustomerForm.elements[i].disabled = false;
			oCustomerForm.elements[i].style.backgroundColor = "#FFFFFF";
		}
		loadCountryLocalInfo(iCountryId);
		switch (iCountryId)
		{
			case iUKId: 
				setUKCounties();
				oPhoneNumberField.onblur = function() { }
				break;
			case iUSId: 
				setCountyField();
				oPhoneNumberField.onblur = function() { fixUSPhoneNumber(this); }
				break;
			default:
				setCountyField();
				oPhoneNumberField.onblur = function() { }
		}
		if (oCountryLocalInfo['county_exists']) {
			oCountyField.style.display = "inline";
			oCountyLabel.style.display = "inline";
		} else {
			oCountyField.style.display = "none";
			oCountyLabel.style.display = "none";
		}
	}
}

function setUKCounties() {
	if (oCountyList == undefined) {
		try {
			oCountyList = loadCounties('customer');
		} catch (oError) {
			oCountyList = null;	
		}
	}			
	oCountyLabel.removeChild(oCountyLabel.firstChild);
	oCountyLabel.appendChild(document.createTextNode(oCountryLocalInfo['county_label'] + ": " + (oCountryLocalInfo['county_required'] ? "*" : "")));
	oPostCodeLabel.removeChild(oPostCodeLabel.firstChild);
	oPostCodeLabel.appendChild(document.createTextNode(oCountryLocalInfo['post_code_label'] + ": " + (oCountryLocalInfo['post_code_required'] ? "*" : "")));
	if (oCountyList == null) {
		return;
	} 
	if (oCountyDiv.firstChild == oCountyField) {
		oCountyDiv.replaceChild(oCountyList, oCountyField);
		for (var i=0; i < oCountyList.options.length; i++) {
			if (oCountyList.options[i].value==oCountyField.value) {
				oCountyList.options[i].selected = true;
			} else {
				oCountyList.options[i].selected = false;
			}
		}
	}
}

function setCountyField() {
	if (oCountyDiv.firstChild == oCountyList) {
		oCountyDiv.replaceChild(oCountyField, oCountyList);
		oCountyField.value = oCountyList.value;
	}
	oCountyLabel.removeChild(oCountyLabel.firstChild);
	oCountyLabel.appendChild(document.createTextNode(oCountryLocalInfo['county_label'] + ": " + (oCountryLocalInfo['county_required'] ? "*" : "")));
	oPostCodeLabel.removeChild(oPostCodeLabel.firstChild);
	oPostCodeLabel.appendChild(document.createTextNode(oCountryLocalInfo['post_code_label'] + ": " + (oCountryLocalInfo['post_code_required'] ? "*" : "")));
}
	
function validateCustomer(frm, newCustomer) {
	var errMsg = "";
	errMsg += isForename(frm.elements['customer[forename]']);
	errMsg += isSurname(frm.elements['customer[surname]']);
	errMsg += isTitle(frm.elements['customer[title]']);
	if (frm.elements['customer[country_id]'].value == 0) { errMsg += "Please select your country of residence\n"; }
	errMsg += isAddress1(frm.elements['customer[address_1]']);
	errMsg += isAddress2(frm.elements['customer[address_2]']);
	errMsg += isTown(frm.elements['customer[town]']);
	errMsg += isCounty(frm.elements['customer[county]']);
	errMsg += isPostCode(frm.elements['customer[post_code]']);
	errMsg += isPhoneNumber(frm.elements['customer[phone]']);
	errMsg += isEmail(frm.elements['customer[email]']);
	errMsg += isUserName(frm.elements['customer[user_name]']);
	if (newCustomer) {
		errMsg += isPassword(frm.elements['customer[password]']);
		errMsg += passwordsMatch(frm.elements['customer[password]'], frm.elements['customer[cpassword]']);
		errMsg += isSecurityQuestionID(frm.elements['customer[security_question_id]'], frm.elements['customer[security_answer]']);
	}
	if (errMsg.length > 0) {
		alert(errMsg);
		return false;
	} else {
		return true;
	}
}