/* city, county, and state function */

var _stateSelect = null;
var _countySelect = null;
var _citySelect = null;
var _areaSelect = null;
var _districtSelect = null;

var _state_id = 17;
var _state = 'ID';

var _states = new Array();
var _counties = new Array();
var _listSources = new Array();
var _areas = new Array();
var _districts = new Array();
var _default_state_obj = null;

var ListSource = Class.create();
ListSource.prototype=
{
   initialize: function(id, name, prefix)
   {
      this.id = id;
      this.name = name;
      this.prefix = prefix;
      this.counties = new Array();
      this.districts = new Array();
   },
   addCounty: function (county)
   {
      this.counties[this.counties.length] = county;
   },
   createOption: function()
   {
      return new Option(this.name, this.prefix);
   },
   createCountyOptions: function() 
   {
      if (this.counties.length == 0) 
      {
         ajaxEngine.sendRequest('doListSourceUpdate', "list_id=" + this.id, "object_id=listSourceUpdater", "prefix="+this.prefix);
      }
      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;
         }
         if (this.counties.length == 1)
         {
             _countySelect.options[1].selected = true;
             this.counties[0].createCityOptions();
         }
         this.updateCounties();
      }
   },
   updateCounties: function()
   {
      _counties = new Array();
      for (var i=0; i<this.counties.length; i++)
      {
         _counties[i] = this.counties[i];
      }
   },
   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;
   },

   addDistrict: function (district)
   {
      this.districts[this.districts.length] = district;
   },
   
   createDistrictOptions: function()
   {
       if (this.districts.length == 0)
       {
           ajaxEngine.sendRequest('doListSourceUpdateDistrict', "list_id=" + this.id, "object_id=listSourceUpdaterDistrict", "prefix="+this.prefix);
      }
      else
      {
         var size = _districtSelect.options.length;
         for (var i = 0; i < this.districts.length; i++)
         {
             _districtSelect.options[i + 1] = this.districts[i].createOption();
         } //remove any other districts from the previous district listing
         for (var i = this.districts.length; i < size; i++)
         {
             _districtSelect.options[this.districts.length + 1] = null;
         }
         if (this.districts.length == 1)
         {
             _districtSelect.options[1].selected = true;
         }
         this.updateCounties();
      }
   },
   updateDistricts: function()
   {
      _districts = new Array();
      for (var i=0; i<this.districts.length; i++)
      {
         _districts[i] = this.districts[i];
      }
   }
};

var State = Class.create();
State.prototype = 
{
   initialize: function(id, name, list_id) 
   {
      this.id = id;
      this.name = name;
      this.list_id = list_id;
      this.counties = new Array();
   },
   addCounty: function(county) 
   {
      this.counties[this.counties.length] = county;
   },
   createOption: function() 
   {
      var option = new Option(this.name, this.id);
      if (this.id == 17)
      {
          option.selected = true;
      }
      return(option);

   },

   createCountyOptions: function() 
   {
      if (this.counties.length == 0) 
      {
         ajaxEngine.sendRequest('doStateUpdate', "state_id=" + this.id, "object_id=stateUpdater", "list_id="+this.list_id);
      }
      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;
         }
         if (this.counties.length == 1)
         {
            _countySelect.options[1].selected = true;
         }
         if (this.counties.length == 1)
         {
             _countySelect.options[1].selected = true;
             this.counties[0].createCityOptions();
         }
      }
  },
  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, list_id) 
  {
    this.id = id;
    this.name = name;
    this.lat = lat;
    this.lng = lng;
    this.state_id = state_id;
    this.list_id = list_id;
    this.cities = new Array();
    this.updateCity = -1; // This value is used to tell the returning ajax request which city to set as the selected one.
    this.high_schools = new Array();
  },
  setUpdateCity: function(city_id)
  {
     this.updateCity = city_id;
  },
  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", "list_id="+this.list_id);
    }
    else 
    {
      var size = _citySelect.options.length;
      for (var i = 0; i < this.cities.length; i++) {
        _citySelect.options[i + 1] = this.cities[i].createOptionNoZip();
      } //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;
      }
      if (this.updateCity != -1)
      {
         util_setSelectValue(_citySelect, this.updateCity);
         this.updateCity = -1;
      }
    }
  },
  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;
  },
  getZipsByCity: function(city) {
    var zips = Array();
    for (var i = 0; i < this.cities.length; i++) {
      if (this.cities[i].name == city.name) {
        zips.push(this.cities[i].zip);
      }
    }
    return (zips);
  }
};

