var elementNameMonth = 'blank';
var elementNameDay   = 'blank';
var elementNameYear  = 'blank';

// Calendars in the main search -------------------------------
SEARCH_MAIN_FORM_NAME   = "search";
SEARCH_SIDE_FORM_NAME   = "sidesearch";
CRUISE_AVAIL_FORM_NAME  = "availForm";

SEARCH_MAIN_FORM_OBJECT  = "";
SEARCH_SIDE_FORM_OBJECT  = "";
CRUISE_AVAIL_FORM_OBJECT = "";

// Write out the calender popup stuff
document.write(CalendarPopup_getStyles());

var MAIN_START_CALENDAR     = new CalendarPopup("CalPopUp");
MAIN_START_CALENDAR.offsetX = 25;
MAIN_START_CALENDAR.offsetY = 0;
MAIN_START_CALENDAR.setReturnFunction("setMainStartDate");

var MAIN_END_CALENDAR       = new CalendarPopup("CalPopUp");
MAIN_END_CALENDAR.offsetX   = 25;
MAIN_END_CALENDAR.offsetY   = 0;
MAIN_END_CALENDAR.setReturnFunction("setMainEndDate");

var SEGMENT_DEPART_CALENDAR0 = new CalendarPopup("CalPopUp");
SEGMENT_DEPART_CALENDAR0.offsetX = 25;
SEGMENT_DEPART_CALENDAR0.offsetY = 0;
SEGMENT_DEPART_CALENDAR0.setReturnFunction("setMainStartDate");

var SEGMENT_DEPART_CALENDAR1 = new CalendarPopup("CalPopUp");
SEGMENT_DEPART_CALENDAR1.offsetX = 25;
SEGMENT_DEPART_CALENDAR1.offsetY = 0;
SEGMENT_DEPART_CALENDAR1.setReturnFunction("setMainStartDate");

var SEGMENT_DEPART_CALENDAR2 = new CalendarPopup("CalPopUp");
SEGMENT_DEPART_CALENDAR2.offsetX = 25;
SEGMENT_DEPART_CALENDAR2.offsetY = 0;
SEGMENT_DEPART_CALENDAR2.setReturnFunction("setMainStartDate");

var SEGMENT_DEPART_CALENDAR3 = new CalendarPopup("CalPopUp");
SEGMENT_DEPART_CALENDAR3.offsetX = 25;
SEGMENT_DEPART_CALENDAR3.offsetY = 0;
SEGMENT_DEPART_CALENDAR3.setReturnFunction("setMainStartDate");

var SEGMENT_DEPART_CALENDAR4 = new CalendarPopup("CalPopUp");
SEGMENT_DEPART_CALENDAR4.offsetX = 25;
SEGMENT_DEPART_CALENDAR4.offsetY = 0;
SEGMENT_DEPART_CALENDAR4.setReturnFunction("setMainStartDate");

var SEGMENT_DEPART_CALENDAR5 = new CalendarPopup("CalPopUp");
SEGMENT_DEPART_CALENDAR5.offsetX = 25;
SEGMENT_DEPART_CALENDAR5.offsetY = 0;
SEGMENT_DEPART_CALENDAR5.setReturnFunction("setMainStartDate");


var SEGMENT_ARRIVE_CALENDAR0 = new CalendarPopup("CalPopUp");
SEGMENT_ARRIVE_CALENDAR0.offsetX = 25;
SEGMENT_ARRIVE_CALENDAR0.offsetY = 0;
SEGMENT_ARRIVE_CALENDAR0.setReturnFunction("setMainStartDate");

var SEGMENT_ARRIVE_CALENDAR1 = new CalendarPopup("CalPopUp");
SEGMENT_ARRIVE_CALENDAR1.offsetX = 25;
SEGMENT_ARRIVE_CALENDAR1.offsetY = 0;
SEGMENT_ARRIVE_CALENDAR1.setReturnFunction("setMainStartDate");

var SEGMENT_ARRIVE_CALENDAR2 = new CalendarPopup("CalPopUp");
SEGMENT_ARRIVE_CALENDAR2.offsetX = 25;
SEGMENT_ARRIVE_CALENDAR2.offsetY = 0;
SEGMENT_ARRIVE_CALENDAR2.setReturnFunction("setMainStartDate");

var SEGMENT_ARRIVE_CALENDAR3 = new CalendarPopup("CalPopUp");
SEGMENT_ARRIVE_CALENDAR3.offsetX = 25;
SEGMENT_ARRIVE_CALENDAR3.offsetY = 0;
SEGMENT_ARRIVE_CALENDAR3.setReturnFunction("setMainStartDate");

var SEGMENT_ARRIVE_CALENDAR4 = new CalendarPopup("CalPopUp");
SEGMENT_ARRIVE_CALENDAR4.offsetX = 25;
SEGMENT_ARRIVE_CALENDAR4.offsetY = 0;
SEGMENT_ARRIVE_CALENDAR4.setReturnFunction("setMainStartDate");

