//Defines the functions that are used by all pages that contain the booking panel or parts of it.

//Unavailable routes that should be removed from default routes
var unavailableRoutes = new Array();
//adding existing unavailable routes
unavailableRoutes = unavailableRoutes.concat(['CPTJNB', 'JNBCPT']);
//Singapore
unavailableRoutes = unavailableRoutes.concat(['SINADL', 'SINBNE', 'SINMEL', 'SINPER', 'SINSYD', 'ADLSIN', 'BNESIN', 'MELSIN', 'PERSIN', 'SYDSIN']);
//Virgin Blue
unavailableRoutes = unavailableRoutes.concat(['SYDADL', 'SYDBNE', 'SYDCBR', 'SYDMEL', 'SYDOOL', 'ADLSYD', 'BNESYD', 'CBRSYD', 'MELSYD', 'OOLSYD']);
//British Midland
unavailableRoutes = unavailableRoutes.concat(['LONAMS', 'LONLHRAMS', 'LONDUB', 'LONLHRDUB', 'LONBRU', 'LONLHRBRU','LONPMI', 'LONLHRPMI', 'LONNAP', 'LONLHRNAP', 'LONVCE', 'LONLHRVCE', 'LONHAJ', 'LONLHRHAJ', 'AMSLON', 'AMSLONLHR', 'DUBLON', 'DUBLONLHR', 'BRULON', 'BRULONLHR', 'PMILON', 'PMILONLHR', 'NAPLON', 'NAPLONLHR', 'VCELON', 'VCELONLHR','HAJLON', 'HAJLONLHR']);
/*
 * Removes the unavailable routes from default routes.
 */
function removeUnavailableRoutes() {
  for (var i = 0; i < unavailableRoutes.length; i++) {
    var unavailableRoute = unavailableRoutes[i];
    for (var j = 0; j < ourRoutes.length; j++) {
      var route = ourRoutes[j];
      if (route.departCity + route.arriveCity == unavailableRoute) {
        //remove the current route
        ourRoutes.splice(j, 1);
        break;
      }
    }
  }
}

//Function to check whether the cities are in the allowed routes
function checkDepartCities(outBoundDepart, inBoundDepart, searchType){

  if(!checkDepartCity(outBoundDepart, searchType)){
    return true;
  }

  if(!checkDepartCity(inBoundDepart, searchType)){
    return true;
  }
  return false;
}

//function to check city is in the departures list
function checkDepartCity(departureCityName, searchType){
  for (var i = 0; i < ourRoutes.length; i++)
  {
    var route = ourRoutes[i];
    // if this route departs from our departcity then true
    if (qualifiesForSearchType(route, searchType) && (route.departCity == departureCityName))
    {
      return true;
    }
  }
  for (var i = 0; i < matesSpecialRoutes.length; i++) {
    if (matesSpecialRoutes[i].departCity == departureCityName) {
      return true;
    }
  }
  return false;
}

//function to return whether city is in the list of can depart cities
function isDepartCity(cityToDepart)
{
  for (var i = 0; i < cityNames.length; i++)
  {
    var city = cityNames[i];
    if(city.code == cityToDepart){
      if (city.canDepart) return true;
    }
  }
  return false;
}

/*
  Determines whether the given route is allowed for the given search type.
*/
function qualifiesForSearchType(route, searchType){
  if (searchType == "milesPlusMoney"){
    return route.allowsMPM;
  }
  else if (searchType == "redeemMiles"){
    return route.allowsRedemption;
  }
  else{
    return true;
  }
}

// gets the city name from the city code
function getCityName(cityCode)
{
  var city;
  // loop through the cities till we find this one
  for (var i=0; i < cityNames.length; i++)
  {
    city = cityNames[i];
    if (city.code == cityCode) break;
  }
  return city.name;
}

// gets the default arrival city for this departure city
function getDefaultArrival(departCity){
  var arrivalCity;
  if (departCity.substring(0,3) == 'LON')
  {
    arrivalCity = 'NYC';
  }
  else if (departCity == 'MAN')
  {
    arrivalCity = 'ORL';
  }
  else
  {
    arrivalCity = 'LON';
  }
  return arrivalCity;
}

// Clears the select box
function clearOptions(theSelect){
  if (theSelect.type=='select-one')
      while(theSelect.length>0)
          theSelect.options[0]=null;
}

//---------------- JS types -----------------------------------

// The CabinDetails class represents info on a cabin such as cabin ID
// (for submission to the backend) and a friendly name for display.
function CabinDetails(id, fn, flx, nei, sk)
{
    this.myId=id;
    this.myFriendlyName=fn;
  this.flexible = flx;
  this.neither = nei;
  this.sortKey = sk;
}

// represents a city for the drop downs. This is not used by the Multiple Destinations page.
function City(cd, nm, cdp, c_id)
{
  this.code = cd;
  this.name = nm;
  this.country_id = c_id;
  this.canDepart = cdp;
}

// The Route class represents an individiual route
function Route(dc, ac, cl, ms, allowsRedemption, allowsMPM)
{
    this.departCity = dc;
  this.arriveCity = ac;
  this.cabinList = cl;
  this.multiSector = ms;
  this.allowsRedemption = allowsRedemption;
  this.allowsMPM = allowsMPM;
}
function Route(dc, ac, cl, ms, allowsRedemption, allowsMPM, lcId)
{
    this.departCity = dc;
  this.arriveCity = ac;
  this.cabinList = cl;
  this.multiSector = ms;
  this.allowsRedemption = allowsRedemption;
  this.allowsMPM = allowsMPM;
  this.letterCodeId = lcId;
}

