var TravelocityJsEncodeAirport = {
// aiportLocationsSuggest
// input : input to encode
// callback : function to call back when DWR call completes
  getAirportLocations: function ( input, callback )
  {
    if ( input == null || input == '' )
      return;

    if ( input.length > 3 )
      input = input.substring(0, 2);

    EncodeAirportLocation.getAirportLocations(callback, input, 1000);
  },

// Function to format data for display
// Wraps everything but the city code with <div class='informal'> to tell
// Scriptaculous to ignore that content when populating the text field with
// the selected item.
//
// This function has knowledge of the format of 'data' that is passed in from
// the DWR call inside getAirportLocations( )
  airportLocationsAutoCompleterFormatter: function ( data, input )
  {
    var displayInfo;
    var tempArray = [ data.length ];

    data = data.sort(TravelocityJsEncodeAirport.encodeSortCompare);
    // Move exact code match to top of list if found
    for ( var i = 0; i < data.length; i++ )
    {
      if ( data[i].locationCode.toUpperCase() == input.toUpperCase() )
      {
        var match = data[i];
        data.splice(i, 1);
        data.splice(0, 0, match);
      }
    }

    // Remove any non index 0 matches
    var cityName;
    var locationName;
    var inputlower = input.toLowerCase();

    var idx = 1;
    while ( data[idx] )
    {
      cityName = data[idx].cityName.toLowerCase();
      locationName = data[idx].locationName.toLowerCase();

      if ( cityName.indexOf(inputlower) == 0 || locationName.indexOf(inputlower) == 0 )
        idx++;
      else
        data.splice(idx, 1);
    }
    // Title attribue
    var title = "";
    for ( var i = 0; i < data.length; i++ )
    {
      title = data[i].cityName + ", "

      if ( data[i].countryCode == 'US' )
        title = title.concat(data[i].stateCode);
      else
        title = title.concat(data[i].countryCode);

      title = title.concat(" - ");

      if ( data[i].locationName.length > 0 )
        title = title.concat(data[i].locationName)

      title = title.concat(" ( " + data[i].locationCode + " )");

      displayInfo = "<span title='" + title + "'><div class='informal'>";
      displayInfo = displayInfo.concat(data[i].locationName);
      displayInfo = displayInfo.concat(', ');

      if ( data[i].countryCode == 'US' )
        displayInfo = displayInfo.concat(data[i].stateCode);
      else
        displayInfo = displayInfo.concat(data[i].countryCode);

      displayInfo = displayInfo.concat('</div><div style="display:none;">' + data[i].locationCode + '</div>');
      displayInfo = displayInfo.concat('</span>');
      tempArray[i] = displayInfo;
    }

    return tempArray;
  },

  encodeDataArrayToString: function ( data )
  {
    var ret = data.cityName;

    if ( data.countryCode == 'US' )
      ret = ret.concat(data.stateCode);
    else
      ret = ret.concat(data.countryCode);

    ret = ret.concat(data.locationCode);
    return ret;
  },

  encodeSortCompare: function ( a, b )
  {
    var itemA = TravelocityJsEncodeAirport.encodeDataArrayToString(a);
    var itemB = TravelocityJsEncodeAirport.encodeDataArrayToString(b);

    if ( itemA < itemB )
      return -1;
    if ( itemB < itemA )
      return 1;

    return 0;
  }
}