var SEGMENT_ARRIVE_CALENDAR5 = new CalendarPopup("CalPopUp");
SEGMENT_ARRIVE_CALENDAR5.offsetX = 25;
SEGMENT_ARRIVE_CALENDAR5.offsetY = 0;
SEGMENT_ARRIVE_CALENDAR5.setReturnFunction("setMainStartDate");

var SEGMENT_DEPART_CALENDAR_DUMMY = new CalendarPopup("CalPopUp");
SEGMENT_DEPART_CALENDAR_DUMMY.offsetX = 25;
SEGMENT_DEPART_CALENDAR_DUMMY.offsetY = 0;
SEGMENT_DEPART_CALENDAR_DUMMY.setReturnFunction("setMainStartDate");

// Calendars in the side search  -------------------------------
var SIDE_START_CALENDAR     = new CalendarPopup("CalPopUp");
SIDE_START_CALENDAR.offsetX = 25;
SIDE_START_CALENDAR.offsetY = 0;
SIDE_START_CALENDAR.setReturnFunction("setSideStartDate");

var SIDE_END_CALENDAR       = new CalendarPopup("CalPopUp");
SIDE_END_CALENDAR.offsetX   = 25;
SIDE_END_CALENDAR.offsetY   = 0;
SIDE_END_CALENDAR.setReturnFunction("setSideEndDate");

var SIDE_SEGMENT_DEPART_CALENDAR0 = new CalendarPopup("CalPopUp");
SIDE_SEGMENT_DEPART_CALENDAR0.offsetX = 25;
SIDE_SEGMENT_DEPART_CALENDAR0.offsetY = 0;
SIDE_SEGMENT_DEPART_CALENDAR0.setReturnFunction("setSideStartDate");

var SIDE_SEGMENT_DEPART_CALENDAR1 = new CalendarPopup("CalPopUp");
SIDE_SEGMENT_DEPART_CALENDAR1.offsetX = 25;
SIDE_SEGMENT_DEPART_CALENDAR1.offsetY = 0;
SIDE_SEGMENT_DEPART_CALENDAR1.setReturnFunction("setSideStartDate");

var SIDE_SEGMENT_DEPART_CALENDAR2 = new CalendarPopup("CalPopUp");
SIDE_SEGMENT_DEPART_CALENDAR2.offsetX = 25;
SIDE_SEGMENT_DEPART_CALENDAR2.offsetY = 0;
SIDE_SEGMENT_DEPART_CALENDAR2.setReturnFunction("setSideStartDate");

var SIDE_SEGMENT_DEPART_CALENDAR3 = new CalendarPopup("CalPopUp");
SIDE_SEGMENT_DEPART_CALENDAR3.offsetX = 25;
SIDE_SEGMENT_DEPART_CALENDAR3.offsetY = 0;
SIDE_SEGMENT_DEPART_CALENDAR3.setReturnFunction("setSideStartDate");

var SIDE_SEGMENT_DEPART_CALENDAR_DUMMY = new CalendarPopup("CalPopUp");
SIDE_SEGMENT_DEPART_CALENDAR_DUMMY.offsetX = 25;
SIDE_SEGMENT_DEPART_CALENDAR_DUMMY.offsetY = 0;
SIDE_SEGMENT_DEPART_CALENDAR_DUMMY.setReturnFunction("setSideStartDate");


// --------------------------------------------------------------------
// Initialize the global form Objects, have to do this post document load.
// --------------------------------------------------------------------
function initSearchFormJS()
{
  if (SEARCH_MAIN_FORM_OBJECT == "") {
    SEARCH_MAIN_FORM_OBJECT = eval("document." + SEARCH_MAIN_FORM_NAME);
  }

  if (SEARCH_SIDE_FORM_OBJECT == "") {
    SEARCH_SIDE_FORM_OBJECT = eval("document." + SEARCH_SIDE_FORM_NAME);
  }
  
  if (CRUISE_AVAIL_FORM_OBJECT == "") {
    CRUISE_AVAIL_FORM_OBJECT = eval("document." + CRUISE_AVAIL_FORM_NAME);
  }
}

// --------------------------------------------------------------------
// Get the field for the starting date month
// --------------------------------------------------------------------
function getStartMonthObject(formObject)
{
  if (eval('formObject.'+elementNameMonth)) {
    return eval('formObject.'+elementNameMonth);
  }

  if (document.getElementById('CheckinMonth')) {
    // This is the hotel product
    return document.getElementById('CheckinMonth');
  }

  if (formObject.cruise_month) {
    return formObject.cruise_month;
  }

  if (formObject.pickup_month) {
    // This is car product.
    return formObject.pickup_month;
  }

  if (formObject.pickupMonth) {
    // This is car product.
    return formObject.pickupMonth;
  }

  if (formObject.depart_month) {
    // This is air product.
    return formObject.depart_month;
  }

  if (formObject.checkin_month) {
    // This is hotel product.
    return formObject.checkin_month;
  }
}

