
function AddressHelper() {

	this.countryFieldElem = document.getElementById(AddressHelper.countryFieldId);
	this.stateFieldElem = document.getElementById(AddressHelper.stateFieldId);
	this.stateTextFieldElem = document.getElementById(AddressHelper.stateTextFieldId);

	this.stateFieldParent = this.findParentContainer(this.stateFieldElem);
	this.stateTextFieldParent = this.findParentContainer(this.stateTextFieldElem);
	
	this.handleCountryChange();

}

AddressHelper.countryFieldId = "country_field";
AddressHelper.stateFieldId = "state_field";
AddressHelper.stateTextFieldId = "stateText_field";

AddressHelper.prototype = {

	/**
	 * 
	 */
	findParentContainer: function(elem) {
		if (elem == null) return null;
		
		try {
			var parentElem = elem.parentNode; 
			while (parentElem != null && parentElem.getAttribute("name") != "DataEntryField") {
				parentElem = parentElem.parentNode;
			}
			return parentElem;

		} catch (e) {
		}
		
		return null;
	},
	
	/**
	 * Do not use this. Register the event handler using HTML Event Action.
	 */
	registerEventHandler: function() {
		var myThis = this;
		
		EventUtil.registerEvent(this.financeAndTermsElem, "blur", function() { myThis.handleCountryChange(); });
	},
	
	/**
	 * Hides the field that should not be displayed and clear its value.
	 *
	 * For Australia will hide the text entry (stateText) field and clear the value in the text entry field.
	 * For other countries will hide the select entry (state) field and clear the selected state.
	 */
	handleCountryChange: function() {
		if (this.stateFieldParent != null && this.stateTextFieldParent != null) {
			if (this.countryFieldElem != null && this.countryFieldElem.value == "Australia") {
				this.stateFieldParent.style.display = "";

				this.stateTextFieldParent.style.display = "none";
				this.stateTextFieldElem.value = "";
			} else {
				this.stateTextFieldParent.style.display = "";
				
				this.stateFieldParent.style.display = "none";
				this.stateFieldElem.selectedIndex = 0;
			}
		}
	}
}