// Dynamic Location
//
var SelAirport = '';
var SelCountry = '';
var SelDest = '';
var SelResort = '';
var SelAccomm = '';
PreviousDateInvalid = false;

// Update dropdowns with selected value if date/departure airport changed
function ShowSelection(elementid,noupdate) {
switch(elementid) {
  case 'SearchAirport':
 // Airport
 if (SelAirport != 'Any') {
   objSel = getElem('SearchAirport')
    for (z=0;z<objSel.length;z++) {
    if (objSel.options[z].value == SelAirport) { objSel.selectedIndex = z; }
   }
  if (!noupdate) { DynamicUpdate(elementid,'change') }
 }
 break;

  case 'SearchLocation':
 // Location
 if ((SelCountry != 'Any') || (SelDest != 'Any')) {
   objSel = getElem('SearchLocation')
    for (z=0;z<objSel.length;z++) {
    arrThisLocation = objSel.options[z].value.split(',')
   // Check a country code is specified
   if (arrThisLocation.length < 2) {
    arrThisLocation[1] = arrThisLocation[0]
    }
   // Destination
   if (arrThisLocation[1] != null) {
      if (arrThisLocation[1] == SelDest) {
      objSel.selectedIndex = z;
      }
    } else {
    // Country
   if (arrThisLocation[0] == SelCountry) { objSel.selectedIndex = z; }
    }
   }
  if (!noupdate) { DynamicUpdate(elementid,'change') }
 }
 break;
 // Resort
  case 'SearchResort':
 if (SelResort != 'Any') {
   objSel = getElem('SearchResort')
    for (z=0;z<objSel.length;z++) {
    if (objSel.options[z].value == SelResort) { objSel.selectedIndex = z; }
   }
  if (!noupdate) { DynamicUpdate(elementid,'change') }
 }
 break;
  case 'SearchAccommodation':
 // Resort
 if (SelAccomm != 'Any') {
   objSel = getElem('SearchAccommodation')
  for (z=0;z<objSel.length;z++) {
    if (objSel.options[z].value == SelAccomm) { objSel.selectedIndex = z; }
   }
 }
 break;
 }
}

// Save current selection
function SaveSelection() {
 SelAirport = getElem('SearchAirport').value
  arrSelLocation = getElem('SearchLocation').value.split(',')
  // Check a country code is specified
 if (arrSelLocation.length < 2) {
  SelCountry = '';
    SelDest = arrSelLocation[0]
 } else {
    SelCountry = arrSelLocation[0]
    SelDest = (!arrSelLocation[1]) ? '' : arrSelLocation[1]
 }
  SelResort = getElem('SearchResort').value
  SelAccomm = getElem('SearchAccommodation').value
}

