function openLocationFinder(fieldName, flightType, callback, failedValidation) {
  textVal = document.forms[searchForm].elements[fieldName].value;
  url = openLocationFinder_URL + '/travel/locationFinder.do?productType=' + flightType + '&text=' + textVal + '&callback=' + callback + '&callbackType=locationNeeded';
  if (failedValidation) {
    if (failedValidation.length > 0) {
      url = url + '&failedValidation=true';
    }
  }
  new_openWindow(url, 320, 450, '1', '0', 'finder');
}

function repopulateDepart(description, ref, dest, destRef) {

  var returnVal = description;
  if (returnVal == '') {
    returnVal += dest;
  }

  //document.forms[searchForm].elements['departureLocation.userValue'].value = returnVal;
  document.forms[searchForm].elements['departureLocation.valueCopy'].value = returnVal;

  if (description == '' && ref == '') {
    document.forms[searchForm].elements['departureLocation.referenceValue'].value = destRef;
  }
  else {
    document.forms[searchForm].elements['departureLocation.referenceValue'].value = ref;
  }

}

function repopulateDestination(description, ref, dest, destRef) {
  var returnVal = description;
  if (returnVal == '') {
    returnVal += dest;
  }

  document.forms[searchForm].elements['destinationLocation.userValue'].value = returnVal;
  document.forms[searchForm].elements['destinationLocation.valueCopy'].value = returnVal;

  if (description == '' && ref == '') {
    document.forms[searchForm].elements['destinationLocation.referenceValue'].value = destRef;
  }
  else {
    document.forms[searchForm].elements['destinationLocation.referenceValue'].value = ref;
  }
}

function updateHotelFromDestination() {

  document.forms[searchForm].elements['hotelLocation.userValue'].value = document.forms[searchForm].elements['destinationLocation.userValue'].value;
}

function updateHotelFromDestinationCode() {
  var temp = getHotelLocation(document.forms[searchForm].elements['destinationLocationCode.userValue'].value) ;
  document.forms[searchForm].elements['hotelLocationCode.userValue'].value = temp;
}

function departureLocationOnFocus() {

  var field = document.forms[searchForm].elements['departureLocation.userValue'];
  if (field.value == SearchFlightsHotelsForm_DEFAULT_DEPARTURE_AIRPORT)
    field.value = '';
  field.style.color = '#000000';
}

function destinationLocationOnFocus() {

  var field = document.forms[searchForm].elements['destinationLocation.userValue'];
  if (field.value == SearchFlightsHotelsForm_DEFAULT_DESTINATION_AIRPORT)
    field.value = '';
  field.style.color = '#000000';
}

function openHotelsLocationFinder() {

  openLocationFinder('hotelLocation.userValue', 'hotel', 'repopulateHotelLocation');
}


function repopulateHotelLocation(description, ref, dest, destRef) {

  var returnVal = new String(description);
  if (dest.length > 0) {
    if (returnVal.length > 0) returnVal = returnVal + ', ';
    returnVal = returnVal + dest;
  }

  document.forms[searchForm].elements['hotelLocation.userValue'].value = returnVal;
  document.forms[searchForm].elements['hotelLocation.valueCopy'].value = returnVal;

  if (ref != '') {
    document.forms[searchForm].elements['hotelLocation.referenceValue'].value = ref;
  }
  else {
    document.forms[searchForm].elements['hotelLocation.referenceValue'].value = destRef;
  }
}

function hotelLocationOnFocus() {

  var field = document.forms[searchForm].elements['hotelLocation.userValue'];
  if (field.value == 'SearchFlightsHotelsForm_DEFAULT_HOTEL_LOCATION')
    field.value = '';
  field.style.color = '#000000';
}

function getFormName() {

  // return "searchFlightsHotelsForm" + currentForm;
  return searchForm;
}


