// columns indexes for sorting:
var sort_time = 3;
var sort_genernic = -1;
var sort_bags = -1;
var sort_terminal = 5;
var sort_gate = 5;
var sort_airline = 0;
var sort_trn = 1;
var sort_city = 2;

// column indexes for normal webfids
var col_airline = 0;
var col_TRN = 1;
var col_city = 2;
var col_sttSmallerFormatted = 3;
var col_status = 4;
var col_bags = -1;
var col_gate = 5;
var col_terminal = 6;
var col_codeShareLogo = 7;
var col_CDS = 8;
var col_marker = 9;

// set col indexes on page load, arrival and departure page have different layouts
function populateColumnIndexes(airline, trn, city, time, status, terminal, codeShareLogo, CDS, bags, gate, marker){
	col_airline = airline;
	sort_airline = airline;

	col_TRN = trn;
	sort_trn = trn;
		
	col_city = city;
	sort_city = city;

	col_sttSmallerFormatted = time;
	sort_time = time;

	col_status = status;

	col_terminal = terminal;
	sort_terminal = terminal; 

	col_codeShareLogo = codeShareLogo;

	col_CDS = CDS;

	col_bags = bags;
	sort_bags = bags;

	col_gate = gate;
	sort_gate = gate;

	col_marker = marker;
}

// returns true if flight xmlDocument[x] should be in the list.
// returns false if not
function shouldFlightBeListed(x){
	var xmlDoc = parent.xmlDocument;
  	var time = xmlDoc.getElementsByTagName("timeInMillis").item(x).firstChild.data;
    var airlineName = xmlDoc.getElementsByTagName("airlineName").item(x).firstChild.data;
  	var cityName = xmlDoc.getElementsByTagName("city").item(x).firstChild.data;
  	var ary = new Array();
  	ary[0] = xmlDoc.getElementsByTagName("flightNumber").item(x).firstChild.data;
  	ary[1] = ",";
  	ary[2] = xmlDoc.getElementsByTagName("stt").item(x).firstChild.data;
  	var flightNumber = ary.join("");

	if ((parent.AIRLINE && parent.AIRLINE != airlineName) ||
		(parent.FLIGHT && parent.FLIGHT != flightNumber) ||
		(parent.ORIGIN && parent.ORIGIN != cityName) ||
		(parent.TIME_FROM && parent.TIME_FROM >= time ) ||
		(parent.TIME_TO && parent.TIME_TO <= time)) {
	    	return false;
	    } else{
			return true;
		}
} //shouldFlightBeListed
 
