/* city, county, and state function */

var _stateSelect = null;
var _countySelect = null;
var _citySelect = null;
var _areaSelect = null;
var _districtSelect = null;

var _state_id = 10;
var _state = 'CO';

var _states = new Array();
var _counties = new Array();
var _listSources = new Array();
var _areas = new Array();
var _districts = new Array();

var ListSource = Class.create();
ListSource.prototype=
{
   initialize: function(id, name, prefix)
   {
      this.id = id;
      this.name = name;
      this.prefix = prefix;
      this.counties = 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;
         }
         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;
   }

};

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, 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.
  },
  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].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;
      }
      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) {
    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);
  },
  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);
   }
};

_counties[_counties.length] = new County(2752, 'Adams', 39.8698, -104.38, _state_id, 14);
_counties[_counties.length] = new County(2789, 'Alamosa', 37.4682, -105.874, _state_id, 14);
_counties[_counties.length] = new County(2751, 'Arapahoe', 38.8496, -102.178, _state_id, 14);
_counties[_counties.length] = new County(2791, 'Archuleta', 37.208, -106.979, _state_id, 14);
_counties[_counties.length] = new County(2783, 'Baca', 37.3188, -102.564, _state_id, 14);
_counties[_counties.length] = new County(2786, 'Bent', 37.9556, -103.073, _state_id, 14);
_counties[_counties.length] = new County(2754, 'Boulder', 40.0105, -105.277, _state_id, 14);
_counties[_counties.length] = new County(2753, 'Broomfield', 39.9552, -105.062, _state_id, 14);
_counties[_counties.length] = new County(2797, 'Chaffee', 38.74, -106.239, _state_id, 14);
_counties[_counties.length] = new County(2776, 'Cheyenne', 38.8298, -102.609, _state_id, 14);
_counties[_counties.length] = new County(2766, 'Clear Creek', 39.7084, -105.662, _state_id, 14);
_counties[_counties.length] = new County(2790, 'Conejos', 37.09, -106.01, _state_id, 14);
_counties[_counties.length] = new County(2793, 'Costilla', 37.3267, -105.451, _state_id, 14);
_counties[_counties.length] = new County(2785, 'Crowley', 38.1916, -103.857, _state_id, 14);
_counties[_counties.length] = new County(2802, 'Custer', 38.0774, -105.423, _state_id, 14);
_counties[_counties.length] = new County(2806, 'Delta', 38.7417, -108.068, _state_id, 14);
_counties[_counties.length] = new County(2758, 'Denver', 39.7551, -104.988, _state_id, 14);
_counties[_counties.length] = new County(2803, 'Dolores', 37.474, -108.499, _state_id, 14);
_counties[_counties.length] = new County(2756, 'Douglas', 39.348, -104.995, _state_id, 14);
_counties[_counties.length] = new County(2761, 'Eagle', 39.656, -106.826, _state_id, 14);
_counties[_counties.length] = new County(2757, 'El Paso', 38.8247, -104.562, _state_id, 14);
_counties[_counties.length] = new County(2755, 'Elbert', 39.2212, -104.536, _state_id, 14);
_counties[_counties.length] = new County(2799, 'Fremont', 38.4776, -105.477, _state_id, 14);
_counties[_counties.length] = new County(2810, 'Garfield', 38.5516, -106.296, _state_id, 14);
_counties[_counties.length] = new County(2760, 'Gilpin', 37.9463, -103.186, _state_id, 14);
_counties[_counties.length] = new County(2767, 'Grand', 40.0852, -106.14, _state_id, 14);
_counties[_counties.length] = new County(2798, 'Gunnison', 38.5444, -106.927, _state_id, 14);
_counties[_counties.length] = new County(2801, 'Hinsdale', 37.785, -107.296, _state_id, 14);
_counties[_counties.length] = new County(2787, 'Huerfano', 37.6539, -104.926, _state_id, 14);
_counties[_counties.length] = new County(2765, 'Jackson', 40.6631, -106.362, _state_id, 14);
_counties[_counties.length] = new County(2750, 'Jefferson', 39.3783, -105.799, _state_id, 14);
_counties[_counties.length] = new County(2782, 'Kiowa', 39.3466, -104.463, _state_id, 14);
_counties[_counties.length] = new County(2778, 'Kit Carson', 38.7643, -102.795, _state_id, 14);
_counties[_counties.length] = new County(2792, 'La Plata', 37.4005, -108.063, _state_id, 14);
_counties[_counties.length] = new County(2764, 'Lake', 38.0311, -107.315, _state_id, 14);
_counties[_counties.length] = new County(2769, 'Larimer', 40.6281, -105.569, _state_id, 14);
_counties[_counties.length] = new County(2781, 'Las Animas', 38.065, -103.221, _state_id, 14);
_counties[_counties.length] = new County(2777, 'Lincoln', 39.0414, -103.605, _state_id, 14);
_counties[_counties.length] = new County(2773, 'Logan', 40.5784, -103.354, _state_id, 14);
_counties[_counties.length] = new County(2809, 'Mesa', 39.1668, -108.139, _state_id, 14);
_counties[_counties.length] = new County(2795, 'Mineral', 37.6837, -106.919, _state_id, 14);
_counties[_counties.length] = new County(2811, 'Moffat', 37.9993, -105.909, _state_id, 14);
_counties[_counties.length] = new County(2804, 'Montezuma', 39.5809, -105.867, _state_id, 14);
_counties[_counties.length] = new County(2800, 'Montrose', 38.4717, -107.864, _state_id, 14);
_counties[_counties.length] = new County(2770, 'Morgan', 37.3275, -106.02, _state_id, 14);
_counties[_counties.length] = new County(2784, 'Otero', 37.9542, -103.73, _state_id, 14);
_counties[_counties.length] = new County(2807, 'Ouray', 38.0261, -107.671, _state_id, 14);
_counties[_counties.length] = new County(2759, 'Park', 39.2765, -106.095, _state_id, 14);
_counties[_counties.length] = new County(2772, 'Phillips', 40.5942, -102.358, _state_id, 14);
_counties[_counties.length] = new County(2812, 'Pitkin', 38.6088, -106.516, _state_id, 14);
_counties[_counties.length] = new County(2788, 'Prowers', 38.0821, -102.767, _state_id, 14);
_counties[_counties.length] = new County(2780, 'Pueblo', 38.2655, -104.629, _state_id, 14);
_counties[_counties.length] = new County(2813, 'Rio Blanco', 39.9379, -108.044, _state_id, 14);
_counties[_counties.length] = new County(2794, 'Rio Grande', 37.6157, -106.375, _state_id, 14);
_counties[_counties.length] = new County(2763, 'Routt', 40.4611, -107.034, _state_id, 14);
_counties[_counties.length] = new County(2796, 'Saguache', 38.0887, -106.142, _state_id, 14);
_counties[_counties.length] = new County(2808, 'San Juan', 37.8021, -107.717, _state_id, 14);
_counties[_counties.length] = new County(2805, 'San Miguel', 37.0994, -104.395, _state_id, 14);
_counties[_counties.length] = new County(2775, 'Sedgwick', 40.9337, -102.524, _state_id, 14);
_counties[_counties.length] = new County(2762, 'Summit', 39.6411, -106.108, _state_id, 14);
_counties[_counties.length] = new County(2779, 'Teller', 40.4329, -106.005, _state_id, 14);
_counties[_counties.length] = new County(2771, 'Washington', 40.0022, -103.243, _state_id, 14);
_counties[_counties.length] = new County(2768, 'Weld', 40.5012, -104.315, _state_id, 14);
_counties[_counties.length] = new County(2774, 'Yuma', 40.1223, -102.72, _state_id, 14);
_listSources[_listSources.length] = new ListSource(14,'METROLIST','metrolist');

var _countyUpdater = null;
var _stateUpdater = null;
var _listSourceUpdater = 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) 
   {
/*      for (var i = 0; i < _listSources.length; i++) 
      {
         _listSourceSelect.options[i] = _listSources[i].createOption();
      }

      _listSourceSelect.onchange = function() 
      {
         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();
      };
      _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")));
            }

            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();
      }
   }
}

var DistrictUpdater = Class.create();
DistrictUpdater.prototype = 
{
   initialize: function()
   {
      this.cb = null;
      ajaxEngine.registerRequest('doDistrictUpdate', '/ajax/county_list.php');
      ajaxEngine.registerAjaxObject("districtUpdater", this);
   },
   ajaxUpdate: function(ajaxResponse) 
   {
      var districtList = ajaxResponse.childNodes[0];
      var districts = countyList.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(i+1, 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];
      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];
               listSource.addCounty(new County(county.getAttribute("id"), county.firstChild.nodeValue, county.getAttribute("lat"), county.getAttribute("lng"), -1, listSource.id));
            }

            listSource.createCountyOptions();
            listSource.updateCounties();
         }
      }

      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 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 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();
}

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 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);
}

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);
}


