/* city, county, and state function */ var _stateSelect = null; var _countySelect = null; var _citySelect = null; var _state_id = 1; var _state = ""; var _states = new Array(); var _counties = new Array(); var State = Class.create(); State.prototype= { initialize: function(id, name) { this.id = id; this.name = name; this.counties = new Array(); }, addCounty: function(county) { this.counties[this.counties.length] = county; }, createOption: function() { return new Option(this.name, this.id); }, createCountyOptions: function() { if(this.counties.length == 0) { ajaxEngine.sendRequest('doStateUpdate', "state_id=" + this.id, "object_id=stateUpdater"); } else { var size = _countySelect.options.length; for(var i = 0; i < this.counties.length; i++) { _countySelect.options[i+1] = this.counties[i].createOption(); } //remove any other counties from the previous county listing for(var i = this.counties.length; i < size; i++) { _countySelect.options[this.counties.length+1] = null; } } }, getCountyById: function(county_id) { for(var i = 0; i < this.counties.length; i++) { if(this.counties[i].id == county_id) { return this.counties[i]; } } return null; } }; var County = Class.create(); County.prototype= { initialize: function(id, name, lat, lng, state_id) { this.id = id; this.name = name; this.lat = lat; this.lng = lng; this.state_id = state_id; this.cities = new Array(); }, addCity: function(city) { this.cities[this.cities.length] = city; }, createOption: function() { return new Option(this.name, this.id); }, createCityOptions: function() { if(this.cities.length == 0) { ajaxEngine.sendRequest('doCountyUpdate', "county_id="+this.id, "state_id=" + this.state_id, "object_id=countyUpdater"); } else { var size = _citySelect.options.length; for(var i = 0; i < this.cities.length; i++) { _citySelect.options[i+1] = this.cities[i].createOption(); } //remove any other cities from the previous county listing for(var i = this.cities.length; i < size; i++) { _citySelect.options[this.cities.length+1] = null; } } }, getCityById: function(city_id) { for(var i = 0; i < this.cities.length; i++) { if(this.cities[i].id == city_id) { return this.cities[i]; } } return null; }, getCityByZip: function(zip) { for(var i = 0; i < this.cities.length; i++) { if(this.cities[i].zip == zip) { return this.cities[i]; } } return null; } }; var City = Class.create(); City.prototype= { initialize: function(id, name, zip, lat, lng) { this.id = id; this.name = name; this.zip = zip; this.lat = lat; this.lng = lng; }, createOption: function() { return new Option(this.zip+" - "+this.name, this.id); } }; _counties[_counties.length] = new County(2905, 'Beaver', 38.2769, -112.641, _state_id); _counties[_counties.length] = new County(2893, 'Box Elder', 41.5005, -112.958, _state_id); _counties[_counties.length] = new County(2894, 'Cache', 41.8153, -111.997, _state_id); _counties[_counties.length] = new County(2895, 'Carbon', 39.6408, -110.561, _state_id); _counties[_counties.length] = new County(2889, 'Daggett', 40.8278, -109.525, _state_id); _counties[_counties.length] = new County(2885, 'Davis', 40.9635, -112.116, _state_id); _counties[_counties.length] = new County(2881, 'Duchesne', 40.1634, -110.401, _state_id); _counties[_counties.length] = new County(2897, 'Emery', 38.9231, -111.252, _state_id); _counties[_counties.length] = new County(2904, 'Garfield', 40.7261, -112.163, _state_id); _counties[_counties.length] = new County(2898, 'Grand', 38.9999, -109.615, _state_id); _counties[_counties.length] = new County(2906, 'Iron', 37.8116, -113.26, _state_id); _counties[_counties.length] = new County(2902, 'Juab', 39.6631, -112.816, _state_id); _counties[_counties.length] = new County(2903, 'Kane', 37.2719, -111.77, _state_id); _counties[_counties.length] = new County(2901, 'Millard', 39.0644, -113.032, _state_id); _counties[_counties.length] = new County(2887, 'Morgan', 41.0422, -111.68, _state_id); _counties[_counties.length] = new County(2909, 'Piute', 38.3289, -112.143, _state_id); _counties[_counties.length] = new County(2890, 'Rich', 41.5713, -111.279, _state_id); _counties[_counties.length] = new County(2883, 'Salt Lake', 40.6682, -111.908, _state_id); _counties[_counties.length] = new County(2896, 'San Juan', 37.7491, -110.227, _state_id); _counties[_counties.length] = new County(2900, 'Sanpete', 39.4225, -111.632, _state_id); _counties[_counties.length] = new County(2899, 'Sevier', 38.5894, -112.258, _state_id); _counties[_counties.length] = new County(2886, 'Summit', 40.6517, -111.505, _state_id); _counties[_counties.length] = new County(2888, 'Tooele', 40.5343, -112.298, _state_id); _counties[_counties.length] = new County(2884, 'Uintah', 41.1435, -111.931, _state_id); _counties[_counties.length] = new County(2882, 'Utah', 40.177, -111.536, _state_id); _counties[_counties.length] = new County(2891, 'Wasatch', 40.2953, -111.256, _state_id); _counties[_counties.length] = new County(2908, 'Washington', 37.1304, -113.497, _state_id); _counties[_counties.length] = new County(2907, 'Wayne', 38.3301, -110.867, _state_id); _counties[_counties.length] = new County(2892, 'Weber', 41.2552, -111.957, _state_id); var _countyUpdater = null; var _stateUpdater = null; function initLocation(countySelect, citySelect, stateSelect) { _countySelect = countySelect; _citySelect = citySelect; _stateSelect = stateSelect; for(var i = 0; i < _counties.length; i++) { _countySelect.options[i+1] = _counties[i].createOption(); } if(citySelect != null) { _countySelect.onchange = function(){ updateCitySelect(); }; } if(_stateSelect != null) { for(var i = 0; i < _states.length; i++) { _stateSelect.options[i+1] = _states[i].createOption(); } _stateSelect.onchange = function(){ updateCountySelect(); }; _stateUpdater = new StateUpdater(); } _countyUpdater = new CountyUpdater(); } var CountyUpdater = Class.create(); CountyUpdater.prototype= { initialize: function() { this.cb = null; ajaxEngine.registerRequest('doCountyUpdate', '/ajax/city_list.php'); ajaxEngine.registerAjaxObject("countyUpdater", this); }, getByZip: function(zip) { ajaxEngine.sendRequest('doCountyUpdate', "zip="+zip, "state_id=" + _state_id, "object_id=countyUpdater"); }, ajaxUpdate: function(ajaxResponse) { var countyId = 0; var city = ajaxResponse.childNodes[0]; var cities = city.childNodes; if(cities.length > 0) { countyId = city.getAttribute("county_id"); var county = getCountyById(countyId); if(county != null) { for(var i = 0; i < cities.length; i++) { var city = cities[i]; county.addCity(new City(city.getAttribute("id"), city.firstChild.nodeValue, city.getAttribute("zip"), city.getAttribute("lat"), city.getAttribute("lng"))); } county.createCityOptions(); } } if(this.cb != null) { this.cb(countyId); } } }; var StateUpdater = Class.create(); StateUpdater.prototype= { initialize: function() { this.cb = null; ajaxEngine.registerRequest('doStateUpdate', '/ajax/county_list.php'); ajaxEngine.registerAjaxObject("stateUpdater", this); }, ajaxUpdate: function(ajaxResponse) { var countyList = ajaxResponse.childNodes[0]; var counties = countyList.childNodes; if(counties.length > 0) { var stateId = countyList.getAttribute("state_id"); var state = getStateById(stateId); if(state != null) { for(var i = 0; i < counties.length; i++) { var county = counties[i]; state.addCounty(new County(county.getAttribute("id"), county.firstChild.nodeValue, county.getAttribute("lat"), county.getAttribute("lng"), state.id)); } state.createCountyOptions(); } } if(this.cb != null) { this.cb(); } } }; function getCountyById(county_id) { if(_states.length == 0) { for(var i = 0; i < _counties.length; i++) { if(_counties[i].id == county_id) { return _counties[i]; } } } else { var state = getSelectedState(); return state.getCountyById(county_id); } return null; } function getStateById(state_id) { for(var i = 0; i < _states.length; i++) { if(_states[i].id == state_id) { return _states[i]; } } return null; } function updateCitySelect() { var countyId = _countySelect.options[_countySelect.selectedIndex].value; var county = getCountyById(countyId); if(county != null)county.createCityOptions(); } function updateCountySelect() { var stateId = _stateSelect.options[_stateSelect.selectedIndex].value; var state = getStateById(stateId); if(state != null)state.createCountyOptions(); } function getSelectedCounty() { var countyId = _countySelect.options[_countySelect.selectedIndex].value; if(_states.length == 0) { return getCountyById(countyId); } else { var state = getSelectedState(); return state.getCountyById(countyId); } } function getSelectedCity() { var county = getSelectedCounty(); if(county == null) { return null; } var cityId = _citySelect.options[_citySelect.selectedIndex].value; return county.getCityById(cityId); } function getSelectedState() { var stateId = _stateSelect.options[_stateSelect.selectedIndex].value; return getStateById(stateId); }