﻿var ObjPrintCity;//文本框
var hidObjPrintCity;//隐藏域
var focusId=1;
var HotCitys,AllCitys;
document.write("<div id='divCity' style='position: absolute;z-index:10;width:220;'></div>");  
var arrAllCity;
var arrHotCity;

function splitCity()
{
    if(arrAllCity==null || arrAllCity=="")
    {   
        defCity=hidObjPrintCity.value;
       // alert(defCity);
        if(defCity!=""){
        	var acitys=defCity.split("|");
        	if((acitys.length>1 && (acitys[1]==null || acitys[1]=="" || acitys[1]=="undefined")) || acitys.length<2){  //
        		 defCity =document.getElementById("hiddenCodeAndText").value;
        	}
        	if(defCity)
            HotCitys="默认城市|"+defCity.split('|')[1]+"|"+defCity.split('|')[0]+","+HotCitys;
        }
        arrAllCity=AllCitys.split(',');
        arrHotCity=HotCitys.split(',');        
    }    
};

var posLib = {
    getClientLeft:function (el) {
     var r = el.getBoundingClientRect();
     return r.left- this.getBorderLeftWidth(this.getCanvasElement(el));
    },

    getClientTop:    function (el) {
     var r = el.getBoundingClientRect();
      return r.top - this.getBorderTopWidth(this.getCanvasElement(el));
    },

    getLeft:    function (el) {
      return this.getClientLeft(el) + this.getCanvasElement(el).scrollLeft;
    },

    getTop:    function (el) {
      return this.getClientTop(el) + this.getCanvasElement(el).scrollTop;
    },

    getInnerLeft:    function (el) {
      return this.getLeft(el) + this.getBorderLeftWidth(el);
    },

    getInnerTop:    function (el) {
      return this.getTop(el) + this.getBorderTopWidth(el);
    },

    getWidth:    function (el) {
      return el.offsetWidth;
    },

    getHeight:    function (el) {
      return el.offsetHeight;
    },

    getCanvasElement:    function (el) {
      var doc = el.ownerDocument || el.document;    // IE55 bug
      if (doc.compatMode == "CSS1Compat")
        return doc.documentElement;
      else
        return doc.body;
    },

    getBorderLeftWidth:    function (el) {
      return el.clientLeft;
    },

    getBorderTopWidth:    function (el) {
      return el.clientTop;
    },

    getScreenLeft:    function (el) {
      var doc = el.ownerDocument || el.document;    // IE55 bug
      var w = doc.parentWindow;
      return w.screenLeft + this.getBorderLeftWidth(this.getCanvasElement(el)) + this.getClientLeft(el);
    },

    getScreenTop:    function (el) {
      var doc = el.ownerDocument || el.document;    // IE55 bug
      var w = doc.parentWindow;
      return w.screenTop  + this.getClientTop(el);//+ this.getBorderTopWidth(this.getCanvasElement(el))
    }
}

function Split(popup_str,popup_n,popup_s){ //字符串,取第几个数据,分割字符
	var popup_split;
	 popup_split=popup_str.split(popup_s);
	return popup_split[popup_n];
}

function ShowGetTicketCity(tbObjId,hidObjId,e, m_HotCitys, m_AllCitys,atr)
{
	if(typeof(tbObjId)=='string')
		ObjPrintCity=document.getElementById(tbObjId);
	else
		ObjPrintCity=tbObjId;
	if(typeof(hidObjId)=='string')
		hidObjPrintCity=document.getElementById(hidObjId);
	else
		hidObjPrintCity=hidObjId;
	
	//oPopup.document.body.onclick = Htc_OnClick;
	//oPopup.document.body.onmousemove = Htc_onmousemove;
	GetCitys("",e, m_HotCitys, m_AllCitys,atr);
	var newX =posLib.getLeft(ObjPrintCity);
	var newY =posLib.getTop(ObjPrintCity)+20;	
	document.getElementById("divCity").style.top=newY+"px";
	document.getElementById("divCity").style.left=newX+"px";
	//alert(hidObjPrintCity.Value+"----5555");
}