// --------------------------------------------------------------------
// Get the field for the starting date day
// --------------------------------------------------------------------
function getStartDayObject(formObject)
{
  if (eval('formObject.'+elementNameDay)) {
    return eval('formObject.'+elementNameDay);
  }

  if (document.getElementById('CheckinDay')) {
    // This is the hotel product
    return document.getElementById('CheckinDay');
  }

  if (formObject.pickup_day) {
    // This is car product.
    return formObject.pickup_day;
  }

  if (formObject.pickupDay) {
    // This is car product.
    return formObject.pickupDay;
  }

  if (formObject.depart_month) {
    // This is air product.
    return formObject.depart_day;
  }

  if (formObject.checkin_month) {
    // This is hotel product.
    return formObject.checkin_day;
  }

  return false;
}

// --------------------------------------------------------------------
// Get the field for the starting date year
// --------------------------------------------------------------------
function getStartYearObject(formObject)
{
  if (eval('formObject.'+elementNameYear)) {
    return eval('formObject.'+elementNameYear);
  }

  if (document.getElementById('CheckinYear')) {
    // This is the hotel product
    return document.getElementById('CheckinYear');
  }

  if (formObject.cruise_year) {
    return formObject.cruise_year;
  }

  if (formObject.pickup_year) {
    // This is car product.
    return formObject.pickup_year;
  }

  if (formObject.pickupYear) {
    // This is car product.
    return formObject.pickupYear;
  }

  if (formObject.depart_year) {
    // This is air product.
    return formObject.depart_year;
  }

  if (formObject.checkin_year) {
    // This is hotel product.
    return formObject.checkin_year;
  }
}

// --------------------------------------------------------------------
// Get the field for the ending date month
// --------------------------------------------------------------------
function getEndMonthObject(formObject)
{

  if (document.getElementById('CheckoutMonth')) {
    // This is the hotel product
    return document.getElementById('CheckoutMonth');
  }

  if (formObject.return_month) {
    // This is a car product
    return formObject.return_month;
  }

  if (formObject.returnMonth) {
    // This is a car product
    return formObject.returnMonth;
  }

  if (formObject.dest_month) {
    // This is a air product.
    return formObject.dest_month;
  }

  if (formObject.checkout_month) {
    // This is a hotel product.
    return formObject.checkout_month;
  }

  return false;
}

// --------------------------------------------------------------------
// Get the field for the ending date day
// --------------------------------------------------------------------
function getEndDayObject(formObject)
{

  if (document.getElementById('CheckoutDay')) {
    // This is the hotel product
    return document.getElementById('CheckoutDay');
  }

  if (formObject.return_month) {
    // This is a car product.
    return formObject.return_day;
  }

  if (formObject.returnMonth) {
    // This is a car product.
    return formObject.returnDay;
  }

  if (formObject.dest_month) {
    // This is a air product.
    return formObject.dest_day;
  }

  if (formObject.checkout_month) {
    // This is a hotel product.
    return formObject.checkout_day;
  }

  return false;
}

// --------------------------------------------------------------------
// Get the field for the ending date year
// --------------------------------------------------------------------
function getEndYearObject(formObject)
{

  if (document.getElementById('CheckoutYear')) {
    // This is the hotel product
    return document.getElementById('CheckoutYear');
  }

  if (formObject.return_year) {
    // This is a car product.
    return formObject.return_year;
  }

  if (formObject.returnYear) {
    // This is a car product.
    return formObject.returnYear;
  }

  if (formObject.dest_year) {
    // This is a air product.
    return formObject.dest_year;
  }

  if (formObject.checkout_year) {
    // This is a hotel product.
    return formObject.checkout_year;
  }

  return false;
}

// --------------------------------------------------------------------
// Set the start date, called from calendar
// --------------------------------------------------------------------
function setStartDate(formObject, y, m, d)
{
  var startMonthObject = getStartMonthObject(formObject);
  var startDayObject   = getStartDayObject(formObject);
  var startYearObject  = getStartYearObject(formObject);
  var endMonthObject   = getEndMonthObject(formObject);
  var endDayObject     = getEndDayObject(formObject);
  var endYearObject    = getEndYearObject(formObject);

  startMonthObject.selectedIndex = m - 1;
  updateStartCalendarDays(formObject);

  if (startDayObject) {
    startDayObject.selectedIndex   = d - 1;
  }

  if (startYearObject) {
    for (i = 0; i < startYearObject.options.length; i++) {
      if (startYearObject.options[i].value == y) {
        startYearObject.selectedIndex = i;
        break;
      }
    }
  }

  if (isMainSearchForm(formObject)) {
    MAIN_START_CALENDAR.currentDate=new Date(y, m -1, d);
  }

  if (isSideSearchForm(formObject)) {
    SIDE_START_CALENDAR.currentDate=new Date(y, m -1, d);
  }

  // Jump the end date according to TRAVEL_DAYS_APART from the start date
  update_travel_date(startMonthObject,startDayObject,endMonthObject,endDayObject,TRAVEL_DAYS_APART, startYearObject, endYearObject);

  // Update the calendar days based on the new end date.
  updateEndCalendarDays(formObject);

  if (elementNameMonth == 'dummyMonth') {
   formObject.segDepartMonth1.selectedIndex = formObject.dummyMonth.selectedIndex;
   formObject.segDepartDay1.selectedIndex   = formObject.dummyDay.selectedIndex;
   formObject.segDepartTime1.selectedIndex  = formObject.dummyTime.selectedIndex;
  }

  return true;
}

