﻿
// /*load_(30.13749999, 31.40694444);*/

//globals
//var msg = 'Enter city name...';
var msg = '';
var tempIDArrayFrom = new Array();
var tempIDArrayTo = new Array();

function loadIndex() {
	// most current browsers support document.implementation
	if (document.implementation && document.implementation.createDocument) {
		xmlDoc = document.implementation.createDocument("", "", null);
		//alert('F '+xmlDoc);
	}
	// MSIE uses ActiveX
	else if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		//alert('ie '+xmlDoc);
	}
	//download xml file
	//xmlDoc.async = "false";
	//load xml
	//xmlDoc.load("./html_forms/airports.xml");
}

//trim text [rTrim & lTrim]
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function searchXML( xDiv , control ) {
	//get usr entered value
	var inputVal = document.getElementById(control).value;
	//all input letters r upper cased
	//document.getElementById(control).value.toUpperCase();
	//trim and upperCase it
	var inputValToUpper = trim(inputVal.toUpperCase());
	//alert(inputValToUpper);
	//the div
	var div_ = document.getElementById(xDiv);
	//load all airport nodes from the xml
	var allitems = xmlDoc.getElementsByTagName("airport");
	
	//reset variables and counters
	var counter = 0;
	var colorID = 0;
	var results = 0;
	
	//empty the div b4 load any new data inside it
	div_.innerHTML = '';
	//show the results div if it's hidden
	div_.style.display = 'block';
	
	//if usr enters a value, make search
	if( inputVal != '' ){
	
	    _top = 0;
		//alert(allitems.length);
		//v.1, search all airport codes <airport code="XXX">	
		//v.2, search all airport city names <city>XXX</city>
		for (i=0;i<allitems.length;i++) {
			
			//get code attribute for each airport in the xml
			//attributes now r not searched, instead, we use it to display the airport attributes in the div
			var attr = allitems[i].getAttribute('code');
			//[new feature] get city name to search with
			var cityName = xmlDoc.getElementsByTagName("city")[i].childNodes[0].nodeValue;
			//trim and upperCase it
			attrTrimmedAndToUpper = trim(attr.toUpperCase());
			cityNameTrimmedAndToUpper = trim(cityName.toUpperCase());
			
			//only 10 results displayed
			if( results == 10 ) break;
			
			//search values started with the entered value
			if( ( cityNameTrimmedAndToUpper.indexOf( inputValToUpper , 0 ) != -1 )
				&& 
			    ( cityNameTrimmedAndToUpper.indexOf( inputValToUpper , 0 ) == 0 ) )
			{
			
				//color select [experimental]
				color = '';
				/*if( colorID == 0 ){
					color = '#DDFEE4';
					colorID++;
				}else{
					color = '#DDFEE4';
					colorID--;
				}*/
				
				//change class to green
				div_.className = 'ac';
			
				//airport code
				code = attrTrimmedAndToUpper;
				//airport desc
				content_ = xmlDoc.getElementsByTagName("city")[i].childNodes[0].nodeValue;
				//IE6 fix, hide select menu
				//selectList('ctl00_content_PAX_NO','none');
				//write new airport entry
				//$(xDiv).style.border= '2px solid red';
				//$(xDiv).style.height = 10;
				
                //$(xDiv).style.marginTop = -187;
				writeDivContent( xDiv , i , color , control , code , content_ );
				
				//this is to know if there's any results found
				counter++;
				//just 10 results counter
				results++;
				
			}
		}
		
		//if no results, display this msg
		if( counter == 0 ){
			//change class to red
			div_.className = 'ae';
			div_.innerHTML = 'No airports...';
		}
		
	}else{
		//change class to green
		div_.className = 'ac';
		//if nothing is entered in the textbox, display this msg
		div_.innerHTML = msg;
	}
}

function writeDivContent( oDiv , i , color , control , code , content ){

	var div_ = document.getElementById(oDiv);
	//change class to green
	div_.className = 'ac';
	
	//_top += 20;
	//alert(_top);
	//div_.style.height = _top;
	
	//write new entry in the results div [with top -cell height]
	div_.innerHTML += "<div id='x"+oDiv+i+"' onmouseover='over(this.id);' onmouseout='out(this.id);' style='background-color: #DDFEE4; width: 100%; cursor: pointer; height: 20px;' onclick=checkAirJet("+i+",'"+oDiv+"','"+control+"');return false;>" + content.slice(0,15) + " - [<span id='sx"+oDiv+i+"'>" + code + "</span>] " + "</div>";

}