function GetCitys(str,e, m_HotCitys, m_AllCitys,att)
{
    HotCitys = m_HotCitys;
    AllCitys = m_AllCitys;
    arrAllCity=null;
    arrHotCity=null;
    
    splitCity();
	var KeyCode;
	if(e) KeyCode=parseInt(e.keyCode);
	else KeyCode=parseInt(event.keyCode);
	if(KeyCode!=38 && KeyCode!=40 && KeyCode!=13)
	{
		str=str.toLowerCase();
		var count=0;
		var tab;
		tab = '<table width="200" border="0" cellpadding="0" cellspacing="0" style=" border:1px solid #000000;">';
		tab+= '<tr>';
		tab+= '   <td id="Popup_Tab" bgcolor="#ffffff" align="center">';
		tab+= '	<table width="100%" border="0" cellpadding="0" cellspacing="0" bgColor="#ffffff" style="border-bottom:1px solid #000000">';
		tab+= '  <tr align="center">';
		tab+= '    <td height="23" align="left" id="td_Title" style="color:#fff" bgColor="#999999">';
		
		    if(str!="")
		     tab+='<font color="red">'+str+'</font>,';
		     
		tab+= '可输入拼音/中文搜索或↑↓选择</td>';
		tab+= '  </tr>';
//		tab+= '  <tr align="center">';
//		tab+= '    <td height="1" ></td>';
//		tab+= '  </tr>';
		tab+= '	</table>';
		tab+= ' <table id="tbl_Iterms" width="100%" border="0" cellspacing="0" cellpadding="2" style="font-size: 9pt" >';
		
		if(str=="")
		{
			for(i=0;i<arrHotCity.length;i++)
			{
				if(arrHotCity[i].indexOf(str)>-1)
				{				
					tab+= '  <tr id="trIterm'+count+'" name="'+arrHotCity[i].split('|')[2]+'|' +arrHotCity[i].split('|')[1]+'"';
					tab+='  onMouseOver="SetChangesIterm('+count+')"';
					tab+=' onClick="SetItermValue(event)"'+' align="center" style="cursor:hand;color:#666666" bgColor="#ffffff">';
					tab+= '<td align="left" width="120">'+arrHotCity[i].split('|')[0]+'</td>';
					if(att=="f"){
					    tab+= '<td align="right" width="80">'+arrHotCity[i].split('|')[1]+ '('+ arrHotCity[i].split('|')[2] +')</td>';
					}else{
					    tab+= '<td align="right" width="80">'+arrHotCity[i].split('|')[1]+'</td>';
					}
					tab+= '  </tr>';
					count++;
				}
			}
		}
		else
		{
		    str = str.toUpperCase();
		    var match='';

			for(i=0;i<arrAllCity.length;i++)
			{
			    //优先选择首字母匹配
			    if(arrAllCity[i].toUpperCase().indexOf(str)==0)
			    {
			            match = '';
			            match+= '  <tr id="trIterm'+count+'" name="'+arrAllCity[i].split('|')[2]+'|'+arrAllCity[i].split('|')[1]+'"';
					    match+='  onMouseOver="SetChangesIterm('+count+')"';
					    match+=' onClick="SetItermValue(event)"'+' align="center" style="cursor:hand;color:#666666" bgColor="#ffffff">';
					    match+= '<td align="left" width="120">'+arrAllCity[i].split('|')[0]+'</td>';
					    //tab+= '<td align="left" width="80">'+arrAllCity[i].split('|')[1]+'</td>';
					    if(att=="f"){
					        match+= '<td align="right" width="120">'+arrAllCity[i].split('|')[1]+ '('+ arrAllCity[i].split('|')[2] +')</td>';
					    }else{
					        match+= '<td align="right" width="80">'+arrAllCity[i].split('|')[1]+'</td>';
					    }
					    match+= '  </tr>';
					    count++;
					    tab += match;
					    if(count>=15)
						    break;					    
				}
			}
			for(i=0;i<arrAllCity.length&&count<15;i++)
			{
			    //非首字母匹配
			    if(arrAllCity[i].toUpperCase().indexOf(str)>0)
			    {
			            match = '';
			            match+= '  <tr id="trIterm'+count+'" name="'+arrAllCity[i].split('|')[2]+'|'+arrAllCity[i].split('|')[1]+'"';
					    match+='  onMouseOver="SetChangesIterm('+count+')"';
					    match+=' onClick="SetItermValue(event)"'+' align="center" style="cursor:hand;color:#666666" bgColor="#ffffff">';
					    match+= '<td align="left" width="120">'+arrAllCity[i].split('|')[0]+'</td>';
					    if(att=="f"){
					        match+= '<td align="right" width="120">'+arrAllCity[i].split('|')[1]+ '('+ arrAllCity[i].split('|')[2] +')</td>';
					    }else{
					        match+= '<td align="right" width="80">'+arrAllCity[i].split('|')[1]+'</td>';
					    }
					    match+= '  </tr>';
					    count++;
					    tab += match;
					    if(count>=15)
						    break;					    
				}
			}
						        
		}
		tab+= '    </table>';
		tab+= '   </td>';
		tab+= '  </tr>';
		tab+= '</table>';
		tab+= '</td>';
		tab+= '</tr>';
		tab+= '</table>';
		//若是IE浏览器，则加上层背景
		if(navigator.userAgent.toLowerCase().indexOf("ie")!=-1&&navigator.userAgent.toLowerCase().indexOf("opera")==-1&&navigator.appVersion.split(";")[1].replace('MSIE','')<8)
		    tab += "<iframe id='divCityFrame' frameborder='0' tabindex='-1' style='z-index:-1;position: absolute;left:0px;top:0px;width:expression(this.parentNode.offsetWidth+\"px\");height:expression(this.parentNode.offsetHeight+\"px\");' scrolling='no' src=''></iframe>";
		    
		document.getElementById("divCity").innerHTML=tab;
		focusId=0;
		document.getElementById("divCity").style.display="block";
	}
	//alert("GetCitys ----6666");
	
	SetFocusIterm(str, e);
}

