// Javascript for the Star Alliance flight search pages


// Define a list of Microsoft XML HTTP ProgIDs.
//
var XMLHTTPREQUEST_MS_PROGIDS = new Array(
  "Msxml2.XMLHTTP.7.0",
  "Msxml2.XMLHTTP.6.0",
  "Msxml2.XMLHTTP.5.0",
  "Msxml2.XMLHTTP.4.0",
  "MSXML2.XMLHTTP.3.0",
  "MSXML2.XMLHTTP",
  "Microsoft.XMLHTTP"
);

//
// Define ready state constants.
//
var XMLHTTPREQUEST_READY_STATE_UNINITIALIZED = 0;
var XMLHTTPREQUEST_READY_STATE_LOADING       = 1;
var XMLHTTPREQUEST_READY_STATE_LOADED        = 2;
var XMLHTTPREQUEST_READY_STATE_INTERACTIVE   = 3;
var XMLHTTPREQUEST_READY_STATE_COMPLETED     = 4;

// Toggles expand & collapse
function toggle(section,obj){
	id=getRowNumber(obj);
	if (section=="outbound"){
	var label=document.getElementById(section+"DetailLabel"+Outbound[id][12]);
    var icon =document.getElementById(section+"DetailIcon"+Outbound[id][12]);
	}else{
		var label=document.getElementById(section+"DetailLabel"+InboundDetail[id][12]);
		
    var icon =document.getElementById(section+"DetailIcon"+InboundDetail[id][12]);
	}
	collapseAll(section,id);
	var tr=document.getElementById(section+"DetailBox"+id).parentNode;
	
	if(tr.style.display=="block"||tr.style.display=="table-row"){
		tr.style.display="none";
		
		label.innerHTML=labelCollapsed;
		
    icon.src=expandIcon;

	}else{
		if(navigator.appName.indexOf("Microsoft") > -1){
			tr.style.display="block";
		} else {
			tr.style.display="table-row";
		}
		label.innerHTML=labelExpanded;
		
    icon.src=collapseIcon;

	}
}

// Populate array with expandable flight detail boxes, so that it can be used in the table sorting
// Sort rows by duration as default
function initResultList(){
	if(Outbound != null){
		for (var i=0;i<Outbound.length;i++){
			obj=document.getElementById("outboundDetailBox"+i);
			outboundDetailBox[i]=new Array(obj.innerHTML);
		}
	}
	if(InboundDetail != null && InboundDetail.length != undefined){
		for (var i=0;i<InboundDetail.length;i++){
			obj=document.getElementById("inboundDetailBox"+i);
			
			inboundDetailBox[i]=new Array(obj.innerHTML);
		}
	}

  // Sort by duration - server delivers unsorted rows
  if(Outbound.length > 0){
	  //myObjectBubbleSort('outbound',Outbound, Outbound.length,5,2);
	  //showObjectArray('outbound',Outbound, Outbound.length,11);
    displaySortingArrow('down', 'outbound', 3);
  }
  if(InboundDetail.length > 0){
	  //myObjectBubbleSort('inbound',InboundDetail, InboundDetail.length,5,2);
	  //showObjectArray('inbound',InboundDetail, InboundDetail.length,11);
    displaySortingArrow('down', 'inbound', 3);
  }

}

// Get the number of the table row in which this element is displayed
function getRowNumber(obj){
	temp1=obj.getAttribute('id').split("l");
	temp2=temp1[2].split("_");
	//alert(temp2[0]);
	return(temp2[0]);
}

function collapseAll(section,exception){
  var arraylength;
  if (!exception){exception=-1;}
  if (section=="outbound"){arrayLength=Outbound.length;}
  else{arrayLength=InboundDetail.length;}
	for (var i=0;i<arrayLength;i++){
		if (i!=exception){
      if (section=="outbound"){
        var label=document.getElementById(section+"DetailLabel"+Outbound[i][12]);
        var icon =document.getElementById(section+"DetailIcon"+Outbound[i][12]);

      }
      else{
        var label=document.getElementById(section+"DetailLabel"+InboundDetail[i][12]);
        var icon =document.getElementById(section+"DetailIcon"+InboundDetail[i][12]);
      }
			tr=document.getElementById(section+"DetailBox"+i).parentNode;
			tr.style.display="none";
			//alert(label);

			label.innerHTML=labelCollapsed;
		
      		icon.src=expandIcon;


		}
	}
}

function activateFlightDetail(section,obj){
  toggle(section,obj);
  getFlightDetails(section,obj);
}

//
// Returns XMLHttpRequest object.
//
function getXMLHttpRequest(){
  var httpRequest = null;

  // Create the appropriate HttpRequest object for the browser.
  if (window.XMLHttpRequest != null){
    httpRequest = new window.XMLHttpRequest();
  }
  else if (window.ActiveXObject != null){
    // Must be IE, find the right ActiveXObject.
    var success = false;
    for (var i = 0;i < XMLHTTPREQUEST_MS_PROGIDS.length && !success;i++){
      try{
        httpRequest = new ActiveXObject(XMLHTTPREQUEST_MS_PROGIDS[i]);
        success = true;
      }
      catch (ex){}
    }
  }

  // Display an error if we couldn't create one.
  if (httpRequest == null)alert("Error in HttpRequest():\n\n" + "Cannot create an XMLHttpRequest object.");
  // Return it.
  return httpRequest;
}