// --------------------------------------------------------------------
// Set the end date, called from calendar
// --------------------------------------------------------------------
function setEndDate(formObject, y, m, d)
{
  var endMonthObject   = getEndMonthObject(formObject);
  var endDayObject     = getEndDayObject(formObject);
  var endYearObject    = getEndYearObject(formObject);

  endMonthObject.selectedIndex = m - 1;
  updateEndCalendarDays(formObject);
  endDayObject.selectedIndex   = d - 1;
  if (endYearObject) {
    for (i = 0; i < endYearObject.options.length; i++) {
      if (endYearObject.options[i].value == y) {
        endYearObject.selectedIndex = i;
        break;
      }
    }
  }

  if (isMainSearchForm(formObject)) {
    MAIN_END_CALENDAR.currentDate=new Date(y, m -1, d);
  }

  if (isSideSearchForm(formObject)) {
    SIDE_END_CALENDAR.currentDate=new Date(y, m -1, d);
  }
}

// --------------------------------------------------------------------
// Assign the airport code to the appropriate field.
// --------------------------------------------------------------------
function aAssignAirportCode(aFormName,field,code) {
  airportField = eval("document." + aFormName + '.' + field);
  if (airportField) {
    airportField.value = code;
  }
}

// --------------------------------------------------------------------
// Called from Airport list page.
// --------------------------------------------------------------------
function assignAirportCode(field,code)
{
  initSearchFormJS();

  // We have to assign it to both, since we don't know where the airport code was chosen from.
  if (SEARCH_MAIN_FORM_OBJECT) {
    aAssignAirportCode(SEARCH_MAIN_FORM_NAME,field,code);
  }

  if (SEARCH_SIDE_FORM_OBJECT) {
    aAssignAirportCode(SEARCH_SIDE_FORM_NAME,field,code);
  }
  
  if (CRUISE_AVAIL_FORM_OBJECT) {
    aAssignAirportCode(CRUISE_AVAIL_FORM_NAME,field,code);
  }
}

// --------------------------------------------------------------------
// Abstract show calendar function
// --------------------------------------------------------------------
function aShowCalendar(formObject, calendarName, linkId, month, day, year)
{
  elementNameMonth = month;
  elementNameDay   = day;
  elementNameYear  = year;

  calObj = eval(calendarName);

  startMonthObject = getStartMonthObject(formObject);
  startDayObject   = getStartDayObject(formObject);
  startYearObject  = getStartYearObject(formObject);

  if (calendarName == 'MAIN_END_CALENDAR' || calendarName == 'SIDE_END_CALENDAR') {
    startMonthObject = getEndMonthObject(formObject);
    startDayObject   = getEndDayObject(formObject);
    startYearObject  = getEndYearObject(formObject);
  }

  startMonth = startMonthObject.selectedIndex;
  startDay   = startDayObject.selectedIndex + 1;

  if (!startYearObject) {
    startYear  = get_valid_year(startMonth, startDay); // declared in CalenderPopup.js
  }
  else {
    startYear = startYearObject.options[startYearObject.selectedIndex].value;
  }

  calObj.currentDate = new Date(startYear, startMonth, startDay);

  if (calObj.isVisible) {
    calObj.hideCalendar();
  }
  else {
    calObj.showCalendar(linkId);
  }

  return false;
}

// --------------------------------------------------------------------
// Set the depart date on the main search form
// --------------------------------------------------------------------
function setMainStartDate(y, m, d) {
  initSearchFormJS();

  if (SEARCH_MAIN_FORM_OBJECT) {
    setStartDate(SEARCH_MAIN_FORM_OBJECT,y,m,d);
  }
}

// --------------------------------------------------------------------
// Set the dest date on the main search form
// --------------------------------------------------------------------
function setMainEndDate(y, m, d) {
  initSearchFormJS();

  if (SEARCH_MAIN_FORM_OBJECT) {
    setEndDate(SEARCH_MAIN_FORM_OBJECT, y, m, d);
  }
}

// --------------------------------------------------------------------
// Set the depart date on the side search form
// --------------------------------------------------------------------
function setSideStartDate(y, m, d) {
  initSearchFormJS();

  if (SEARCH_SIDE_FORM_OBJECT) {
    setStartDate(SEARCH_SIDE_FORM_OBJECT,y,m,d);
  }
}

