﻿
//temp arrays
var tmpCode = new Array();
var tmpLat = new Array();
var tmpLon = new Array();
var tmpCity = new Array();
var tmpCountry = new Array();
var tempArrayFromAdvancedSearch = new Array();
//***************************************************************
//send the ajax request
function searchAJAX( inputID ) {

    var code = document.getElementById( inputID );
    
    if( code.value == '' ){
        
        //show green label
        document.getElementById( 'searching_ac' ).innerHTML = 'Enter city name...';
        //disable btn
        document.getElementById( 'asrch' ).disabled = true;    
        
    }else{
    
        var asearch_loading = document.getElementById( inputID + '_loading' );

        asearch_loading.className = 'show';
        
        http.open( 'get', 'extendedSearch.aspx?c=' + code.value , false );
        http.onreadystatechange = function (){ handleResponseExtendedSearch( inputID ) };
	    http.send( null );
	
	}

}
//***************************************************************
//handle the ajax response
function handleResponseExtendedSearch( id ){
	if(http.readyState == 4 && http.status == 200){
        
        var response = http.responseText;
        //alert(response);
        
        //empty temp arrays
        tmpCode.length = 0;
        tmpLat.length = 0;
        tmpLon.length = 0;
        tmpCity.length = 0;
        tmpCountry.length = 0;

        if( response ) {
        
            //will split and pack arrays
            var mResponse = response.split("|");
            
            //empty the div b4 load any new data inside it
	        document.getElementById( id + '_ac' ).innerHTML = '';
	        //show the results div if it's hidden
	        document.getElementById( id + '_ac' ).style.display = 'block';
	        
	        
            var asearch_loading = document.getElementById( id + '_loading' );
            asearch_loading.className = 'hide';
    
            //for( i=0 ; i<mResponse.length ; i++){
            for( i=0 ; i<5 ; i++){
            
                //alert(mResponse[i]);
                item_ = mResponse[i].split("@");
                
                tmpCode[i] = item_[0];
                tmpLat[i] = item_[1];
                tmpLon[i] = item_[2];
                tmpCity[i] = item_[3];
                tmpCountry[i] = item_[4];
                
                //alert(tmpCode[i]);alert(tmpLat[i]);alert(tmpLon[i]);alert(tmpCity[i]);alert(tmpCountry[i]);
                //alert(tmpCode.length);
                
                writeASearchContent( id + '_ac' , i , id , tmpCode[i] , tmpCity[i] );
                                                
            }
		}
	}
}
//***************************************************************
function writeASearchContent( oDiv , i , control , code , content ){

	var div_ = document.getElementById( oDiv );
	//change class to green
	div_.className = 'ac';
	
	//set overflow
	//div_.style.overflow = 'visible';
	
	//write new entry in the results div
	div_.innerHTML += "<div id='x"+oDiv+i+"' onmouseover='over(this.id);' onmouseout='out(this.id);' style='width: 100%; cursor: pointer; height: 20px;' onclick=setValue("+i+",'"+oDiv+"','"+control+"',1)>" + content.slice(0,15) + " - [<span id='sx"+oDiv+i+"'>" + code + "</span>] " + "</div>";

}
//***************************************************************
//save the selected value data to the tempArrayFromAdvancedSearch
function onGo(){
    // click ok
    // save the data associated with the selected value to the temp array
    // set hidden value to 1 for advanced search
    // hide layers
    
    //get the hidden id
    var assi = document.getElementById( 'asearch_selected_id' );
	//alert( assi.value );
	
	//damn manual, i couldnt find a pattern after thinking for 5 hrs !!!
	switch( field ){
	    case "from_one_leg":
	        //alert('f');
	        var from_one_leg_temp_arr = [];
	        from_one_leg_temp_arr[0] = tmpCode[assi.value];
            from_one_leg_temp_arr[1] = tmpLat[assi.value];
            from_one_leg_temp_arr[2] = tmpLon[assi.value];
            from_one_leg_temp_arr[3] = tmpCity[assi.value];
            from_one_leg_temp_arr[4] = tmpCountry[assi.value];
	        //alert(from_one_leg_temp_arr[0]);
	        //set hidden to 1 for adv. search
	        document.getElementById( 'from_one_leg_as' ).value = 1;
	        //set value to parent textbox
	        document.getElementById( field ).value = from_one_leg_temp_arr[0];
            //hide green label from parent control
            document.getElementById( 'from_one_leg_ac' ).style.display = 'none';
	        break;
	    case "to_one_leg":
	        //alert('t');
	        var to_one_leg_temp_arr = [];
	        to_one_leg_temp_arr[0] = tmpCode[assi.value];
            to_one_leg_temp_arr[1] = tmpLat[assi.value];
            to_one_leg_temp_arr[2] = tmpLon[assi.value];
            to_one_leg_temp_arr[3] = tmpCity[assi.value];
            to_one_leg_temp_arr[4] = tmpCountry[assi.value];
	        //set hidden to 1 for adv. search
	        document.getElementById( 'to_one_leg_as' ).value = 1;
	        //set value to parent textbox
	        document.getElementById( field ).value = to_one_leg_temp_arr[0];
            //hide green label from parent control
            document.getElementById( 'to_one_leg_ac' ).style.display = 'none';
	        break;
	    case "from_round_trip":
	        //alert('frt');
	        var from_round_trip_temp_arr = [];
	        from_round_trip_temp_arr[0] = tmpCode[assi.value];
            from_round_trip_temp_arr[1] = tmpLat[assi.value];
            from_round_trip_temp_arr[2] = tmpLon[assi.value];
            from_round_trip_temp_arr[3] = tmpCity[assi.value];
            from_round_trip_temp_arr[4] = tmpCountry[assi.value];
	        //set hidden to 1 for adv. search
	        document.getElementById( 'from_round_trip_as' ).value = 1;
	        //set value to parent textbox
	        document.getElementById( field ).value = from_round_trip_temp_arr[0];
            //hide green label from parent control
            document.getElementById( 'from_round_trip_ac' ).style.display = 'none';
	        break;
	    case "to_round_trip":
	        //alert('trt');
	        var to_round_trip_temp_arr = [];
	        to_round_trip_temp_arr[0] = tmpCode[assi.value];
            to_round_trip_temp_arr[1] = tmpLat[assi.value];
            to_round_trip_temp_arr[2] = tmpLon[assi.value];
            to_round_trip_temp_arr[3] = tmpCity[assi.value];
            to_round_trip_temp_arr[4] = tmpCountry[assi.value];
	        //set hidden to 1 for adv. search
	        document.getElementById( 'to_round_trip_as' ).value = 1;
	        //set value to parent textbox
	        document.getElementById( field ).value = to_round_trip_temp_arr[0];
            //hide green label from parent control
            document.getElementById( 'to_round_trip_ac' ).style.display = 'none';
	        break;
	    case "from1":
	        //alert('f1');
	        var from1_temp_arr = [];
	        from1_temp_arr[0] = tmpCode[assi.value];
            from1_temp_arr[1] = tmpLat[assi.value];
            from1_temp_arr[2] = tmpLon[assi.value];
            from1_temp_arr[3] = tmpCity[assi.value];
            from1_temp_arr[4] = tmpCountry[assi.value];
	        //set hidden to 1 for adv. search
	        document.getElementById( 'from1_as' ).value = 1;
	        //set value to parent textbox
	        document.getElementById( field ).value = from1_temp_arr[0];
            //hide green label from parent control
            document.getElementById( 'from1_ac' ).style.display = 'none';
	        break;
	    case "to1":
	        //alert('t1');
	        var to1_temp_arr = [];
	        to1_temp_arr[0] = tmpCode[assi.value];
            to1_temp_arr[1] = tmpLat[assi.value];
            to1_temp_arr[2] = tmpLon[assi.value];
            to1_temp_arr[3] = tmpCity[assi.value];
            to1_temp_arr[4] = tmpCountry[assi.value];
	        //set hidden to 1 for adv. search
	        document.getElementById( 'to1_as' ).value = 1;
	        //set value to parent textbox
	        document.getElementById( field ).value = to1_temp_arr[0];
            //hide green label from parent control
            document.getElementById( 'to1_ac' ).style.display = 'none';
	        break;
	}
	//hide layers
	trans_div = document.getElementById( 'container2' );
    adv_srch = document.getElementById( 'advanced_search' );
    //cbg = document.getElementById( 'cbg2' );
    
    adv_srch.style.display = 'none';
    trans_div.style.display = 'none';
    //cbg.style.display = 'none';
        
    //offfffffffffffffffffffff
    //this shit function without pattern
    //shit
}
//***************************************************************
var field; //global
function openAdvancedSearch( inputID ){
        
    trans_div = document.getElementById( 'container2' );
    adv_srch = document.getElementById( 'advanced_search' );
    //ccontent_div = document.getElementById('cbg2');
    
    trans_div.style.display = 'block';
    adv_srch.style.display = 'block';
    //alert(document.getElementById('ccontent').style.display);
	//ccontent_div.style.display = 'block';
    //alert(document.getElementById('ccontent').style.display);
        
    //alert( inputID.slice(0,inputID.length-3) );
    field = inputID.slice( 0 , inputID.length-3 ); //from_one_leg[_ac]
    
    //disable btn
    document.getElementById( 'asrch' ).disabled = true;
    //empty textbox
    document.getElementById( 'searching' ).value = '';
    //show green label
    document.getElementById( 'searching_ac' ).style.display = 'block';
}
//***************************************************************