function getFlightDetails(tripType,obj){
  id=getRowNumber(obj);
  myDetailBox=document.getElementById(tripType+"DetailBox"+id);
  if (tripType=="inbound"){
    xmlURL=InboundDetail[id][10];	
  }else{
    xmlURL=Outbound[id][10];
  }
  xmlhttp.open('GET', xmlURL, true);
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == XMLHTTPREQUEST_READY_STATE_COMPLETED) {
      var xmlObj=xmlhttp.responseXML;
      parseFlightXML(xmlObj);
    }
  }
  // Try Catch needed if file does not exist
  try {xmlhttp.send(null)}
  catch (e) {}
}

function parseFlightXML(xmlObj){
  var legs = getNumberOfLegs(xmlObj);
  var legTable="";
  var totalDistance = 0;
  if(legs>0){
  	for (var leg=0;leg<legs;leg++){
  		totalDistance += parseInt(getTagValue(xmlObj,leg,"distance"));
  	}
  }
  if (legs>0){
    for (var leg=0;leg<legs;leg++){
      legTable+=renderLegDisplay(xmlObj,leg,totalDistance);
    }
  }else{
    legTable=xmlErrorMessage;
  }
  myDetailBox.innerHTML=legTable;
}

function getNumberOfLegs(xmlObj){
  var legs=0;
  try{legs=xmlObj.documentElement.getElementsByTagName("leg").length}
  catch(e){}
  return legs;
}
function getLegTitle(xmlObj,leg)      {return xmlObj.documentElement.getElementsByTagName("leg").item(leg).getAttribute("title")}
function getTagValue(xmlObj,leg,tag){
  var value="";
  // If XML tag is corrupt or empty, return an empty string
  try{value=xmlObj.documentElement.getElementsByTagName(tag).item(leg).firstChild.nodeValue;}
  catch (e){}
  if (getNumberOfLegs(xmlObj)!=xmlObj.documentElement.getElementsByTagName(tag).length){
      value="";
  }
  return value;
}

function renderLegDisplay(xmlObj,leg,totalDistance){
  var legTable;
  legTable =  "<table border=0 cellpadding=0 cellspacing=0 class=flightDetails>";
  legTable += "<tr>";
  legTable += "<td colspan=2 width=370>";
  legTable += "<strong>"+getLegTitle(xmlObj,leg)+"</strong> - "+"<br>";
  legTable += "<strong>"+getTagValue(xmlObj,leg,"departure")+"</strong> ";
  legTable += "<a class='resLink' href='"+getTagValue(xmlObj,leg,"departureAirportURL")+"'>";
  legTable += "<strong>"+getTagValue(xmlObj,leg,"departureAirport")+"</strong> (";
  legTable += getTagValue(xmlObj,leg,"departureAirportCode")+")</a>, ";
  legTable += getTagValue(xmlObj,leg,"departureCountry")+" - ";
  legTable += "<strong>"+getTagValue(xmlObj,leg,"arrival")+"</strong> ";
  legTable += "<a class='resLink' href='"+getTagValue(xmlObj,leg,"arrivalAirportURL")+"'>";
  legTable += "<strong>"+getTagValue(xmlObj,leg,"arrivalAirport")+"</strong> (";
  legTable += getTagValue(xmlObj,leg,"arrivalAirportCode")+")</a>, ";
  legTable += getTagValue(xmlObj,leg,"arrivalCountry")+ " ("+getTagValue(xmlObj,leg,"distance")+ " miles) </td>";
  if(leg<1){
  	legTable += "<td valign=middle align=left width=175>" + "Total distance "+"<strong>"+totalDistance+"</strong>"+"<strong>"+ " miles"+"</strong>"+"</td>";
  }
  else{
	legTable += "<td valign=top width=175></td>";
  }
  legTable += "</tr><tr>";
  legTable += "<td>"+getTagValue(xmlObj,leg,"durationText")+"</td>";
  legTable += "<td>"+getTagValue(xmlObj,leg,"operatorText")+", ";
  legTable += getTagValue(xmlObj,leg,"mealText")+", ";
  legTable += getTagValue(xmlObj,leg,"aircraftText")+"</td>";
  legTable += "<td rowspan=2>";
  legTable += "<ul class='vfeSectionLink'><li class='last'>";
  legTable += "<a class='resLink' href='"+ urlPrefix + getTagValue(xmlObj,leg,"flightStatusURL")+"'>";
  legTable += "Check flight status"+"</a></li></ul>";
  legTable += "</td></tr></table>";
//alert(getTagValue(xmlObj,leg,"flightStatusText"));
  return legTable;
}


