// Legacy settings
var currentForm = 1;
var searchForm = 'searchFHForm';
var openLocationFinder_URL = '';
var HotelRoomType_DOUBLE_ROOM_getType = '1';
var HotelRoomType_SINGLE_ROOM_getType = '0';
var HotelRoomType_getHotelRoomType_0_getAllocation = 1;
var HotelRoomType_getHotelRoomType_1_getAllocation = 2;
var HotelRoomType_getHotelRoomType_2_getAllocation = 2;
var HotelRoomType_getHotelRoomType_3_getAllocation = 3;
var HotelRoomType_getHotelRoomType_4_getAllocation = 4;
var travel_searchFlightsHotels_quicksearch = '/travel/searchCriteria.do?searchType=searchFlightsAndHotels&amp;fromQuickSearch=true';
var travel_searchFlights_quicksearch = '/travel/searchCriteria.do?searchType=searchFlights&amp;fromQuickSearch=true';
var travel_searchHotels_quicksearch = '/travel/searchCriteria.do?searchType=searchHotels&amp;fromQuickSearch=true';
var bean_destinationLocationCode_userValue = '';
var bean_hotelLocationCode_userValue = '';
var bean_departureLocation_userValue = 'LON';
var FLIGHT_DATE_TEXT = {dep: "Departure date:", ret: "Return date:"};
var HOTEL_DATE_TEXT = {dep: "Check-in date:", ret: "Check-out date:"};


var cal;

/**
 * Updates the form with the dates from the calendar
 */
function syncDates() {
  jQuery("input[name='checkinDay']").val(cal.getOutboundDay().selectedValues()[0]);
  jQuery("input[name='checkinMonthYear']").val(cal.getOutboundMonthYear().selectedValues()[0]);
  jQuery("input[name='checkoutDay']").val(cal.getInboundDay().selectedValues()[0]);
  jQuery("input[name='checkoutMonthYear']").val(cal.getInboundMonthYear().selectedValues()[0]);
}

/**
 * Displays the Flight and Hotel search form
 */
function displayFlightAndHotel() {
  jQuery("#dep-from-C, #dest-C, #dep-date-C, #dep-from-C, #ret-date-C, #flx-b, #rm-type01-C, #rm-type02-C, #rmNum01-C, #rmNum02-C, #adult-C, #child-C, #inf-C, #class-C, #star-C, #ad-srch_moreFlightHotelOptions").show();
  jQuery("#dest-H-C, #ad-srch_moreHotelOptions, #ad-srch_moreFlightOptions").hide();
  jQuery("form[name=searchFHForm] input[type=hidden][name=searchType]")[0].value = "searchFlightsAndHotels";
  jQuery("#dep-date-legend, #dep-date-span").text(FLIGHT_DATE_TEXT.dep);
  jQuery("#ret-date-legend, #ret-date-span").text(FLIGHT_DATE_TEXT.ret);
}

/**
 * Displays the Hotel search form
 */
function displayHotel() {
  jQuery("#dest-H-C, #rm-type01-C, #rm-type02-C, #rmNum01-C, #rmNum02-C, #star-C, #dep-date-C, #ret-date-C, #ad-srch_moreHotelOptions").show();
  jQuery("#dep-from-C, #dest-C, #adult-C, #child-C, #inf-C, #ad-srch_moreFlightHotelOptions, #ad-srch_moreFlightOptions, #class-C, #flx-b").hide();
  jQuery("form[name=searchFHForm] input[type=hidden][name=searchType]")[0].value = "searchHotels";
  jQuery("#dep-date-legend, #dep-date-span").text(HOTEL_DATE_TEXT.dep);
  jQuery("#ret-date-legend, #ret-date-span").text(HOTEL_DATE_TEXT.ret);
}

/**
 * Displays the Flight search form
 */
function displayFlight() {
  jQuery("#dep-from-C, #dest-C, #dep-date-C, #ret-date-C, #flx-b, #adult-C, #child-C, #inf-C, #class-C, #ad-srch_moreFlightOptions").show();
  jQuery("#dest-H-C, #rm-type01-C, #rm-type02-C, #rmNum01-C, #rmNum02-C, #star-C, #ad-srch_moreFlightHotelOptions, #ad-srch_moreHotelOptions").hide();
  jQuery("form[name=searchFHForm] input[type=hidden][name=searchType]")[0].value = "searchFlights";
  jQuery("#dep-date-legend, #dep-date-span").text(FLIGHT_DATE_TEXT.dep);
  jQuery("#ret-date-legend, #ret-date-span").text(FLIGHT_DATE_TEXT.ret);
}

jQuery(document).ready(function () {
  cal = new Calendar(jQuery("#d-date"), jQuery("select[name='departureDateDay']"), jQuery("select[name='departureDateMonth']"), jQuery("#r-date"), jQuery("select[name='returnDateDay']"), jQuery("select[name='returnDateMonth']"), syncDates, null, null, 1, true);

  cal.checkDates(true);
	cal.getOutbound().datepicker({minDate: cal.getFirstDate(), maxDate: cal.getLastDate(), showOn: "none", onClose: function (dateText) {cal.updateDates(dateText, true);}, showBelow: cal.getOutboundMonthYear(), numberOfMonths: 2, mandatory: true, changeMonth: false, changeYear: false, dateFormat: 'yy-mm-dd', otherCalendar: jQuery("#r-date"), firstDay: 1});

	cal.getInbound().datepicker({minDate: cal.getFirstDate(), maxDate: cal.getLastDate(), showOn: "none", onClose: function (dateText) {cal.updateDates(dateText, false);}, showBelow: cal.getInboundMonthYear(), numberOfMonths: 2, mandatory: true, changeMonth: false, changeYear: false, dateFormat: 'yy-mm-dd', otherCalendar: jQuery("#d-date"), firstDay: 1});

  jQuery('#outbound').click(function () {
    cal.getOutbound().datepicker('show');
  });
  jQuery('#inbound').click(function () {
    cal.getInbound().datepicker('show');
  });
  updateLayer("", "");

  jQuery(".ad-srch a").click(function () {
    var form = jQuery("form[name=searchFHForm]")[0];
    form.action = this.href;
    form.submit();
    return false;
  });

  if (jQuery("form[name=searchType] input[type=radio][checked]").size() == 0) {
    jQuery("form[name=searchType] input[type=radio]")[0].checked = true;
  }

  jQuery('#flt-htl').click(displayFlightAndHotel);
  jQuery('#htl').click(displayHotel);
  jQuery('#flt').click(displayFlight);

  jQuery("#flt-htl[checked]").each(displayFlightAndHotel);
  jQuery("#htl[checked]").each(displayHotel);
  jQuery("#flt[checked]").each(displayFlight);

  jQuery("*[name=departureLocation.userValue]").change(updateHotelFromDestinationCode);
  jQuery("*[name=destinationLocationCode.userValue]").change(function () {
    updateHotelFromDestinationCode();
    otherDestinationOnClick();
  });
  jQuery("*[name=hotelLocationCode.userValue]").change(function () {
    hotelLocationCodeOnChange();
    otherHotelLocationOnClick();
  });
  jQuery("*[name=numAdults]").change(synchHotelRooms);

  //jQuery("form[name=searchFHForm]").loadSearchForm();
});