// --------------------------------------------------------------------
// Set the dest date on the side search form
// --------------------------------------------------------------------
function setSideEndDate(y, m, d) {
  initSearchFormJS();

  if (SEARCH_SIDE_FORM_OBJECT) {
    setEndDate(SEARCH_SIDE_FORM_OBJECT, y, m, d);
  }
}

// --------------------------------------------------------------------
// Switch the air flight type
// --------------------------------------------------------------------
function airSwitchFlightType(aForm, flightType, airIndexURL)
{
  aForm.flight_type.value = flightType;
  if (airIndexURL && airIndexURL.length) {
    aForm.action = airIndexURL;
    aForm.submit();
  }
}

// --------------------------------------------------------------------
// Check if the flight is round trip.
// --------------------------------------------------------------------
function isRoundTrip(aForm)
{
  // Make sure the flight_type field exists in the form.
  if (!aForm.flight_type) {
    return false;
  }

  // 0 means that it is a round trip flight
  if (aForm.flight_type.value == 0) {
    return true;
  }

  return false;
}

// --------------------------------------------------------------------
// Check if the flight is oneway.
// --------------------------------------------------------------------
function isOneWay(aForm)
{

  // Make sure the flight_type field exists in the form.
  if (!aForm.flight_type) {
    return false;
  }

  // 1 means that it is a oneway flight
  if (aForm.flight_type.value == 1) {
    return true;
  }

  return false;
}



// --------------------------------------------------------------------
// Determine whether or not it is the main search form
// --------------------------------------------------------------------
function isMainSearchForm(formObject)
{
  // Quick check on the formObject to see if it exists
  if (!formObject) {
    return false;
  }

  if (formObject.name == SEARCH_MAIN_FORM_NAME) {
    return true;
  }

  return false;
}

// --------------------------------------------------------------------
// Determine whether or not it is the side search form
// --------------------------------------------------------------------
function isSideSearchForm(formObject)
{
  // Quick check on the formObject to see if it exists
  if (!formObject) {
    return false;
  }

  if (formObject.name == SEARCH_SIDE_FORM_NAME) {
    return true;
  }

  return false;
}


// --------------------------------------------------------------------
// Get the starting calendar object based on the form we are in
// --------------------------------------------------------------------
function getStartCalendarObject(formObject)
{
  if (isMainSearchForm(formObject)) {
    return MAIN_START_CALENDAR;
  }

  if (isSideSearchForm(formObject)) {
    return SIDE_START_CALENDAR;
  }
}


// --------------------------------------------------------------------
// Get the ending calendar object based on the form we are in
// --------------------------------------------------------------------
function getEndCalendarObject(formObject)
{
   if (isMainSearchForm(formObject)) {
    return MAIN_END_CALENDAR
  }

  if (isSideSearchForm(formObject)) {
    return SIDE_END_CALENDAR;
  }
}

// --------------------------------------------------------------------
// Update the days of the start date months select drop down and the start
// calendar.
// --------------------------------------------------------------------
function updateStartCalendarDays(formObject, updateEnd, month, day)
{
  if (updateEnd == null && !document.getElementById('pickup_city')) {
    updateEnd = true;
  }
  else {
    updateEnd = false;
  }

  // Get the start date field objects
  startMonthObject = getStartMonthObject(formObject);
  startDayObject   = getStartDayObject(formObject);
  startYearObject  = getStartYearObject(formObject);

  if (!startYearObject) {
    startYearObject = null;
  }

  // Get the end date field objects
  endMonthObject   = getEndMonthObject(formObject);
  endDayObject     = getEndDayObject(formObject);
  endYearObject    = getEndYearObject(formObject);

  if (!endYearObject) {
    endYearObject = null;
  }

  // Reset the days on the start date and update the calendar
  resetDays(startMonthObject,startDayObject, startYearObject,getStartCalendarObject(formObject));

  // Update the end date based on the start date
  update_travel_date(startMonthObject,startDayObject,endMonthObject,endDayObject,TRAVEL_DAYS_APART, startYearObject, endYearObject);

  // Reset the days of the end date and update the calendar
  if (updateEnd) {
    updateEndCalendarDays(formObject);
  }

  return true;
}

// --------------------------------------------------------------------
// Update the days of the end date days select drop down and the start
// calendar.
// --------------------------------------------------------------------
function updateEndCalendarDays(formObject)
{
  // Get the start date field objects
  startMonthObject = getStartMonthObject(formObject);
  startDayObject   = getStartDayObject(formObject);

  // Get the end date field objects
  endMonthObject    = getEndMonthObject(formObject);
  endDayObject      = getEndDayObject(formObject);
  endYearObject     = getEndYearObject(formObject);

  endCalendarObject = getEndCalendarObject(formObject);

  endMonth = endMonthObject.selectedIndex;
  endDay   = endDayObject.selectedIndex + 1;

  if (!endYearObject) {
    endYear  = get_valid_year(endMonth,endDay); // declared in CalenderPopup.js
    endYearObject = null;
  }
  else {
    endYear  = endYearObject.options[endYearObject.selectedIndex].value;
  }

  // Reset the days on the start date.
  resetDays(endMonthObject,endDayObject, endYearObject);

  // Set the calendar to the current date.
  getEndCalendarObject(formObject).currentDate = new Date(endYear, endMonth, endDay);

  return true;
}

