﻿var keyFrequency = 0 ; //键盘敲击频率计算器
//接受输入event的判断操作
var overcolor = "#33a6e0";
var outcolor = "#FFFFFF";

function keyboardEvent(ob,e, hiddenObj){
    //alert("keyboardEvent");
    //alert(e.keyCode);
    
    if (keyFrequency){ return false; }
    else{
      if( //e.keyCode!=8 && //Backspace键
		  e.keyCode!=9 && //tab键
          e.keyCode!=13 && //Enter键
          e.keyCode!=27 && //ESC键
          //e.keyCode!=37 && //方向左
          e.keyCode!=38 && //方向上
          //e.keyCode!=39 && //方向右
          e.keyCode!=40  ) { //方向下

          keyFrequency  = setTimeout("this.f()","100"); //如果连续敲击的间隔少于0.8秒，则不进行查询操作。
      }
		
      f = function(){p.show(ob,hiddenObj,false); keyFrequency = null;}; //清除计时器;
      return false;
    }
}

//显示绑定港口(true or false)
function keyboardEvent_bind(ob,e, hiddenObj){
    //alert("keyboardEvent");
    //alert(e.keyCode);
    
    if (keyFrequency){ return false; }
    else{
      if( //e.keyCode!=8 && //Backspace键
		  e.keyCode!=9 && //tab键
          e.keyCode!=13 && //Enter键
          e.keyCode!=27 && //ESC键
          //e.keyCode!=37 && //方向左
          e.keyCode!=38 && //方向上
          //e.keyCode!=39 && //方向右
          e.keyCode!=40  ) { //方向下

          keyFrequency  = setTimeout("this.f()","100"); //如果连续敲击的间隔少于0.8秒，则不进行查询操作。
      }
		
      f = function(){p.show(ob,hiddenObj,true); keyFrequency = null;}; //清除计时器;
      return false;
    }
}

//初始化ajax对象
function InitAjax()
{
　var ajax=false; 
　try { 
　　ajax = new ActiveXObject("Msxml2.XMLHTTP"); 
　} catch (e) { 
　　try { 
　　　ajax = new ActiveXObject("Microsoft.XMLHTTP"); 
　　} catch (E) { 
　　　ajax = false; 
　　} 
　}
　if (!ajax && typeof XMLHttpRequest!='undefined') { 
　　ajax = new XMLHttpRequest(); 
　} 
　return ajax;
}

function PortBox(objName) {
	this.style = {
		borderColor				: "#666", // 边框颜色
		bodyBackColor			: "#FFF", // 背景色
		bodyFontColor			: "333"
	};
	this.Obj = objName;
	this.portInput = null;
	this.hiddenObj = null;
};

PortBox.prototype.toString = function() {
	str = this.getStyle();
	str += '<div class="portbox" style="display:none;" onselectstart="return false" oncontextmenu="return false" id="PortBox"></div>';
	return str;
};

PortBox.prototype.getStyle = function() {
	str = '<style type="text/css">\n';
	str += 'div.portbox { position:absolute; overflow:auto; overflow:hidden; padding:1px 2px; height:160px; background:'+this.style.bodyBackColor+'; border:1px solid ' + this.style.borderColor + '; line-height:16px; text-decoration:none; }\n';
	str += 'div.portbox div { padding-right:16px; font-size:12px; }\n';
	str += 'div.portbox div dt { float:left; }\n';
        str += 'div.portbox div dd { float:right;}\n';
        str += 'div.portbox div.on{ background:'+overcolor+'; color:#FFFFFF; cursor:pointer; }\n';
        str += 'div.portbox div:hover { background:'+overcolor+'; color:#FFFFFF; cursor:pointer; }\n';
	str += 'div.portbox div.off { background:'+outcolor+'; color:'+this.style.bodyFontColor+'; }\n';
	str += 'div.portbox div.err { background:#FFF; color:#FF3333; }\n';
	str += 'div.portbox div.tip { background:#F2F2F2; color:#333; }\n';
	str += '</style>';
	return str;
};


PortBox.prototype.show = function() {

        

	if (arguments.length >  3  || arguments.length == 0) { 	alert("对不起！传入参数不对！" ); return; }

	portboxObj = this;
	this.portInput = inputObj = targetObj = _port = arguments[0];
	this.hiddenObj = hidObj = arguments[1];
	
	this.bindPort(_port.value, arguments[2]);

	document.onkeydown = selectaline;
};

PortBox.prototype.hide = function() { getObjById("PortBox").style.display = "none"; };

