﻿var trgtelem;
var tempairportcodelist;
var msgdivid ;
var finalairportlist = new Array ();
var isCityListClicked = false;
var isTextSelected = false;
function smartSearchList(trgt,citylist,airportcodelist) { 

if (document.getElementById('airportcity_div_table')){ document.body.removeChild(document.getElementById('airportcity_div_table')); }
     if (citylist == null){citylist = new Array();}
        tempairportcodelist=airportcodelist;
        trgtelem=trgt;
        trgtelem.onselect = function() { isTextSelected = true; removeSmartOpt(); isTextSelected = false;};
        var airportcodeselectOption = $get("DHTMLSelAirPortCity");
        
        if ((airportcodeselectOption.style.display == "none") || (airportcodeselectOption.trgt != trgt)) {
        if (document.getElementById('message_div')){ document.body.removeChild(document.getElementById('message_div')); }
           
		    messageDiv = document.createElement('div');
		   
		    messageDiv.style.overflow="auto";
		    messageDiv.style.height='60px';
		    messageDiv.style.zIndex=100005;
		    messageDiv.id = 'message_div';
		    messageDiv.style.position='absolute';
		    messageDiv.style.textAlign="left"
		    messageDiv.style.top = eval(curTop(trgt) + trgt.offsetHeight) + "px";
		    messageDiv.style.width=(trgt.offsetWidth + 10) + "px";
		    messageDiv.style.left = curLeft(trgt) + "px";
		    messageDiv.style.border ="1px solid #AAAAAA";
		    messageDiv.style.display = "block"; 
		    if(citylist.length != 0)
		    {
            var resourceString = RestoolClientSideSmartSearchMessage.SmartSearchMessage;
            }
            else
            {
            var resourceString = RestoolClientSideSmartSearchMessage.SmartSearchMessageNoResult;
            messageDiv.style.height='30px';
            }
		    document.body.appendChild(messageDiv);
		    
		    msgdivid = $get("message_div")
            msgdivid.className ="actbHColor";
            resourceString = resourceString.replace ("XX",eval(citylist.length));
            resourceString = resourceString.replace ("<city>",trgt.value);
            msgdivid.innerHTML = resourceString;
            
		    if (citylist.length != 0){   		
    		d = document.createElement('div');
    		d.style.overflow="auto";
		    d.style.height='200px';
		    d.id = 'airportcity_div_table';
		    d.style.position='absolute';
		    d.style.top = eval(curTop(trgt) + trgt.offsetHeight + 60) + "px";
		    d.style.left = curLeft(trgt) + "px";
		    document.body.appendChild(d);  		
		    a = document.createElement('table');
		    a.cellSpacing='1px';
		    a.cellPadding='2px';
		    a.className="actbBgColor";
		    a.style.width=(trgt.offsetWidth + 10) + "px";
		    a.style.textAlign="left";
		    a.style.overflow='auto';
		    a.style.border ="1px solid #AAAAAA";
		    a.id = 'airportcity_table';
		    
		    d.appendChild(a);
           
             for (var i = 0; i < citylist.length; i++){            
            
               r = a.insertRow(-1);
               c = r.insertCell(-1);
		       c.className="actbTextColor";
		       c.className="actbFFamily";
		       c.id = 'tat_td'+(i);
		       c.className="actbFSize";
		       c.setAttribute('pos',i);
               c.innerHTML = citylist[i];//.LD ;
		       c.style.cursor='pointer';
		       c.style.cursor='hand';
		       c.onmouseover = airportCityHighlightRow;		      
		       c.onclick = function() {isCityListClicked = true;};
		       c.onmouseout = function() {isCityListClicked = false;};
		     }
            airportcodeselectOption = $get("airportcity_div_table");
            airportcodeselectOption.style.display = "block"; 
            }
            document.onclick = removeSmartOpt;  
         }
}

function smartSearchAirPortCode(airportcodelist,loc) { 

if (document.getElementById('airportcode_div_table')){ document.body.removeChild(document.getElementById('airportcode_div_table')); }
     if (airportcodelist == null){airportcodelist = new Array();}
     if(airportcodelist.length != 0){
        var selectOptionAirPortCode = $get("DHTMLSelAirPortCode");
        if ((selectOptionAirPortCode.style.display == "none") || (selectOptionAirPortCode.trgtelem != trgtelem)) {
		    d = document.createElement('div');
		    d.style.overflow="auto";
		    d.style.height='200px';
		    d.id = 'airportcode_div_table';
		    d.style.position='absolute';
		    
            d.style.top = eval( $get("airportcity_div_table").offsetTop + $get("tat_td" + loc).offsetTop - $get("airportcity_div_table").scrollTop) + "px"; 
		    d.style.width=(trgtelem.offsetWidth + 10) + "px";
		    d.style.left = eval(curLeft(trgtelem)+ $get("airportcity_div_table").offsetWidth)+ "px";

		    document.body.appendChild(d);    		
		    a = document.createElement('table');
		    a.cellSpacing='1px';
		    a.cellPadding='2px';
		    a.className="actbBgColor";
		    a.style.width=(trgtelem.offsetWidth + 10) + "px";
		    a.style.textAlign="left";
		    a.style.overflow='auto';
		    a.style.border ="1px solid #AAAAAA";
		    a.id = 'airportcode_table';
		    
		    d.appendChild(a);
           
            finalairportlist = airportcodelist[loc];
            for (var i = 0; i < finalairportlist.length; i++){
               r = a.insertRow(-1);
               c = r.insertCell(-1);
		       c.className="actbTextColor";
		       c.className="actbFFamily";
		       c.className="actbFSize";
    	       c.onmousedown= airportSetValue;
    	       c.innerHTML = finalairportlist[i];
		       c.style.cursor='pointer';
		       c.style.cursor='hand';
		       
		       c.onmouseover = airportCodeHighlightRow;
		       c.onmouseout = airportCodeRemovehighlightRow; 
		       
		   }
            selectOptionAirPortCode = $get("airportcode_div_table");
           
         }
         selectOptionAirPortCode.style.display = "block"; 
         
         }
     }
 
function airportCodeHighlightRow(){
    this.className="actbHColor";
}
    
function airportCodeRemovehighlightRow(){
   this.className="actbBgColor";
}
    
function airportCityHighlightRow(){
    for (var i=0;i< $get("airportcity_table").rows.length;i++)
    {
       $get("tat_td" + i).className = "actbBgColor";
    }
    this.className="actbHColor";
    removeEvent(document,"mousedown",removeSmartOpt);
    smartSearchAirPortCode(tempairportcodelist,this.getAttribute('pos'));
}

function airportSetValue(){
    isCityListClicked = false;
    trgtelem.value=this.innerHTML;
    removeSmartOpt();
}
    
function removeSmartOpt(){ 

    if(isCityListClicked != true || isTextSelected == true)
    {
    msgdivid.style.display = "none";
    if (document.getElementById('airportcity_div_table')){ 
        document.body.removeChild(document.getElementById('airportcity_div_table'));
        }
    if (document.getElementById('airportcode_div_table')){
        document.body.removeChild(document.getElementById('airportcode_div_table'));
        }
    document.onclick = null;
    }
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();