
function AjaxSearch()
{
}

AjaxSearch.prototype.addOptionToSelect = function(id,value,text)
{
  var select = document.getElementById(id);
  if (select != null)
  {
    select.options[select.options.length] = new Option(text, value, false);
  }
}

AjaxSearch.prototype.showRegionLoading = function()
{
  var id = document.getElementById("regionLoading");
  if (id != null)
  {
    id.style.display = "block";
  }
}

AjaxSearch.prototype.hideRegionLoading = function()
{
  var id = document.getElementById("regionLoading");
  if (id != null)
  {
    id.style.display = "none";
  }
}

AjaxSearch.prototype.showStateLoading = function()
{
  var id = document.getElementById("stateLoading");
  if (id != null)
  {
    id.style.display = "block";
  }
}

AjaxSearch.prototype.hideStateLoading = function()
{
  var id = document.getElementById("stateLoading");
  if (id != null)
  {
    id.style.display = "none";
  }
}

AjaxSearch.prototype.showCityLoading = function()
{
  var id = document.getElementById("cityLoading");
  if (id != null)
  {
    id.style.display = "block";
  }
}

AjaxSearch.prototype.hideCityLoading = function()
{
  var id = document.getElementById("cityLoading");
  if (id != null)
  {
    id.style.display = "none";
  }
}

AjaxSearch.prototype.refillStatesSelect = function(url)
{
  if (document.getElementById("search_by_state").checked == true)
  {
    document.getElementById("state_id").disabled = true;
    this.url = url;
    var ac = new AjaxCommunicator();
    ac.setUrl(url);
    ac.setParameter("command","get_states");
    var regionId = document.getElementById("region_id");
    if (regionId.selectedIndex > -1)
    {
      ac.setParameter("region_id",regionId.options[regionId.selectedIndex].value);  
    }
    else
    {
      ac.setParameter("region_id","");  
    }
    ac.invoke("",
              "clsAjaxSearch.onRefillStatesSelectSuccess",
              "clsAjaxSearch.onRefillStatesSelectError");
    this.showStateLoading();
  }
  return true;
}

AjaxSearch.prototype.onRefillStatesSelectSuccess = function(parameters,
                                                            ajaxCommunicator,
                                                            responseText,
                                                            responseXMLObject)
{
  var tags = responseXMLObject.getElementsByTagName("state");
  var selected = Input_GetValueById("state_id");
  Input_ClearById("state_id");
  for(var i=0; i<tags.length; i++) 
  {
    var state = tags[i];
    this.addOptionToSelect("state_id",
                           state.getAttribute("state_id"),
                           state.getAttribute("name"));
  }
  Input_SetValueById("state_id",selected);
  this.hideStateLoading();
  document.getElementById("state_id").disabled = false;
  this.refillCitiesSelect(this.url);  
}

AjaxSearch.prototype.onRefillStatesSelectError = function(parameters,
                                                          ajaxCommunicator,
                                                          responseText,
                                                          responseXMLObject)
{
  this.hideStateLoading();
  document.getElementById("state_id").disabled = false;
}

AjaxSearch.prototype.refillCitiesSelect = function(url)
{
  if (document.getElementById("search_by_city").checked == true)
  {
    document.getElementById("city").disabled = true;
    this.url = url;
    var ac = new AjaxCommunicator();
    ac.setUrl(url);
    ac.setParameter("command","get_cities");
    var regionId = document.getElementById("region_id");
    if (regionId.selectedIndex > -1)
    {
      ac.setParameter("region_id",regionId.options[regionId.selectedIndex].value);  
    }
    else
    {
      ac.setParameter("region_id","");  
    }
    var stateId = document.getElementById("state_id");
    if (stateId.selectedIndex > -1)
    {
      ac.setParameter("state_id",stateId.options[stateId.selectedIndex].value);  
    }
    else
    {
      ac.setParameter("state_id","");  
    }    
    ac.invoke("",
              "clsAjaxSearch.onRefillCitiesSelectSuccess",
              "clsAjaxSearch.onRefillCitiesSelectError");
    this.showCityLoading();
  }
  return true;
}

AjaxSearch.prototype.onRefillCitiesSelectSuccess = function(parameters,
                                                            ajaxCommunicator,
                                                            responseText,
                                                            responseXMLObject)
{
  var tags = responseXMLObject.getElementsByTagName("city");
  var selected = Input_GetValueById("city");
  Input_ClearById("city");
  for(var i=0; i<tags.length; i++) 
  {
    var state = tags[i];
    this.addOptionToSelect("city",
                           state.getAttribute("city_id"),
                           state.getAttribute("name"));
  }
  Input_SetValueById("city",selected);
  this.hideCityLoading();
  document.getElementById("city").disabled = false;
}

AjaxSearch.prototype.onRefillCitiesSelectError = function(parameters,
                                                          ajaxCommunicator,
                                                          responseText,
                                                          responseXMLObject)
{
  this.hideCityLoading();
  document.getElementById("city").disabled = false;
}

  var clsAjaxSearch = new AjaxSearch();