PortBox.prototype.bindPort = function(ev, port_bind) {//port_bind,是否显示绑定港口(true or false)

	//getObjById("PortBox").innerHTML = "";
	
	xmlurl = '/autoComplete.do?keyword='+escape(encodeURIComponent(ev))+'&lists=10&pageNum='+currentPage+'&portBind='+port_bind;

	try{

　		var ajax = InitAjax();
		ajax.open("Get", xmlurl, true);
		
		ajax.onreadystatechange = function() 
		{ 
			if (ajax.readyState == 4 && ajax.status == 200) 
			{ 
				var xmlDOM = ajax.responseText;
				var tmpArr = xmlDOM.split('</count>');
				
				var tmpParam = tmpArr[0].split('>');
				var tmpPam = tmpParam[1].split('|');	

				num = parseInt(tmpPam[0]);
				pages = parseInt(tmpPam[1]);

                                if (num == 0){rownum = 3}else{ rownum = num} //如果结果为0，则要输入三行字因此行数设为3；

				var objHeight = 16 * (rownum+1);
				getObjById("PortBox").innerHTML = tmpArr[1];//'<iframe id="shifrm" frameBorder="0" scrolling="no" height="'+objHeight+'"></iframe>'+tmpArr[1];
				getObjById("PortBox").style.height = objHeight + "px";	// 设置BOX的高度

                                
				for(var i=0; i<num; i++) {
					var _dayElement = getObjById("Line" + i);
					_dayElement.onmouseover = Function(portboxObj.Obj + ".onMouseOver(this)");
					_dayElement.onmouseout = Function(portboxObj.Obj + ".onMouseOut(this)");
					_dayElement.onclick = Function(portboxObj.Obj + ".onClick(this)");
				}

				_target = getpos(_port);
				var _obj = getObjById("PortBox");
				_obj.style.left = (_target.x - 1) + 'px';
				_obj.style.top = (_target.y + _port.clientHeight + 2) + 'px';
				_obj.style.display = "";
			}
		　}

		ajax.send(null); 
		
	}
	catch(exception) {}	
};

PortBox.prototype.onMouseOver = function(obj) {
	currentLine = parseInt(obj.id.replace("Line", ""));
	for(i=0;i<num;i++) {
            getObjById("Line" + i).style.backgroundColor ="";
            getObjById("Line" + i).className ="off";
        }
        obj.style.backgroundColor = overcolor;
		obj.className = "on";
        //for (i=0;i<num;i++){
            //getObjById("Line" + i).removeAttribute("style");
        //}
        //obj.style.backgroundColor="#009900";
};

PortBox.prototype.onMouseOut = function(obj) { 
    currentLine = parseInt(obj.id.replace("Line", ""));
    for(i=0;i<num;i++) {
        getObjById("Line" + i).style.backgroundColor ="";
        getObjById("Line" + i).className ="off";
    }
};

PortBox.prototype.onClick = function(obj) { this.portInput.value = obj.title; this.hiddenObj.value=obj.value;this.hide(); };

Number.prototype.NaN0 = function() { return isNaN(this) ? 0 : this; }


var portboxObj = null;
var hiddenObj;
var num = 0;
var inputObj = null;
var hidObj = null;
var currentLine = -1;	// 当前选中第几条
var currentPage = 1;	// 当前选中第几页

function closePortBox(evt) {
	evt = evt || window.event;
	var _target= evt.target || evt.srcElement;
	if(_target != inputObj) getObjById("PortBox").style.display = "none";
}


function selectaline(evt) {	// 上下键控制
	evt = evt || window.event;

	if(num==0) return;

	if(evt.keyCode==38) {	// 按向上键
		if(currentLine!=-1) getObjById("Line" + currentLine).className = "off";
		currentLine = currentLine>0?currentLine-1:num-1;
		getObjById("Line" + currentLine).className = "on";
	}
	else if(evt.keyCode==40) {	// 按向下键
		if(currentLine!=-1) getObjById("Line" + currentLine).className = "off";
		currentLine = currentLine<num-1?currentLine+1:0;
		getObjById("Line" + currentLine).className = "on";
	}
	else if(evt.keyCode==37) currentPage = currentPage>1?currentPage-1:pages;	// 按向左键
	else if(evt.keyCode==39) currentPage = currentPage<pages?currentPage+1:1;	// 按向右键
	else if(evt.keyCode==27) portboxObj.hide();
	else if(evt.keyCode==13 && getObjById("PortBox").style.display=="") {	// 按回车键
		inputObj.value = getObjById("Line" + currentLine).title;
		hidObj.value= getObjById("Line" + currentLine).value;
		portboxObj.hide();
		return false;
	}
	else {
		currentPage = 1;
		currentLine = -1;
	}
	
	// getObjById("z").innerHTML = currentLine;
	if(evt.keyCode==38 || evt.keyCode==40 || evt.keyCode==37 || evt.keyCode==39 || evt.keyCode==27) return false;
}

document.onclick = closePortBox;

var site_index = "/";
var port_type = 1;	// 港口类型
var p = new PortBox("p");
document.write(p);
