/**
 * On page loaded
 */
window.onload = function() {
  var count;
  var select = window.document.skylightsForm.from1;
  var string = "";
  
  count = 1;
  select.options.length = count;
  for (i=0; i<origins.length; i++) {
    string = origins[i].split("|");
    select.options[count++] = new Option(string[1], string[0]);
  }
  
  // check for numberOfMarkets
  numberMarkets = window.document.skylightsForm.numberMarkets;
  
  if (numberMarkets.value == '1') {
    $("#return_date").hide();
  }
  else {
    $('#return_date').show();
  }
}

/**
 * Show selected Airport
 */
function show_arrivals(airport_code) {
  var count;
  var select = window.document.skylightsForm.to1;
  var string = "";
  
  count = 1;
  select.options.length = count;
  for (i=0; i<arrivals.length; i++) {
    string = arrivals[i].split("|");
    
    if (string[0] == airport_code) {
      select.options[count++] = new Option(string[2], string[1]);
    }
  }
}

/**
 * Check number of markets
 */
function numberOfMarkets(trip) {
  numberMarkets = window.document.skylightsForm.numberMarkets;
  numberMarkets.value = trip;
  
  if (trip == '1') {
    $("#return_date").hide();
  }
  else {
    $("#return_date").show();
  }
}

/**
 * Events triggers
 */
function update_return_date() { // update return date based on depart date
  var skyForm = window.document.skylightsForm;
  var numberMarkets = skyForm.numberMarkets;
  
  if (numberMarkets == 2) {
    skyForm.departDay2.value = skyForm.departDay1.value;
    skyForm.departMonth2.value = skyForm.departMonth1.value;
  }
}

/**
 * Alerts
 */
function alert_infant() { // if travel with infant
  var skyForm = window.document.skylightsForm;
  var appName = navigator.appName;
  var appVersion = parseFloat(navigator.appVersion);
  
  if (appName == "Microsoft Internet Explorer" && appVersion == "4") {
    alert("Infant(s) must be within the age of 9 days - 24 months at date(s) of travel");
  }
  else {
    $("#dialogInfant").dialog('open');
  }
}

function validate_form() { // validate user inputs before submission
  var skyForm = window.document.skylightsForm;
  var from1 = skyForm.from1;
  var to1 = skyForm.to1;
  var today = new Date();
  var depart1 = skyForm.departMonth1.value + skyForm.departDay1.value;
  var depart2 = skyForm.departMonth2.value + skyForm.departDay2.value;
  var ADULT = skyForm.ADULT;
  var INFANT = skyForm.INFANT;
  var numberMarkets = skyForm.numberMarkets;
  var appName = navigator.appName;
  var appVersion = parseFloat(navigator.appVersion);
  var promoCode = skyForm.promoCode;
  
  if (INFANT.value > ADULT.value) {
    if (appName == "Microsoft Internet Explorer" && appVersion == "4") {
      alert('If you wish to book a greater number of\nInfants than Adults, please contact our Call Centre at +603 - 7845 4543 for possible arrangements.');
    }
    else {
      $("#dialogGuests").dialog('open');      
    }

    return false;
  }
  
  if (from1.value == "XXX") {
    if (appName == "Microsoft Internet Explorer" && appVersion == "4") {
      alert("Please select the city you will be departing from.");
    }
    else {
      $("#dialogFrom1").dialog('open');
    }
    
    return false;
  }
  
  if (to1.value == "XXX") {
    if (appName == "Microsoft Internet Explorer" && appVersion == "4") {
      alert("Please choose your destination.");
    }
    else {
      $("#dialogTo1").dialog('open');
    }
    
    return false;
  }
  
  if (numberMarkets.value == 2) {
    if (depart1 == depart2) {
      if (appName == "Microsoft Internet Explorer" && appVersion == "4") {
        var ans = confirm("Your return date is the same as your departure date.\n\nIs such a short trip intentional?");

        if (ans) {
          return true;
        }
        else {
          return false;
        }
      }
      else {
        $("#dialogTrip").dialog('open');

        return false;
      }
    }
  }
    
  return true;
}
