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

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

SEARCH_MAIN_FORM_OBJECT = "";
SEARCH_SIDE_FORM_OBJECT = "";

// Write out the calender popup stuff
// removed by Emily

// 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);
  }
}


// Get the field for the starting date month

function getStartMonthObject(formObject)
{
  if (eval('formObject.'+elementNameMonth)) {
    return eval('formObject.'+elementNameMonth);
  }
  if (formObject.pickup_month) {
    // This is car product.
    return formObject.pickup_month;
  }

  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 (formObject.pickup_day) {
    // This is car product.
    return formObject.pickup_day;
  }

  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 ending date month

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

  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 (formObject.return_month) {
    // This is a car product.
    return formObject.return_day;
  }

  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;
}


// Set the start date, called from calendar

function setStartDate(formObject, y, m, d)
{
  var startMonthObject = getStartMonthObject(formObject);
  var startDayObject   = getStartDayObject(formObject);
  var endMonthObject   = getEndMonthObject(formObject);
  var endDayObject     = getEndDayObject(formObject);

  startMonthObject.selectedIndex = m - 1;
  updateStartCalendarDays(formObject);
  
  if (startDayObject) {
    startDayObject.selectedIndex   = d - 1;
  }
  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);

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

  return true;
}


// Set the end date, called from calendar

function setEndDate(formObject, y, m, d)
{
  var endMonthObject   = getEndMonthObject(formObject);
  var endDayObject     = getEndDayObject(formObject);

  endMonthObject.selectedIndex = m - 1;
  updateEndCalendarDays(formObject);
  endDayObject.selectedIndex   = d - 1;

  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);
  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);
  }
}


// Abstract show calendar function

function aShowCalendar(formObject, calendarName, linkId, month, day)
{
  elementNameMonth = month;
  elementNameDay   = day;

  calObj = eval(calendarName);

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

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

  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) {
    updateEnd = true;
  }

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

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

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

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

  // 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);
  endCalendarObject = getEndCalendarObject(formObject);

  endMonth = endMonthObject.selectedIndex
  endDay   = endDayObject.selectedIndex + 1;
  endYear  = get_valid_year(endMonth,endDay); // declared in CalenderPopup.js

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

  // 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;
}

function checkDateCar(aForm)
{

  currDate = new Date();
  currMonth = currDate.getMonth();
  currYear = currDate.getFullYear();

  month1 = aForm.pickup_month.selectedIndex;
  day1 = aForm.pickup_day.selectedIndex + 1;
  year1 = get_valid_year(month1, day1);
  hour1 = aForm.pickup_time.selectedIndex + 1;

  month2 = aForm.return_month.selectedIndex;
    day2 = aForm.return_day.selectedIndex + 1;
  year2 = get_valid_year(month2, day2);
  hour2 = aForm.pickup_time.selectedIndex + 1;

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

  // Invalid date
  if (dateX.getTime() <= currDate.getTime()) {
    message = message + "The pickup date must be later than the current time.\n";
    aForm.pickup_day.focus();
    return false;
  } else if (dateY.getTime() <= dateX.getTime()) {
    if (year1 != year2) {
      message = message + "You have entered an invalid date range. Please check the dates and try again.";
    }else {
      message = message + "The return date must be later than the pickup date.\n";
    }
    aForm.return_day.focus();
    return false;
    }
  return true;
}

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

  roundTrip = isRoundTrip(aForm);

  month1 = aForm.depart_month.selectedIndex;
  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;
  return true;
}
function setReturnDate(y, m, d) {
  formObj = document.search;
  formObj.return_day.selectedIndex = d - 1;
  formObj.return_month.selectedIndex = m - 1;
  return true;
}

function updateTravelYear(monthObj,yearObj)
{
  month     = monthObj.selectedIndex;
  validYear = get_valid_year(month);
  for (i = 0; i < yearObj.options.length; i++) {
    if (yearObj.options[i].value == validYear) {
      yearObj.selectedIndex = i;
      break;
    }
  }
}

function get_valid_year(valid_month, valid_day) {
  if(valid_day == null) {
    valid_day = 1;
  }
  var current_Date = new Date();
  current_month = current_Date.getMonth();
  current_day = current_Date.getDate();
  if(current_month > valid_month || (current_month == valid_month && current_day > valid_day)) {
    valid_year = current_Date.getFullYear() + 1;
  } else {
    valid_year = current_Date.getFullYear();
  }
  return valid_year;
}

function formbuttonClassNew(obj, new_style) {
    obj.className = new_style;
}
function blockClassNew(obj, new_style) {
    obj.className = new_style;
}
