function jsAuto(instanceName,objID){
	this._msg = [];
	this._x = null;
	this._o = document.getElementById(objID);//获取suggest对象
	if (!this._o) return;
	this._f = null;
	this._i = instanceName;
	this._r = null;
	this._c = 0;
	this._s = false;
	this._v = null;
	this._o.style.visibility = "hidden";
	this._o.style.position = "absolute";
	this._o.style.zIndex = "9999";
	this._max = 10;//自动建议最大数
	return this;
};

//键盘事件
jsAuto.prototype.directionKey=function(){ with (this)
	{
	var e = _e.keyCode ? _e.keyCode : _e.which;
	var l = _o.childNodes.length;
	(_c>l-1 || _c<0) ? _s=false : "";	
	if( e==40 && _s ){
		_o.childNodes[_c].className="mouseout";
		(_c >= l-1) ? _c=0 : _c ++;
		_o.childNodes[_c].className="mouseover";
	 }
	 if( e==38 && _s ){
		_o.childNodes[_c].className="mouseout";
		_c--<=0 ? _c = _o.childNodes.length-1 : "";
		_o.childNodes[_c].className="mouseover";
	 }
	 if( e==13 ){//回车键
		if(_o.childNodes[_c] && _o.style.visibility=="visible"){
			_r.value = _x[_c];
			_o.style.visibility = "hidden";
		}
	 }
	 if( !_s ){
	  _c = 0;
	  _o.childNodes[_c].className="mouseover";
	  _s = true;
	 }
	}
};


// mouseEvent.
jsAuto.prototype.domouseover=function(obj) { with (this){
	_o.childNodes[_c].className = "mouseout";
	_c = 0;
	obj.tagName=="DIV" ? obj.className="mouseover" : obj.parentElement.className="mouseover";
}};

jsAuto.prototype.domouseout=function(obj){
	obj.tagName=="DIV" ? obj.className="mouseout" : obj.parentElement.className="mouseout";
};

jsAuto.prototype.doclick=function(msg) { with (this){
	if(_r){
		_r.value = msg;
		_o.style.visibility = "hidden";
	}
	else{
		alert("javascript autocomplete ERROR :\n\n can not get return object.");
	return;
	}
}};

// object method;
jsAuto.prototype.items=function(msg){//装入数据
	if( msg.indexOf('|')>0 ){
		var arrMsg=msg.split('|');
		for(var i=0; i<arrMsg.length; i++){
			this._msg.push(arrMsg[i]);
		}
	}
	else{
		this._msg.push(msg);
	}
	this._msg.sort();
};

jsAuto.prototype.append=function(msg) { with (this){
	_i ? "" : _i = eval(_i);
	_x.push(msg);
	var div = document.createElement("DIV");
	div.onmouseover = function(){_i.domouseover(this)};
	div.onmouseout = function(){_i.domouseout(this)};
	div.onclick = function(){_i.doclick(msg)};
	var re  = new RegExp("(" + _v + ")","i");
	div.style.lineHeight="140%";
	div.className = "mouseout";
	if(_v){
		div.innerHTML = msg.replace(re , "<strong class='doaa'>$1</strong>");
	}
	_o.appendChild(div);
}};

jsAuto.prototype.display=function() { with(this){
	if(_f&&(_v!="")){
		_o.style.left = _r.offsetLeft+"px";
		_o.style.top = _r.offsetTop+_r.clientHeight+"px";
		_o.style.width = _r.offsetWidth-2+"px";//控件宽度,-2是去掉边框
		_o.style.visibility = "visible";
	}
	else{
		_o.style.visibility="hidden";
	}
}};

jsAuto.prototype.handleEvent=function(fValue,fID,event) { with (this){
	if (fValue!=""){
		var re;
		_e = event;
		var e = _e.keyCode ? _e.keyCode : _e.which;
		_x = [];
		_f = false;
		_r = document.getElementById( fID );//得到输入框对象
		_v = fValue;
		_i = eval(_i);
		re = new RegExp(fValue, "i");//匹配关键字
		_o.innerHTML="";
		var jmax=0; //最大建议计数 
		for(var i=0; i<_msg.length; i++){
			if(re.test(_msg[i])){
				_i.append(_msg[i].split('~')[1]);
				_f = true;
				jmax++;
				if(jmax>_max){break;}//跳出关键字
			}
		}	
		_i ? _i.display() : alert("can not get instance");	
		if(_f){
			if((e==38 || e==40 || e==13)){
				_i.directionKey();
			}
			else{
				_c=0;
				_o.childNodes[_c].className = "mouseover";
				_s=true;
			}
		}
 	}
	else{
		_o.style.visibility = "hidden";
	}
}};

