/* routes.js 
  for managing origin-destination dropdown
  requires routes.jsp to be imported first
*/

function populateOriginAirports() {
  var selectedOrigin = document.getElementById("selectedOriginAirport");
  var originAirports = document.getElementById("originAirports");
  var routeSorting = getRouteSorting();
  
  if (selectedOrigin.value !== '') {
      defaultOriginAirport = selectedOrigin.value;	
  }
  
  var origins = new Array();
  for (var origin in routes)
  {
     origins.push(new Array(origin, airports[origin]));
  }
  origins.sort(sortOptionsByText);
  
  for (i = routeSorting.length - 1; i >= 0 ; i--){
	    var route = routeSorting[i];
	    for (j=0; j < origins.length; j++){
	    	var originCode = origins[j][0];
	    	var originDesc = origins[j][1];
	    	
	    	if (originDesc == ''){
	    		origins.splice(j, 1);
	    	}
	    	else if (route == originCode){
	    		origins.splice(j, 1);
	    		origins.unshift(new Array(originCode, originDesc));
	    	}
	    }
  }
  
  origins.unshift(new Array('', selectOriginAirportLabel));
  
  for (i=0; i < origins.length; i++) {
    createOption('originAirports', i, origins[i][0], origins[i][1]);
    if (defaultOriginAirport == originAirports[i].value) {
      originAirports[i].selected = true;
    }
  }
   
  if (selectedOrigin.value !== '') {
  	populateDestinationAirports();
  } 
}

function getRouteSorting(){

	var routeSorting = (document.getElementById("routeSorting")).value;
	var routeSortingArray = new Array(); 
	routeSortingArray = routeSorting.split(',');
    return routeSortingArray;
}

function populateDestinationAirports() {
  var selectedDestination = document.getElementById("selectedDestinationAirport");
  var originAirports = document.getElementById("originAirports");
  var destinationAirports = document.getElementById("destinationAirports");
  var routeSorting = getRouteSorting();
  var selectedDestinationAirport = destinationAirports.value; // populate with current selected airport
  
  if (selectedDestAirport !== '') {
    selectedDestinationAirport = selectedDestAirport;	
  }
  var destinations = new Array();
  for (var destination in routes[originAirports.value]) {
    destinations.push(new Array(destination, airports[destination]));
  }
  destinations.sort(sortOptionsByText);
  
  for (i = routeSorting.length - 1; i >= 0 ; i--){
	    var route = routeSorting[i];
	    for (j=0; j < destinations.length; j++){
	    	var destinationCode = destinations[j][0];
	    	var destinationDesc = destinations[j][1];
	    	
	    	if (destinationDesc == ''){
	    		destination.splice(j, 1);
	    	}
	    	else if (route == destinationCode){
	    		destinations.splice(j, 1);
	    		destinations.unshift(new Array(destinationCode, destinationDesc));
	    	}
	    }
  }
  destinations.unshift(new Array('', selectDestinationAirportLabel));
  
  while (destinationAirports.options.length > 0) {
    destinationAirports.options[0] = null;
  }
  
  for(i=0; i < destinations.length; i++) {
    createOption('destinationAirports', i, destinations[i][0], destinations[i][1]);
    if (selectedDestination.value == destinationAirports[i].value) {
      destinationAirports[i].selected = true;
    }
  }
  
  destinationAirports.disabled = false;
}

function deselectDestinationAirports() {
  var selectedOrigin = document.getElementById("selectedOriginAirport");
  var destinationAirports = document.getElementById("destinationAirports");
  if (selectedOrigin === '') {
    destinationAirports.value = '';
    destinationAirports.disabled = true;
  }
}

function getRouteCategory() {
  var originAirport = document.getElementById("originAirports");
  var destinationAirport = document.getElementById("destinationAirports");
  if (originAirport.value && destinationAirport.value) {
    var route = routes[originAirport.value][destinationAirport.value];
  }
  var routeCategory = (route ? route[2] : null);
  return routeCategory;
}

function updatePassengerTypeSelection() {
  var routeCategory = getRouteCategory();
  var numYouths = document.getElementById("numYouths");
  var numSeniors = document.getElementById("numSeniors");
  if (routeCategory !== null) {
    if (routeCategory == 'A') {
      if (numYouths) { numYouths.value = 0; }
      if (numSeniors) { numSeniors.value = 0; }
      hideDiv("youthAndSeniorOptions");
    } else {
      showDiv("youthAndSeniorOptions");
    }
  }
}
