﻿/* script for flight status display */
function DisableAllSearchFields(){
    $("FlightNumber").disabled = true;
    $("FlightNumber").value = "";
    $("FlightDirection").disabled = true;
    $("Airport").disabled = true;
    $("DepartureAirport").disabled = true;
    $("ArrivalAirport").disabled = true;
}

function FillAirportList(ListObject, selectedHidden){
    if(typeof(Stations)!="undefined"&&typeof(SortedStations)!="undefined"){
        for(var s=0; s<SortedStations.length; s++){
            if(IsOwnStation(SortedStations[s])){
                var AirportOption = new Option(Stations[SortedStations[s]].name, Stations[SortedStations[s]].code);
                ListObject.options[ListObject.options.length] = AirportOption;
                if (selectedHidden != null)
                    if (selectedHidden.value == Stations[SortedStations[s]].code) {
                         AirportOption.setAttribute("selected", "true");
                    }
            }
        }
    }
}

function FillDestinationAirportList(ListObject, AirportValue, selectedHidden){
    ListObject.options.length = 0;

    var dIx = 0;

    for (var i=0; i < SortedStations.length; i++)
	{
		for (j=0; j<Stations[AirportValue].mkts.length; j++)
		{
			var stnCode	= Stations[AirportValue].mkts[j];
			if ( (SortedStations[i] == stnCode) && (Stations[stnCode].validDest == true))
			{
				if ( stnCode == AirportValue ) { dIx = d.length; }

				ListObject.length += 1;

				ListObject.options[ListObject.length-1] = new Option( Stations[stnCode].name );
				ListObject.options[ListObject.length-1].value = Stations[stnCode].code;
				if (selectedHidden != null)
                    if (selectedHidden.value == Stations[stnCode].code) {
                        ListObject.options[ListObject.length-1].setAttribute("selected", "true");
                    }
				break;
			}
		}

		if (ListObject.length-1 == Stations[AirportValue].mkts.length) { break; }
	} 
 
}

function IsOwnStation(StationCode){
    var Result = false;
    for(var d=0; d<Stations[StationCode].mkts.length; d++){
        var origin = StationCode;
        var destination = Stations[StationCode].mkts[d];
        if(!IsOtherAirlineRoute(origin,destination)){
            Result = true;
        }
    }
    return Result;
}

function SubmitSearch(){
    $("SkySales").submit();
}

function ActivateSearchField(FieldName){
    DisableAllSearchFields();
    switch(FieldName){
    
        case "FlightNumber":
            $("FlightNumber").disabled = false;
            $("FlightNumber").focus();
            $("SearchModeFlightNumber").checked = true;
            break;
        case "Airport":
            $("FlightDirection").disabled = false;
            $("Airport").disabled = false;
            $("SearchModeAirport").checked = true;
            break;
        case "Route":
            $("DepartureAirport").disabled = false;
            $("ArrivalAirport").disabled = false;
            $("SearchModeRoute").checked = true;
            break;
        default:
            $("FlightDirection").disabled = false;
            $("Airport").disabled = false;
            $("SearchModeAirport").checked = true;
    }
}

function ReadHiddenfields() {
  DisableAllSearchFields();
  ActivateSearchField($("GW_RadioSelection").value);
  
  
  var flightNumberValue = $("GW_FlightNumberSelection").value;
  if (flightNumberValue != null && flightNumberValue.length > 0) {
    $("FlightNumber").value = flightNumberValue;
  }
  
  // Set the value for the FlightDirection
  var selectDirection = $("FlightDirection");
  var valueDirection = $("GW_FlightDirectionSelection").value;
  if (selectDirection != null) {
      if (valueDirection != null && valueDirection.length > 0) {
        for (var i = 0; i < selectDirection.options.length; i++) {
            if (selectDirection.options[i].value == valueDirection) {
                selectDirection.selectedIndex = i;
                selectDirection.options[i].setAttribute("selected", "true");
            }
        }
      }
  }
    // Set the value for the Date
  var selectDate = $("Date");
  var valueDate = $("GW_DateSelection").value;
  if (selectDate != null) {
      if (valueDate != null && valueDate.length > 0) {
          for (var i = 0; i < selectDate.options.length; i++) {
              if (selectDate.options[i].value == valueDate) {
                selectDate.selectedIndex = i;
                selectDate.options[i].setAttribute("selected", "true");
              } 
          }
      }
  }
  

}