// --------------------------------------------------------------------
// Switch the air flight type accordingly, redirect if airIndexURL is
// defined
// --------------------------------------------------------------------
function airSwitchFlightType(formObject, flightType, airIndexURL)
{
  formObject.flight_type.value = flightType;
  if (airIndexURL && airIndexURL.length) {
    formObject.action = airIndexURL;
    formObject.submit();
  }
}

// --------------------------------------------------------------------
// Switch the air form to one-way
// --------------------------------------------------------------------
function airSearchShowOneWay(formObject)
{
  airSwitchFlightType(formObject,1);

  if (isMainSearchForm(formObject)) {
    hideDiv('mainSearchReturnDate');
  }

  if (isSideSearchForm(formObject)) {
    hideDiv('sideSearchReturnDate');
  }
}

// --------------------------------------------------------------------
// Switch the air form to round trip
// --------------------------------------------------------------------
function airSearchShowRoundTrip(formObject)
{
  airSwitchFlightType(formObject,0);

  if (isMainSearchForm(formObject)) {
    showDiv('mainSearchReturnDate');
  }

  if (isSideSearchForm(formObject)) {
    showDiv('sideSearchReturnDate');
  }
}

// --------------------------------------------------------------------
// Show the correct form based on the flightType passed in:
//   0 : roundtrip
//   1 : one-way
// --------------------------------------------------------------------
function updateAirSearchForm(formObject,flightType)
{
  if (flightType) {
    airSearchShowOneWay(formObject);
  }
  else {
    airSearchShowRoundTrip(formObject);
  }
}

// --------------------------------------------------------------------
// Checks the dates of travel upon submit
// --------------------------------------------------------------------
function checkTravelDates(formObject, no_days)
{
  currDate  = new Date();
  currMonth = currDate.getMonth();
  currYear  = currDate.getFullYear();

  // Get the start date field objects
  startMonthObject = getStartMonthObject(formObject);
  startDayObject   = getStartDayObject(formObject);

  endMonthObject   = getEndMonthObject(formObject);
  endDayObject     = getEndDayObject(formObject);

  // roundTrip = isRoundTrip(aForm);

  // Get the start dates
  startMonth = startMonthObject.selectedIndex
  startDay   = startDayObject.selectedIndex + 1;
  startYear  = get_valid_year(startMonth,startDay); // declared in CalenderPopup.js

  /*
  if (roundTrip) {
    month2 = aForm.dest_month.selectedIndex;
    day2 = aForm.dest_day.selectedIndex + 1;
    year2 = get_valid_year(month2, day2);
  }

  dateX = new Date(year1, month1, day1);

  if (roundTrip) {
    dateY = new Date(year2, month2, day2);
  }

  // Invalid date
  if (dateX.getTime() < currDate.getTime()) {
    message = message + "The departure date must be at least 1 day from today.\n";
    aForm.depart_day.focus();
    return false;
  }
  else if (roundTrip && dateY.getTime() < dateX.getTime()) {
    message = message + "The return date must be later than the departure date.\n";
    aForm.dest_day.focus();
    return false;
  }
  else if (no_days != null && dateX.getTime() > currDate.getTime() + (no_days * (1000 * 60 * 60 * 24))) {
    message = message + "The departure date must be earlier than "+ no_days +" days from now for bidding.\n";
    aForm.depart_day.focus();
  }
  */
  return true;
}

// --------------------------------------------------------------------
// Validate the search form
// --------------------------------------------------------------------
function validateSearchForm(aForm)
{
  message = '';
  if(aForm.departure && aForm.destination.value == '' && aForm.destination && aForm.departure.value == '') {
    message = "Departure and Destination can not be empty.\n";
    aForm.departure.focus();
  } else if(aForm.departure && aForm.departure.value == '') {
    message = "Departure can not be empty.\n";
    aForm.departure.focus();
  } else if(aForm.destination && aForm.destination.value == '') {
    message = "Destination can not be empty.\n";
    aForm.destination.focus();
  }

  checkTravelDates(aForm);

  if(message != '') {
    alert(message);
    return false;
  }
  if(window.loadSplash) loadSplash();
  return true;
}


function checkCruiseForm(formObj)
{
  if (checkField(formObj.DestinationID, "Please select a destination.")) {
    return true;
  }
  return false;
}

function checkCruiseLineForm(formObj)
{
  if (checkField(formObj.VendorID, "Please select a cruise line.")) {
    return true;
  }
  return false;
}


function validateForm(aForm)
{
  message = '';

  if(aForm.pickup_month) {
    checkDateCar(aForm);
  }
  else if(aForm.depart_month) {
    checkDateAir(aForm, TRAVEL_DAYS_APART);
  }
  else if(aForm.checkin_month) {
    checkDateHotel(aForm, TRAVEL_DAYS_APART);
  }

  if(message != '') {
    alert(message);
    return false;
  }

  if(window.loadSplash) loadSplash();

  return true;
}


