﻿//// JScript File
var DIV_BG_COLOR = "#ffffff";//"#FFE0C0";
var DIV_HIGHLIGHT_COLOR = "#8888e2";//"#6699FF";
var DIV_FONT = "Arial";
var FONT_COLOR= "#666666";
var DIV_PADDING = "2px";
var DIV_BORDER = "1px solid #CCC";
var queryField;
var stationCodeField="hfStationCode";
var divName;
var ifName;
var lastVal = "";
var val = "";
var globalDiv;
var divFormatted = false;
var specifyFunctionName;
var searchName="";
var cityID=1;//1:北京 2:武汉
var cityName="北京";

function mainLoop()
{
    //val = escape( queryField.value );
    val=escape(searchName);
    if (specifyFunctionName!="GetBusNumBySearches"&&specifyFunctionName!="GetUndergroundStation"&&val.length<2)
        return;
    
    if( lastVal != val )
    {
        //var response = _Default.GetStationBySearches( val );
            
        var response;
        //alert(specifyFunctionName);
        switch (specifyFunctionName)
        {
            case "GetStationBySearches": response=f54u.web.AjaxAllMethod.GetStationBySearches( val,1, 0 );
                break;
            case "GetRelationStationBySearches": response=f54u.web.AjaxAllMethod.GetStationBySearches( val,1, 1 );
                break;                
            case "GetTravelsItems": 
	            var area=document.getElementById("WucHeader1_ddlCity");            
                //alert(area.options[area.selectedIndex].value)
                if (area==null)
                    area=document.getElementById("ddlCity");            
                if (area==null)
                    return; 
                                           
                response=f54u.web.AjaxAllMethod.GetTravelsItems(cityID, area.options[area.selectedIndex].value ,val );
                break;
            case "GetAreaInfo":                
                response=f54u.web.AjaxAllMethod.GetAreaInfo(cityID ,val );
                break;
            case "GetBusNumBySearches":                
                response=f54u.web.AjaxAllMethod.GetBusNumBySearches(val,cityID);
                break;
            case "GetUndergroundStation":                
                response=f54u.web.AjaxAllMethod.GetUndergroundStation(val,cityID);
                break;
                                
        }
        if (specifyFunctionName=="GetTravelsItems")
            showTravelQueryDiv( response.value );
        else        
            showQueryDiv( response.value );
            
        lastVal = val;
    }
    setTimeout('mainLoop()', 100);
    return true;
}  

function InitQueryCode(funName, queryFieldName, hiddenDivName, sName )
{    
    queryField = document.getElementById( queryFieldName );
    queryField.onblur = hideDiv;
    queryField.onkeydown = keypressHandler;
    queryField.autocomplete = "off";
    specifyFunctionName=funName;
    
    if (sName==null||sName=="")
        searchName=queryField.value;
    else
        searchName=sName.substring(0,2);
          
    if( hiddenDivName )
    {
        divName = hiddenDivName;
    }
    else
    {
        divName = "querydiv";
    }
    
    ifName = "queryiframe";
    //mainLoop()
    setTimeout("mainLoop()",100);

}

function getDiv(divID)
{
    //if(!globalDiv)
    {

        if(!document.getElementById(divID))
        {
            var newNode = document.createElement("div");
            newNode.setAttribute("id", divID);
            document.body.appendChild(newNode);
        }
        globalDiv = document.getElementById(divID);
        var x = queryField.offsetLeft;
        var y = queryField.offsetTop + queryField.offsetHeight;
//        var x = (event.clientX - (event.x + 2)) + document.body.scrollLeft;
//        var y = (event.clientY - event.y) + document.body.scrollTop+18;
        var parent = queryField;
        while(parent.offsetParent)
        {
            parent = parent.offsetParent;
            x += parent.offsetLeft;
            y += parent.offsetTop;
        }
        var tt=document.getElementById("inputEnd");

        
        if(!divFormatted)
        {
            globalDiv.style.backgroundColor = DIV_BG_COLOR;
            globalDiv.style.fontFamily = "Arial, Helvetica, sans-serif";
            globalDiv.style.padding = DIV_PADDING;
            globalDiv.style.border = DIV_BORDER;
            globalDiv.style.width = "120px";//100px
            globalDiv.style.fontSize = "8pt";//"90%";
            globalDiv.style.textAlign = "left";
            globalDiv.style.color = FONT_COLOR;
            globalDiv.style.fontWeight= "bold";
            globalDiv.style.position = "absolute";
            //globalDiv.style.left = x + "px";
            globalDiv.style.top = y + "px";
            globalDiv.style.visibility = "hidden";
            globalDiv.style.zIndex = 10000;
            divFormatted = true;
			
        }
        globalDiv.style.left = x + "px";
        globalDiv.style.top = y + "px";
    }

    return globalDiv;
}

