/******************************************************************************** 
** 作者： 刘冰清
** 创始时间：2007-11-1 
** 描述： 用于Gov Usr两个模块的首页中地标查询框的JS 
** 修改人：
** 修改时间：
** 描述： 
**    修改了… 
*********************************************************************************/ 

var txtValue = "";
var ddlValue="";
var txtOldValue = "";
var txtCurrentValue = "";
var count=0;
var rowNum = 0;
var hasChange = false;
var hasMouseDown = false;
var hasMouseMove = false;
function txtKeyUp(txtObj,ddlObj)//当在文本框按下键并起来的时候   txtObj未申明
{
    //首先清空上一次内容
     var hidObj = document.getElementById(txtObj.id.replace('txt','hid'));
    //document.all.divTemp.style.display="none";  
    
    if(ddlObj=='ddlCityNameStart')
    {
        ddlValue=document.getElementById("ddlCityNameStart").value;
    }
    else
    {
        ddlValue=document.getElementById("ddlCityNameEnd").value;
    }
    
	if(event.keyCode==40)
	{
		if(divTemp.hasChildNodes()&&divTemp.childNodes[0].childNodes[0])
		{
			if(hasChange){getSearchContent(txtValue,ddlValue,txtObj);hasChange = false;};
			nextData(txtObj);
		}
	}
	else if(event.keyCode==38)
	{
		if(divTemp.hasChildNodes()&&divTemp.childNodes[0].childNodes[0])
		{
			if(hasChange){getSearchContent(txtValue,ddlValue,txtObj);hasChange = false;};
			previousData(txtObj);
		}		
	}
    else if(event.keyCode==13)
    {
        document.all.divTemp.style.display="none";
        	if(document.all.radPublic.checked!=true)
	   document.getElementById("dropVehicleType").style.display="block";
    }
	else
	{
	    
		txtCurrentValue = txtObj.value;
		txtValue =txtObj.value; 
		
		if(txtCurrentValue != txtOldValue)
		{
		    hidObj.value="";
			txtOldValue = txtObj.value;	
			if(txtValue!=""){
			    getSearchContent(txtValue,ddlValue,txtObj);
			}
			rowNum = 0;
		}
	}
	
}
function  startSearch(poiNames,txtObj)//开始匹配数据
{
	var obj = document.all.divTemp;
	obj.style.top = txtObj.getBoundingClientRect().bottom-2+document.body.scrollTop;
	obj.style.left = txtObj.getBoundingClientRect().left+document.documentElement.scrollLeft;

//	obj.style.width = txtObj.offsetWidth;
	obj.style.width = 300;
	document.getElementById("dropVehicleType").style.display="none";
	var arrNames =poiNames;
    	
    htmStr = "<table border='0' width='100%' align='right' valign='top' cellpadding='1' cellspacing='0'>";
	for(var i=0;i<arrNames.length;i++)
	{
	    var poiNames=arrNames[i].split('^');
	    htmStr += "<tr onMouseOver='tdMouseOver(this)' height='23px'  id='gtr"+i+"'  onMouseDown='evaluateToText(this,\""+txtObj.id+"\")' onMouseOut='tdMouseOut(this)'>";   
//	    if(poiNames[0].length>10)
//	    {
//		    htmStr+="<td title='"+poiNames[0]+"' style='font-size:12px;color:#000000'>"+poiNames[0].substring(0,8)+"……&nbsp;&nbsp;&nbsp;</td><td style='font-size:12px;color:#000000;' align='right'>查询"+poiNames[1]+"次</td></tr>";
//	    }
//	    else
//	    {
		    htmStr+="<td title='"+poiNames[0]+"' style='font-size:12px;color:#000000;'><nobr>"+poiNames[0]+"&nbsp;&nbsp;&nbsp;</nobr></td><td style='font-size:12px;color:#000000' align='right'><nobr>查询"+poiNames[1]+"次</nobr></td></tr>";
//	    }
	}
//	htmStr+=" <iframe frameborder=\"no\"  border=\"0\"  id=\"iframe1\" style=\"position:absolute; visibility:inherit; z-index:-1;display:block; width:200; height:180; filter='progid XImageTransform.Microsoft.Alpha(style=0,opacity=0)';\"></iframe>"
	htmStr+="</table>";		
	obj.innerHTML = htmStr;
//	if(obj.offsetWidth<151)
//	{
//	    obj.style.width=155;
//	}
    obj.style.height = 25*arrNames.length;
	switchDisplayForDiv(obj,true);
}
function switchDisplayForDiv(obj,isShow)//控件让文本框是否显示
{
	if(!isShow)
	{
		count = 0;
	}
	var displayStr = isShow?'block':'none';
	obj.style.display = displayStr;
}
function tdMouseOver(obj)//移上去的事件
{
	obj.style.background = "#0099FF";
    rowNum = parseInt(obj.id.substring(3,obj.id.length));
    var objParent = obj.parentElement;
    for(var i=0;i<objParent.childNodes.length;i++)
    {
        if(objParent.childNodes[i].id == obj.id){
            continue;
        }
        objParent.childNodes[i].style.background = "";
    }    
}
function tdMouseOut(obj)//移开时的事件
{
	obj.style.background="";
}
function evaluateToText(obj,txtObj)//给文本框赋值,在鼠标点击行之后
{
    //alert(obj.childNodes[0].innerText);//这个事件不能用onclick而要用onMouseOver
    document.getElementById(txtObj).value  = obj.childNodes[0].title;
    //alert(obj.childNodes[0].title);
    document.all.divTemp.style.display='none';
	hasChange = true;
	hasMouseDown = true;
	if(document.all.radPublic.checked!=true)
	   document.getElementById("dropVehicleType").style.display="block";
}

