// popupWindow function
function popupWindow(url) {
  window.open(url, 'popupwindow', 'width=480,height=520,resizable=yes,scrollbars=yes,status=yes');
  return false;
}

function checkDest(form) {
  var is_wintersun = false;
  if (getProduct().code == 'WSUN') is_wintersun = true;
  var currentDate = createDate(prepare_date($('date_Day'), $('date_Month'), $('date_Year')));
  var now = new Date();
  if ($F('as_country') == "") {
    alert("Please Select A Destination");
    $('as_country').focus();
  } else if ($F('as_country') != "" && $F('as_destination') == "") {
    alert("Please Select A Resort");
    $('as_destination').focus();
  } else if ($F('as_nights') == '') {
    alert("Please select a duration");
    $('as_nights').focus();
  } else if ( now.getTime() >= currentDate.getTime() ) {
    alert("Please Select a Departure Date After Today");
    $('date_Day').focus();
  } else if (is_wintersun == true && $F('as_nights') == "14") {
    alert("Winter Sun holidays are only available for 7 nights duration");
    $('as_nights').focus();
  } else {
    $('as_depDate').value = formatFSSDate(currentDate);
    if ((getProduct() != 'ao') && ($F('as_airport') == 'BFS')) {
      $('companyNo').value = 2;
      $('currency').value = 'sterling';
    } else {
      $('companyNo').value = 1;
      $('currency').value = 'euro';
    }
    form.submit();
  }
}

function changeR(optDefault) {
  selIndex = 0;
  var form = document.holidaysearch;
  var catid = $F('as_country');
  var country = new Object({code: '', name: 'Any Destination', resorts: new Array(), airports: new Array()});

  for (var i = 0; i < geography.length; i++) {
    if (catid == geography[i].code) {country = geography[i]; break;}
  }

  var blankItem = new Array(new Object({code: ' ', name: 'Any'}));
  
  var product = getProduct();
  if (product.code == 'cfo') {
    populateDDB_json($('as_destination2'), blankItem.concat(country.airports));
    populateDDB_json($('as_destination'), blankItem.concat(country.resorts));
  } else if (product.code == 'ao') {    
    populateDDB_json($('as_destination2'), blankItem.concat(country.airports));
    populateDDB_json($('as_destination'), blankItem.concat(country.resorts));
  } else {
    populateDDB_json($('as_destination2'), blankItem.concat(country.airports));
    populateDDB_json($('as_destination'), blankItem.concat(country.resorts), function(resort) {return resort.code == ' ' || resortValid(resort)});
  }
}

function buildCountries() {
  var blankItem = new Array(new Object({code: '', name: 'Any Destination', resorts: new Array(), airports: new Array()}));
  var product = getProduct();
  if (product.code == 'cfo') {
    populateDDB_json($('as_country'), blankItem.concat(geography), function(country) { return (country.code == '') || (country.airports.length > 0)});
  } else if (product.code == 'ao') {    
    populateDDB_json($('as_country'), blankItem.concat(geography), function(country) { return (country.code == '') || (country.resorts.length > 0)});
  } else {
    populateDDB_json($('as_country'), blankItem.concat(geography), function(country) { return (country.code == '' || countryValid(country))});
  }
  $('as_country').onchange();
}

function getProduct() {  
  var productName = getDropDownText($('as_holType'));
  var product = products[0];
  for (var i = 0; i < products.length; i++) if (products[i].name == productName) product = products[i];
  return product;
}

function countryValid(country) {
  var product = getProduct();
  for (var i = 0 ; i < country.resorts.length; i++) {
    if (product.resorts.indexOf(country.resorts[i].code) != -1) 
      return true;  
  }  
  return false;
}

function resortValid(resort) {
  var product = getProduct();
  return (product.resorts.indexOf(resort.code) != -1);
}

function toggleDisplay(id) {
  var list = document.getElementById(id);
  if (list.style.visibility == 'hidden') {
    list.style.visibility = 'visible';
    list.style.display = 'block';
  } else if (list.style.visibility == 'visible') {
    list.style.visibility = 'hidden';
    list.style.display = 'none';
  } else {
    list.style.visibility = 'hidden';
    list.style.display = 'none';
  }
}
function holType() {
  var holType = $F('as_holType');
  if (holType == 'logon_pkg_search') {
    $('lbl_as_destination').show();
    $('as_destination').show();
    $('lbl_as_airport').show();
    $('as_airport').show();
    $('lbl_as_destination2').hide();
    $('as_destination2').hide();
    $('as_depDate').name = 'departureDate';
    $('as_country').name = 'destCC';
    $('module').value = 'pkg';
    $('itFlag').value = 'Y';
    buildCountries(false);
  } else if (holType == 'logon_CFO_search') {
    $('lbl_as_destination').hide();
    $('as_destination').hide();
    $('lbl_as_airport').show();
    $('as_airport').show();
    $('lbl_as_destination2').show();
    $('as_destination2').show();
    $('as_depDate').name = 'departureDate';
    $('as_country').name = 'destCC';
    $('module').value = 'cfo';
    $('itFlag').value = 'N';
    buildCountries(true);
  } else if (holType == 'logon_AO_search') {
    $('lbl_as_destination').show();
    $('as_destination').show();
    $('lbl_as_airport').hide();
    $('as_airport').hide();
    $('lbl_as_destination2').hide();
    $('as_destination2').hide();
    $('as_depDate').name = 'arrivalDate'; 
    $('as_country').name = 'country';
    $('module').value = 'ao';    
    $('itFlag').value = 'A';
    buildCountries(false);     
  }
  buildCountries();
}
