/* city, county, and state function */

var _stateSelect = null;
var _countySelect = null;
var _citySelect = null;
var _areaSelect = null;
var _districtSelect = null;

var _state_id = 9;
var _state = 'CA';

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(2998, 'Alameda', 37.7763, -122.277, _state_id, 6);
_counties[_counties.length] = new County(3020, 'Alpine', 32.847, -116.761, _state_id, 6);
_counties[_counties.length] = new County(3016, 'Amador', 38.4158, -120.823, _state_id, 6);
_counties[_counties.length] = new County(3027, 'Butte', 49.8735, -112.78, _state_id, 6);
_counties[_counties.length] = new County(3007, 'Calaveras', 38.1717, -120.508, _state_id, 6);
_counties[_counties.length] = new County(3025, 'Colusa', 39.2078, -122.01, _state_id, 6);
_counties[_counties.length] = new County(3000, 'Contra Costa', 37.9109, -121.988, _state_id, 6);
_counties[_counties.length] = new County(3014, 'Del Norte', 41.6909, -123.979, _state_id, 6);
_counties[_counties.length] = new County(3019, 'El Dorado', 37.6767, -119.779, _state_id, 6);
_counties[_counties.length] = new County(2987, 'Fresno', 36.7433, -119.789, _state_id, 6);
_counties[_counties.length] = new County(3026, 'Glenn', 39.5218, -122.014, _state_id, 6);
_counties[_counties.length] = new County(3012, 'Humboldt', 40.7336, -123.944, _state_id, 6);
_counties[_counties.length] = new County(2981, 'Imperial', 32.8375, -115.57, _state_id, 6);
_counties[_counties.length] = new County(2982, 'Inyo', 36.626, -117.219, _state_id, 6);
_counties[_counties.length] = new County(2986, 'Kern', 35.3542, -119.074, _state_id, 6);
_counties[_counties.length] = new County(2985, 'Kings', 36.1388, -119.895, _state_id, 6);
_counties[_counties.length] = new County(3011, 'Lake', 39.3585, -120.942, _state_id, 6);
_counties[_counties.length] = new County(3031, 'Lassen', 40.4461, -120.664, _state_id, 6);
_counties[_counties.length] = new County(2975, 'Los Angeles', 34.0522, -118.243, _state_id, 6);
_counties[_counties.length] = new County(2991, 'Madera', 37.3325, -119.655, _state_id, 6);
_counties[_counties.length] = new County(3002, 'Marin', 37.8715, -122.507, _state_id, 6);
_counties[_counties.length] = new County(2993, 'Mariposa', 37.4832, -119.964, _state_id, 6);
_counties[_counties.length] = new County(3010, 'Mendocino', 39.3094, -123.796, _state_id, 6);
_counties[_counties.length] = new County(2992, 'Merced', 37.2977, -120.476, _state_id, 6);
_counties[_counties.length] = new County(3030, 'Modoc', 41.5905, -120.728, _state_id, 6);
_counties[_counties.length] = new County(2990, 'Mono', 38.0403, -119.145, _state_id, 6);
_counties[_counties.length] = new County(2989, 'Monterey', 36.5937, -121.882, _state_id, 6);
_counties[_counties.length] = new County(2999, 'Napa', 38.2974, -122.293, _state_id, 6);
_counties[_counties.length] = new County(3023, 'Nevada', 39.2588, -121.018, _state_id, 6);
_counties[_counties.length] = new County(2976, 'Orange', 33.7881, -117.853, _state_id, 6);
_counties[_counties.length] = new County(3017, 'Placer', 39.014, -120.743, _state_id, 6);
_counties[_counties.length] = new County(3028, 'Plumas', 40.0235, -120.799, _state_id, 6);
_counties[_counties.length] = new County(2979, 'Riverside', 33.9465, -117.401, _state_id, 6);
_counties[_counties.length] = new County(2997, 'Sacramento', 38.5737, -121.487, _state_id, 6);
_counties[_counties.length] = new County(3005, 'San Benito', 36.5097, -121.081, _state_id, 6);
_counties[_counties.length] = new County(2978, 'San Bernardino', 34.2513, -116.87, _state_id, 6);
_counties[_counties.length] = new County(2980, 'San Diego', 32.7188, -117.164, _state_id, 6);
_counties[_counties.length] = new County(2996, 'San Francisco', 37.7752, -122.419, _state_id, 6);
_counties[_counties.length] = new County(3006, 'San Joaquin', 36.6074, -120.189, _state_id, 6);
_counties[_counties.length] = new County(2988, 'San Luis Obispo', 35.2696, -120.67, _state_id, 6);
_counties[_counties.length] = new County(2994, 'San Mateo', 37.5591, -122.322, _state_id, 6);
_counties[_counties.length] = new County(2983, 'Santa Barbara', 34.4234, -119.704, _state_id, 6);
_counties[_counties.length] = new County(2995, 'Santa Clara', 37.3523, -121.968, _state_id, 6);
_counties[_counties.length] = new County(3004, 'Santa Cruz', 36.9779, -122.033, _state_id, 6);
_counties[_counties.length] = new County(3029, 'Shasta', 40.5986, -122.491, _state_id, 6);
_counties[_counties.length] = new County(3024, 'Sierra', 39.5659, -120.634, _state_id, 6);
_counties[_counties.length] = new County(3015, 'Siskiyou', 41.5009, -122.583, _state_id, 6);
_counties[_counties.length] = new County(3001, 'Solano', 38.2854, -122, _state_id, 6);
_counties[_counties.length] = new County(3003, 'Sonoma', 38.2919, -122.458, _state_id, 6);
_counties[_counties.length] = new County(3009, 'Stanislaus', 37.6062, -120.937, _state_id, 6);
_counties[_counties.length] = new County(3021, 'Sutter', 39.1567, -121.749, _state_id, 6);
_counties[_counties.length] = new County(3032, 'Tehama', 40.0215, -122.125, _state_id, 6);
_counties[_counties.length] = new County(3013, 'Trinity', 40.6726, -123.035, _state_id, 6);
_counties[_counties.length] = new County(2984, 'Tulare', 36.1965, -119.344, _state_id, 6);
_counties[_counties.length] = new County(3008, 'Tuolumne', 37.9527, -120.242, _state_id, 6);
_counties[_counties.length] = new County(2977, 'Ventura', 34.2659, -119.258, _state_id, 6);
_counties[_counties.length] = new County(3018, 'Yolo', 38.7337, -121.806, _state_id, 6);
_counties[_counties.length] = new County(3022, 'Yuba', 39.1322, -121.625, _state_id, 6);
_listSources[_listSources.length] = new ListSource(6,'Yosemite Gateway Association of Realtors','ygaor');

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);
}