// update flightRow, with data from XML[x]
function updateRow(x,flightRow){
	var xmlDoc = parent.xmlDocument;
	var mainDoc = parent.mainFrame.document;
	var topDoc = parent.topFrame.document;
		
	var font = flightRow.cells[col_status].getElementsByTagName("font")[0];
	if (font && font.innerHTML != xmlDoc.getElementsByTagName("status").item(x).firstChild.data){
		var text = xmlDoc.getElementsByTagName("status").item(x).firstChild.data;
		font.innerHTML = text;		
		
		var sub = text.substring(0,2);
		if (sub == 'No') { font.className = 'now'; }
			else if (sub == 'Ar' || sub == 'La') { font.className = 'arrived'; }
				else if (sub == 'De') { font.className = 'delayed'; }
					else if (sub == 'Ca') { font.className = 'cancelled'; }
						else { font.className = 'default' }
	}
	
	if (topDoc.getElementById("bags")){ 
		if (flightRow.cells[col_bags].childNodes[0].data != xmlDoc.getElementsByTagName("bags").item(x).firstChild.data)
			flightRow.cells[col_bags].childNodes[0].data = xmlDoc.getElementsByTagName("bags").item(x).firstChild.data;
	}
	
	if (topDoc.getElementById("gate")) {
		if (flightRow.cells[col_gate].childNodes[0].data != xmlDoc.getElementsByTagName("gate").item(x).firstChild.data)
			flightRow.cells[col_gate].childNodes[0].data = xmlDoc.getElementsByTagName("gate").item(x).firstChild.data;
	}
	
	if (flightRow.cells[col_terminal].childNodes[0].data != xmlDoc.getElementsByTagName("terminal").item(x).firstChild.data)
		flightRow.cells[col_terminal].childNodes[0].data = xmlDoc.getElementsByTagName("terminal").item(x).firstChild.data;
	
	var codes = xmlDoc.getElementsByTagName("codeShares").item(x);
   		var flights = codes.getElementsByTagName("codeShareFlight");
   		if (flights != null && flights.length >= 1 && flightRow.cells[col_codeShareLogo].childNodes[0].tagName != "IMG"){
	
		var logoCell = mainDoc.createElement("img");
		holdCDS = flights.item(0).getElementsByTagName("csFlight").item(0).firstChild.data;
		logoCell.src = flights.item(0).getElementsByTagName("csLogo").item(0).firstChild.data;
		logoCell.width = codeShare_width;
		logoCell.height = codeShare_height;
		logoCell.title = holdCDS;
		logoCell.alt = holdCDS;
		flightRow.cells[col_codeShareLogo].removeChild(flightRow.cells[col_codeShareLogo].childNodes[0]);
		flightRow.cells[col_codeShareLogo].appendChild(logoCell);
		flightRow.cells[col_CDS].appendChild(mainDoc.createTextNode(holdCDS.substring(3)));
	}
	
	if (flightRow.cells[col_marker])
		flightRow.cells[col_marker].childNodes[0].data = "1";
}//updateRow

function link(menuItem) {
     if (menuItem == '0') {
		parent.ORIGIN = null;
		parent.DESTINATION = null;
		parent.TIME_FROM = null;
		parent.TIME_TO = null;
		parent.AIRLINE = null;
		parent.FLIGHT = null;
		parent.mainFrame.document.location = "webfids?action=showAllArrivals";
		parent.topFrame.document.location = "webfids?action=menu_forward&direction=A";
     }
     else if (menuItem == '1') {
		parent.ORIGIN = null;
		parent.DESTINATION = null;
		parent.ORIGIN = null;
		parent.DESTINATION = null;
		parent.TIME_FROM = null;
		parent.TIME_TO = null;
		parent.AIRLINE = null;
		parent.FLIGHT = null;
		parent.mainFrame.document.location = "webfids?action=showAllDepartures";
		parent.topFrame.location = "webfids?action=menu_forward&direction=D";
     }
}

function updateMessages(){
  	var msgs = parent.xmlDocument.getElementsByTagName("messages");
  	if (msgs == null)
  		return;
	if (msgs[0].childNodes.length == 0)
		return;
  	// clear old messages first
  	var mTable = parent.topFrame.document.getElementById("messageTable").tBodies[0];
  	while (mTable.rows.length > 0) {
  		mTable.deleteRow(0);
  	}
  	
  	// add new Messages
  	var length = msgs[0].childNodes.length;
  	for(x=0;x<1;x++){
  		var msg = msgs[0].childNodes[x].firstChild;
  		var row = parent.topFrame.document.createElement("tr");
  			var arrowCell = parent.topFrame.document.createElement("td");
  				arrowCell.width = "25";
  				arrowCell.className = "message";
  			arrowCell.appendChild(parent.topFrame.document.createTextNode(msg.data.substring(0,1)));
  			var msgCell = parent.topFrame.document.createElement("td");
  				msgCell.width = "775";
  				msgCell.className = "message";
  			msgCell.appendChild(parent.topFrame.document.createTextNode(msg.data.substring(1,msg.data.length)));
  		row.appendChild(arrowCell);
  		row.appendChild(msgCell);
  		mTable.appendChild(row);
  	}
}

