// logo sizes
var airline_width = 138;
var airline_height = 31;
var codeShare_width = 138;
var codeShare_height = 31;

var useJavsScriptSearch = false;

    // incase there is any disconnection, need to clear table and rebuild to take off old flights
    function clearTable(){
	    var flightTable = parent.mainFrame.document.getElementById("iFlightTable"); 
    	var cnt = 0;
    	flightTable = flightTable.tBodies[0];
    	while (flightTable.rows.length > 0){
   			flightTable.deleteRow(0);
    	}
    }
    
    // takes time in a format of "12:00a" or "4:00p"  and returns the millisecs of that time, uses todays date if not given
    function parseTime(value){
    	var date = new Date();
	    var ary = value.split("T");
	    var hours = 0;
	    var min = 0;
	    if (ary[1] != null){
	       	value = ary[1];
	       	var str = ary[0].split("-");
	       	hours = parseInt(value.substring(0,2),10);
	       	min = parseInt(value.substring(3,5),10);
	       	date.setFullYear(str[0],str[1]-1,str[2]);
	       	date.setHours(hours,min,00,00);
	    } else {
			var y=2;
	   		hours = parseInt(value.charAt(0),10);
	   		if (value.charAt(1) != ":") {
	   			y++;
	   			hours *= 10;
	   			hours += parseInt(value.charAt(1),10);
	   		}
			min += parseInt(value.charAt(y),10)*10 + parseInt(value.charAt(y+1),10);
			if ((value.charAt(y+2) == "A" || value.charAt(y+2) == "a") && hours ==12)	// if it is 12 am its really 00:00
				hours = 0;
			if ((value.charAt(y+2) == "P" || value.charAt(y+2) == "p") && hours != 12)// add for military time unless it is 12PM (noon)
				hours += 12;
			date.setHours(hours,min,00,00);
		}
		return(date.valueOf());
	}// parseTime
	
	
	function setupSearch(value, changed){
		switch(changed){
   			case -2:{
   					break;
   			}
   			case -1: {
   					if (parent.AIRLINE == null && parent.FLIGHT == null && parent.ORIGIN == null && parent.DESTINATION == null && parent.TIME_FROM == null && parent.TIME_TO == null)
   						return;
   					break;
   					}
	   		case 0: {
	   			if (value == dropdownAirline)
	   				parent.AIRLINE = null;
	   			else
	   				parent.AIRLINE = value.split(" (")[0];
	   			break;
	   		}
			case 1: {
	   			if (value == dropdownFlightNumber)
	   				parent.FLIGHT = null;
	   			else
	   				parent.FLIGHT = value;
	   			break;
	   		}
	   		case 2: {
	   			if (value == dropdownCity)
	   				parent.ORIGIN = null;
	   			else
	   				parent.ORIGIN = value.split(" (")[0];
	   			break;
	   		}
	   		case 3: {
	   			if (value == dropdownCity)
	   				parent.DESTINATION = null;
	   			else
	   				parent.DESTINATION = value.split(" (")[0];
	   			break;
	   		}
	   		case 4: {
	   			if (value == dropdownFrom){
					parent.TIME_FROM = null;
	   			}
	   			else{
	   				parent.TIME_FROM = parseTime(value);
	   				if (parent.TIME_TO && parent.TIME_FROM >= parent.TIME_TO) // if from > to add 1 day to to
	   					parent.TIME_TO = parent.TIME_TO + 86400000;
	   			}
	   			break;
	   		}
	   		case 5: {
	   			if (value == dropdownTo) {
					parent.TIME_TO = null; //parseTime('11:59p') + 86400000;
	   			}
	   			else {
	   				parent.TIME_TO = parseTime(value);
	   				if (parent.TIME_FROM && parent.TIME_FROM >= parent.TIME_TO) // if from > to add 1 day to to
	   					parent.TIME_TO = parent.TIME_TO + 86400000;
	   			}
	   			break;
	   		}
	   		case 7: {
	   			if (value == "")
	   				parent.CONCOURSES = null;
	   			else
	   				parent.CONCOURSES = value;
	   		}
   		} //switch
		if (useJavsScriptSearch)
			search();
	}
    
    //applys the new search spec to the list
    function search(){
    	var xmlDoc = parent.xmlDocument;
    	var mainDoc = parent.mainFrame.document;
    	var length = xmlDoc.getElementsByTagName("numFlights").item(0).firstChild.data;
	    var flightTable = mainDoc.getElementById("iFlightTable").tBodies[0];
	    clearTable();
	    for(var x=0;x<length;x++){
	    	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] = "_row";
	    	var flightRow =  mainDoc.getElementById(ary.join("")); 
			if (shouldFlightBeListed(x))
				flightTable.appendChild(makeRow(x));	
			else if (flightRow != null){
				flightTable.deleteRow(flightRow.rowIndex);
			}
		}// for
		newSort();
    }//search
    
    function createSimpleCell(mainDoc,text){
    	var cell = mainDoc.createElement("td");
   		cell.className = "flightData";
   		cell.appendChild(mainDoc.createTextNode(text));
   		return cell;
    }
    
    // returns 0 if day1 == day2
    // returns 1 if day1 is greater otherwise -1
    function compareDay(day1, day2){
    	sorted = parseInt(day1[0],10);
		unsorted = parseInt(day2[0],10);
		
		if (sorted == unsorted) { // if same year
			sorted = parseInt(day1[1],10);
			unsorted = parseInt(day2[1],10);
			if(sorted == unsorted) { // if same month
				sorted = parseInt(day1[2],10);
    			unsorted = parseInt(day2[2],10);
				if(sorted == unsorted) { // if same day 
					return(0);
				} 
			}
		}
		if (sorted > unsorted) {
			return 1;
		} else {
			return -1;
		}
    }
    
    // sorting/compare functions; 
    // first sort condition -> second condition -> etc...---------------------------------------------------------------------
    // each sorting/compare function takes in two iFightTable rows
    // each sorting/compare function will return:
    	// 0 (zero) if equal
    	// positive number if first arg is greater
    	// negative number if second arg is great 
    	
    // sort by airline -> flight number -> time
    function sortAirline(sorted,unsorted){
    	var comp = compareAirline(sorted,unsorted);
    	if (comp == 0){
    		comp = compareTRN(sorted,unsorted);
    		if (comp == 0)
    			comp = compareTime(sorted,unsorted);
    	}
    	return comp;
    } //sortAirline
    
    // flight number -> time -> airline -> city
    function sortFlight(sorted,unsorted){
    	var comp = compareTRN(sorted,unsorted);
    	if (comp == 0){
    		comp = compareTime(sorted,unsorted);
    	 	if (comp == 0){
    			comp = compareAirline(sorted,unsorted);
    			if (comp == 0)
    				comp = compareCity(sorted,unsorted);	
    		}
    	}
    	return comp;
    }    
    
    // city -> time -> airline -> flight number 
    function sortCity(sorted,unsorted){
    	var comp = compareCity(sorted,unsorted);
    	if (comp == 0){
    		comp = compareTime(sorted,unsorted);
    		if (comp == 0){
    			comp = compareAirline(sorted,unsorted);
    			if (comp == 0)
    				comp = compareTRN(sorted,unsorted);	
    		}
    	}
    	return comp;
    }
    
    // time -> city -> airline -> flight number
    function sortTime(sorted,unsorted){
    	var comp = compareTime(sorted,unsorted);
    	if (comp == 0){
    		comp = compareCity(sorted,unsorted);
    		if (comp == 0){
    			comp = compareAirline(sorted,unsorted);
    			if (comp == 0)
    				comp = compareTRN(sorted,unsorted);	
    		}
    	}
    	return comp;
    }
    
    // gate -> time -> airline -> flight
    function sortGate(sorted,unsorted){
    	var comp = compareGate(sorted,unsorted);
    	if (comp == 0){
    		comp = compareTime(sorted,unsorted);
    		if (comp == 0){
    			comp = compareAirline(sorted,unsorted);
    			if (comp == 0)
    				comp = compareTRN(sorted,unsorted);	
    		}
    	}
    	return comp;
    }
    
    /// bags -> time -> airline -> flight
    function sortBags(sorted,unsorted){
    	var comp = compareBags(sorted,unsorted);
    	if (comp == 0){
    		comp = compareTime(sorted,unsorted);
    		if (comp == 0){
    			comp = compareAirline(sorted,unsorted);
    			if (comp == 0)
    				comp = compareTRN(sorted,unsorted);	
    		}
    	}
    	return comp;
    }
    
    // terminal -> time -> airline -> flight
    function sortTerminal(sorted,unsorted){
    	var comp = compareTerminal(sorted,unsorted);
    	if (comp == 0){
    		comp = compareTime(sorted,unsorted);
    		if (comp == 0){
    			comp = compareAirline(sorted,unsorted);
    			if (comp == 0)
    				comp = compareTRN(sorted,unsorted);	
    		}
    	}
    	return comp;
    }
    
    // what ever col # -> time -> airline -> flight
    function genernicSort(sorted,unsorted){
    	var comp = compare(sorted,unsorted);
    	if (comp == 0){
    		comp = compareTime(sorted,unsorted);
    		if (comp == 0){
    			comp = compareAirline(sorted,unsorted);
    			if (comp == 0)
    				comp = compareTRN(sorted,unsorted);	
    		}
    	}
    	return comp;
    }
    
    // compare col_genernicSort column
    function compare(x,y){
    	x = x.cells[sort_genernic].childNodes[0].data; 
		y = y.cells[sort_genernic].childNodes[0].data;
		return stringCompare(x,y);
    }
    
    // compare Bags
    function compareBags(x,y){
    	x = x.cells[sort_bags].childNodes[0].data; 
		y = y.cells[sort_bags].childNodes[0].data;
		return stringCompare(x,y);
    }
    
     // compare terminals
    function compareTerminal(x,y){
    	x = x.cells[sort_terminal].childNodes[0].data; 
		y = y.cells[sort_terminal].childNodes[0].data;
		return stringCompare(x,y);
    }
    
    //compare gates
    function compareGate(x,y){
    	x = x.cells[sort_gate].childNodes[0].data;
		y = y.cells[sort_gate].childNodes[0].data;
		var len = x.length;
		var gateSorted = "";
		var terminalSorted = "";
		var z=len-1;
		while (!isNaN(x.charAt(z))&& z >= 0)
			z--;
		gateSorted = x.substring(z+1,len);
		terminalSorted  = x.substring(0,z+1);
		
		len = y.length;
		z=len-1;
		var gateUnsorted = "";
		var terminalUnsorted = "";
		while (!isNaN(y.charAt(z)) && z >= 0)
			z--;
		gateUnsorted = y.substring(z+1,len);
		terminalUnsorted  = y.substring(0,z+1);						
						
		if (terminalSorted == terminalUnsorted) {
			//x = parseInt(gateSorted,10);
			//y = parseInt(gateUnsorted,10);
			// comparing strings anyway, not sure why I convert them to int, which causes a NaN on spots like: 23A:
			x = gateSorted;
			y = gateUnsorted;
		}
    	return stringCompare(x,y);
    }
    
    // compare airline names
    function compareAirline(x,y){
    	x = x.cells[sort_airline].id; 
		y = y.cells[sort_airline].id;
		return stringCompare(x,y);
    }
    
    // compare Time
    function compareTime(x,y){
    	x = x.cells[sort_time].id;
 		y = y.cells[sort_time].id;
 		return  x - y;
    }
    
    // compare flight numbers
    function compareTRN(x,y){
    	x = x.cells[sort_trn].childNodes[0].data; 
		y = y.cells[sort_trn].childNodes[0].data;
		x = parseInt(x,10);
		y = parseInt(y,10);
		return x-y;
    }
    
    // compare city names
    function compareCity(x,y){
    	x = x.cells[sort_city].childNodes[0].data; 
		y = y.cells[sort_city].childNodes[0].data;
		
		return stringCompare(x,y);
    }
    
    function stringCompare(x,y){
    	if (x == y)
			return 0;
		else if (x > y)
			return 1;
		else
			return -1;
    }
    
    
   	// end sorting functions--------------------------------------------------------------------------------------------------------
    
   	// this function is called when the user clicks to sort a Column,
	// it sets actSortCol and actSortType and then calls sort();
    function sortBy(col){
    	//populateColumnIndexes();
    	if (document.getElementById("long_form").actSortCol.value == col){
			var type = document.getElementById("long_form").actSortType.value;
			if (type == "asc")
				document.getElementById("long_form").actSortType.value = "desc";
			else
				document.getElementById("long_form").actSortType.value = "asc";
		} else {
			document.getElementById("long_form").actSortCol.value = col;
			document.getElementById("long_form").actSortType.value = "asc";
		}
    	newSort();
    } // sortBy(col)
 
    // This function will use the XML to check each row
	// if the data in the row has changed it will update the row
	// if the row is not in the table it will create it
	// if a row is not in the XML it will be deleted
	function update(){
		if (parent.xmlDocument == null)
			return;
	 	var time = (new Date()).valueOf();
	 	var mainDoc = parent.mainFrame.document;
	 	var flightsAdded = 0
	 	var flights  = parent.xmlDocument.getElementsByTagName("flight");
	 	var len = flights.length;
	 		
	 	var flightTable = mainDoc.getElementById("iFlightTable").tBodies[0];
	 	for(var x=0;x<len;x++){
	 		var flight = flights[x];
	 		var ary = new Array();
	 		ary[0] = flight.getElementsByTagName("flightNumber")[0].firstChild.data;
			ary[1] = "_";
			ary[2] = flight.getElementsByTagName("stt")[0].firstChild.data;
			ary[3] = "_";
			ary[4] = flight.getElementsByTagName("city")[0].firstChild.data;
			ary[5] = "_row";
	 		var	id = ary.join("");
	 		var flightRow =  mainDoc.getElementById(id);
	 		if (flightRow != null){
	 			updateRow(x,flightRow);
	 		} else if(shouldFlightBeListed(x)){  // should this row be on the list:
	  			flightTable.appendChild(makeRow(x));	
			flightsAdded = flightsAdded + 1;
		}
	 	}// for
	 	// delete any rows not marked, also change marks back to zero
	 	var x=0;
	 	while (x<flightTable.rows.length){
	 		var flightRow = flightTable.rows[x];
			var data = flightRow.cells[col_marker].childNodes[0];
			if (data.data == "0")
			   	flightTable.deleteRow(flightRow.rowIndex);
			else{
				data.data = "0";
				x++;
			}
	 	}
		newSort();
	}// update

    /***Code Share start***************************************************8*********************************/
    
    var timeOutRefs = new Array();
	var timeOutCnt = 0;
	
  	// setup and call rollCodeShares
    function runCodeShares(){
    	var xmlDoc = parent.xmlDocument;
    	if (xmlDoc == null)	return;
    	
    	var codes = xmlDoc.getElementsByTagName("codeShares");
    	for(var x=0;x<codes.length;x++){
    		var flights = codes.item(x).getElementsByTagName("codeShareFlight");
    		if (flights != null && flights.length > 1){
				var ary1 = new Array();
				ary1[0] = xmlDoc.getElementsByTagName("flightNumber").item(x).firstChild.data;
				ary1[1] = "_";
				ary1[2] = xmlDoc.getElementsByTagName("stt").item(x).firstChild.data;
				ary1[3] = "_";
				ary1[4] = xmlDoc.getElementsByTagName("city").item(x).firstChild.data;
				ary1[5] = "_row";
				var id = ary1.join(""); 
				var ary = new Array(flights.length);
				for(var y=0;y<flights.length;y++){
					ary[y] = new Array(2);
					ary[y][0] = flights.item(y).getElementsByTagName("csFlight").item(0).firstChild.data;
					ary[y][1] = flights.item(y).getElementsByTagName("csLogo").item(0).firstChild.data;
				}
				rollCodeShare(id,ary,1);
    		}
    	}
    }
    
    //note: ary comes in as a string
    function rollCodeShare(id,ary,cnt){
    	if (id == null || ary == null || cnt == null) return;
    	
    	var tmp = new String(ary);
    	ary = tmp.split(",");
    	
    	if ((cnt*2) >= ary.length)
    		cnt = 0;
    	var flightNumber = ary[cnt*2];
    	
		var row = parent.mainFrame.document.getElementById(id);
		if (row == null)
			return;
		row.cells[col_codeShareLogo].childNodes[0].src = ary[cnt*2+1];
		row.cells[col_codeShareLogo].childNodes[0].alt = flightNumber;
		row.cells[col_codeShareLogo].childNodes[0].title = flightNumber;

		row.cells[col_CDS].innerHTML = flightNumber.substring(3,flightNumber.length);
		cnt = cnt + 1;
		var ref = setTimeout("rollCodeShare('" + id + "','" + ary + "'," + cnt + ")", rollingCodeShareRefreshTime);
		addRef(ref);
    }
    
    function addRef(newRef){
    	timeOutRefs[timeOutCnt] = newRef;
    	timeOutCnt = timeOutCnt + 1;
    }
    
    function clearRefs(){
    	for(var x=0;x<timeOutCnt;x++)
    		clearTimeout(timeOutRefs[x]);
    	timeOutCnt =0;
    }
    
    /***Code Share end********************************************************************************************/
    
    // for sorting columns:
    function submitForm(newSortCol){
    	clearRefs();
    	var form = document.getElementById("long_form");
    	if (form){
    		form.newSortCol.value = newSortCol;
    		form.submit();
    		if (form.actSortCol.value == newSortCol){
    			var type = form.actSortType.value;
	    		if (type == "asc"){
	    			form.actSortType.value = "desc";
	    		}else{
	    			form.actSortType.value = "asc";
	    		}
	    	} else {
    			form.actSortCol.value = newSortCol;
    			form.actSortType.value = "asc";
    		}
    		fixHeaderImg(form.actSortType.value);
    	}
    	form.newSortCol.value = "";
    	return false;
    }
    
    function fixHeaderImg(sortType){
    	var header = document.getElementById("flightHeader2");
    	var img = header.getElementsByTagName("IMG");
    	var deepImg = img[0].cloneNode(false);
    	if (sortType == "asc")
    		deepImg.src = "images/arrow_up.gif"
    	else
    		deepImg.src = "images/arrow_down.gif"
    	img[0].parentNode.removeChild(img[0])
    	var column = document.getElementById(document.getElementById("long_form").actSortCol.value);
   		column.childNodes[0].appendChild(deepImg);
   	}
   	
   	function newSort(){
   		var mainDoc = parent.mainFrame.document;
   		var topDoc = parent.topFrame.document;
   		var time = (new Date()).valueOf();
   		var flightTable = mainDoc.getElementById("iFlightTable").tBodies[0];
   		var sortCol = topDoc.getElementById("long_form").actSortCol.value;
    	var sortType = topDoc.getElementById("long_form").actSortType.value;
   		var length = flightTable.rows.length;
		var sortingFunction = null;	
		switch(sortCol){
			case "airline": {	sortingFunction = sortAirline; break; }
			case "TRN": {	sortingFunction = sortFlight; break; }
			case "city": {	sortingFunction = sortCity; break; }
			case "sttFormatted": {	sortingFunction = sortTime; break; }
			case "sttSmallerFormatted": {	sortingFunction = sortTime; break; }
			case "gate": {	sortingFunction = sortGate; break; }
			case "stt": {	sortingFunction = sortTime; break; }
			case "bags": {	sortingFunction = sortBags; break; }
			case "terminal": {	sortingFunction = sortTerminal; break; }
			case "CXR": {	sortingFunction = sortAirline; break; }
			case "CTY": {	sortingFunction = sortCity; break; }
			case "STN": {	sortingFunction = sortGate; break; }
			default: {	
				sortingFunction = genernicSort; 
				sort_genernic = topDoc.getElementById(sortCol).cellIndex;
			}
		} //switch
		var rows = new Array();
		for(var x=0;x<length;x++){
    		rows[x] = flightTable.rows[x];
    	}
   		rows.sort(sortingFunction);
   		if (sortType == "desc")
   			rows.reverse();
   		for(var x=0;x<length;x++){
    		flightTable.appendChild(rows[x]);
    	}
    	// stripe rows, its faster to do it in another loop then in the one above:
    	var temp = true;
    	for(var x=0;x<length;x++){
    		if (temp)
    			flightTable.rows[x].className = "flightData1";	
    		else
    			flightTable.rows[x].className = "flightData2";
    		temp = !temp;
    	}
   	}