function originValidation(origin) {
  var compDate = getSelectedDate();
  for (var i = 0; i < currentDateSet.list.length; i++) {
    if ((currentDateSet.list[i].origin == origin.code) &&
        (currentDateSet.list[i].dest != '000') &&
        (currentDateSet.list[i].fromDate <= compDate) &&  
        (currentDateSet.list[i].toDate >= compDate)) {
      return true;          
    }
  }
  return false;
}

function destinationValidation(country) {
  var originCode = $F('originAirport');
  var compDate = getSelectedDate();
  for (var i = 0; i < currentDateSet.list.length; i++) {
    if ((currentDateSet.list[i].origin == originCode) && 
        (currentDateSet.list[i].dest == country.code) && 
        (currentDateSet.list[i].fromDate <= compDate) &&  
        (currentDateSet.list[i].toDate >= compDate))
        return true;          
  }
  return false;
}

function init() {
  // Purge 'old' records from packageDatesArray
  var absMinDate = new Date();
  var i = 0; 
  while (i < packageDatesArray.list.length) {
    if (packageDatesArray.list[i].toDate < absMinDate) packageDatesArray.list.splice(i, 1); 
    else i++;
  }
  $('dateRangeStart').value = formatFSSDate(absMinDate);
  
  // Absolute latest date to book is the later of 'dateRangeEnd' and the last date set up via packageDatesArray
  var totalMaxDate = new Date();
  for (var i = 0; i < origins.list.length; i++) {
    if (maxDate(origins.list[i].code) > totalMaxDate) totalMaxDate = maxDate(origins.list[i].code);  
  }
  $('dateRangeEnd').value = formatFSSDate(totalMaxDate);   
  
  // Build 'dummy' packageDatesArray entries for the 'Please Select' option, based in the min/max dates for each origin.
  for (var i = 0; i < origins.list.length; i++) {
    packageDatesArray.add(new Object({origin:origins.list[i].code, dest:'000', fromDate:minDate(origins.list[i].code), toDate:maxDate(origins.list[i].code)}));
  }
  for (var i = 0; i < origins.list.length; i++) {
    skiDatesArray.add(new Object({origin:origins.list[i].code, dest:'000', fromDate:minDate(origins.list[i].code), toDate:maxDate(origins.list[i].code)}));
  }
    
  for (var i = 0; i < origins.list.length; i++) {
    returnDatesArray.add(new Object({origin:airports.list[i].code, dest:'000', fromDate:minDate(airports.list[i].code), toDate:maxDate(airports.list[i].code)}));
  }
    
  // Create 'Any' options for each country. 
  // Only create if the country has 0 or >1 resorts
  for (var i = 0; i < destinations.list.length; i++) {
    if (destinations.list[i].resorts.list.length == 0) {
      destinations.list[i].resorts.add(new Object({code:'', name:'Any'}));
    } else if (destinations.list[i].resorts.list.length != 1) {
      destinations.list[i].resorts.list.splice(0,0, new Object({code:'', name:'Any'}));
    }
  }

  holType();
  Event.observe('OutDay','change',changeDate);
}

function changeOrigin() {
  if (mode == 'SPC') {
    return;
  } else if (mode == 'CFO') {
    populateDDB_json($('destinationAirport2'), currentDestinations.list, destinationValidation);
    populateDDBAry($('destinationAirport'), new Array('',''));
  } else {
    populateDDB_json($('destinationAirport'), currentDestinations.list, destinationValidation);
    populateDDBAry($('destinationAirport2'), new Array('',''));
  }
  updateDestination();
}

function updateDestination() {
  if (mode == 'SPC') {
    return;
  } else if (mode != 'CFO') {
    populateDDB_json($('resort'), currentDestinations.find($F('destinationAirport')).resorts.list);
  } else {
    populateDDBAry($('resort'), new Array('',''));
  }
}

function changeDate() {
  var curMaxDate = maxDate();
  var curMinDate = minDate();
  
  if (getSelectedDate() > curMaxDate) {
    alert("Sorry, the last date for which you can check availability is " + longDate(curMaxDate));
    setDate(formatFSSDate(curMaxDate));
  } else if (getSelectedDate() < curMinDate) {
    alert("Sorry, the earliest date for which you can check availability is " + longDate(curMinDate));
    setDate(formatFSSDate(curMinDate));
  } else {  
    populateDDB_json($('originAirport'), currentOrigins.list, originValidation);
    $('departureDate').value = prepare_date($('OutDay'), $('OutMon'), $('OutYear'));
    changeOrigin();
  }
}

function maxDate(originCode) {
  var result = new Date();
  for (var i = 0; i < currentDateSet.list.length; i++) {
    if (((originCode == null) || (currentDateSet.list[i].origin == originCode)) && 
        (currentDateSet.list[i].toDate > result))
    result = currentDateSet.list[i].toDate; 
  }
  return result;
}

function minDate(originCode) {
  var result = new Date('2099','12','31');
  for (var i = 0; i < currentDateSet.list.length; i++) {
    if (((originCode == null) || (currentDateSet.list[i].origin == originCode)) && 
        (currentDateSet.list[i].fromDate < result))
    result = currentDateSet.list[i].fromDate; 
  }
  return result;
}


