﻿
//create the ajax obj
function createRequestObject() {

    var req;

    if(window.XMLHttpRequest){

        // Firefox, Safari, Opera...
        req = new XMLHttpRequest();

    } else if(window.ActiveXObject) {

        // Internet Explorer 5+
        req = new ActiveXObject("Microsoft.XMLHTTP");

    } else {

        // There is an error creating the object,
        // just as an old browser is being used.
        alert('Problem creating the XMLHttpRequest object');

    }

    return req;

}

// Make the XMLHttpRequest object
var http = createRequestObject();
//***************************************************************
//send the ajax request
function sendRequest( inputID , homepage_or_auctions ) {

    var code = document.getElementById( inputID ).value;
    var nearby = document.getElementById( inputID + '_nearby' );
    //var nearby_loading = document.getElementById( inputID + '_nearby_loading' );

    nearby.className = 'hide';
    //nearby_loading.className = 'show';
    
    //alert(code);
    http.open( 'get', 'extendedSearch.aspx?c=' + code , true );
    http.onreadystatechange = function (){ handleResponseNearbyAirports( inputID , homepage_or_auctions ) };
	http.send( null );

}
//***************************************************************
//hide the loading image [specially onError]
function sLoading( id , state ){
    if( state == 1 ) $( id + "_loading" ).className = 'show';
    else{
        $( id ).className = 'show';
        $( id + "_loading" ).className = 'hide';
    }
}
//***************************************************************
//handle the ajax response
function handleResponseNearbyAirports( id , homepage_or_auctions ){
	if(http.readyState == 4 && http.status == 200){
        
        var response = http.responseText;
        //alert(response);
        
        if( response != 0 ) {
            //reverse the visual effects
            var textboxValue = document.getElementById( id );
            var nearby = document.getElementById( id + '_nearby' );
            var nearby_loading = document.getElementById( id + '_nearby_loading' );
            
            if(! homepage_or_auctions ){
                //show the maps container and close btn
                var vmaps_container = document.getElementById('vmaps_container');
	            var close_ = document.getElementById('vmaps_container_close');
	            vmaps_container.style.display = 'block';
	            close_.style.display = 'block';
            }
            
            nearby.className = 'show';
            nearby_loading.className = 'hide';
            
			airportCodes_ = response.split('|');
			
			//define empty array to hold the found values
			var valAll = [];
			
			//pack the 1st value
			//the main point used in the map [the center point]
			//update : will not search xml from now on, everything is server-side
			//valAll[0] = searchXMLForNearbyAirports( textboxValue.value );
			//alert(response);
			//alert(response[0]);
			//alert(airportCodes_[0]);
			valAll[0] = airportCodes_[0].replace(/@/ig,'|');
			//alert(valAll[0]);
			
			for( i=0,m=1 ; i<airportCodes_.length ; i++,m++ ){
			    
			    aC = airportCodes_[i];
			    
			    //replace @ in the response value with |
			    val = aC.replace(/@/ig,"|");
			    
			    //search the xml file and return all the data for each code
			    //val = searchXMLForNearbyAirports( aC );
			    
			    //combine all values
			    valAll[m] = val;
			}
			
			//load map
			init_map_for_nearby_airports( valAll );
		}else{
		    sLoading( id + "_nearby" , 2 );
		    alert("No airports found!");
		}
	}
}
//***************************************************************
//NOT used
//search the xml using the returned values from the back-end
//function searchXMLForNearbyAirports( aCode ){
//   
//    //alert( aCode );
//   
//    //trim and upperCase
//    var aCodePrepared = trim( aCode.toUpperCase() );
//    
//	//load all airport nodes from the xml
//	var allAirportItems = xmlDoc.getElementsByTagName("airport");
//	
//    //search
//    for ( j=0 ; j<allAirportItems.length ; j++ ) {
//        
//        //get attribute
//        var attributePrepared = trim( allAirportItems[j].getAttribute('code').toUpperCase() );
//        
//        //if found, load the airport data
//        if( attributePrepared == aCodePrepared ){
//            
//            cityName__ = xmlDoc.getElementsByTagName("city")[j].childNodes[0].nodeValue;
//            desc__ = xmlDoc.getElementsByTagName("desc")[j].childNodes[0].nodeValue;
//            lon__ = xmlDoc.getElementsByTagName("lon")[j].childNodes[0].nodeValue;
//            lat__ = xmlDoc.getElementsByTagName("lat")[j].childNodes[0].nodeValue;
//            country__ = xmlDoc.getElementsByTagName("country")[j].childNodes[0].nodeValue;
//            
//        }
//        
//    }
//    
//    return cityName__ + '|' + desc__ + '|' + lon__ + '|' + lat__ + '|' + country__;

//}
//***************************************************************

