// commonSearch.js 
// Created on: 17 Nov 2006
// Author: Mauro Ferratello :-Þ - Zero Computing S.r.l.
// Version 0.1

//var _jsUrl = "js/search";
var _jsUrl = "../common/js/search";
document.writeln("<script src='" + _jsUrl + "/listboxEvents.js' type='text/javascript'></" + "script>");
  
var countries = new Object();
var _timeout = new Array();
cities = new Object();

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

// ##############################################################
// #  country: oggetto contenente le informazioni sulle nazioni #
// ##############################################################
function country(id, latitude, longitude, zoom) { 
  this.id = id;
  this.latitude = latitude;
  this.longitude = longitude;
  this.zoom = zoom;
}
// ####################################################################
// #  city: oggetto contenente le informazioni sulle citta'           #
// ####################################################################
function city(id, name) { 
  this.id = id;
  this.name = name;
  this.latitude = 0;
  this.longitude = 0; 
  this.hotels = 0;
  this.title = 'Clicca per selezionare e vedere sulla mappa';
}

function getCities(type, value)
{
  var language = languageCode;
  var country, key;
  var requestType = 'getCities';
//alert(type+' - '+value+' = '+$F(value));  
  if(type == "country")
  {
    try
    {
      country = $F(value);
      key = '';
    }
    catch (Exception)
    {
      alert('Unable to get cities from country: service is temporary unavailable.');
    }  
  }
  else if (type == "key")
  {
    key = value;
    country = '';
  }  
  //alert(country+' - '+key);
  
  var url = '/HotelAvailabilitySearch/AutoCompletion';
  //var url= 'http://smartass-dmz.zero.it:8082/HotelAvailabilitySearch/AutoCompletion';
  //var url= 'http://212.121.64.37:8082/HotelAvailabilitySearch/AutoCompletion'; //IP di smartass.
  
  var pars = 'requestType='+requestType+'&language='+language+'&country='+country+'&key='+key;
  _showSpinner(true);
//alert(pars);
  resetCity();
  myAjax = new Ajax.Request(  url, {method: 'get', parameters: pars, onFailure: function(originalRequest){handleError(originalRequest);}, evalScripts : false , onSuccess: function(originalRequest){getWindowContent(originalRequest, type);} });   
  return;
}

//###########################################################################
//# carico direttamente tutti i dati che dovrei mostrare nel fumetto, tanto #
//# sono poche informazioni, nn vale la pena di fare un'altra chiamata ajax.#
//# Uso l'oggetto city con i vari parametri ke mi servono.                  #
//###########################################################################
function getWindowContent(originalRequest, type) {
  if(originalRequest.responseText != -1) {

    var htmlresponse = originalRequest.responseXML.getElementsByTagName("city");
    var cityObj;
    //resetCity();
    for (a = 0; a < htmlresponse.length; a++ )
    {
      //alert(htmlresponse[a].firstChild.nodeValue);
      if (htmlresponse[a].getAttribute('id') != null && htmlresponse[a].getAttribute('id') != undefined)
      {
        cityObj = new city(htmlresponse[a].getAttribute('id'), htmlresponse[a].getAttribute('name'))
        cityObj.latitude = htmlresponse[a].getAttribute('latitude');
        cityObj.longitude = htmlresponse[a].getAttribute('longitude');
        cityObj.hotels = htmlresponse[a].getAttribute('n_hotels');
        cityObj.country = htmlresponse[a].getAttribute('country');
        cityObj.key = htmlresponse[a].getAttribute('key');
        
        if (cityObj.latitude == 0 && cityObj.longitude == 0)
          cityObj.title = "Coordinate non disponibili";
        // i dati vengono passati tutti come attributi.
        /*
        var tmp;
        tmp = htmlresponse[a].getElementsByTagName("latitude");
        cityObj.latitude = tmp[0].firstChild.nodeValue;
        tmp = htmlresponse[a].getElementsByTagName("longitude");
        cityObj.longitude = tmp[0].firstChild.nodeValue;
        tmp = htmlresponse[a].getElementsByTagName("hotels");
        cityObj.hotels = tmp[0].firstChild.nodeValue;
        */
 //       alert(cityObj.id+' - '+cityObj.name+' | '+cityObj.latitude+':'+cityObj.longitude+' | '+cityObj.hotels+' hotels found.');

        var cityText = "<span title='"+cityObj.title+"' style='font-weight: bold;'>"+cityObj.name+"</span> - "+cityObj.country+"";
        if (cityObj.key != null && cityObj.key != '' && cityObj.key != 'undefined' && cityObj.key != cityObj.name )
          cityText = cityText+" ("+cityObj.key+")";
        cityText = cityText+" :"+cityObj.hotels+" hotel";
        //cities_listbox.addOption(cityObj.name,cityObj.id,false);
        cities_listbox.addOption(cityText,cityObj.id,false);
        cities[cityObj.id]= cityObj;
        if (type == "key")
          //setSearchCityMarker(cityObj, true);        
          setSearchCityMarker(cityObj, false);        
        else
          setSearchCityMarker(cityObj, false);        
 
      }

    }
    if (type == "key")
      placeAllMarkers();          
/*
    var htmlresponse = originalRequest.responseXML.getElementsByTagName("tab1");
    var tab1 = htmlresponse[0].firstChild.nodeValue;    
    var htmlresponse = originalRequest.responseXML.getElementsByTagName("tab2");
    var tab2 = htmlresponse[0].firstChild.nodeValue;    
    var htmlresponse = originalRequest.responseXML.getElementsByTagName("tab3");
    var tab3 = htmlresponse[0].firstChild.nodeValue;    
*/
  }
  else {
    alert('Error retrieving cities. \n Service temporary unavailable.');
  }
  _showSpinner(false);
  return;  
}
function handleError(originalRequest)
{
  //alert('Error sending request. \n Service temporary unavailable.\n'+originalRequest.responseText);
  return;
}
function onSelectCityFunction()
{
  var idCity = cities_listbox.getSelectedIndex();
  openCityMarkerPopup(idCity);
  setSelectedCityDescription(idCity);
  return;  
}
function setSelectedCityDescription(idCity)
{
  //alert(idCity);  
  if (idCity != '' && idCity > 0)
  {
    var cityObj = cities[idCity];  
    $('cityDescr').value= cityObj.name;
//    Element.show('cityDescrSpan');
  }
  else
  {
    $('cityDescr').value= "";
//    Element.hide('cityDescrSpan');
  }
  
  return;
}



  function resetCity()
  {
    cities_listbox.reset();

    cities = new Object();

    /*if (delete cities) //non funziona la delete
    {
      alert("Cities deleted");
      var cities = new Object();
    }
    */
    cleanMap();
    setSelectedCityDescription('');
  }
function checkForm()
{
  if (cities_listbox.getSelectedIndex() != null && cities_listbox.getSelectedIndex() != undefined 
    && cities_listbox.getSelectedIndex() > 0)
  {
    $('idCity').value = cities_listbox.getSelectedIndex();
    return true;
  }
  return false;

}
