/* city, county, and state function */

var _stateSelect = null;
var _countySelect = null;
var _citySelect = null;
var _areaSelect = null;
var _districtSelect = null;

var _state_id = 1;
var _state = 'UT';

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(2905, 'Beaver', 38.2769, -112.641, _state_id, 2);
_counties[_counties.length] = new County(2893, 'Box Elder', 41.5005, -112.958, _state_id, 2);
_counties[_counties.length] = new County(2894, 'Cache', 41.8153, -111.997, _state_id, 2);
_counties[_counties.length] = new County(2895, 'Carbon', 39.6408, -110.561, _state_id, 2);
_counties[_counties.length] = new County(2889, 'Daggett', 40.8278, -109.525, _state_id, 2);
_counties[_counties.length] = new County(2885, 'Davis', 40.9635, -112.116, _state_id, 2);
_counties[_counties.length] = new County(2881, 'Duchesne', 40.1634, -110.401, _state_id, 2);
_counties[_counties.length] = new County(2897, 'Emery', 38.9231, -111.252, _state_id, 2);
_counties[_counties.length] = new County(2904, 'Garfield', 40.7261, -112.163, _state_id, 2);
_counties[_counties.length] = new County(2898, 'Grand', 38.9999, -109.615, _state_id, 2);
_counties[_counties.length] = new County(2906, 'Iron', 37.8116, -113.26, _state_id, 2);
_counties[_counties.length] = new County(2902, 'Juab', 39.6631, -112.816, _state_id, 2);
_counties[_counties.length] = new County(2903, 'Kane', 37.2719, -111.77, _state_id, 2);
_counties[_counties.length] = new County(2901, 'Millard', 39.0644, -113.032, _state_id, 2);
_counties[_counties.length] = new County(2887, 'Morgan', 41.0422, -111.68, _state_id, 2);
_counties[_counties.length] = new County(2909, 'Piute', 38.3289, -112.143, _state_id, 2);
_counties[_counties.length] = new County(2890, 'Rich', 41.5713, -111.279, _state_id, 2);
_counties[_counties.length] = new County(2883, 'Salt Lake', 40.6682, -111.908, _state_id, 2);
_counties[_counties.length] = new County(2896, 'San Juan', 37.7491, -110.227, _state_id, 2);
_counties[_counties.length] = new County(2900, 'Sanpete', 39.4225, -111.632, _state_id, 2);
_counties[_counties.length] = new County(2899, 'Sevier', 38.5894, -112.258, _state_id, 2);
_counties[_counties.length] = new County(2886, 'Summit', 40.6517, -111.505, _state_id, 2);
_counties[_counties.length] = new County(2888, 'Tooele', 40.5343, -112.298, _state_id, 2);
_counties[_counties.length] = new County(2884, 'Uintah', 41.1435, -111.931, _state_id, 2);
_counties[_counties.length] = new County(2882, 'Utah', 40.177, -111.536, _state_id, 2);
_counties[_counties.length] = new County(2891, 'Wasatch', 40.2953, -111.256, _state_id, 2);
_counties[_counties.length] = new County(2908, 'Washington', 37.1304, -113.497, _state_id, 2);
_counties[_counties.length] = new County(2907, 'Wayne', 38.3301, -110.867, _state_id, 2);
_counties[_counties.length] = new County(2892, 'Weber', 41.2552, -111.957, _state_id, 2);
_listSources[_listSources.length] = new ListSource(2,'Wasatch Front Regional MLS','wfrmls');
_listSources[_listSources.length] = new ListSource(1,'FSBO','fsbo');

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);
}