/**
 * For car search forms.
 */
function checkDateCar(aForm)
{
  // Now
  initDate    = new Date();

  // Current date obj
  currMonth   = initDate.getMonth();
  currYear    = initDate.getFullYear();
  currDay     = initDate.getDate();
  currHour    = initDate.getHours();
  currDate    = new Date(currYear, currMonth, currDay, currHour, 0, 0, 0);

  // Pickup date obj
  if (aForm.elements['pickup_month'].type == 'select-one') {
    month1      = aForm.pickup_month.selectedIndex;
  }
  else {
    month1      = aForm.pickup_month.value - 1;
  }

  if (aForm.elements['pickup_day'].type == 'select-one') {
    day1        = aForm.pickup_day.selectedIndex + 1;
  }
  else {
    day1        = aForm.pickup_day.value;
  }

  if (aForm.elements['pickup_year']) {
    year1 = aForm.pickup_year.value;
  }
  else {
    year1     = get_valid_year(month1, day1);
  }

  hour1       = aForm.pickup_time.value;
  dateX       = new Date(year1, month1, day1, hour1, 0, 0, 0);

  // Return date obj
  if (aForm.elements['return_month'].type == 'select-one') {
    month2      = aForm.return_month.selectedIndex;
  }
  else {
    month2      = aForm.return_month.value - 1;
  }

  if (aForm.elements['return_day'].type == 'select-one') {
    day2        = aForm.return_day.selectedIndex + 1;
  }
  else {
    day2        = aForm.return_day.value;
  }

  if (aForm.elements['return_year']) {
    year2     = aForm.return_year.value;
  }
  else {
    year2     = get_valid_year(month2, day2);
  }

  hour2       = aForm.return_time.value;
  dateY       = new Date(year2, month2, day2, hour2, 0, 0, 0);

  // pieces of time in a day.
  var day_mseconds = (24 * 60 * 60 * 1000);

  // car search must be on or after this timestamp
  var min_time    = currDate.getTime() + (day_mseconds * CARMINDAYS);
  var minDate     = new Date(min_time);

  // Amount of lead time required by this site to book cars.
  var min_hours_warn = CARMINDAYS * 24;

  // Check Pickup city.
  if(aForm.elements['pickup_city'] && aForm.pickup_city.value == '') {
    message = message + "Pick up location can not be empty.\n";
    aForm.pickup_city.focus();
  }

  // Return city is an optional field, driven by config/gds for oneway rentals
  if (aForm.elements['return_city']) {
    // Set return city to the value of the pickup city by default
    if (aForm.return_city.value == '' && aForm.pickup_city.value != '') {
      setDefaultReturnCity(aForm.return_city);
    }
    // if a return city is available validate that a vendor has been selected, required for one way rentals.
    validateOneWay(aForm);
  }

  // Pickup date validation, before today?
  if (dateX.getTime() < currDate.getTime()) {
    message = message + "The pickup date and time must be later than the current date and time.\n";
    aForm.pickup_day.value   = minDate.getDate();
    aForm.pickup_month.value = minDate.getMonth() + 1;
    aForm.pickup_time.value  = minDate.getHours() + 1;
    if (aForm.elements['pickup_year']) {
      aForm.pickup_year.value  = minDate.getYear();
    }
    if (aForm.elements['pickup_day'].type != 'hidden') {
      aForm.pickup_day.focus();
    }
  }

  // Pickup date validation, check for min days?
  if (min_hours_warn > 0 && dateX.getTime() <= min_time) {
    message = message + "The pickup date and time must be at least ";
    message = message + min_hours_warn;
    message = message + " hours from now.\n";

    // write document min values for pickup time, day, month, year...
    aForm.pickup_day.value   = minDate.getDate();
    aForm.pickup_month.value = minDate.getMonth() + 1;
    aForm.pickup_time.value  = minDate.getHours() + 1;
    if (aForm.elements['pickup_year']) {
      aForm.pickup_year.value  = minDate.getYear();
    }
    if (aForm.elements['pickup_day'].type != 'hidden') {
      aForm.pickup_day.focus();
    }
  }

  // Return time is the same or sooner than pickup time
  if (dateY.getTime() <= dateX.getTime()) {
    // different year?
    if (year1 != year2) {
      message = message + "You have entered an invalid date range. Please check the dates and try again.\n";
    }
    else {
      message = message + "The return date must be later than the pickup date.\n";
    }

    if (aForm.elements['return_day'].type != 'hidden') {
      aForm.return_day.focus();
    }
  }

  return true;
}


/**
 * for car search forms, make sure that a vendor is selected when chosing a one way rental.
 */