function syncMonths1(theForm) {

  theForm.departureDateMonth.selectedIndex = theForm.checkinMonthYear.selectedIndex;

  departureDate = calculateDepartureDate(theForm);
  returnDate = calculateReturnDate(theForm);

  if (departureDate > returnDate) {
    theForm.returnDateMonth.selectedIndex = theForm.checkinMonthYear.selectedIndex;
    theForm.checkoutMonthYear.selectedIndex = theForm.checkinMonthYear.selectedIndex;
  }
}

function syncMonths2(theForm) {

  theForm.returnDateMonth.selectedIndex = theForm.checkoutMonthYear.selectedIndex;

  departureDate = calculateDepartureDate(theForm);
  returnDate = calculateReturnDate(theForm);

  if (departureDate > returnDate) {
    theForm.departureDateMonth.selectedIndex = theForm.returnDateMonth.selectedIndex;
    theForm.checkinMonthYear.selectedIndex = theForm.returnDateMonth.selectedIndex;
  }
}


function syncHotelMonths1(theForm) {

  departureDate = calculateDepartureDate(theForm);
  returnDate = calculateReturnDate(theForm);

  if (departureDate > returnDate) {
    theForm.returnDateMonth.selectedIndex = theForm.departureDateMonth.selectedIndex;
    theForm.checkoutMonthYear.selectedIndex = theForm.returnDateMonth.selectedIndex;
  }
}


function syncHotelMonths2(theForm) {

  departureDate = calculateDepartureDate(theForm);
  returnDate = calculateReturnDate(theForm);

  if (departureDate > returnDate) {
    theForm.checkinMonthYear.selectedIndex = theForm.departureDateMonth.selectedIndex;
    theForm.checkoutMonthYear.selectedIndex = theForm.returnDateMonth.selectedIndex;
  }
}
// sets hotel room type and quantities
// note: the calculations only assumes single and double rooms - triples, quadruples etc.
// need to be selected manually
function synchHotelRooms() {

  // Get number of adult passengers
  var nAdultPassengers = parseInt(document.forms[searchForm].elements['numAdults'].value);
  var nChildPassengers = parseInt(document.forms[searchForm].elements['numChildren'].value);

  var nTotalPassengers = nAdultPassengers; // +  nChildPassengers;
  // number of singles = passengers mod 2
  var nSingleRooms = nTotalPassengers % 2;

  // number of doubles = passengers / 2 (java style)
  var nDoubleRooms = (nTotalPassengers - nSingleRooms) / 2;

  if (nDoubleRooms > 0) {
    // Set the first room
    document.forms[searchForm].elements['roomType1'].value = HotelRoomType_DOUBLE_ROOM_getType;
    document.forms[searchForm].elements['roomNumber1'].value = nDoubleRooms;

    if (nSingleRooms > 0) {
      // Set the second room
      document.forms[searchForm].elements['roomType2'].value = HotelRoomType_SINGLE_ROOM_getType;
      document.forms[searchForm].elements['roomNumber2'].value = nSingleRooms;
    }
  } else {
    // Set the first room
    document.forms[searchForm].elements['roomType1'].value = HotelRoomType_SINGLE_ROOM_getType;
    document.forms[searchForm].elements['roomNumber1'].value = nSingleRooms;
  }

  // Set a blank second room?
  if (nSingleRooms == 0 || nDoubleRooms == 0) {
    document.forms[searchForm].elements['roomType2'].selectedIndex = 0;
    document.forms[searchForm].elements['roomNumber2'].selectedIndex = 0;
  }

}