// returns a row created with item x in parent.xmlDocument
function makeRow(x){
  	var xmlDoc = parent.xmlDocument;
  	var mainDoc = parent.mainFrame.document;
  	var topDoc = parent.topFrame.document;
  	
  	var row = mainDoc.createElement("tr");
  	row.className = "flightData2";
	var ary = new Array();
	ary[0] = xmlDoc.getElementsByTagName("flightNumber").item(x).firstChild.data;
	ary[1] = "_";
	ary[2] = xmlDoc.getElementsByTagName("stt").item(x).firstChild.data;
	ary[3] = "_";
	ary[4] = xmlDoc.getElementsByTagName("city").item(x).firstChild.data;
	ary[5] = "_row";
  	row.id = ary.join("");

  	if (topDoc.getElementById("airline")) {
		var imgCell = mainDoc.createElement("td");
		imgCell.className = "flightData";
		imgCell.id = xmlDoc.getElementsByTagName("airlineName").item(x).firstChild.data;
		var airlineLogoCell=null;
		// if there is a logo get the image, if not display the airline name
		if (xmlDoc.getElementsByTagName("airlineLogo").item(x).firstChild.data != "\u00A0" ){
			airlineLogoCell = mainDoc.createElement("img");
			airlineLogoCell.src = xmlDoc.getElementsByTagName("airlineLogo").item(x).firstChild.data;
			airlineLogoCell.width = airline_width;
			airlineLogoCell.height = airline_height;
			airlineLogoCell.title = imgCell.id;
			airlineLogoCell.alt = imgCell.id;
		} else{
			airlineLogoCell = mainDoc.createTextNode(imgCell.id);
		}
	    imgCell.appendChild(airlineLogoCell);
	  	row.appendChild(imgCell);
  	}
  	
  	if (topDoc.getElementById("TRN")) 
		row.appendChild(createSimpleCell(mainDoc,xmlDoc.getElementsByTagName("TRN").item(x).firstChild.data));

  	if (topDoc.getElementById("city")) 
		row.appendChild(createSimpleCell(mainDoc,xmlDoc.getElementsByTagName("city").item(x).firstChild.data));	

  	if (topDoc.getElementById("sttSmallerFormatted")) {
  		var ary = new Array();
  		ary[0] =  xmlDoc.getElementsByTagName("sttSmallerFormatted").item(x).firstChild.data;
  		ary[1] = " ";
  		ary[3] = xmlDoc.getElementsByTagName("sttShortDate").item(x).firstChild.data;
  		var cell = createSimpleCell(mainDoc,ary.join(""));
		cell.id = xmlDoc.getElementsByTagName("timeInMillis").item(x).firstChild.data;
		row.appendChild(cell);
	}

  	if (topDoc.getElementById("status")) {
		var statusCell = mainDoc.createElement("td");
  		statusCell.className = "flightData";
		var font  = mainDoc.createElement("font");
		var text = xmlDoc.getElementsByTagName("status").item(x).firstChild.data;
		var sub = text.substring(0,2);
		if (sub == 'No') { font.className = 'now'; }
			else if (sub == 'Ar' || sub == 'La') { font.className = 'arrived'; }
				else if (sub == 'De') { font.className = 'delayed'; }
					else if (sub == 'Ca') { font.className = 'cancelled'; }
						else { font.className = 'default'; }
		font.appendChild(mainDoc.createTextNode(text));
  		statusCell.appendChild(font);
		row.appendChild(statusCell);
	}

  	if (topDoc.getElementById("gate"))
  		row.appendChild(createSimpleCell(mainDoc,xmlDoc.getElementsByTagName("gate").item(x).firstChild.data));

  	if (topDoc.getElementById("sttFormatted")) {
  		var cell = createSimpleCell(mainDoc,xmlDoc.getElementsByTagName("sttFormatted").item(x).firstChild.data);
  		cell.id = xmlDoc.getElementsByTagName("timeInMillis").item(x).firstChild.data;
		row.appendChild(cell);
	}

  	if (topDoc.getElementById("stt"))
  		row.appendChild(createSimpleCell(mainDoc,xmlDoc.getElementsByTagName("stt").item(x).firstChild.data));

  	if (topDoc.getElementById("att"))
  		row.appendChild(createSimpleCell(mainDoc,xmlDoc.getElementsByTagName("att").item(x).firstChild.data));

  	if (topDoc.getElementById("attFormatted"))
  		row.appendChild(createSimpleCell(mainDoc,xmlDoc.getElementsByTagName("attFormatted").item(x).firstChild.data));

  	if (topDoc.getElementById("ett"))
  		row.appendChild(createSimpleCell(mainDoc,xmlDoc.getElementsByTagName("ett").item(x).firstChild.data));

  	if (topDoc.getElementById("ettFormatted"))
  		row.appendChild(createSimpleCell(mainDoc,xmlDoc.getElementsByTagName("ettFormatted").item(x).firstChild.data));

  	if (topDoc.getElementById("bags"))
  		row.appendChild(createSimpleCell(mainDoc,xmlDoc.getElementsByTagName("bags").item(x).firstChild.data));	

  	if (topDoc.getElementById("CXR"))
  		row.appendChild(createSimpleCell(mainDoc,xmlDoc.getElementsByTagName("CXR").item(x).firstChild.data));

  	if (topDoc.getElementById("flight"))
  		row.appendChild(createSimpleCell(mainDoc,xmlDoc.getElementsByTagName("flightNumber").item(x).firstChild.data));

  	if (topDoc.getElementById("CTY"))
  		row.appendChild(createSimpleCell(mainDoc,xmlDoc.getElementsByTagName("CTY").item(x).firstChild.data));

  	if (topDoc.getElementById("DIR"))
	row.appendChild(createSimpleCell(mainDoc,xmlDoc.getElementsByTagName("DIR").item(x).firstChild.data));
	
  	if (topDoc.getElementById("TYP"))
  		row.appendChild(createSimpleCell(mainDoc,xmlDoc.getElementsByTagName("TYP").item(x).firstChild.data));

  	if (topDoc.getElementById("direction"))
	row.appendChild(createSimpleCell(mainDoc,xmlDoc.getElementsByTagName("direction").item(x).firstChild.data));

  	if (topDoc.getElementById("terminal"))
  		row.appendChild(createSimpleCell(mainDoc,xmlDoc.getElementsByTagName("terminal").item(x).firstChild.data));

	var holdCDS = null;
  	if (topDoc.getElementById("codeShareLogo")) {
  			var cell = mainDoc.createElement("td");
  			cell.className = "flightData";    			
  			var codes = xmlDoc.getElementsByTagName("codeShares").item(x);
  			var flights = codes.getElementsByTagName("codeShareFlight");
  			if (flights != null && flights.length >= 1){					
			var logoCell = mainDoc.createElement("img");
			holdCDS = flights.item(0).getElementsByTagName("csFlight").item(0).firstChild.data;
			logoCell.src = flights.item(0).getElementsByTagName("csLogo").item(0).firstChild.data;
			logoCell.width = codeShare_width;
			logoCell.height = codeShare_height;
			logoCell.title = holdCDS;
			logoCell.alt = holdCDS;
			cell.appendChild(logoCell);
		} else{
			cell.appendChild(mainDoc.createTextNode(xmlDoc.getElementsByTagName("codeShareLogo").item(x).firstChild.data));
		}
		row.appendChild(cell);
	}

  	if (topDoc.getElementById("CDS")) {
		var statusCell = mainDoc.createElement("td");
   		statusCell.className = "flightData";
   		var text = null;
   		if (holdCDS == null){
    		//statusCell.id = xmlDoc.getElementsByTagName("CDS").item(x).firstChild.data;
    		text = mainDoc.createTextNode(xmlDoc.getElementsByTagName("CDS").item(x).firstChild.data.substring(3));
    	} else  {
    		//statusCell.id = holdCDS;
    		text = mainDoc.createTextNode(holdCDS.substring(3));
    	}
   		statusCell.appendChild(text);
		row.appendChild(statusCell);
	}
	
	// create marker:
	var cell = createSimpleCell(mainDoc,"1");
	cell.style.display = "none";
	row.appendChild(cell);
	
	return(row);
}//makeRow