﻿var ObjPrintCity;//文本框
var hidObjPrintCity;//隐藏域
var focusId=1;
var HotCitys,AllCitys,InternalCitys,ForeignCitys;
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;
        if(defCity!="")
            HotCitys="默认城市|"+defCity.split('|')[1]+"|"+defCity.split('|')[0]+","+HotCitys;
        //arrAllCity=AllCitys.split(',');
        arrAllCity=String(InternalCitys+(ForeignCitys!=null?(","+ForeignCitys):"")).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_InternalCitys,m_ForeignCitys)
{
	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_InternalCitys,m_ForeignCitys);
	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";
}

function GetCitys(str,e, m_HotCitys, m_InternalCitys,m_ForeignCitys)
{
    HotCitys = m_HotCitys;
    InternalCitys = m_InternalCitys;
    ForeignCitys = m_ForeignCitys;
    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 #666666;">';
		tab+= '<tr>';
		tab+= '   <td id="Popup_Tab" bgcolor="#faf7e7" align="center">';
		tab+= '	<table width="98%" border="0" cellpadding="0" cellspacing="0" bgColor="#faf7e7" style="border-bottom:1px solid #666666">';
		tab+= '  <tr align="center">';
		tab+= '    <td height="23" align="left" id="td_Title" style="color:#70bd20">';
		
		    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="98%" 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" bgColor="#faf7e7">';
					tab+= '<td align="left" width="120">'+arrHotCity[i].split('|')[0]+'</td>';
//					if(att=="f"){
					    tab+= '<td align="right" width="80">'+arrHotCity[i].split('|')[1] +'</td>'; //+ '('+ arrHotCity[i].split('|')[2]
//					}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" bgColor="#faf7e7">';
					    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]+ '</td>'; //'('+ arrAllCity[i].split('|')[2] +
//					    }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" bgColor="#faf7e7">';
					    match+= '<td align="left" width="120">'+arrAllCity[i].split('|')[0]+'</td>';
//					    if(att=="f"){
					        match+= '<td align="right" width="120">'+arrAllCity[i].split('|')[1]+ '</td>'; //('+ arrAllCity[i].split('|')[2] +')
//					    }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";
	}
	
	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="抱歉！找不到\"<font color='red'>"+str+"</font>\"";
	}	
	
}

//选择
function SetItermValue()
{
    try
    {
        if(document.getElementById("divCity").style.display!="none")
        {
	        ObjPrintCity.value=document.getElementById("trIterm"+focusId).getAttribute("name").split('|')[1];
	        var str_city = document.getElementById("trIterm"+focusId).getAttribute("name");
	        //北京上海编码转换BBB->111,SSS->222
	        hidObjPrintCity.value=str_city;
	        IsInternalOrForeign(str_city);
	        var tempfromcitystr = document.getElementById("hddSingleFromCity").value;
	        if(Number(document.getElementById("hddTicketSearchAction").value)>2)
	        {
	            document.getElementById("hddSingleFromCity").value = tempfromcitystr.replace("BBB","111").replace("SSS","222");
	        }
	        else
	        {
	            document.getElementById("hddSingleFromCity").value = tempfromcitystr.replace("111","BBB").replace("222","SSS");
	        }
	        document.getElementById("divCity").style.display="none";
	        //默认出票城市
	        if(document.getElementById("txtSinglePrintCity")!=null&&ObjPrintCity==document.getElementById("txtSingleFromCity"))
	        autoSetPrintCity(ObjPrintCity.value);
	        //将焦点转移到下一个输入框
	        //nextInputFocus(ObjPrintCity);
	    }	    
	}
	catch(ex)
	{
//	    alert(ex);
	};
}

function IsInternalOrForeign(str_city)
{
    if(InternalCitys!=null&&InternalCitys!=""&&ForeignCitys!=null&&ForeignCitys!="")
    {
        var arrstr_city = str_city.split("|");
        
        var ticketSearchAction = 0;
        if(ForeignCitys.indexOf("|"+arrstr_city[1]+"|")<0)
        {//国内
            if (document.getElementById("rbSingle").checked)
            {//单程
                ticketSearchAction = 0;
            }
            else
            {//往返
                ticketSearchAction = 1;
            }
        }
        else
        {//国际
            if (document.getElementById("rbSingle").checked)
            {//单程
                ticketSearchAction = 3;
            }
            else
            {//往返
                ticketSearchAction = 4;
            }
        }
        
        document.getElementById("hddTicketSearchAction").value=ticketSearchAction.toString();
        
        return ticketSearchAction;
    }
}

//移动
function SetChangesIterm(newfocusId)
{
	try{
			document.getElementById("trIterm"+focusId).bgColor="#faf7e7";
			document.getElementById("trIterm"+focusId).style.color="#000000";
			document.getElementById("trIterm"+newfocusId).bgColor="#eee5b4";		
			document.getElementById("trIterm"+newfocusId).style.color="#cc0000";
			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);
	};
}