//This function is used to calculate the number of adults for a combined search. It is called as the onchange
//event when the room type or number of rooms is changed. The number of adults is calculated by multiplying the number
//of rooms by the capacity of the room type selected, and the result is stored in the numAdults form property.
function updateNumAdultsCombinedSearch(theForm) {

  // reset the second room
  theForm.roomType2.value = -1;
  theForm.roomNumber2.value = 0;

  var roomType = theForm.roomType1.value;
  var numRooms = theForm.roomNumber1.value;
  var adultsPerRoom = 0;
  if (roomType == 0) {
    adultsPerRoom = HotelRoomType_getHotelRoomType_0_getAllocation;
  }
  else if (roomType == 1) {
    adultsPerRoom = HotelRoomType_getHotelRoomType_1_getAllocation;
  }
  else if (roomType == 2) {
      adultsPerRoom = HotelRoomType_getHotelRoomType_2_getAllocation;
    }
    else if (roomType == 3) {
        adultsPerRoom = HotelRoomType_getHotelRoomType_3_getAllocation;
      }
      else if (roomType == 4) {
          adultsPerRoom = HotelRoomType_getHotelRoomType_4_getAllocation;
        }

  var numAdults = adultsPerRoom * numRooms;
  theForm.numAdults.value = numAdults;

}

function hotelLocationCodeOnChange() {

  document.forms[searchForm].elements['hotelLocation.userValue'].defaultSelection = document.forms[searchForm].elements['hotelLocation.copy'].value;
  document.forms[searchForm].elements['hotelLocation.copy'].value = false;
  // document.forms[searchForm].elements['hotelLocation.userValue'].defaultSelection = document.forms[searchForm].elements['hotelLocation.copy'].value
  document.forms[searchForm].elements['hotelLocation.copy'].value = false;
}


function swapToAccomodationOnly() {


}

function otherDestinationOnClick()
{
  var field = document.forms[searchForm].elements['destinationLocationCode.userValue'];
  if (field.value == 'Other') {
    changeDropDown();

    var theForm = document.forms['searchType'];
    if (theForm.radioButtonValue.value == "") {
      location.href = travel_searchFlightsHotels_quicksearch;
    }
    else {
      location.href = travel_searchFlights_quicksearch;
    }
  }
}

function otherHotelLocationOnClick()
{

  var field = document.forms[searchForm].elements['hotelLocationCode.userValue'];
  if (field.value == 'Other')
  //changeHDropDown();
    location.href = travel_searchHotels_quicksearch;
}


function getHotelLocation(flightLoc) {

  for (x = 1; x < f_dests.length; x++) {
    if (f_dests[x][1] == flightLoc) {
      return f_dests[x][2];
    }


  }

}


function doSubmit() {

  setRadioButtonValue();
  type = document.forms['searchType'].radioButtonValue.value;

  if (type == 'flighthotel') {

    // document.forms[searchForm].elements['hotelLocationCode.userValue'].value=getHotelLocation( document.forms[searchForm].elements['destinationLocationCode.userValue'].value);
    updateHotelFromDestinationCode();
    document.forms[searchForm].elements['destinationLocation.userValue'].value = "";
    document.forms[searchForm].elements['hotelLocation.userValue'].value = "";


  }
  else if (type == 'hotel') {

    document.forms[searchForm].elements['hotelLocation.userValue'].value = "";

  }
  else if (type == 'flight') {
      updateHotelFromDestinationCode();
      document.forms[searchForm].elements['destinationLocation.userValue'].value = "";


      /* ensure the second room type is blank */
      document.forms[searchForm].elements['roomType2'].value = -1;
      document.forms[searchForm].elements['roomNumber2'].value = 0;

      if (document.forms[searchForm].elements['numAdults'].value == 1) {
        document.forms[searchForm].elements['roomType1'].value = 0;
        document.forms[searchForm].elements['roomNumber1'].value = 1;

      }
      else {
        if (document.forms[searchForm].elements['numAdults'].value % 2 != 0) {
          document.forms[searchForm].elements['roomType2'].value = 0;
          document.forms[searchForm].elements['roomNumber2'].value = 1;
        }

        document.forms[searchForm].elements['roomType1'].value = 1;
        document.forms[searchForm].elements['roomNumber1'].value = document.forms[searchForm].elements['searchType'].value / 2;

      }
    }


}


/*----*/
/* this function is used to determine and remember which radio button was selected ie type of search selected */

function setRadioButtonValue() {

  var theForm = document.forms['searchType'];
  var i;
  for (i = 0; i < theForm.searchTypeRadio.length; ++i) {
    if (theForm.searchTypeRadio[i].checked)
      break;
  }
  theForm.radioButtonValue.value = theForm.searchTypeRadio[i].value;
}