function holType(ht) {
  if (ht == null && $('holType_SUN') != null) {
    if ($('holType_SUN').checked) ht = 'SUN';
    else if ($('holType_SKI').checked) ht = 'SKI';
    else if ($('holType_CFO').checked) ht = 'CFO';
  }  
  if (ht == null) ht = 'SUN';  
  mode = ht;  
  if (ht != null && $('holType_SUN') != null) {
    $('holType_SUN').checked = (ht == 'SUN');
    $('holType_SKI').checked = (ht == 'SKI');
    $('holType_CFO').checked = (ht == 'CFO');
  }  
    
  if (ht == 'SUN') {
    currentDateSet = packageDatesArray;
    currentOrigins = origins;
    currentDestinations = destinations;  
    $('icon_SUN').src = 'http://www.sunwayholidays.ie/test/images/holiday_icon.gif';
    $('icon_SKI').src = 'http://www.sunwayholidays.ie/test/images/ski_icon_off.gif'; 
    $('icon_CFO').src = 'http://www.sunwayholidays.ie/test/images/flight_icon_off.gif';
    $$('li.active').each(function (o) {o.removeClassName('active');});
    $('item_SUN').addClassName('active');
    $('module').value = 'pkg';
    $('step').value = 'logon_pkg_search';
    
    $('resort', 'destinationAirport', 'lbl_resort', 'lbl_accomType','accomType').invoke('show');
    $('journeyType','destinationAirport2').invoke('hide');
    
    populateDDB_json($('duration'), durationOpts.list);
  } else if (ht == 'SKI') {    
    currentDateSet = skiDatesArray;
    currentOrigins = origins;
    currentDestinations = destinations;   
    $('icon_SUN').src = 'http://www.sunwayholidays.ie/test/images/holiday_icon_off.gif';
    $('icon_SKI').src = 'http://www.sunwayholidays.ie/test/images/ski_icon.gif'; 
    $('icon_CFO').src = 'http://www.sunwayholidays.ie/test/images/flight_icon_off.gif';
    $$('li.active').each(function (o) {o.removeClassName('active');});
    $('item_SKI').addClassName('active');
    $('module').value = 'pkg';
    $('step').value = 'logon_pkg_search';
    populateDDB_json($('duration'), durationOpts.list);
    $('resort', 'destinationAirport', 'lbl_resort', 'lbl_accomType','accomType').invoke('show');
    $('journeyType','destinationAirport2').invoke('hide');
  } else if (ht == 'CFO') {
    currentDateSet = returnDatesArray;
    currentOrigins = airports;
    currentDestinations = airports; 
    $('icon_SUN').src = 'http://www.sunwayholidays.ie/test/images/holiday_icon_off.gif';
    $('icon_SKI').src = 'http://www.sunwayholidays.ie/test/images/ski_icon_off.gif'; 
    $('icon_CFO').src = 'http://www.sunwayholidays.ie/test/images/flight_icon.gif';
    $$('li.active').each(function (o) {o.removeClassName('active');});
    $('item_CFO').addClassName('active');
    $('module').value = 'cfo';
    $('step').value = 'logon_cfo_search';
    $('resort', 'destinationAirport', 'lbl_resort', 'lbl_accomType','accomType').invoke('hide');
    $('journeyType','destinationAirport2').invoke('show');
    oneWay();
  }  
   if (ht == 'SPC') {
    $('specialsForm').show();
    $('searchForm').hide();
    $('fssSearchButton').hide();
    $('icon_SUN').src = 'http://www.sunwayholidays.ie/test/images/holiday_icon_off.gif';
    $('icon_SKI').src = 'http://www.sunwayholidays.ie/test/images/ski_icon_off.gif'; 
    $('icon_CFO').src = 'http://www.sunwayholidays.ie/test/images/flight_icon_off.gif';
    $('icon_SPC').src = 'http://www.sunwayholidays.ie/test/images/specials_icon.gif';
    $$('li.active').each(function (o) {o.removeClassName('active');});
    $('item_SPC').addClassName('active');
   } else {   
    $('icon_SPC').src = 'http://www.sunwayholidays.ie/test/images/specials_icon_off.gif';
    $('specialsForm').hide();
    $('searchForm').show();
    $('fssSearchButton').show();
   } 
  
  changeDate();
}

function oneWay() {
  if ($('oneway_Y').checked) {
    currentDateSet = onewayDatesArray;
    populateDDBAry($('duration'), new Array ("('One-way','  ')"));
  } else {
    currentDateSet = returnDatesArray;
    populateDDB_json($('duration'), durationOpts.list);
  }  
}

function formSubmit() {
  changeDate();
  $('searchForm').submit();
}

var durationOpts = new HashMap();
durationOpts.add(new Object({code: '07', name: 'One week'}));
durationOpts.add(new Object({code: '14', name: 'Two weeks'}));

var currentDateSet = packageDatesArray;
var currentOrigins = origins;
var currentDestinations = destinations;
var mode;
