﻿
//var txtBx;
//var targetDiv;
var method = 'get';
var URL = 'WebForm2.aspx';
var params = '?cityCode=';
var loadingIcon;
var response;
//##############################################################
//send the textbox value if it exceeds 3 letters
function searchXML2( src , target ){
    
    //reset the textbox flags to 0
    //alert(src);
    $( src + '_suggest' ).value = 0;
    
    //searchBox
    var txtBx = $( src );
    //identify result div
    var targetDiv = target;
    
    //more than 3 letters
    if( txtBx.value.length > 2 ){
    
        //send request
        getAirports( src , target );
        //alert(txtBx.value);
        
    }
    
}
//##############################################################
//send the ajax request
function getAirports( src , target ){

    var code = $(src).value;
    
    loadingIcon = $( src + '_nearby_loading' );
    loadingIcon.className = 'show';
    
    http.open( method , URL + params + code , true );
    http.onreadystatechange = function (){ handleResponseGetAirports( src , target ) };
	http.send( null );

}
//##############################################################
//function jeval(str){return eval('(' +  str + ')')} //var response = jeval(http.responseText);
//##############################################################
function trimText(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function handleResponseGetAirports( src ,  target ){

    if(http.readyState == 4 && http.status == 200){
        
        //response is JSON-enabled object
        start = http.responseText;
        if( start == '' ){
            response = '';
        }else{
            response = eval('('+start+')');
        }
        
        if( response != '' ) {
            
            //hide select controls
            selectListShowHide('none');
            
            var len = response.airports.length;
            
            //hide the loading img
            loadingIcon.className = 'hide';
            
            //empty div
            $( target ).innerHTML = '';
            //change class to green
	        $( target ).className = 'ac';
	        $( target ).style.display = 'block';
            
            for( i=0 ; i < len ; i++ ){
            
                
                _code = response.airports[i].airport.code;
                _lon = response.airports[i].airport.lon;
                _lat = response.airports[i].airport.lat;
                _city = response.airports[i].airport.city;
                _country = response.airports[i].airport.country;
                _desc = response.airports[i].airport.desc;
                _an = response.airports[i].airport.airport_name;
                
                //slicing
                //if( _an.length > 15 ){
                 //   _an = _an.slice(0,15) + "...";
                //}
                
                //more trimmimg
                longDataString = trimText(_city) + trimText(_country) + trimText(_code) + trimText(_an);
                constant = 35;
                difference = 0;
                //alert(longDataString);
                //alert(longDataString.length);
                if( longDataString.length > constant ){
                    difference = longDataString.length - constant;
                }
                
                if( src.indexOf('from')==0){//is from
                //alert('from out');
                
                    if( src.indexOf('from_ent')==0){//is ent
                    
                    ///////////////////////////////////////////////////////
                    ///////////////////////////////////////////////////////
                    //         aircraft catalog suggest with map         //
                    ///////////////////////////////////////////////////////
                    ///////////////////////////////////////////////////////
                    
                        //alert('checkairjet');//alert(src);
                        //JSON processing
                        //write new entry in the results div
	                    $( target ).innerHTML += "<div id='x"+target+i+"' onmouseover='over(this.id);' onmouseout='out(this.id);' style='width: 100%; cursor: pointer; height: 20px;' onclick=checkAirJet("+i+",'"+target+"','"+src+"','',"+_lat+","+_lon+");return false;>" + changeCase(_city) + ", " + changeCase(_country) + " <strong>[<span id='sx"+target+i+"'>" + _code + "</span>]</strong> " + ' - ' +  changeCase(_an.slice(0,_an.length-difference)) + "</div>";
	                    
	                        //$("fixedtipdiv").innerHTML = _an + '<br />' + _city + '<br />' + _country;     
	                    
                    }else{
                        //alert('setValue');
                    ///////////////////////////////////////////////////////
                    ///////////////////////////////////////////////////////
                    //                   homepage                        //
                    ///////////////////////////////////////////////////////
                    ///////////////////////////////////////////////////////
                    
                        
                        
                        //JSON processing
                        //write new entry in the results div
	                    $( target ).innerHTML += "<div id='x"+target+i+"' onmouseover='over(this.id);' onmouseout='out(this.id);' style='width: 100%; cursor: pointer; height: 20px;' onclick=setValue("+i+",'"+target+"','"+src+"','',"+_lat+","+_lon+");return false;>" + changeCase(_city) + ", " + changeCase(_country) + " <strong>[<span id='sx"+target+i+"'>" + _code + "</span>]</strong> " + ' - ' +  changeCase(_an.slice(0,_an.length-difference)) + "</div>";
	                 //alert(changeCase(_an.slice(0,_an.length-difference)));   
	                 //alert(_an.length);   
	                    //oneLeg_roundTrip.push( _lat , _lon );
	                    //$("fixedtipdiv").innerHTML = _an + '<br />' + _city + '<br />' + _country;   
	                    
                    }
                    
                }else{
                    ///////////////////////////////////////////////////////
                    ///////////////////////////////////////////////////////
                    //               catalog - to textbox                //
                    ///////////////////////////////////////////////////////
                    ///////////////////////////////////////////////////////
                    //JSON processing
                    //write new entry in the results div
	                $( target ).innerHTML += "<div id='x"+target+i+"' onmouseover='over(this.id);' onmouseout='out(this.id);' style='width: 100%; cursor: pointer; height: 20px;' onclick=setValue("+i+",'"+target+"','"+src+"','',"+_lat+","+_lon+");return false;>" + changeCase(_city) + ", " + changeCase(_country) + " <strong>[<span id='sx"+target+i+"'>" + _code + "</span>]</strong> " + ' - ' +  changeCase(_an.slice(0,_an.length-difference)) + "</div>";
	             
	                //$("fixedtipdiv").innerHTML = _an + '<br />' + _city + '<br />' + _country;     
	                
                }
	            
            }
            
        }else{
            //hide the loading img
            loadingIcon.className = 'hide';
            
            //remove existing results
            //$( target ).innerHTML = 'Enter city name...';
            
            alert('No airports found!');
        }
    }
    
}
//##############################################################
//Initial Caps
function changeCase(frmObj) {
    var index;
    var tmpStr;
    var tmpChar;
    var preString;
    var postString;
    var strlen;
    
    //tmpStr = frmObj.value.toLowerCase();
    tmpStr = frmObj.toLowerCase();
    strLen = tmpStr.length;
    
    if (strLen > 0)  {
        for (index = 0; index < strLen; index++)  {
            if (index == 0)  {
                tmpChar = tmpStr.substring(0,1).toUpperCase();
                postString = tmpStr.substring(1,strLen);
                tmpStr = tmpChar + postString;
            }else {
                tmpChar = tmpStr.substring(index, index+1);
                if (tmpChar == " " && index < (strLen-1))  {
                    tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
                    preString = tmpStr.substring(0, index+1);
                    postString = tmpStr.substring(index+2,strLen);
                    tmpStr = preString + tmpChar + postString;
                }
            }
        }
    }
    //frmObj.value = tmpStr;
    return tmpStr;
}
//##################################################################