/* function to format the radio button */
function displayRadioText(type) {

  if (type == 'flighthotel') {
    currentForm = 1;
    //  document.getElementById("sid1").style.fontWeight="bold";
    //  document.getElementById("sid2").style.fontWeight="normal";
    //  document.getElementById("sid3").style.fontWeight="normal";
  }
  // else if (type == 'hotel') {
  //   document.getElementById("sid1").style.fontWeight="normal";
  //   document.getElementById("sid2").style.fontWeight="bold";
  //   document.getElementById("sid3").style.fontWeight="normal";
  // }
  // else if (type == 'flight') {
  //  document.getElementById("sid1").style.fontWeight="normal";
  //  document.getElementById("sid2").style.fontWeight="normal";
  //  document.getElementById("sid3").style.fontWeight="bold";
  // }

}

/* this function is called when the homepage loads to determine which type of search option should be selected */
function updateLayer(value, text) {
  /* load all the drop down lists */

  var hotelDestCounter = 0;
  var flightDestCounter = 0;
  for (var i = 0; i < f_dests.length; i++) {
    if (f_dests[i][1].charAt(0) == '#') {
      hotelDestCounter++;
      document.forms[searchForm].elements['hotelLocationCode.userValue'].options[ hotelDestCounter ] = new Option( f_dests[i][0], f_dests[i][2]);
    }
    else {
      flightDestCounter++;
      hotelDestCounter++;
      document.forms[searchForm].elements['destinationLocationCode.userValue'].options[flightDestCounter] = new Option(f_dests[i][0], f_dests[i][1]);
      document.forms[searchForm].elements['hotelLocationCode.userValue'].options[hotelDestCounter] = new Option(f_dests[i][0], f_dests[i][2]);
    }

  }

  for (var i = 0; i < uk_d.length; i++) {
    document.forms[searchForm].elements['departureLocation.userValue'].options[i + 1] = new Option(uk_d[i][0], uk_d[i][1]);
  }
}

/* This function is used to set up the quicksearch form */
function setupQuickSearch() {
  var theForm = document.forms['searchType'];
  var i;
  var nothingChecked = false;
  for (i = 0; i < theForm.searchTypeRadio.length; ++i) {
    if (theForm.searchTypeRadio[i].checked) {
      nothingChecked = false;
      break;
    } else {
      nothingChecked = true;
    }
  }
  if (!nothingChecked) {
    updateLayer1(theForm.searchTypeRadio[i].value, 'dropDown');
  } else {
    /* Set the default checked value to Flights & Hotels radio button */
    theForm.searchTypeRadio[0].checked = true;
    updateLayer1('flighthotel', 'dropDown');
  }

}

/* This function is called from the radio butttons in order to change the bold appearance */
function updateLayer1(type, setting) {
  changeForm(type, setting);
  displayRadioText(type);
}

