//Defines functions used by the following pages: Home and Search.
//The functions that are used by other pages too can be found in common_booking.js

var BOOKING_TYPE_ONE_WAY = "no";
var SEARCH_TYPE_REDEMPTION = "redeemMiles";
var SEARCH_TYPE_MPM = "milesPlusMoney";

var cabinGroups;
var currentSearchOption = null;

/**
 *  Sets the cabin groups according to the booking type. This method is designed to be called only from the
 *  booking pages.
 * @param searchTypeName = the name of the radio button needed selecting whether the search is One-Way or Round-Trip
 */
function setCabinGroups(searchTypeName){
  var bookingType;
  var searchType = getSearchType(searchTypeName);
  if ((searchType == SEARCH_TYPE_REDEMPTION) || (searchType == SEARCH_TYPE_MPM)){
    //for Redemtion and MPM used the Redemption cabin groups
    bookingType = 3;
  }
  else if (isBookingMethodMates()) {
    bookingType=6;
  }
  else{
    bookingType = 1;
  }
  cabinGroups = allCabinGroups[bookingType];
}

function isBookingMethodMates(){
  return document.getElementsByName("mates").length > 0;
}

/**
 *  Populates the given dropdown with the arrival cities for the given departure cities.
 *  @param departCity   = the departure city
 *  @param arrivalCity    = the selected arrival city
 *  @arrivalDropDown    = the drop-down whose contains will be changed
 *  @param searchTypeName   = name of the radio button element needed selecting whether the search is One-Way or Round-Trip
 */
function doOutboundArrival(departCity, arrivalCity, arrivalDropdown, searchTypeName){
  var searchType = getSearchType(searchTypeName);

  // clear down the select box
  clearOptions(arrivalDropdown);

  var optionIndex = 0;
  var selected = 0;
  for (var i = 0; i < ourRoutes.length; i++) {
    var route = ourRoutes[i];

    // if this route departs from our departcity then add it
    if ((route.departCity == departCity) && (allowsBookingType(route, searchType) || route.departCity=="MRU")) {
      var cityName = getCityName(route.arriveCity);
      arrivalDropdown.options[optionIndex++] = new Option(cityName, route.arriveCity);
      // check for the default
      if (route.arriveCity == arrivalCity) {
        selected = optionIndex - 1;
      }
    }
  }
  // set the selected one
  if (arrivalDropdown.length > 0){
        arrivalDropdown.options[selected].selected = true;
    }
}

/**
 * Populates the outbound departure city drop down
 *  Loops through all the cities, adding the ones that have the canDepart flag set to true and that are valid according
 *  to the currently selected booking type
 *
 * @param departCity    = city that should be selected in the dropdown
 * @departureSelectElement  = select element with all departure cities.
 * @param searchTypeName = the name of the radio button needed selecting whether the search is One-Way or Round-Trip
 */
function doOutboundDeparture(departCity, departureSelectElement, searchTypeName) {
  var selected = 0;
  var optionIndex = 0;

    departureSelectElement.options.length = 0;
  for (var i = 0; i < cityNames.length; i++) {
    var city = cityNames[i];

    if ((city.canDepart) && (isAllowedForBookingType(city, searchTypeName))) {
      departureSelectElement.options.length++;
      departureSelectElement.options[optionIndex++] = new Option(city.name, city.code);

      if (city.code == departCity){
        selected = optionIndex - 1;
      }
    }
  }
  // set the selected one
  departureSelectElement.options[selected].selected = true;
}

/**
 *  Determines whether the given city is a valid departure for the currently selected seach type. For exact date and lowest fare the given
 *  city is valid if there is at least one route that departs from this city and allows the selected search type.
 *
 *  @param searchTypeName   = name of the radio button element needed selecting whether the search is One-Way or Round-Trip
 */