function showTravelQueryDiv(resultArray)
{
    var div = getDiv(divName);
    while( div.childNodes.length > 0 )
    {
        div.removeChild(div.childNodes[0]);
    }
    
    if ( resultArray==null|| resultArray.length==0)
    {
        var result = document.createElement("div");
        result.style.cursor = "pointer";
        result.style.padding = "2px 0px 2px 0px";
        result.style.width = div.style.width;//Add width        
        _unhighlightResult(result);
        result.onmousedown = selectResult;
        result.onmouseover = highlightResult;
        result.onmouseout = unhighlightResult;        
        
        var value = document.createElement("span");
        value.className = "value";
        value.style.textAlign = "left";
        value.style.fontWeight = "bold";        
        value.innerHTML = "没有该景点";
        result.appendChild(value);
        div.appendChild(result);        
        showDiv(true);
        return;
    }
    
    for(var i = 0; i < resultArray.length; i++)
    {
        var result = document.createElement("div");
        result.style.cursor = "pointer";
        result.style.padding = "2px 0px 2px 0px";
        result.style.width = div.style.width;//Add width        
        _unhighlightResult(result);
        result.onmousedown = selectResult;
        result.onmouseover = highlightResult;
        result.onmouseout = unhighlightResult;        
        
        var value = document.createElement("span");
        value.className = "value";
        value.style.textAlign = "left";
        value.style.fontWeight = "bold";        
        value.innerHTML = resultArray[i];
        result.appendChild(value);
        div.appendChild(result);        
    }
    showDiv(resultArray.length > 0);
}

function showQueryDiv(resultArray)
{
    var div = getDiv(divName);
    while( div.childNodes.length > 0 )
    {
        div.removeChild(div.childNodes[0]);
    }
    
    if ( resultArray==null|| resultArray.length==0)
    {
        var result = document.createElement("div");
        result.style.cursor = "pointer";
        result.style.padding = "2px 0px 2px 0px";
        result.style.width = div.style.width;//Add width        
        _unhighlightResult(result);
        result.onmousedown = selectResult;
        result.onmouseover = highlightResult;
        result.onmouseout = unhighlightResult;        
        
        var value = document.createElement("span");
        value.className = "value";
        value.style.textAlign = "left";
        value.style.fontWeight = "bold";        
        value.innerHTML = "没有该站点";
        result.appendChild(value);
        div.appendChild(result);        
        showDiv(true);
        return;
    }
    
    for(var i = 0; i < resultArray.length; i++)
    {
        var result = document.createElement("div");
        var arrTemp=resultArray[i].split(',');
        
        result.style.cursor = "pointer";
        result.style.padding = "2px 0px 2px 0px";
        result.style.width = div.style.width;//Add width        
        _unhighlightResult(result);
        result.onmousedown = selectResult;
        result.onmouseover = highlightResult;
        result.onmouseout = unhighlightResult;        
        
        var value = document.createElement("span");
        value.className = "value";
        value.style.textAlign = "left";
        value.style.fontWeight = "bold";        
        value.innerHTML = arrTemp[0];//resultArray[i];
        value.title=arrTemp[1];
        result.appendChild(value);
        div.appendChild(result);        
    }
    showDiv(resultArray.length > 0);
}