function SetFocusIterm(str, e)
{
	var KeyCode
	if(e) KeyCode = parseInt(e.keyCode);
	else KeyCode = parseInt(event.keyCode);
	var RowsCount=document.getElementById("tbl_Iterms")!=null?document.getElementById("tbl_Iterms").rows.length:0;
	var newfocusId=0;
	
	//按Esc键
    if(KeyCode==27)
    {
        document.getElementById("divCity").style.display="none";
        return false;
    }
	//按Enter键或者Tab键
	if(KeyCode==13)
		SetItermValue();
　　//按↑键或者↓键
	if(KeyCode==38 || KeyCode==40)
	{
		if(KeyCode==38)
			newfocusId=focusId>0?focusId-1:0;
		if(KeyCode==40)
			newfocusId=RowsCount>focusId+1?focusId+1:RowsCount-1;
	}
	if(RowsCount>0)
	{
		SetChangesIterm(newfocusId);
	}
	else
	{
	    document.getElementById("td_Title").innerHTML="抱歉！找不到\"<span class='l_red_12c'>"+str+"</span>\"";
	   // SetFocusIterm(document.getElementById("hiddenCodeAndText").value,e);
	}	
	
}

//选择
function SetItermValue()
{
    try
    {
        if(document.getElementById("divCity").style.display!="none")
        {
	        ObjPrintCity.value=document.getElementById("trIterm"+focusId).getAttribute("name").split('|')[1];
	        hidObjPrintCity.value=document.getElementById("trIterm"+focusId).getAttribute("name");
	        document.getElementById("divCity").style.display="none";
	        //默认出票城市
	       // if(document.getElementById("txtSinglePrintCity")!=null&&ObjPrintCity==document.getElementById("txtSingleFromCity"))
	       // autoSetPrintCity(ObjPrintCity.value);
	        //将焦点转移到下一个输入框
	        //nextInputFocus(ObjPrintCity);
	    }	    
	}
	catch(ex)  //查找的值 没有找到时会报错
	{
		 var hiddenCodeAndText =  document.getElementById("hiddenCodeAndText").value;
		    ObjPrintCity.value=hiddenCodeAndText.split('|')[1];
	        hidObjPrintCity.value=hiddenCodeAndText.split('|')[0];
	        document.getElementById("divCity").style.display="none";
//	    alert(ex);
	};
	initLinkageOptions();
	
}

//移动
function SetChangesIterm(newfocusId)
{
	try{
			document.getElementById("trIterm"+focusId).bgColor="#ffffff";
			document.getElementById("trIterm"+focusId).style.color="#666666";
			document.getElementById("trIterm"+newfocusId).bgColor="#ff9900";//移动时背景色		
			document.getElementById("trIterm"+newfocusId).style.color="#000000";
			focusId=newfocusId;
		}
		catch(ex)
		{
		}
}
//
function autoSetPrintCity(cityName)
{
    document.getElementById("txtSinglePrintCity").value=cityName;
    document.getElementById("hddSinglePrintCity").value=GetPrintCityState(cityName) + "|" + cityName;    
}

if( document.addEventListener )//for dom
{
    document.addEventListener( "click", hideCityItem, true )
}
else if( parent.document.attachEvent )//for ie
{
    document.attachEvent( "onclick", hideCityItem);
}

function hideCityItem(e)
{
    try{
        var Object = "divCity"
        if(e!=null&&e.target!=null)
        { 
            sid = e.target.id;
            if(e.target.parentNode.id.indexOf("trIterm")<0&&ObjPrintCity!=null&&sid!=ObjPrintCity.id)
            {
                if (sid!=document.getElementById(Object).id) 
                    document.getElementById(Object).style.display = "none";
            }
        }
        else 
        {
            sid = event.srcElement.id;
            if(event.srcElement.parentElement.id.indexOf("trIterm")<0&&ObjPrintCity!=null&&sid!=ObjPrintCity.id)
            {
                if (sid!=document.getElementById(Object).id) 
                    document.getElementById(Object).style.display = "none";
            }
        }
    }catch(ex)
	{
 	   // alert(ex);
	};
}