jsAuto.prototype.handleEvent2=function(fValue,fID,event) { with (this){
	if (fValue!=""){
		var re;
		_e = event;
		var e = _e.keyCode ? _e.keyCode : _e.which;
		_x = [];
		_f = false;
		_r = document.getElementById( fID );//得到输入框对象
		_v = fValue;
		_i = eval(_i);
		re = new RegExp(fValue, "i");//匹配关键字
		_o.innerHTML="";
		var jmax=0; //最大建议计数 
		for(var i=0; i<_msg.length; i++){
			if(re.test(_msg[i])){
				_i.append(_msg[i]);
				_f = true;
				jmax++;
				if(jmax>_max){break;}//跳出关键字
			}
		}	
		_i ? _i.display() : alert("can not get instance");	
		if(_f){
			if((e==38 || e==40 || e==13)){
				_i.directionKey();
			}
			else{
				_c=0;
				_o.childNodes[_c].className = "mouseover";
				_s=true;
			}
		}
 	}
	else{
		_o.style.visibility = "hidden";
	}
}};


jsAuto.prototype.hidden=function() { with (this){
 	_o.style.visibility = "hidden";
}};


//window.onerror=new Function("return true;");//消除错误

//////选择字符母分类建议/////////////////
function showCats(){//显示线路分类
	cat=document.getElementById("cat");
	catlist=line_cat.split("|");//从“_lineData.js”引入变量_lineClass的站点分类数组值
	for (i=0;i<catlist.length; i++){ 
		cat.options[i+1] = new Option(catlist[i],i);
	}		
}

function changeCat(t){//改变线路分类
	text=t.options[t.selectedIndex].text;
	value=t.options[t.selectedIndex].value;
	cats=document.getElementById("cats");
	if (value==""){
		cats.options.length = 1;
		cats.options[0] = new Option("<-|选择线路分类","");		
	}
	else{
		cats.options[0] = new Option(text,"");
		lines=eval("line_cat_"+value);
		lines=lines.split("|");
		cats.options.length = 1;
		for (i=0;i<lines.length; i++){ 
			cats.options[i+1] = new Option(lines[i],lines[i]);
		}
	}
}

function ABC(id){//自动生成分类字母
	id=document.getElementById(id);
	cats=point_cat.split("|");//从“_lineData.js”引入变量_lineClass的站点分类数组值
	len=cats.length;
	for (i=0;i<len; i++){
		id.options[i+1] = new Option(cats[i],i);
	}
}
function changeLocation(t,line){
	text=t.options[t.selectedIndex].text;
	value=t.options[t.selectedIndex].value;
	lines=document.getElementById(line);
	if (value==""){
		lines.options.length = 1;
		lines.options[0] = new Option("<-|请先选择分类","");		
	}
	else{
		lines.options[0] = new Option("选择站点["+text+"]","");
		stops=eval("point_cat_"+value);
		stops=stops.split("|");
		lines.options.length = 1;
		for (i=0;i<stops.length; i++){ 
			v=stops[i].split("~")[1];
			lines.options[i+1] = new Option(v,v);
		}
	}
}

function setV(t,id){
	document.getElementById(id).value=t.options[t.selectedIndex].value;
}
////////////检验提交表单的函数/////////////////////////
function so1(obj,t){
	var A=document.getElementById('A');
	var B=document.getElementById('B');
	if(A.value == ""){
		alert("请输入起点名称！");
		A.focus();
		return false;
	}
	if(B.value == ""){
		alert("请输入终点名称！");
		B.focus();
		return false;
	}
	if(A.value == B.value){
		alert("起点名称和终点名称不允许相同！");
		B.focus();
		return false;
	}
	if (checkSite(A.value)==false){
		A.focus();
		return false;
	}
	if (checkSite(B.value)==false){
		B.focus();
		return false;
	}
	t.form.submit();
}

function so2(obj){
	if(obj._o.style.visibility!="hidden"){return false;}
	var L=document.getElementById('L');
	if(L.value==""){
		alert("请输入查询线路名称！");
		L.focus();
		return false;
	}
	if (checkLine(L.value)==false){
		L.focus();
		return false;
	}
}

function so3(obj){
	if(obj._o.style.visibility!="hidden"){return false;}
	var C=document.getElementById('C');
	if(C.value ==""){
		alert("请输入查询站点名称！");
		C.focus();
		return false;
	}
	if (checkSite(C.value)==false){
		C.focus();
		return false;
	}
}



function checkSite(site){//检查表单是否存在
	var f=false;
	var msg=jsAutoInstance._msg;
	for(var i=0; i<msg.length; i++){
		if(site==msg[i].split('~')[1]){
			f=true;
			break;
		}
	}
	if (f==false){
		alert("站点《"+site+"》不存在!\n请你重新输入或选择站点!!");
		return false;
	}
	else{
		return true;	
	}
}

function checkLine(line){//检查线路是否
	var f=false;
	var msg=suggest2._msg;
	for(var i=0; i<msg.length; i++){
		if(line==msg[i]){
			f=true;
			break;
		}
	}
	if (f==false){
		alert("线路《"+line+"》不存在!\n请你重新输入或选择线路!!");
		return false;
	}
	else{
		return true;	
	}
}

function addFavorite(){//加入收藏夹
	window.external.addFavorite(window.location.href,document.title);	
}

//////////////自动函数//////////////////////////////////
function doo(){//页页自动加载本函数
	showCats();
	ABC("letterA");	
	ABC("letterB");	
	ABC("letterC");	
}
window.onload=doo;