function txtChange()//重新开始
{
	rowNum = 0;
	txtValue = "";
	txtOldValue = "";
	txtCurrentValue = "";
    hasMouseMove = false;
    hasMouseDown = false;
}
function getSearchContent(keyWord,ddlObj,txtObj){
    var poiNames=new Array();   
    var poiCount=new Array();
   var date = new Ajax.Request(
        '/Web/MapService/Services/JourneyResourceService.asmx/GetStartPlaceForLike?likeStr='+keyWord+'&cityId='+ddlObj,
        {method: 'get', onComplete: function(response){
            var doc = new ActiveXObject("MSXML2.DOMDocument.3.0");
            var xmlTmp = response.responseText.replace(' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/"','');            
            doc.loadXML(xmlTmp);
            var nodes = doc.documentElement.childNodes[1].childNodes[0].childNodes;//debugger;
            for(var i= 0;i<nodes.length;i++)
            {
                poiNames[i]=nodes[i].selectSingleNode("POI_NAME").text+ "^" + nodes[i].selectSingleNode("POI_COUNT").text;
            }
           // debugger;
            startSearch(poiNames,txtObj);
         }}
    );
}  
function nextData(txtObj)
{
    hasMouseMove = true;
	var rowObj = eval("document.all.gtr"+rowNum);
	if(rowObj!=null)
	{			
		rowObj.style.background = "#0099FF";
		txtObj.value = rowObj.childNodes[0].title;
		if(document.getElementById("gtr"+parseInt(rowNum-1))!=null)
		{
			document.getElementById("gtr"+parseInt(rowNum-1)).style.background = "";

		}
		rowNum ++;
	}
	else
	{
		//alert('到底了');
	}
}
function previousData(txtObj)
{	
    hasMouseMove = true;		
	if(rowNum<2){
		var rowObj = document.all.tr0;
		//alert('到顶了');
	}
	else{
		var rowObj = eval("document.all.gtr"+parseFloat(rowNum-2));
		txtObj.value = rowObj.childNodes[0].title;
		if(rowObj!=null)
		{				
			rowObj.style.background = "#0099FF";	
			if(document.getElementById("gtr"+parseInt(rowNum-1))!=null)
			{
				document.getElementById("gtr"+parseInt(rowNum-1)).style.background = "";

			}
			rowNum --;
		}	
		else
		{
			//alert('到顶部啦');
		}
	}
}
	