function isAllowedForBookingType(city, searchTypeName){
  var searchType = getSearchType(searchTypeName);

  if ((searchType == SEARCH_TYPE_MPM) || (searchType == SEARCH_TYPE_REDEMPTION) || isBookingMethodMates()){
    for (var i = 0; i < ourRoutes.length; i++){
      var route = ourRoutes[i];
      if ((route.departCity == city.code) && allowsBookingType(route, searchType)){
        return true;
      }
    }
    return false;
  }
  else{
    return true;
  }
}

/**
 *  Determines whether the given route allows the given booking type.
 *  @param route      = route which we are investigating
 *  @param searchType = name of the radio button element needed selecting whether the search is One-Way or Round-Trip
 */
function allowsBookingType(route, searchType){
  if (searchType == SEARCH_TYPE_REDEMPTION){
    return route.allowsRedemption;
  }
  else if (searchType == SEARCH_TYPE_MPM){
    return route.allowsMPM;
  }
  else{
    return true;
  }
}

/**
 * Gets a route for the given depart and arrive city.
 *
 * @param departCity the departure city
 * @param arriveCity the arrival city
 */
function getRoute(departCity, arriveCity) {
  // this will occur on first entry into the booking page as the cities are not set when this method is first called
  if (departCity == '' || arriveCity == '') {
    return -1;
  }
  for (var i = 0; i < ourRoutes.length; i++) {
    if (ourRoutes[i].departCity == departCity && ourRoutes[i].arriveCity == arriveCity) {
      return ourRoutes[i];
    }
  }
  for (var i = 0; i < matesSpecialRoutes.length; i++) {
    if (matesSpecialRoutes[i].departCity == departCity && matesSpecialRoutes[i].arriveCity == arriveCity) {
      return matesSpecialRoutes[i];
    }
  }
}

/**
 *  Determines the search type that is currently selected.
 *  @param searchTypeName = the name of the radio button needed selecting whether the search is One-Way or Round-Trip
 *      searchTypeName = "search_type"
 */
function getSearchType(searchTypeName){
    return document.getElementById(searchTypeName).value;
}
/**
 * Tries to select the given value in the given dropdown. If the given value does not exist in the dropdown, the first value will be selected.
 */
function tryToSelect(dropdown, valueToSelect){
  var exists = false;
  for (i = 0; i < dropdown.options.length; i++){
    if (dropdown.options[i].value == valueToSelect){
      exists = true;
      break;
    }
  }
  if (exists){
    dropdown.value = valueToSelect;
  }
  else{
    dropdown.selectedIndex = 0;
  }
}
/**
 *  Outbound Departure drop down changed
 *
 * @param departureSelect   = the departure select element (document.main.departure)
 * @param arrivalSelect     = the arrival select element (document.main.arrival)
 * @param departureCabin    = the departure cabin
 * @param returnCabin     = the return cabin
 * @param searchTypeName = the name of the radio button needed selecting whether the search is One-Way or Round-Trip
 */
function obDepChanged(departureSelect, arrivalSelect, departureCabin, returnCabin, searchTypeName) {
  // do outbound arrival cities
  var departCity = departureSelect.options[departureSelect.selectedIndex].value;
  doOutboundArrival(departCity, getDefaultArrival(departCity), arrivalSelect, searchTypeName);

  // do outbound classes, inbound cities and classes if necessary
  obArrivalChanged(departureSelect, arrivalSelect, departureCabin, returnCabin);
}
/**
 *   Outbound arrival drop down changed
 *  @param departureCabin   = the departure cabin drop down. (document.main.classType)
 *  @param returnCabin      = the return cabin (document.main.classTypeReturn)
 */
function obArrivalChanged(departure, arrival, departureCabin, returnCabin) {
  doOutboundCabin(departure, arrival, departureCabin.value, departureCabin);
  if (!nonBookingPage){
    doInboundCabin(departure, arrival, departureCabin, returnCabin, returnCabin.value);
  }
}
/**
 *  Outbound cabins drop down has changed
 *  @param departureCabin   = the departure cabin drop down.(document.main.classType)
 */
