
/**
 * These functions are used by the searchform.php file
 */

var theForm;
var fDepSearch;
var fDestSearch;
var fDepDate;
var fRetDate;
var _destURL = "http://www.netflights.com/flights/farelist.asp";

function formInit() {
  theForm = document.forms['sform'];

  fDepSearch = new dcxLiveSearch.LiveSearch('depcode', {varname:'q', script:'iatasearch.php', field_id:'depcode', 'minlen' : 3, onselect_callback:'ls_departure_callback'});
  fArrSearch = new dcxLiveSearch.LiveSearch('arrcode', {varname:'q', script:'iatasearch.php', field_id:'arrcode', 'minlen' : 3, onselect_callback:'ls_destination_callback'});

  // set the initial journey and route types
  var o = theForm.getElementsByTagName("INPUT");
  for (var z=0; z<o.length; z++) {
    var obj = o[z];
    if (obj.type == 'radio') {
      if (obj.name == 'journeytype' && obj.checked) { SetJourneyType(obj); }
      if (obj.name == 'routetype' && obj.checked) { SetRouteType(obj); }
    }
  }
}

function formValidate() {
  var depArr, depDate, arrArr, arrDate;

  // check a departure airport has been provided
  if (!theForm.f_departcode.value) {
    alert("You have not selected where you are flying from");
    return false;
  }

  // check a destination airport has been provided
  if (!theForm.f_destcode.value) {
    alert("You have not selected where you want to fly to");
    return false;
  }

  // check a departure date has been provided
  if (!theForm.depdate.value || !theForm.depdate.value.match(/\d{2}-\d{2}-\d{4}/i)) {
    alert("You have not entered a departure date - please enter one in the form DD-MM-YYYY");
    return false;
  }

  // set up the depDate fields as needed
  depArr = theForm.depdate.value.split(/-/, 3);
  depDate = depArr[2] + depArr[1] + depArr[0];
  theForm.f_depdate.value = depArr[0] + "-" + depArr[1] + depArr[2];

  // check a return date has been provided IF travel type is RETURN
  if (theForm.f_journeytype.value == 'R') {
    if (!theForm.arrdate.value || !theForm.arrdate.value.match(/\d{2}-\d{2}-\d{4}/i)) {
      alert("You have not entered a return date but have requested to look for return tickets, please enter a return date in the form DD-MM-YYYY");
      return false;
    }

    arrArr = theForm.arrdate.value.split(/-/, 3);
    arrDate = arrArr[2] + arrArr[1] + arrArr[0];
    theForm.f_retdate.value = arrArr[0] + "-" + arrArr[1] + arrArr[2];
    if (arrDate < depDate) {
      alert("You have selected a date to return which is earlier than the date you want to depart");
      return false;
    }
  }

  // check that number of adults, children and seniors does not exceed 6
  var totTravellers = parseInt(theForm.numadults.options[theForm.numadults.selectedIndex].value) +
    parseInt(theForm.numchildren.options[theForm.numchildren.selectedIndex].value) +
    parseInt(theForm.numseniors.options[theForm.numseniors.selectedIndex].value);

  if (totTravellers > 6) {
    alert("Sorry, we cannot search for a group of more than 6 at this time");
    return false;
  }

  if (totTravellers == 0) {
    alert("You do not seem to have selected a number of adults, children or seniors who are travlling");
    return false;
  }

  // we get to here everything is ok.
  var QS = BuildParameters();
  var URL = _destURL + QS;
  /*
  alert("depart from '" + theForm.f_departcode.value +"', land at '"+ theForm.f_destcode.value +"', journey type='"+theForm.f_journeytype.value+"', route type='"+theForm.f_routetype.value+
    "', number of travellers = "+ totTravellers);
  alert(URL);
  */
  var w = window.open(URL, theForm.target, 'top=50,left=50,width=500,height=500');
  return false;
}

function SetJourneyType(obj) {
  theForm.f_journeytype.value = obj.value;
}

function SetRouteType(obj) {
  theForm.f_routetype.value = obj.value;
}

function ls_departure_callback(obj) {
  obj.debugPrint('key = ' + obj.key + ', val = ' +obj.val);
  theForm.f_departcode.value = obj.val;
  return obj.key;
}

function ls_destination_callback(obj) {
  obj.debugPrint('key = ' + obj.key + ', val = ' +obj.val);
  theForm.f_destcode.value = obj.val;
  return obj.key;
}

/**
 * http://www.netflights.com/flights/farelist.asp
 *   ?inoadults=2                             : number of adults 0=none
 *   &inoseniors=0                            : number of seniors 0=none
 *   &inochildren=0                           : number of children 0=none
                ** note: total number of adults, children and seniors must not be more than 6 **
 *   &ldepartureday=12                        : day in month of departure [dd]
 *   &ldeparturemonthyear=082009              : departure month and year [mmyyyy]
 *   &lreturnday=19                           : day in month of return [dd]
 *   &lreturnmonthyear=082009                 : return month and year [mmyyyy]
 *   &sjourneytype=R                          : Type of journey [R=Return, O=One-way]
 *   &sdepartureairportcode=LHR               : IATA departure airport code
 *   &sdestinationcode=MCO                    : IATA destination airport code
 *   &selectedTravelClass=1                   : Travel Class [0=Any/all, 1=Economy, 2=Business, 3=First, 4=Enhanced Economy]
 *   &FlightFilter=A                          : [A=All Flights, B=Non-stop/direct, S=Stop-over en-route]
 *   &pricesFromRoute=True                    : Determines whether prices are displayed from route? - needs further documentation
 */

function BuildParameters() {
  var QS = "";
  QS += "?inoadults=" + theForm.numadults.options[theForm.numadults.selectedIndex].value;
  QS += "&inoseniors=" + theForm.numseniors.options[theForm.numseniors.selectedIndex].value;
  QS += "&inochildren=" + theForm.numchildren.options[theForm.numchildren.selectedIndex].value;
  var dDate = theForm.f_depdate.value.split(/-/, 2);
  QS += "&ldepartureday=" + dDate[0];
  QS += "&ldeparturemonthyear=" + dDate[1];
  if (theForm.f_journeytype.value == 'R') {
    var rDate = theForm.f_retdate.value.split(/-/, 2);
    QS += "&lreturnday="+rDate[0];
    QS += "&lreturnmonthyear="+rDate[1];
  }
  QS += "&sjourneytype="+ theForm.f_journeytype.value;
  QS += "&sdepartureairportcode=" + theForm.f_departcode.value;
  QS += "&sdestinationcode=" + theForm.f_destcode.value;
  QS += "&selectedTravelClass=" + theForm.travelclass.options[theForm.travelclass.selectedIndex].value;
  QS += "&FlightFilter=" + theForm.f_routetype.value;
  QS += "&pricesFromRoute=True";
  return QS;
}