﻿﻿	var keyObj = null;
	var pop_objs;//
	var child_objs;
	var taskid = 0;
	var delaySec = 150; 										// 默认延迟多少毫秒出现提示信息
	var name;
	var first_position;
	var next_position;
	var nextChange;
	/******级联获取目的地*******/
	function getDestination(value,name){
		pageCount=0;
		curPage=1;
		if($.trim(value).length > 0){
			var sArr = this.destinationOrStartCityArr;
			var tmpArr = getDataFromArrays($("#originID").next().val(),sArr,3,'o');
			tmpArr = getDataFromArrays(value,tmpArr,0,'o');
			first_position = 2;
			next_position = 1;
			getPageContent(tmpArr);
		}else{
			var sArr = this.destinationOrStartCityArr;
			var tmpArr = getDataFromArrays($("#originID").next().val(),sArr,3,'o');
			first_position = 2;
			next_position = 1;
			getPageContent(tmpArr);
		}
	}
	/*获取页面内容信息*/
	function getPageContent(tmpArr){
		var popDivContent = "<ul id='query_content'>";
		
		//popDivContent = popDivContent+ getPageTable(tmpArr);
		popDivContent = popDivContent+ getPageTableEx(tmpArr);
		$("#"+this.name).html('');
		$(popDivContent).appendTo('#'+this.name); 	
		$("#"+this.name).show();
			
		$("#query_content").mousedown(function(){
			$("#temp_div").css("display","none");
		});
		$("#query_content li").css("cursor","pointer").css("height","18px")
		.css("padding-left","5px").css("padding-top","5px")
		.css("padding-right","5px").css("width","60px").css("float","left");
		$("#query_content li").mouseover(function(){			
			$(this).addClass('li_over');
		}).mouseout(function(){
			$(this).removeClass('li_over');
		});	
		//$("#query_content .down").css("background-color","red");
	}
	
	var pageCount=0;//总页数
	var curPage=1;
	
	function changePage(event,curPage){
		this.curPage=curPage;
		
		event.stopPropagation;//阻止事件的传递
		event.cancelBubble = true;
		
		var sArr = this.destinationOrStartCityArr;
		var tmpArr = getDataFromArrays($("#originID").next().val(),sArr,3,'o');
		first_position = 2;
		next_position = 1;
		getPageContent(tmpArr);
		
	}
	
	// add by cuihongqiang for display all dest city in one page.
	function getPageTableEx(tmpArr){
		var popDivContent = "";
		if(tmpArr.length == 0){
			popDivContent = "<li width='100px'><font color='red'>没找到</font></li>";
			$("#temp_div").css("width","100px");			
		}else{				
			for(var i = 0; i< tmpArr.length; i++){
				var tmp = tmpArr[i];
				var code    = tmp[this.first_position];
				var content = tmp[this.next_position];
				popDivContent = popDivContent + "<li onmousedown=getContents('"+code+"','"+content+"')>"+content+"</li>";			
			}
			if(tmpArr.length<6)	$("#temp_div").css("width",(tmpArr.length * 70)+"px");
			else	$("#temp_div").css("width","420px");	
		}
		popDivContent = popDivContent + "</ul>";
		return popDivContent;
	}	
	
	/*生成表格信息*/
	function getPageTable(tmpArr){
		var popDivContent = "";
		if(tmpArr.length == 0){
			popDivContent = "<li width='100px'><font color='red'>没找到</font></li>";	
		}else{	
			pageCount=Math.ceil(tmpArr.length/9);
			var lastNum=0;
			if(pageCount==1||curPage==pageCount){
				lastNum=tmpArr.length;
			}else{
				lastNum=curPage*9;
			}
			if(pageCount>0){
				for(var i = (curPage-1)*9; i< lastNum; i++){
					var tmp = tmpArr[i];
					var code    = tmp[this.first_position];
					var content = tmp[this.next_position];
					popDivContent = popDivContent + "<li onmousedown=getContents('"+code+"','"+content+"')>"+content+"</li>";			
				}
				popDivContent = popDivContent+"<table class='down'width='100%'><tr>";
				if(curPage>1){
					popDivContent=popDivContent+"<td onmousedown=changePage(event,"+(curPage-1)+") style='padding: 2px,2px,2px;color: blue'>上一页</td>";
				}
				
				if(curPage<pageCount){
					popDivContent=popDivContent+"<td onmousedown=changePage(event,"+(curPage+1)+") style='padding: 2px,2px,2px;color: blue'>下一页</td>";
				}
				popDivContent = popDivContent+"</tr></table>";
			}
			/**
			for(var i = 0; i< tmpArr.length; i++){
				var tmp = tmpArr[i];
				var code    = tmp[this.first_position];
				var content = tmp[this.next_position];
				popDivContent = popDivContent + "<li onmousedown=getContents('"+code+"','"+content+"')>"+content+"</li>";			
			}*/
		}
		popDivContent = popDivContent + "</ul>";
		return popDivContent;
	}
	/*点击下拉div是保存关键字和内容*/
	function getContents(code,content){
		$(this.pop_objs).attr("value",content);
		$(this.pop_objs).next().attr("value",code);
		//alert($(this.pop_objs).next().attr("value"));
		if(this.nextChange == "nextChange")		//为下一个空件改变做准备
			$(this.pop_objs).next().change();
		$(this.pop_objs).next().next().attr("value","");
		$(this.pop_objs).parent().next().find("input").val("");
		$("#temp_div").css("display","none");
		$(this.pop_objs).next().next().focus();
	}
	/***************************************************fix_div_coordinate*********************************************/
	//函数功能：控制提示div的位置，使之刚好出现在文本输入框的下面
	function fix_div(obj,nm){
		var x = $(obj).offset().left;
		var y = $(obj).offset().top+$(obj).height()+4;
		var w = $(obj).width()+3;
		$("#"+nm).css({left:x,top:y,position:"absolute",visibility:"visible",width:"420px","zIndex":888});
	}
    
	function hidden_suggest(nm){
		$("#"+nm).css("display","none");
	}
	
	function is_showsuggest(){
		var value = $("#"+name).attr("display");
		if(value == "none")
			return false;
		else
			return true;
	}
	
	function show_suggest(){
		$("#"+name).css("display","none");
	}
	function ltrim(strtext){
		return strtext.replace(/[\$&\|\^*%#@! ]+/, '');
	}
        
	/***************************************************keys_handleup*********************************************/
	//函数功能：用于处理当用户用向上的方向键选择内容时的事件
	function keys_handleup(){
		if(keyObj == null){
			keyObj = $("#temp_div li:first");
			keyObj.addClass("li_over");
		}else{
			var tmp = $("#temp_div li:first");
			if($(keyObj).text() != $(tmp).text()){
				keyObj.removeClass("li_over");
				keyObj = keyObj.prev();
				keyObj.addClass("li_over");
			}
		}
	}
    
	/***************************************************keys_handledown*********************************************/
	//函数功能：用于处理当用户用向下的方向键选择内容时的事件
	function keys_handledown(){
		if(keyObj == null){
			keyObj = $("#temp_div li:first");
			keyObj.addClass("li_over");
		}else{
			var tmp = $("#temp_div li:last");
			if($(keyObj).text() != $(tmp).text()){
				keyObj.removeClass("li_over");
				keyObj = keyObj.next();
				keyObj.addClass("li_over");
			}
		}
	}
	/***************************************************keys_handleEnter*********************************************/
	//函数功能：用于处理当用户用向下的方向键选择内容时的事件
	function keys_handleEnter(){
		$(keyObj).mousedown();
	}
	
	$(function(){
		$(document).mousedown(function(){
			keyObj = null;
		});
	});
	function sleep(n){
		var start=new Date().getTime(); //for opera only
		while(true) if(new Date().getTime()-start>n) break;
	}
	
	function ajaxac_getkeycode(e)
	{
		var code;
		if (!e) var e = window.event;
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
		return code;
	}
    
    /***************************************************display*********************************************/
	//函数功能：入口函数，将提示层div显示出来
	//输入参数：object 当前输入所在的对象，如文本
	//输入参数：e IE事件对象
	
	
	//display(this,event,'temp_div','destination')
	//obj-->被触发的对象， evnet-->ie事件，nm-->目标层，url-->出发地还是目的地  
	function display(obj,event,nm,url,flag){
		this.nextChange = flag;
		this.pop_objs = obj;
		if (!event) event = window.event;
		event.stopPropagation;
		event.cancelBubble = true;
		if (event.target) targ = event.target;  else if (event.srcElement) targ = event.srcElement;
		if (targ.nodeType == 3)  targ = targ.parentNode;
		
		var inputkeys = ajaxac_getkeycode(event);
		
		name = nm;
		switch(inputkeys){
			case 13://回车键
				keys_handleEnter();
				return;break;
			case 38: //向上方向
				keys_handleup();
			    return;break;
			case 40: //向下方向
				if(is_showsuggest()) keys_handledown(); else show_suggest();
			    return;break; 
			case 39: //向右方向
				return;break;
			case 37: //向左方向
				return;break;
			case 18: //对应Alt
				hidden_suggest(nm);
			    return;break;
			case 27: //对应Esc
				hidden_suggest(nm);
			    return;break;
		}

		if(window.opera) sleep(300);								//延迟0.1
		if(taskid) window.clearTimeout(taskid);		
		if(url == "destination"){
			if($.trim($("#originID").next().val()).length==0){
				$("#originID").focus();
				$("#originID").click();
				return false;
			}else
				taskid=setTimeout("getDestination('"+obj.value+"','"+url+"');" , delaySec);
		}
		fix_div(obj,nm);
	}
	
	function getDataFromArrays(value,arr,num,other){
		var position;
		value = value.toLocaleUpperCase();
		var tmpArr = new Array();
		value = $.trim(value);
		if(value == "")
			return tmpArr;
		for(var i=0;i<arr.length;i++){
			var firstArr = arr[i];
			var tf = firstArr[num];//.substring(0,value.length);//英文查询
			if(tf.indexOf(value) != -1){
				tmpArr.push(firstArr);
			}else{
				other == 'o'?position=1:position=2;
				tf = firstArr[position];
				if(tf.indexOf(value) != -1){
					tmpArr.push(firstArr);
				}
			}
		}
		return tmpArr;
	}
	
	
	
	function checkDestName(obj){
		var destName=$(obj).val();
		var destId=$(obj).next().val();
		
		var destRealName="";
		
		if(destId==""){
			$(obj).val("");
			return;
		}
		
		var flag=false;
		for(var i=0;i<this.destinationOrStartCityArr.length;i++){
			
			if(destinationOrStartCityArr[i][2]==destId){
				if(destinationOrStartCityArr[i][1]==destName){
					flag=true;
				}
				destRealName=destinationOrStartCityArr[i][1];
			}
			
		}
		
		if(!flag&&""!=destId&&destName!=""){
			$(obj).val(destRealName);
		}
		
	}