//check if there's an air jet selected
//function checkAirJet(i,oDiv,control){alert(i);alert(oDiv);alert(control);
function checkAirJet(control,hdn){//alert(control);alert(hdn);
    if( $('selected_airjet').value == '' ){
        alert('Please select an airjet first!');
        //return false;
    }else{
        //alert('OK!');
        //alert($('selected_airjet').value);
        //setValue(i,oDiv,control);
        //setValue(control);
        
        $('_mostafa').innerHTML = '<span style="float:left; padding:20px;" class="text">The range of the <strong>'+$('selected_airjet').value+'</strong> around the airport <strong>'+ $(control).value +'</strong> is:</span><a href="#" onclick="Effect.Shrink($(divID));xx();return false;" style="float:right; padding:20px;" class="text">Close</a>';
        
        //append map
        $('_mostafa').innerHTML += '<div id="map" style="#width:100%; height:300px; border:1px solid; padding:5px;top:40px; #top:-10px; display:block;"></div>';
    
        if( Effect.Grow($(divID),{duration:.5}) ){
            $('cover').style.display='block';
            setTimeout(function(){getLonLat(hdn)},2000);
        }
    }
}
//*********************************************************************************
//global divID
divID = "_mostafa";
//*********************************************************************************
//used in catalog
//function setValue( id , oDiv , control ){//alert('catalog');
function setValue(control){//alert(control);alert($(control).value);
    /* //Temp remove
	selectedValue = document.getElementById( "sx" + oDiv + id ).innerHTML;
	document.getElementById( control ).value = selectedValue;
	document.getElementById(oDiv).style.display = 'none';
	*/
	//IE6 fix, show select menu
	//selectList('ctl00_content_PAX_NO','inline');
	
	//prove we've selected from the suggest menu
	////document.getElementById( control + '_suggest' ).value = 1;
	
	//close btn
    //$('_mostafa').innerHTML = '<span style="float:left; padding:20px;" class="text">The range of the <strong>'+$('selected_airjet').value+'</strong> around the airport <strong>'+$(control).value+'</strong> is:</span><a href="#" onclick="Effect.Shrink($(divID));xx();return false;" style="float:right; padding:20px;" class="text">Close</a>';
    $('_mostafa').innerHTML = '<span style="float:left; padding:20px;" class="text">The range of the <strong>'+$('selected_airjet').value+'</strong> around the airport <strong>'+ $(control).value +'</strong> is:</span><a href="#" onclick="Effect.Shrink($(divID));xx();return false;" style="float:right; padding:20px;" class="text">Close</a>';
    
    //append map
    $('_mostafa').innerHTML += '<div id="map" style="#width:100%; height:300px; border:1px solid; padding:5px;top:40px; #top:-10px; display:block;"></div>';
    
    //alert(tempIDArrayFrom.length);
    //alert(tempIDArrayTo.length);
    //saveToTempArray( id , oDiv );
    //alert(tempIDArrayFrom.length);
    //alert(tempIDArrayTo.length);
    
}
//*********************************************************************************
//used in catalog
function saveToTempArray( id , div ){
    //save the JSON ids to call them l8r by this id to get lon , lat...
    //take only last 2
    //alert(div.indexOf('from'));
    if(div.indexOf('from')==0){
        //alert('From');
        //alert(response.airports[id].airport.code);
        //tempIDArrayFrom[0] = new Array( response.airports[id].airport.code , response.airports[id].airport.lon , response.airports[id].airport.lat , response.airports[id].airport.city , response.airports[id].airport.country , response.airports[id].airport.desc );
        tempIDArrayFrom[0] = response.airports[id].airport.code;
        tempIDArrayFrom[1] = response.airports[id].airport.lon;
        tempIDArrayFrom[2] = response.airports[id].airport.lat;
        tempIDArrayFrom[3] = response.airports[id].airport.city;
        tempIDArrayFrom[4] = response.airports[id].airport.country;
        tempIDArrayFrom[5] = response.airports[id].airport.desc;

        //ff = response.airports[id].airport.code+'|'+response.airports[id].airport.lon+'|'+response.airports[id].airport.lat+'|'+response.airports[id].airport.city+'|'+response.airports[id].airport.country+'|'+response.airports[id].airport.desc;
    }else if(div.indexOf('to')==0){
        //alert('To');
        //tempIDArrayTo[0] = new Array( response.airports[id].airport.code , response.airports[id].airport.lon , response.airports[id].airport.lat , response.airports[id].airport.city , response.airports[id].airport.country , response.airports[id].airport.desc );
        tempIDArrayTo[0] = response.airports[id].airport.code;
        tempIDArrayTo[1] = response.airports[id].airport.lon;
        tempIDArrayTo[2] = response.airports[id].airport.lat;
        tempIDArrayTo[3] = response.airports[id].airport.city;
        tempIDArrayTo[4] = response.airports[id].airport.country;
        tempIDArrayTo[5] = response.airports[id].airport.desc;
    }
    
    //alert(tempIDArrayFrom[0]);
    //alert(tempIDArrayFrom[0][1]);
    //alert(tempIDArrayTo.length);
}
//*********************************************************************************

function over( id ){
	document.getElementById(id).style.background = '#76A8DB';
	document.getElementById(id).style.color = '#FFFFFF';
}
function out( id ){
	document.getElementById(id).style.background = '#DDFEE4';
	document.getElementById(id).style.color = '';
}
//IE 6 fix, showHide select lists
function selectList( sList , state ){

    var pax_menu = document.getElementById( sList );

    if (navigator.userAgent.indexOf('MSIE') != -1) {
    
        //alert('IE');
    
        //hide the pax menu for IE 6
        pax_menu.style.display = state;
        
    }

}

//catalog
document.onclick = function () {
	var divs = document.getElementsByTagName('div');
	for (var i=0 ; i < divs.length ; i++ ) {
		if (divs[i].className == 'ac' || divs[i].className == 'ac' ) { divs[i].className = 'ac_no_display'; divs[i].innerHTML = msg; }
		if (divs[i].className == 'ae') { divs[i].className = 'ac'; divs[i].innerHTML = msg; }
	}
	//IE6 fix, show select menu
	//selectList('ctl00_content_PAX_NO','inline');
	if( document.getElementById('cover').style.display == 'block' ) selectListShowHide('none');
	else selectListShowHide('inline');
}
