var ajHotelCityObject = null;
var ajAirportObject = null;
var ajLastCity = '';
var ajLastAirport = '';

function refreshCityAirport(countryCode, theCity, theAirport) {
  refreshHotelCity(theCity, countryCode);
  refreshAirport(theAirport, countryCode);
}

function refreshCity(countryCode, theCity) {
  refreshHotelCity(theCity, countryCode);
}

// Initiate connection
function refreshHotelCity(theCity, countryCode) {
  // Remove all except first one.
  while (theCity.length > 1) {
    theCity.options[theCity.length-1] = null;
  }
  
  if (countryCode != "")  {
    // Disable and load
    theCity.disabled = true;
    
    if (ajHotelCityObject == null) {
      ajHotelCityObject = GetXmlHttpObject();
    }
  
    if (ajHotelCityObject != null)  {
      if (ajHotelCityObject.readyState == 4 || ajHotelCityObject.readyState == 0) {
        var url = "ajax.php?type=HOTEL_CITY&country=" + countryCode;
        ajHotelCityObject.open("GET",url,true);
        ajHotelCityObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        ajHotelCityObject.send(null);
        ajHotelCityObject.onreadystatechange = function() {
          refreshHotelCityChange(theCity);
        }
      }
    }
  }
}

// Parse information
function refreshHotelCityChange(theCity) {
  if (ajHotelCityObject != null) {
    if (ajHotelCityObject.readyState==4 && ajHotelCityObject.status == 200) {
      if (ajHotelCityObject.responseText.substring(0,2) == "OK") {
        var vals = ajHotelCityObject.responseText.substring(3);
        
        var idx = 0;
        if (vals != "") {
          var arrCities = vals.split("\n");
          for (c=0; c<arrCities.length; c++) {
            idx++;
            var cityVal = arrCities[c];
            var cityPart = cityVal.split(";");
            var sel = false;
            
            if (cityPart[0] == ajLastCity) {
              sel = true;
            }
            objopt = new Option(cityPart[1], cityPart[0], false, sel); 
            theCity.options[idx] = objopt;
          }
        }
        
        // Select (postback)
        if (eval('_pb'+theCity.form.name)[theCity.name] != undefined) {
          theCity.value = eval('_pb'+theCity.form.name)[theCity.name];
        }
        theCity.disabled = false; // Enable
      }
      else  {
        alert(ajHotelCityObject.responseText);
      }
    }
    else if (ajHotelCityObject.readyState==4 && ajHotelCityObject.status != 200) {
      alert("Error calling processing script. Please try again."); 
    }
  }
}

// Initiate connection
function refreshAirport(theAirport, countryCode) {
  var objsel = ajForm.ta_airport;
  
  // Remove all except first one.
  while (theAirport.length > 1) {
    theAirport.options[theAirport.length-1] = null;
  }
  
  if (countryCode != "")  {
    // Disable and load
    theAirport.disabled = true;
    
    if (ajAirportObject == null) {
      ajAirportObject = GetXmlHttpObject();
    }
  
    if (ajAirportObject != null)  {
      if (ajAirportObject.readyState == 4 || ajAirportObject.readyState == 0) {
        var url = "ajax.php?type=AIRPORT&country=" + countryCode;
        ajAirportObject.open("GET",url,true);
        ajAirportObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        ajAirportObject.send(null);
        ajAirportObject.onreadystatechange = refreshAirportChange(theAirport);
      }
    }
  }
}

// Parse information
function refreshAirportChange(theAirport) {
  if (ajAirportObject != null) {
    if (ajAirportObject.readyState==4 && ajAirportObject.status == 200) {
      if (ajAirportObject.responseText.substring(0,2) == "OK") {
        var vals = ajAirportObject.responseText.substring(3);
        
        var idx = 0;
        if (vals != "") {
          var arrAirports = vals.split("\n");
          for (c=0; c<arrAirports.length; c++) {
            idx++;
            var airportVal = arrAirports[c];
            var airportPart = airportVal.split(";");
            var sel = false;
            
            if (airportPart[0] == ajLastAirport) {
              sel = true;
            }
            objopt = new Option(airportPart[0], airportPart[0], false, sel); 
            theAirport.options[idx] = objopt;
          }
        }
        
        // Select (postback)
        if (eval('_pb'+theAirport.form.name)[theAirport.name] != undefined) {
          theAirport.value = eval('_pb'+theAirport.form.name)[theAirport.name];
        }
        
        theAirport.disabled = false; // Enable
      }
      else  {
        alert(ajAirportObject.responseText);
      }
    }
    else if (ajAirportObject.readyState==4 && ajAirportObject.status != 200) {
      alert("Error calling processing script. Please try again."); 
    }
  }
}