function obCabinChanged(departure, arrival, departureCabin, returnCabinSelect) {
  // set the inbound cabin to this one
  doInboundCabin(departure, arrival, departureCabin, returnCabinSelect, departureCabin.value);
}
 /**
  * populates the inbound cabin drop down
  * @param returnCabinSelect = document.main.classTypeReturn;
  * @param departCabinSelect = document.main.classType;
  * @param departure = document.main.departure;
  * @param arrival = document.main.arrival
  */
  function doInboundCabin(departure, arrival, departCabinSelect, returnCabinSelect, cabinId){
    clearOptions(returnCabinSelect);

    // get the outbound cabin
    var outboundCabinId = departCabinSelect.options[departCabinSelect.selectedIndex].value;
    var outboundCabins = cabinGroups[getCabinList(departure, arrival)];
    var flexible = false;

    // now checked whether it is lowest, flexible or neither
    for (var i = 0; i < outboundCabins.length; i++)
    {
      var cabin = outboundCabins[i];
      if (cabin.myId == outboundCabinId)
      {
        flexible = cabin.flexible;
      }
    }

    var optionIndex = 0;
    // get the array of cabins
    var inboundCabins = cabinGroups[getCabinList(arrival, departure)];
    // loop through these populating the class drop down
    for (var i = 0; i < inboundCabins.length; i++)
    {
      var cabin = inboundCabins[i];
      //if flexible is true then add only the flexible cabins, otherwise add only the non flexible cabins
      //(neither and lowest)
      if (flexible == cabin.flexible){
        var currentOption = new Option(cabin.myFriendlyName, cabin.myId);
        returnCabinSelect.options[optionIndex++] = currentOption;
        if (cabin.myId == cabinId){
          currentOption.selected = true;
        }
      }
    }
}

/**
 * populates the outbound cabin drop down
 * @param departure     = document.main.departure
 * @param arrival     = document.main.arrival
 * @param departCabinSelect     = the outbound cabin select (document.main.classType)
 */
function doOutboundCabin(departure, arrival, cabinId, departCabinSelect) {
  var cabinList = getCabinList(departure, arrival);
  clearOptions(departCabinSelect);

  var selected = 0;
  // get the array of cabins
  var cabins = cabinGroups[cabinList];
  // loop through these populating the class drop down
  for (var i = 0; i < cabins.length; i++) {
    var cabin = cabins[i];
    departCabinSelect.options[i] = new Option(cabin.myFriendlyName, cabin.myId);
    if (cabin.myId == cabinId) {
      selected = i;
    }
  }

  departCabinSelect.options[selected].selected = true;
  checkOutboundCabins(departure, arrival, departCabinSelect);
}
/**
 * checks that the outbound cabins tally with the inbound
 * if the inbound cabins are 'neither' then filter the
 * outbound for all but 'lowest' cabins
 * @param departure     = document.main.departure
 * @param arrival     = document.main.arrival
 * @param departCabinSelect     = document.main.classType
 */
function checkOutboundCabins(departure, arrival, departCabinSelect) {
  var inboundCabinList = getCabinList(arrival, departure);

  if (inboundCabinList != -1) {
    var inboundCabins = cabinGroups[inboundCabinList];
    if (inboundCabins[0].neither) {
      var outboundCabins = cabinGroups[getCabinList(departure, arrival)];
      if (!outboundCabins[0].neither) {
        clearOptions(departCabinSelect);

        var optionIndex = 0;
        for (var i = 0; i < outboundCabins.length; i++) {
          var cabin = outboundCabins[i];
          if (!cabin.flexible) {
            departCabinSelect.options[optionIndex++] = new Option(cabin.myFriendlyName, cabin.myId);
          }
        }
      }
    }
  }
}
/**
  Enables or disables the controls for the return flight depending on whether round trip or one way was selected.
  returnCalendarInput = document.main.returnDate
  returnCalendarId = 'returnDate'
  returnCabinSelect = document.main.classTypeReturn
 **/