var City = Class.create();
City.prototype = {
  initialize: function(id, name, zip, lat, lng, zoom) {
    this.id = id;
    this.name = name;
    this.zip = zip;
    this.lat = lat;
    this.lng = lng;
	this.zoom = zoom;
  },
  createOption: function() {
    return new Option(this.zip + " - " + this.name, this.id);
  },
  createOptionNoZip: function() {
    return new Option(this.name, this.id);
  }
};

var Area = Class.create();
Area.prototype = 
{
   initialize: function(id, name, lat, lng) 
   {
      this.id = id;
      this.name = name;
      this.lat = lat;
      this.lng = lng;
   },
   createOption: function() 
   {
      return new Option(this.name, this.id);
   }
};

var District = Class.create();
District.prototype = 
{
   initialize: function(id, name, lat, lng) 
   {
      this.id = id;
      this.name = name;
      this.lat = lat;
      this.lng = lng;
   },
   createOption: function() 
   {
      return new Option(this.name, this.id);
   }
};

_default_state_obj = _states[_states.length] = new State('17','ID','36');
_counties[_counties.length] = new County(2873, 'Ada', 43.4599, -116.244, 17, 36);
_counties[_counties.length] = new County(2872, 'Adams', 44.856, -116.495, 17, 36);
_counties[_counties.length] = new County(2837, 'Bannock', 42.8614, -112.722, 17, 36);
_counties[_counties.length] = new County(2842, 'Bear Lake', 42.3013, -111.341, 17, 36);
_counties[_counties.length] = new County(2880, 'Benewah', 47.2273, -116.785, 17, 36);
_counties[_counties.length] = new County(2838, 'Bingham', 43.2449, -112.298, 17, 36);
_counties[_counties.length] = new County(2849, 'Blaine', 43.3425, -114.595, 17, 36);
_counties[_counties.length] = new County(2867, 'Boise', 43.5974, -116.3, 17, 36);
_counties[_counties.length] = new County(2878, 'Bonner', 48.3685, -116.545, 17, 36);
_counties[_counties.length] = new County(2855, 'Bonneville', 43.3233, -111.782, 17, 36);
_counties[_counties.length] = new County(2879, 'Boundary', 48.7506, -116.541, 17, 36);
_counties[_counties.length] = new County(2840, 'Butte', 43.6099, -113.242, 17, 36);
_counties[_counties.length] = new County(2851, 'Camas', 44.0039, -112.232, 17, 36);
_counties[_counties.length] = new County(2869, 'Canyon', 47.5753, -116.412, 17, 36);
_counties[_counties.length] = new County(2841, 'Caribou', 42.7191, -111.597, 17, 36);
_counties[_counties.length] = new County(2848, 'Cassia', 42.3379, -113.644, 17, 36);
_counties[_counties.length] = new County(2858, 'Clark', 44.2718, -112.306, 17, 36);
_counties[_counties.length] = new County(2862, 'Clearwater', 46.0224, -115.891, 17, 36);
_counties[_counties.length] = new County(2843, 'Custer', 44.3873, -114.697, 17, 36);
_counties[_counties.length] = new County(2866, 'Elmore', 43.4332, -115.619, 17, 36);
_counties[_counties.length] = new County(2844, 'Franklin', 42.0151, -111.804, 17, 36);
_counties[_counties.length] = new County(2856, 'Fremont', 44.3192, -111.602, 17, 36);
_counties[_counties.length] = new County(2874, 'Gem', 47.5086, -115.867, 17, 36);
_counties[_counties.length] = new County(2850, 'Gooding', 42.9385, -114.713, 17, 36);
_counties[_counties.length] = new County(2863, 'Idaho', 43.8255, -115.832, 17, 36);
_counties[_counties.length] = new County(2859, 'Jefferson', 43.8407, -112.162, 17, 36);
_counties[_counties.length] = new County(2853, 'Jerome', 42.7241, -114.518, 17, 36);
_counties[_counties.length] = new County(2876, 'Kootenai', 47.701, -116.55, 17, 36);
_counties[_counties.length] = new County(2865, 'Latah', 46.8363, -116.684, 17, 36);
_counties[_counties.length] = new County(2845, 'Lemhi', 44.8518, -113.62, 17, 36);
_counties[_counties.length] = new County(2864, 'Lewis', 46.2305, -116.363, 17, 36);
_counties[_counties.length] = new County(2852, 'Lincoln', 43.5114, -111.964, 17, 36);
_counties[_counties.length] = new County(2860, 'Madison', 43.7761, -111.691, 17, 36);
_counties[_counties.length] = new County(2854, 'Minidoka', 42.7533, -113.49, 17, 36);
_counties[_counties.length] = new County(2861, 'Nez Perce', 46.2429, -116.711, 17, 36);
_counties[_counties.length] = new County(2846, 'Oneida', 42.2498, -112.536, 17, 36);
_counties[_counties.length] = new County(2868, 'Owyhee', 43.4183, -116.2, 17, 36);
_counties[_counties.length] = new County(2875, 'Payette', 44.0746, -116.935, 17, 36);
_counties[_counties.length] = new County(2839, 'Power', 42.7192, -112.812, 17, 36);
_counties[_counties.length] = new County(2877, 'Shoshone', 42.9354, -114.407, 17, 36);
_counties[_counties.length] = new County(2857, 'Teton', 43.8865, -111.668, 17, 36);
_counties[_counties.length] = new County(2847, 'Twin Falls', 42.5632, -114.46, 17, 36);
_counties[_counties.length] = new County(2871, 'Valley', 44.6837, -115.454, 17, 36);
_counties[_counties.length] = new County(2870, 'Washington', 44.4958, -116.771, 17, 36);
_listSources[_listSources.length] = new ListSource(36,"Coeur d'Alene Idaho MLS",'cimls');
if (_default_state_obj != null)
{
    _counties.each(function (county)
    {
        _default_state_obj.addCounty(county);
    });
}