// this function should replace the update function above to change the fields in one form
function changeForm(searchType, setting) {

  /* load all the dropdown lists */
  // get all the div elements from the homepge
  var allDivs = document.getElementsByTagName('div');
  var theForm = document.forms[searchForm];
  switch (searchType) {
    // a combined search has been selected
    case "flighthotel":
      document.getElementById("dep-from-C").style.display = "block";
      document.getElementById("dest-C").style.display = "block";
      document.getElementById("dest-H-C").style.display = "none";
      document.getElementById("dep-date-C").style.display = "block";
      document.getElementById("dep-from-C").style.display = "block";
      document.getElementById("ret-date-C").style.display = "block";
      document.getElementById("flx-b").style.display = "block";
      document.getElementById("rm-type01-C").style.display = "block";
      document.getElementById("rm-type02-C").style.display = "block";
      document.getElementById("rmNum01-C").style.display = "block";
      document.getElementById("rmNum02-C").style.display = "block";
      document.getElementById("adult-C").style.display = "block";
      document.getElementById("child-C").style.display = "block";
      document.getElementById("inf-C").style.display = "block";
      document.getElementById("class-C").style.display = "block";
      document.getElementById("star-C").style.display = "block";
      document.getElementById("ad-srch_moreFlightHotelOptions").style.display = "inline";
      document.getElementById("ad-srch_moreHotelOptions").style.display = "none";
      document.getElementById("ad-srch_moreFlightOptions").style.display = "none";
    //  document.getElementById("QS_LblOutChkIn").innerHTML="Outbound/Check-in";
    //  document.getElementById("QS_LblInChkOut").innerHTML="Inbound/Check-out";
    //  document.getElementById("QS_FOadults").style.display="none";
    //  document.getElementById("QS_flightHotelRooms").style.display="block";
    //  document.getElementById("QS_FHdeptDropDown").style.display="block";
    //  document.getElementById("QS_FHdestDropDown").style.display="block";
    //  document.getElementById("QS_HOdestDropDown").style.display="none";
    //	document.getElementById("QS_hotelStarRating").style.display="none";
    //	document.forms[searchForm].elements['hotelLocationCode.userValue'].style.display="none";
    // we also need to display and hide the following form fields
    //theForm.elements['departureLocation.userValue'].style.display="block";
    //   theForm.elements['destinationLocationCode.userValue'].style.display="block";
    // set the type of search being performed
      document.getElementById("dep-date-legend").innerHTML = "Departure date:";
      document.getElementById("dep-date-span").innerHTML = "Departure date:";
      document.getElementById("ret-date-legend").innerHTML = "Return date:";
      document.getElementById("ret-date-span").innerHTML = "Return date:";
      theForm.elements['searchType'].value = 'searchFlightsAndHotels';
    //  updateNumAdultsCombinedSearch(theForm);
      break;
    case "hotel":
      document.getElementById("dep-from-C").style.display = "none";
      document.getElementById("dest-C").style.display = "none";
      document.getElementById("dest-H-C").style.display = "block";
      document.getElementById("dep-date-C").style.display = "block";
      document.getElementById("ret-date-C").style.display = "block";
      document.getElementById("rm-type01-C").style.display = "block";
      document.getElementById("rm-type02-C").style.display = "block";
      document.getElementById("rmNum01-C").style.display = "block";
      document.getElementById("rmNum02-C").style.display = "block";
      document.getElementById("adult-C").style.display = "none";
      document.getElementById("child-C").style.display = "none";
      document.getElementById("inf-C").style.display = "none";
      document.getElementById("star-C").style.display = "block";
      document.getElementById("ad-srch_moreFlightHotelOptions").style.display = "none";
      document.getElementById("ad-srch_moreHotelOptions").style.display = "inline";
      document.getElementById("ad-srch_moreFlightOptions").style.display = "none";
      document.getElementById("dep-date-legend").innerHTML = "Check-in date:";
      document.getElementById("dep-date-span").innerHTML = "Check-in date:";
      document.getElementById("ret-date-legend").innerHTML = "Check-out date:";
      document.getElementById("ret-date-span").innerHTML = "Check-out date:";
      document.getElementById("class-C").style.display = "none";
      document.getElementById("flx-b").style.display = "none";


    //      document.forms[searchForm].elements['hotelLocationCode.userValue'].style.display="block";
    // set the type of search being performed
      document.forms[searchForm].elements['searchType'].value = 'searchHotels';
      break;
    // a FLIGHT ONLY search has been selected
    case "flight":
      document.getElementById("dep-from-C").style.display = "block";
      document.getElementById("dest-C").style.display = "block";
      document.getElementById("dest-H-C").style.display = "none";
      document.getElementById("dep-date-C").style.display = "block";
      document.getElementById("ret-date-C").style.display = "block";
      document.getElementById("flx-b").style.display = "block";
      document.getElementById("rm-type01-C").style.display = "none";
      document.getElementById("rm-type02-C").style.display = "none";
      document.getElementById("rmNum01-C").style.display = "none";
      document.getElementById("rmNum02-C").style.display = "none";
      document.getElementById("adult-C").style.display = "block";
      document.getElementById("child-C").style.display = "block";
      document.getElementById("inf-C").style.display = "block";
      document.getElementById("class-C").style.display = "block";
      document.getElementById("star-C").style.display = "none";
      document.getElementById("ad-srch_moreFlightOptions").style.display = "inline";
      document.getElementById("ad-srch_moreFlightHotelOptions").style.display = "none";
      document.getElementById("ad-srch_moreHotelOptions").style.display = "none";
      document.getElementById("dep-date-legend").innerHTML = "Departure date:";
      document.getElementById("dep-date-span").innerHTML = "Departure date:";
      document.getElementById("ret-date-legend").innerHTML = "Return date:";
      document.getElementById("ret-date-span").innerHTML = "Return date:";
    //    document.getElementById("QS_FHdeptDropDown").style.display="block";
    //     document.getElementById("QS_FHdestDropDown").style.display="block";
    //     document.getElementById("QS_flightHotelRooms").style.display="none";
    //	  	document.getElementById("QS_hotelStarRating").style.display="none";
    //	  	document.getElementById("QS_HOdestDropDown").style.display="none";
    // we also need to display and hide the following form fields
    // document.forms[searchForm].elements['departureLocation.userValue'].style.display="block";
    //      document.forms[searchForm].elements['destinationLocationCode.userValue'].style.display="block";
    //      document.forms[searchForm].elements['hotelLocationCode.userValue'].style.display="none";
    //      document.getElementById("QS_FOadults").style.display="block";
    //     document.getElementById("QS_flightHotelRooms").style.display="none";
    // set the type of search being performed
      document.forms[searchForm].elements['searchType'].value = 'searchFlights';

    // synchHotelRooms();
      break;

  }
  return;
}