function doRoundTrip(isRoundTripId, returnCalendarInput, returnCalendarId, returnCabinSelect) {
  var isRoundTrip = document.getElementById(isRoundTripId);

  if (isRoundTrip.value == BOOKING_TYPE_ONE_WAY) {
    closeCalendarById(returnCalendarId);
  }
  returnCalendarInput.disabled = isRoundTrip.value == BOOKING_TYPE_ONE_WAY;
  returnCabinSelect.disabled = isRoundTrip.value == BOOKING_TYPE_ONE_WAY;
  if (allLetterCodes != null && allLetterCodes.length !=0) {
    var flightSearchForm = window.flightSearchForm || document.forms.flightSearch || document.forms.main;
    flightSearchForm.classLetterReturn.disabled = isRoundTrip.value == BOOKING_TYPE_ONE_WAY;
  }
}
/**
 * Gets the cabin list number from the selected departure and arrival options
 * Depending on whether we need the outbound or not, the params are:
 * @param departure     = document.main.departure if outbound; document.main.arrival if not
 * @param arrival     = document.main.arrival if outbound; document.main.departure if not
 */
function getCabinList(departure, arrival){
  var depCityCode = departure.value;
  var arrCityCode = arrival.value;

  var route = getRoute(depCityCode, arrCityCode);

  if (route != -1) {
      // need to return the cabin list number
    return route.cabinList;
  }
  else {
    // need to return the cabin list number
    return -1;
  }
}
/**
 * this is the Main function called on entry to the page
 * @param searchTypeName = the name of the radio button needed selecting whether the search is One-Way or Round-Trip
 * @param departure = document.main.departure;
  @param arrival = document.main.arrival;
  @param departCabinSelect = document.main.classType;
  @param returnCabinSelect = document.main.classTypeReturn;
 */
function setUp(departure, arrival, departCabinSelect, returnCabinSelect, roundTripSelectId, searchTypeName, returnCalendarInput, returnCalendarId, obDepartCity, obArriveCity, obCabin, ibDepartCity, ibArriveCity, ibCabin) {
  removeUnavailableRoutes();

  var searchType = getSearchType(searchTypeName);

  //check whether the Outbound and Inbound departure cities are in the dropdown list or not
  //if one of them are not available, set default departure cities as Gateway City and LON
  if(checkDepartCities(obDepartCity, ibDepartCity, searchType) || !isDepartCity(obDepartCity)){
    obDepartCity = gatewayCityCode;
    defaultArrivalCity = 'LON';
    if(gatewayCityCode=='LON'){//if Gateway Country is UK, set arrival city as NYC
      defaultArrivalCity = 'NYC';
    }
    obArriveCity = defaultArrivalCity;
    ibDepartCity = defaultArrivalCity;
    ibArriveCity = gatewayCityCode;
  }
  //If gateway city is not in the list, set defult to LON and NYC
  if(checkDepartCities(obDepartCity, ibDepartCity, searchType)){
    obDepartCity = 'LON';
    obArriveCity = 'NYC';
    ibDepartCity = 'NYC';
    ibArriveCity = 'LON';
  }

  if (!nonBookingPage){
    //sets the cabin groups according to the current booking type
    setCabinGroups(searchTypeName);
    //sets the currently selected booking type
    currentSearchOption = getSearchType(searchTypeName);
  }

  // do the outbound stuff
  doOutboundDeparture(obDepartCity, departure, searchTypeName);
  doOutboundArrival(departure.value, obArriveCity, arrival, searchTypeName);

  doOutboundCabin(departure, arrival, obCabin, departCabinSelect);
  if (!nonBookingPage){
    doInboundCabin(departure, arrival, departCabinSelect, returnCabinSelect, ibCabin);
  }
  doRoundTrip(roundTripSelectId, returnCalendarInput, returnCalendarId, returnCabinSelect);
}
function doCommomSettings(depCityValue, arrCityValue){
  if (allLetterCodes){
    doCommomSettingsForLetterCodes(depCityValue, arrCityValue);
  }
}