var _countyUpdater = null;
var _stateUpdater = null;
var _listSourceUpdater = null;
var _listSourceUpdaterDistrict = null;
var _areaUpdater = null;
var _districtUpdater = null;


function initLocation(countySelect, citySelect, stateSelect, listSourceSelect) 
{
   _countySelect = countySelect;
   _citySelect = citySelect;
   _stateSelect = stateSelect;
   _listSourceSelect = listSourceSelect;
   var updateCity = false;
   if (_counties.length == 1)
   {
      _countySelect.options[0] = _counties[0].createOption();
      updateCity = true;
   }
   else
   {
      for (var i = 0; i < _counties.length; i++) 
      {
        _countySelect.options[i + 1] = _counties[i].createOption();
      }
   }
   if (citySelect != null) 
   {
      _countySelect.onchange = function() 
      {
         updateCitySelect();
      };
   }

   if (_listSourceSelect != null) 
   {
      // check to see if it is a select or a radio button group
      if (_listSourceSelect.type  && _listSourceSelect.type.toLowerCase() == 'radio')
      {
           var group = _listSourceSelect.form.table_prefix;
           for(var i=0; i<group.length; i++)
           {
               Event.observe(group[i],"change", updateCountySelect2);
           }

/*         _listSourceSelect.form.getInputs('radio','table_prefix').each(
             function (list_el)
             {
                 Event.observe(list_el,"change", updateCountySelect2);
             }
         );
*/
      }
      else if (_listSourceSelect.type && _listSourceSelect.type.toLowerCase() == 'select')
      {
         Event.observe(_listSourceSelect,"change",updateCountySelect2); 
      }

      _listSourceUpdater = new ListSourceUpdater();
   }

   if (_stateSelect != null) 
   {
      for (var i = 0; i < _states.length; i++) 
      {
         _stateSelect.options[i + 1] = _states[i].createOption();
      }
   
      _stateSelect.onchange = function() 
      {
         updateCountySelect();
      };
      util_setSelectValue(_stateSelect, _default_state_obj.id);
      _stateUpdater = new StateUpdater();
   }
   
   _countyUpdater = new CountyUpdater();
   if (updateCity)
   {
      updateCitySelect();
   }
}

function initAreaLocation(areaSelect) 
{
//   _areaUpdater = areaUpdater;
   _areaSelect = areaSelect;
   if(areaSelect != null)
   {
      _areaUpdater = new AreaUpdater();
      for (var i = 0; i < _areas.length; i++) 
      {
        _areaSelect.options[i + 1] = _areas[i].createOption();
      }
   }
}

function initDistrictLocation(districtSelect) 
{
//   _districtUpdater = districtUpdater;
   _districtSelect = districtSelect;
   if(districtSelect != null)
   {
//      _districtUpdater = new DistrictUpdater();
      for (var i = 0; i < _districts.length; i++) 
      {
        _districtSelect.options[i + 1] = _districts[i].createOption();
      }
   }
}


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"), city.getAttribute("zoom")));
            }

            county.createCityOptions();
         }
      }

      if (this.cb != null) 
      {
         this.cb(countyId);
      }
   }
};