function DynamicUpdate(ElementID,ElementEvent) {
   if (ElementID) { ElementEvent = ElementID + '_' + ElementEvent; }

   switch(ElementEvent) {
   case 'calendar_day_keyup':
   case 'calendar_day_change':
   case 'calendar_month_year_keyup':
   case 'calendar_month_year_change':
   case 'search_panel_init':

   if (!PageLoad) {
   if (CheckValidDepartureDate()) {
   if (!PreviousDateInvalid) { SaveSelection() }
   PreviousDateInvalid = false;
       Params = 'Date='+getElem("calendar_day").value + "/" + getElem("calendar_month_year").value
       LoadDropDown(Params,'ShowSelection("SearchAirport");','SearchAirport')
   } else {
       // display the message in Airport drop down and disable other drop downs
      if (DateValue(SearchSelectedDate) < DateValue(dteMinDeparture))
         { ElementAction('SearchAirport','showdatemessage'); }
      else
         { ElementAction('SearchAirport','shownoavailabilitymessage'); }
      ElementAction('SearchLocation','showany')
      ElementAction('SearchResort','showany')
      ElementAction('SearchAccommodation','showany')
      PreviousDateInvalid = true;
      }
   } else {
       //DynamicUpdate('SearchBrochure','change')
    }
   break;

   case 'SearchAirport_keyup':
   case 'SearchAirport_change':
   case 'SearchBrochure_keyup':
   case 'SearchBrochure_change':
   if (!PageLoad) {
      if (CheckValidDepartureDate()) {
         if (!PreviousDateInvalid) { SaveSelection() }
         PreviousDateInvalid = false;
             Params = 'Airport='+getElem("SearchAirport").value
             LoadDropDown(Params,'ShowSelection("SearchLocation")','SearchLocation')
         } else {
        // display the message in Airport drop down and disable other drop downs
         if (DateValue(SearchSelectedDate) < DateValue(dteMinDeparture))
            { ElementAction('SearchAirport','showdatemessage')   }
         else 
            { ElementAction('SearchAirport','shownoavailabilitymessage')   }
         ElementAction('SearchLocation','showdatemessage')
         ElementAction('SearchResort','showany')
         ElementAction('SearchAccommodation','showany')
         PreviousDateInvalid = true;
      }
   } else {
      DynamicUpdate('SearchLocation','change')
   }
     break;

     // Location - Selection changed / Key Up
     case 'SearchLocation_keyup':
     case 'SearchLocation_change':
     if (isany('SearchLocation'))
     {
        ElementAction('SearchResort','showany')
        getElem('SearchCountry').value = 'Any';
        getElem('SearchDestination').value = 'Any';
     }
     else
     {
       if (!PreviousDateInvalid) { SaveSelection() }
       var codes = getElem("SearchLocation").value.split(',')
       if (codes.length == 1)
          {
            getElem('SearchCountry').value = codes[0]
            getElem('SearchDestination').value = 'Any'
          }
          else
          {
              getElem('SearchCountry').value = codes[0]
              getElem('SearchDestination').value = codes[1]
          }

          Params = 'Country='+getElem("SearchCountry").value
           if (getElem("SearchDestination").value != '')
           {
              Params += "&Destination=" + getElem("SearchDestination").value
           }

         if (!PageLoad) { LoadDropDown(Params,'ShowSelection("SearchResort")','SearchResort') }
      }
      DynamicUpdate('SearchResort','change')
      break;

     // Resort - Selection changed / Key Up
     case 'SearchResort_keyup':
     case 'SearchResort_change':
     if (isany('SearchResort')) {
        ElementAction('SearchAccommodation','showany')
      } else {
       Params = 'Resort='+getElem("SearchResort").value
      if (!PageLoad) { LoadDropDown(Params,'ShowSelection("SearchAccommodation")','SearchAccommodation') }
      }
      DynamicUpdate('SearchAccommodation','change')
     break;

   case 'SearchAccommodation_keyup':
   case 'SearchAccommodation_change':
      if (!isany('SearchAccommodation')) {
            if (!PreviousDateInvalid) { SaveSelection() }
         }
         break;
      }
   if ((ElementID == 'calendar_day') || (ElementID == 'calendar_month_year')) {
   if (CheckValidDepartureDate()) { UpdateDayOfWeek() }
   }
}

function DynamicUpdateInit() {
   for(i=0;i<periodArray.length;i++){
     if(DateValue(dteMinDeparture) < DateValue(periodArray[i])){
        PageLoad = false;
        DynamicUpdate('search_panel','init');
     }else {
        break;
     }
   }
   PageLoad = true;
   DynamicUpdate('SearchLocation','change');
   UpdateDayOfWeek();
   PageLoad = false;
}

function UpdateDayOfWeek() {
   var days_of_week = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
   FieldObjDay1 = getElem('calendar_day')
   FieldObjMonthYear1 = getElem('calendar_month_year')
   var arrDateSplit = FieldObjMonthYear1.value.split('/')
   dateVar = arrDateSplit[0] + "/" + FieldObjDay1.options[FieldObjDay1.selectedIndex].value + "/" + arrDateSplit[1];
   var Calendar = new Date(dateVar);
   Calendar.setMonth(arrDateSplit[0]-1);
   Calendar.setYear(arrDateSplit[1]);
   Calendar.setDate(FieldObjDay1.options[FieldObjDay1.selectedIndex].value);
   if (FieldObjDay1.options[FieldObjDay1.selectedIndex].value==Calendar.getDate())
      { getElem('calendar_dow').innerHTML = days_of_week[Calendar.getDay()]; }
   else
      { FieldObjDay1.selectedIndex = FieldObjDay1.selectedIndex - 1; }
}