function selectResult()
{
    _selectResult(this);
}
function _selectResult( item )
{
    var spans = item.getElementsByTagName("span");
    var scode=document.getElementById(stationCodeField);
    if( spans )
    {
        for(var i = 0; i < spans.length; i++)
        {
            if( spans[i].className == "value" )
            {
                queryField.value = spans[i].innerHTML;
                if (scode!=null)
                    scode.value=spans[i].title;
                lastVar = val = escape( queryField.value );
                mainLoop();
                queryField.focus();
                showDiv( false );
                return;
            }
        }
    }
}

function highlightResult()
{
    _highlightResult( this );    
}

function _highlightResult( item )
{
    //item.style.backgroundColor = DIV_HIGHLIGHT_COLOR;
    item.style.color =DIV_HIGHLIGHT_COLOR;
}

function unhighlightResult()
{
    _unhighlightResult( this );
}

function _unhighlightResult( item )
{
    //item.style.backgroundColor = DIV_BG_COLOR;
    item.style.color = FONT_COLOR;
}

function showDiv( show )
{
    var div = getDiv( divName );
    if( show )
    {
        div.style.visibility = "visible";
    }
    else
    {
        div.style.visibility = "hidden";
    }
    adjustiFrame();
}

function hideDiv()
{
    showDiv( false );
}

function keypressHandler(evt)
{
    var div = getDiv( divName );
    if( div.style.visibility == "hidden" )
    {
        return true;
    }
    if( !evt && window.event )
    {
        evt = window.event;
    }
    var key = evt.keyCode;
    
    var KEYUP = 38;
    var KEYDOWN = 40;
    var KEYENTER = 13;
    var KEYTAB = 9;
    if(( key != KEYUP ) && ( key != KEYDOWN ) && ( key != KEYENTER ) && ( key != KEYTAB ))
    {
        return true;
    }
    var selNum = getSelectedSpanNum( div );
    var selSpan = setSelectedSpan( div, selNum );
    if( key == KEYENTER || key == KEYTAB )
    {
        if( selSpan )
        {
            _selectResult(selSpan);
        }
        evt.cancelBubble= true;
        return false;
    }    
    else
    {
        if( key == KEYUP)
        {
            selSpan = setSelectedSpan( div, selNum - 1 );           
        }
        if( key == KEYDOWN )
        {
            selSpan = setSelectedSpan( div, selNum + 1 );
        }
        if( selSpan )
        {
            _highlightResult( selSpan );
        }
    }
    showDiv( true );
    return true;
}

function getSelectedSpanNum( div )
{
    var count = -1;
    var spans = div.getElementsByTagName("div");
    if( spans )
    {
        for( var i = 0; i < spans.length; i++)
        {
            count++;
            //if( spans[i].style.backgroundColor != div.style.backgroundColor )
            if( spans[i].style.color != div.style.color )
            {
                return count;
            }
        }
    }
    return -1;
}
function setSelectedSpan( div, spanNum )
{
    var count = -1;
    var thisDiv;
    var divs = div.getElementsByTagName("div");
    if( divs )
    {
        for( var i = 0; i < divs.length; i++ )
        {
            if( ++count == spanNum )
            {
                _highlightResult( divs[i] );
                thisDiv = divs[i];
            }
            else
            {
                _unhighlightResult( divs[i] );
            }
        }        
    }
    return thisDiv;
}

function adjustiFrame()
{
    if(!document.getElementById(ifName))
    {
        var newNode = document.createElement("iFrame");
        newNode.setAttribute("id", ifName);
        newNode.setAttribute("src","javascript:false;");
        newNode.setAttribute("scrolling","no");
        newNode.setAttribute("frameborder","0");
        document.body.appendChild( newNode );		
    }
    iFrameDiv = document.getElementById( ifName );
    var div = getDiv( divName );    
    try
    {
        iFrameDiv.style.position = "absolute";        
        iFrameDiv.style.width = div.offsetWidth;
        iFrameDiv.style.height = div.offsetHeight;
        iFrameDiv.style.top = div.style.top;
        iFrameDiv.style.left = div.style.left;
        iFrameDiv.style.zIndex = div.style.zIndex - 1;
        iFrameDiv.style.visibility = div.style.visibility;           
    }
    catch (e)
    {}
}
