

(function(){
	var DOM = YAHOO.util.Dom,
		Event = YAHOO.util.Event;

	function CitySelector(prefix){
		this.selCountry = DOM.get(prefix+'selCountry');
		this.selRegion = DOM.get(prefix+'selRegion');
		this.selCity = DOM.get(prefix+'selCity');
		this.txtZipcode = DOM.get(prefix+'txtZipcode');
		this.lnkZipcodeLookup = DOM.get(prefix+'lnkZipcodeLookup');

		if (!this.selCountry || !this.selRegion || !this.selCity){
			Event.onDOMReady(function(){
				this.selCountry = DOM.get(prefix+'selCountry');
				this.selRegion = DOM.get(prefix+'selRegion');
				this.selCity = DOM.get(prefix+'selCity');
				this.txtZipcode = DOM.get(prefix+'txtZipcode');
				this.lnkZipcodeLookup = DOM.get(prefix+'lnkZipcodeLookup');

				this.bindElements();
			}, this, true);
		}
		else {
			this.bindElements();
		}
	}

	CitySelector.prototype._validate = function(){
		if (!this.selCountry || !this.selRegion || !this.selCity){
			throw "Invalid Form";
		}
	};

	CitySelector.prototype.bindElements = function(){
		this._validate();
		var cb = (function(fn, obj){
			return function(xhr){
				fn.call(obj, xhr);
			};
		})(this.loadResults, this);

		Event.on(this.selRegion, 'change', function(){ this.loadData('getCityList', [this.selRegion.value], cb); }, this, true);
		Event.on(this.selCountry, 'change',  function(){ this.loadData('getStateList', [this.selCountry.value], cb); }, this, true);
		Event.on(this.selCity, 'change',  function(){ this.loadData('getCityZip', [this.selCity.value], cb); }, this, true);

		if (this.lnkZipcodeLookup){
			Event.on(this.lnkZipcodeLookup, 'click',  function(){ 
				zip = prompt('Zip Code?','');
				if (zip !== null && zip !== undefined){
					this.loadData('getCityFromZip', [zip], cb); 
				}
			}, this, true);
		}
	};

	CitySelector.prototype.lookupZipcode = function(zip){
		var params = [zip];
		var cb = (function(fn, obj){
			return function(xhr){
				fn.call(obj, xhr);
			};
		})(this.loadResults, this);

		this.loadData('getCityFromZip', params, loadResults);
	};


	CitySelector.prototype.loadData =  function(call, params, cb){
		var paramList = params.join('&params[]=');
		YAHOO.util.Connect.asyncRequest('get', '/jsquery.php?queryname='+call+'&params[]='+paramList, {success: cb});
	};

	CitySelector.prototype.loadResults = function(xhr){
		this._validate();
		var dlState = this.selRegion;
		var dlCountry = this.selCountry;
		var dlCity = this.selCity;
		var txtZipcode = this.txtZipcode;

		//alert(xhr.responseText);
		//Load the country list if it exists.
		var countryList = xhr.responseXML.getElementsByTagName('countryList');
		if (countryList && countryList.length > 0){
			countryList = countryList[0].getElementsByTagName('country');
			if (countryList.length == 0){
				dlState.length = 1;
				dlState.disabled=true;
				dlCity.length = 1;
				dlCity.disabled=true;
				if (txtZipcode) txtZipcode.value = '';
			}
			else {
				dlCountry.length = 1;
				dlCountry.disabled=false;

				dlState.length = 1;
				dlState.disabled=true;
				dlCity.length = 1;
				dlCity.disabled=true;
				if (txtZipcode) txtZipcode.value = '';

				for (var i=0; i<countryList.length; i++){
					var country = countryList[i];
					var opt = document.createElement('option');
					dlCountry.appendChild(opt);
					opt.value = country.getAttribute('id');
					opt.text = country.firstChild.nodeValue;
				}
			}
		}

		//Load the region list
		var regionList = xhr.responseXML.getElementsByTagName('regionList');
		if (regionList && regionList.length > 0){
			regionList = regionList[0].getElementsByTagName('region');
			if (regionList.length == 0){
				dlState.length = 1;
				dlState.disabled = true;
				dlCity.length=1;
				dlCity.disabled=true;
				if (txtZipcode) txtZipcode.value='';
			}
			else {
				dlState.length = 1;
				dlState.disabled=false;
				dlCity.length = 1;
				dlCity.disabled=true;
				if (txtZipcode) txtZipcode.value = '';

				for (var i=0; i<regionList.length; i++){
					var region = regionList[i];
					var opt = document.createElement('option');
					dlState.appendChild(opt);
					opt.value = region.getAttribute('id');
					opt.text = region.firstChild.nodeValue;
				}
			}
		}


		//Load the city list
		var cityList = xhr.responseXML.getElementsByTagName('cityList');
		if (cityList && cityList.length > 0){
			cityList = cityList[0].getElementsByTagName('city');
			if (cityList.length == 0){
				dlCity.length = 1;
				dlCity.disabled=true;
				if (txtZipcode) txtZipcode.value='';
			}
			else {
				dlCity.length = 1;
				dlCity.disabled=false;
				if (txtZipcode) txtZipcode.value = '';

				for (var i=0; i<cityList.length; i++){
					var city = cityList[i];
					var opt = document.createElement('option');
					dlCity.appendChild(opt);
					opt.value = city.getAttribute('id');
					opt.text = city.firstChild.nodeValue;
				}
			}
		}

		var selected = xhr.responseXML.getElementsByTagName('selectedList');
		if (selected && selected.length > 0){
			selected = selected[0];

			var selectedCountry = selected.getElementsByTagName('country');
			if (selectedCountry && selectedCountry.length > 0){
				selectedCountry = selectedCountry[0];
			}
			else {
				selectedCountry = null;
			}

			var selectedState = selected.getElementsByTagName('region');
			if (selectedState && selectedState.length > 0){
				selectedState = selectedState[0];
			}
			else {
				selectedState = null;
			}

			var selectedCity = selected.getElementsByTagName('city');
			if (selectedCity && selectedCity.length > 0){
				selectedCity = selectedCity[0];
			}
			else {
				selectedCity = null;
			}

			if (selectedCountry){
				selectedCountry = selectedCountry.getAttribute('id');
				for (var i=0; i<dlCountry.options.length; i++){
					if (selectedCountry == dlCountry.options[i].value){
						dlCountry.options[i].selected=true;
						break;
					}
				}
			}

			if (selectedState){
				selectedState = selectedState.getAttribute('id');
				for (var i=0; i<dlState.options.length; i++){
					if (selectedState == dlState.options[i].value){
						dlState.options[i].selected=true;
						break;
					}
				}
			}

			if (selectedCity){
				selectedCityId = selectedCity.getAttribute('id');
				for (var i=0; i<dlCity.options.length; i++){
					if (selectedCityId == dlCity.options[i].value){
						dlCity.options[i].selected=true;
						break;
					}
				}

				try {
					txtZipcode.value = selectedCity.getAttribute('zipcode');
				}
				catch (e){}
			}
		}
	};

	window['CitySelector'] = CitySelector;
})();