var AreaUpdater = Class.create();
AreaUpdater.prototype = 
{
   initialize: function()
   {
      this.cb = null;
      ajaxEngine.registerRequest('doAreaUpdate', '/ajax/county_list.php');
      ajaxEngine.registerAjaxObject("areaUpdater", this);
   },
   ajaxUpdate: function(ajaxResponse) 
   {
      var areaList = ajaxResponse.childNodes[0];
      var areas = countyList.childNodes;
      var new_areas = Array();
      if (areas.length > 0) 
      {
         for (var i=0; i<areas.length; i++)
         {
            area = areas[i];
            new_areas[new_areas.length] = new Area(i+1, area.getAttribute("value"), area.getAttribute("lat"), area.getAttribute("lng"));

         }

         var size = _areaSelect.options.length;
         for (var i = 0; i < new_areas.length; i++) 
         {
            _areaSelect.options[i + 1] = new_areas[i].createOption();
         } //remove any other counties from the previous county listing
         for (var i = new_areas.length; i < size; i++) 
         {
            _areaSelect.options[new_areas.length + 1] = null;
         }
      }

      if (this.cb != null) 
      {
         this.cb();
      }
   }
};
/*
DistrictUpdater.prototype =
{
   initialize: function()
   {
      this.cb = null;
      ajaxEngine.registerRequest('doDistrictUpdate', '/ajax/district_list.php');
      ajaxEngine.registerAjaxObject("districtUpdater", this);
   },
   ajaxUpdate: function(ajaxResponse)
   {
      var districtList = ajaxResponse.childNodes[0];
      var districts = districtList.childNodes;
      var new_districts = Array();
      if (districts.length > 0)
      {
         for (var i=0; i<districts.length; i++)
         {
            district = districts[i];
            new_districts[new_districts.length] = new District(district.getAttribute("id"), district.getAttribute("value"), district.getAttribute("lat"), district.getAttribute("lng"));

         }

         var size = _districtSelect.options.length;
         for (var i = 0; i < new_districts.length; i++)
         {
            _districtSelect.options[i + 1] = new_districts[i].createOption();
         } //remove any other counties from the previous county listing
         for (var i = new_districts.length; i < size; i++)
         {
            _districtSelect.options[new_districts.length + 1] = null;
         }
      }

      if (this.cb != null)
      {
         this.cb();
      }
   }
}
*/
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, null));
                }

                state.createCountyOptions();
            }
        }

        if (this.cb != null)
        {
            this.cb();
        }
    }
};


var ListSourceUpdater = Class.create();
ListSourceUpdater.prototype = 
{
    initialize: function() 
    {
        this.cb = null;
        ajaxEngine.registerRequest('doListSourceUpdate', '/ajax/county_list.php');
        ajaxEngine.registerAjaxObject("listSourceUpdater", this);
    },
    ajaxUpdate: function(ajaxResponse) 
    {
        var countyList = ajaxResponse.childNodes[0];
        if (countyList != null)
        {
            var counties = countyList.childNodes;
            if (counties.length > 0)
            {
                var listSourceId = countyList.getAttribute("list_id");
                var listSource = getListSourceById(listSourceId);

                if (listSource != null)
                {
                    _counties = new Array();
                    for (var i = 0; i < counties.length; i++)
                    {
                        var county = counties[i];
			var newCounty = new County(county.getAttribute("id"), county.firstChild.nodeValue, county.getAttribute("lat"), county.getAttribute("lng"), -1, listSource.id);
                        listSource.addCounty(newCounty);
                        
                    }

                    listSource.createCountyOptions();
                    listSource.updateCounties();
                }
            }

            if (this.cb != null)
            {
                this.cb();
            }
        }
    }
};

var ListSourceUpdaterDistrict = Class.create();
ListSourceUpdaterDistrict.prototype =
{
    initialize: function()
    {
        this.cb = null;
        ajaxEngine.registerRequest('doListSourceUpdateDistrict', '/ajax/district_list.php');
        ajaxEngine.registerAjaxObject("listSourceUpdaterDistrict", this);
    },
    ajaxUpdate: function(ajaxResponse)
    {
        var districtList = ajaxResponse.childNodes[0];
        if (districtList != null)
        {
            var districts = districtList.childNodes;
            if (districts.length > 0)
            {
                var listSourceId = districtList.getAttribute("list_id");
                var listSource = getListSourceById(listSourceId);

                if (listSource != null)
                {
                    _districts = new Array();
                    for (var i = 0; i < districts.length; i++)
                    {
                        var district = districts[i];
                        listSource.addDistrict(new District(district.getAttribute("id"), district.firstChild.nodeValue, district.getAttribute("lat"), district.getAttribute("lng"), -1, listSource.id));
                    }

                    listSource.createDistrictOptions();
                    listSource.updateDistricts();
                }
            }

            if (this.cb != null)
            {
                this.cb();
            }
        }
    }
};