function validateOneWay(form)
{
  // if there is a select list, find it's value and test, if not return true and continue.
  if (form.elements['car_company'].type == 'select-one') {
    var car_company_selected = form.elements['car_company'].value;
  }
  else {
    return true;
  }

  // test for an input field for return city, if it is different from the pickup city, force vendor selection (one-way).
  if (form.elements['return_city'].value) {
    if (form.elements['return_city'].value != form.elements['pickup_city'].value && car_company_selected == 0) {
      message = message + 'You must select a specific car company for one way rentals.\n';
      return false;
    }
  }

  return true;
}


/**
 * prefill return city when one way searches are available.
 */
function setDefaultReturnCity(form)
{
  if (form.elements['return_city']) {
    var pickup_city_value = form.pickup_city.value;

    if (form.return_city.value == '') {
      form.return_city.value = pickup_city_value;
    }

    return true;
  }

  return false;
}


/**
 * Air search forms
 */
function checkDateAir(aForm, no_days)
{
  currDate = new Date();
  currMonth = currDate.getMonth();
  currYear = currDate.getFullYear();

  roundTrip = isRoundTrip(aForm);

  month1 = aForm.depart_month.selectedIndex + 1;
  day1 = aForm.depart_day.selectedIndex + 1;
  year1 = get_valid_year(month1, day1);
  hour1 = aForm.depart_time.selectedIndex + 1;

  if (roundTrip) {
    month2 = aForm.dest_month.selectedIndex;
    day2 = aForm.dest_day.selectedIndex + 1;
    year2 = get_valid_year(month2, day2);
    hour2 = aForm.depart_time.selectedIndex + 1;
  }

  dateX = new Date(year1, month1, day1, hour1, 0, 0, 0);

  if (roundTrip) {
    dateY = new Date(year2, month2, day2, hour2, 0, 0, 0);
  }

  // Invalid date
  if (dateX.getTime() <= currDate.getTime()) {
    message = message + "The departure date must be after the current time.\n";
    aForm.depart_day.focus();
    return false;
  }
  else if (dateX.getTime() < (currDate.getTime() + (1000 * 60 * 60 * 24))) {
    message = message + "Same Day Travel or Travel within 24hrs not permitted.\n";
    aForm.depart_day.focus();
    return false;
  }
  else if (roundTrip && dateY.getTime() < dateX.getTime()) {
    message = message + "The return date must be later than the departure date.\n";
    aForm.dest_day.focus();
    return false;
  }
  /* Not sure why we have this so commenting out.
  else if (no_days != null && dateX.getTime() > currDate.getTime() + (no_days * (1000 * 60 * 60 * 24))) {
    alert(dateX.getTime() + ' > ' +  (no_days * (1000 * 60 * 60 * 24)));
    message = message + "The departure date must be earlier than 60 days from today.\n";
    aForm.depart_day.focus();
  }
  */
  return true;
}

function checkDateHotel(aForm, no_days)
{
  currDate = new Date();
  currMonth = currDate.getMonth();
  currYear = currDate.getFullYear();

  month1 = aForm.checkin_month.selectedIndex;
  day1 = aForm.checkin_day.selectedIndex + 1;
  year1 = get_valid_year(month1, day1);

  month2 = aForm.checkout_month.selectedIndex;
    day2 = aForm.checkout_day.selectedIndex + 1;
  year2 = get_valid_year(month2, day2);

  dateX = new Date(year1, month1, day1);
  dateY = new Date(year2, month2, day2);

  // Invalid date
  if (dateX.getTime() < currDate.getTime()) {
    message = message + "The departure date must be at least 1 day from today.\n";
    aForm.checkin_day.focus();
    return false;
  } else if (dateY.getTime() < dateX.getTime()) {
    message = message + "The return date must be later than the departure date.\n";
    aForm.checkout_day.focus();
    return false;
    }

//  else if (no_days != null && dateX.getTime() > currDate.getTime() + (no_days * (1000 * 60 * 60 * 24))) {
//  message = message + "The departure date must be earlier than "+ no_days +" days from now for bidding.\n";
//  aForm.checkin_day.focus();
//  }
//  return true;
}

// --------------------------------------------------------------------
// When using a map to search for a city, this will redirect the parent
// windown to the search page and close the popup window.
// --------------------------------------------------------------------
function search_redirect(url)
{
  opener.location=url;
  this.close();
}

function setPickupDate(y, m, d) {
  formObj = document.search;
  formObj.pickup_day.selectedIndex = d - 1;
  formObj.pickup_month.selectedIndex = m - 1;

  if (formObj.pickup_year) {
    for (i = 0; i < pickup_year.options.length; i++) {
      if (pickup_year.options[i].value == y) {
        pickup_year.selectedIndex = i;
        break;
      }
    }
  }

  return true;
}
function setReturnDate(y, m, d) {
  formObj = document.search;
  formObj.return_day.selectedIndex = d - 1;
  formObj.return_month.selectedIndex = m - 1;

  if (formObj.return_year) {
    for (i = 0; i < return_year.options.length; i++) {
      if (return_year.options[i].value == y) {
        return_year.selectedIndex = i;
        break;
      }
    }
  }

  return true;
}