function toggleStyle() {
  var text = document.getElementById("linkTxtHolidays");
  if (text.className == "txtBoldBlu") {
    <!-- TR 2495, HK: made the changes to display the arrow image when the button isn't selected -->
    document.getElementById("tabHolidays").src = "/images/rollovers/arrow_sm_gry.gif";
    text.className = "navLink";
  }
  else {
    document.getElementById("tabHolidays").src = "/images/common/px.gif";
    text.className = "txtBoldBlu";
  }
}


function goto_advanced_search(url) {
  var f = document.forms["searchFlightsHotelsForm1"];
  var el = f.elements[0];
  f.removeChild(el);
  f.action = url;
  var a = document.createElement('input');
  a.type = 'hidden';
  a.name = 'action';
  a.value = 'search';
  f.appendChild(a);
  f.submit();
}


function changeDropDown() {

  type = document.forms['searchType'].radioButtonValue.value;
  document.forms[searchForm].elements['destinationLocation.userValue'].value = "";
  changeForm(type, 'Text');
}
//
//  function changeDefault() {
//
//      type = document.forms['searchType'].radioButtonValue.value;
//      document.getElementById("QS_FHdestDropDown").style.display="block";
//      document.forms[searchForm].elements['destinationLocationCode.userValue'].value = "";
//      changeForm(type,'dropDown');
//
//  }
//
//  function changeHDropDown() {
//
//          type = document.forms['searchType'].radioButtonValue.value;
//          document.getElementById("QS_FHdestDropDown").style.display="none";
//          document.forms[searchForm].elements['hotelLocationCode.userValue'].value = "";
//          changeForm(type,'Text');
//
//  }
//
//  function changeHDefault() {
//          type = document.forms['searchType'].radioButtonValue.value;
//          document.getElementById("QS_HOdestText").style.display="none";
//          document.getElementById("QS_HOdestTextcalImage").style.display="none";
//          document.getElementById("QS_HOdestTextDefaultChange").style.display="none";
//          document.forms[searchForm].elements['hotelLocation.userValue'].value = "";
//          changeForm(type,'dropDown');
//  }