function getCountyById(county_id)
{
    var county = null;
    if (_states.length != 0)
    {
        var state = getSelectedState();
        if (state != null)
	    county = state.getCountyById(county_id);
    }

    if (county == null)
    {	
        for (var i = 0; i < _counties.length; i++)
        {
            if (_counties[i].id == county_id)
            {
                county = _counties[i];
            }
        }
    }

    return county;
}

function getStateById(state_id)
{
    for (var i = 0; i < _states.length; i++)
    {
        if (_states[i].id == state_id)
        {
            return _states[i];
        }
    }

    return null;
}

function getListSourceById(list_source_id) 
{
    for (var i = 0; i < _listSources.length; i++)
    {
        if (_listSources[i].id == list_source_id)
        {
            return _listSources[i];
        }
    }

    return null;
}

function getListSourceByPrefix(prefix) 
{
    for (var i = 0; i < _listSources.length; i++)
    {
        if (_listSources[i].prefix == prefix)
        {
            return _listSources[i];
        }
    }

    return null;
}

function getAreaById(area_id)
{
    for (var i=0; i< _areas.length; i++)
    {
        if (_areas[i].id == area_id)
        {
            return(_areas[i]);
        }
    }
    return(null);
}

function getDistrictById(district_id)
{
    for (var i=0; i< _districts.length; i++)
    {
        if (_districts[i].id == district_id)
        {
            return(_districts[i]);
        }
    }
    return(null);
}

function getAreaByName(name)
{
    for (var i=0; i< _areas.length; i++)
    {
        if (_areas[i].name == name)
        {
            return(_areas[i]);
        }
    }
    return(null);
}


function getDistrictByName(name)
{
    for (var i=0; i< _districts.length; i++)
    {
        if (_districts[i].name == name)
        {
            return(_districts[i]);
        }
    }
    return(null);
}

function updateCitySelect() 
{
    var countyId = _countySelect.options[_countySelect.selectedIndex].value;
    var county = getCountyById(countyId);
    
    if (county != null)
        county.createCityOptions();
    else if (_citySelect != null)
    {
        _citySelect.options[0].selected = true;
    }
}

function updateCountySelect()
{
    var stateId = _stateSelect.options[_stateSelect.selectedIndex].value;
    var state = getStateById(stateId);

    if (state != null)
        state.createCountyOptions();
}

function updateCountySelect2() 
{
    var prefix = _search.getTablePrefix();
    var listSource = getListSourceByPrefix(prefix);

    if (listSource != null)
        listSource.createCountyOptions();
}

function updateDistrictSelect()
{
    var prefix = _search.getTablePrefix();
    var listSource = getListSourceByPrefix(prefix);

    if (listSource != null)
        listSource.createDistrictOptions();
}


function getSelectedCounty()
{
    var county = null;
    if (_countySelect == null)
    {
        return(null);
    }
    var countyId = _countySelect.options[_countySelect.selectedIndex].value;

    if (_states.length == 0)
    {
        return getCountyById(countyId);
    }
    else
    {
        var state = getSelectedState();
        if (state != null)
            county = state.getCountyById(countyId);
        if (county == null)
            county = getCountyById(countyId);
    }
    return(county);
}

function getSelectedCity()
{
    var county = getSelectedCounty();
    if (county == null)
    {
        return null;
    }

	if (_citySelect)
		var cityId = _citySelect.options[_citySelect.selectedIndex].value;
	else
		return null;
    return county.getCityById(cityId);
}

function getSelectedState()
{
    var stateId;
    if (_stateSelect == null)
    {
        stateId = 17;
    }
    else
    {
        stateId = _stateSelect.options[_stateSelect.selectedIndex].value;
    }
    return getStateById(stateId);
}

function getSelectedArea()
{
    var area_id=   _areaSelect.options[_areaSelect.selectedIndex].value;
    return getAreaById(area_id);
}

function getSelectedDistrict()
{
    var district_id=   _districtSelect.options[_districtSelect.selectedIndex].value;
    return getDistrictById(district_id);
}

