function city(v,t,m,h){
	this.value =v;
	this.text =t;
	this.type =m;
	this.isHot = h;
}

function cls_inputBox(){
	this.cityArr=[]; 
	
	this.add=function(value,text,type,isHot){
		var leng = this.cityArr.length;
		this.cityArr[leng] = new city(value,text,type,isHot);
	};
	this.name=function(key){
		for(var i=0;i<this.cityArr.length;i++){
			if(this.cityArr[i].value==key){
				return this.cityArr[i].text;
			}
		}
		return "";
	};
	this.tvalue=function(t,idx){
		var n=0;
		for(var i=0;i<this.cityArr.length;i++){
			if(this.cityArr[i].type==t){
				if(n==idx){
					return this.cityArr[i].value;
				}
				n++;
			}
		}
		return "";
	}
}

var cList = new cls_inputBox();

function getCityData(){
	var tempCity = "";

	cList.add("Beijing","Beijing",0,1);
	cList.add("Chengdu","Chengdu",0,1);
	cList.add("Guangzhou","Guangzhou",0,1);
	cList.add("Guilin","Guilin",0,1);
	cList.add("Hangzhou","Hangzhou",0,1);
	cList.add("Kunming","Kunming",0,1);
	cList.add("Ningbo","Ningbo",0,1);
	cList.add("Shanghai","Shanghai",0,1);
	cList.add("Shenzhen","Shenzhen",0,1);
	cList.add("Xian","Xian",0,1);
	cList.add("Yangshuo","Yangshuo",0,1);
	cList.add("","-",0,1);
	
	for (var j = 0; j<hotelArea.DB.length;j++){
		if(tempCity != hotelArea.DB[j].city){
			var t=hotelArea.DB[j].city;
			var o=",Beijing,Shanghai,Guangzhou,Xian,Guilin,Shenzhen,Hangzhou,Kunming,Chengdu,Yangshuo,Ningbo,";
			if(o.indexOf("," + t + ",")>-1){
				//cList.add(t,t,0,1);
			}else{
				cList.add(t,t,0,1);
			}
			tempCity = hotelArea.DB[j].city;
		}
	}
}
getCityData();




/*
 ** ------------------ 程序部分 ----------------------
*/

/*
 ** public function --------------------------------------------
*/
function $obj(element){
    if(typeof element == "string"){
		return document.getElementById(element);
	}else{
		return null;
	}
}

function cElement(tagName){
	return document.createElement(tagName);
}

function offsetPosition(oObj){
 	var left = oObj.offsetLeft;
	var top = oObj.offsetTop;
	var oObjHeight = oObj.offsetHeight;
	
	var valueT = 0, valueL = 0;
    do{
		valueT += oObj.offsetTop || 0;
		valueL += oObj.offsetLeft || 0;
		oObj = oObj.offsetParent;
    }while(oObj);
	
	var isIE = document.all && window.external;
	if(isIE)valueT+=1;
	return {"left":valueL,"top": valueT + oObjHeight};
}

String.prototype.trim = function(){return this.replace(/(^\s+)|\s+$/g,"");};   
  
var selectMsg = "";		// "Please input a city or select one from the list!";
var noRecordMsg ="";	// "The city you input doesn't exist, please input or select a valid one!";
		
function focusEle(inputObj,type,theData){
	this.div = null;
	this.iframe = null;
	this.inputObj  = inputObj;
	this.type = type;
	if(typeof(theData)!="undefined"){
		this.Data = theData;
	}else{
		this.Data = cList;
	}
	
	var div = cElement("div");
		
	var msgDiv = cElement("div");
		msgDiv.innerHTML = selectMsg;
	
	this.msgDiv = msgDiv;
	
	var objIframe = cElement("iframe");
	objIframe.src = "/hotel/inc/js/m_InputBox.htm";
	objIframe.frameBorder = 0;
	objIframe.width = "160px";
	objIframe.height = "180px";	
	
	with(msgDiv.style){
		width = objIframe.width + "px";
		backgroundColor = "#FFFAE4";
		color = "#F10112";
		wordWrap = "break-word";
		display = "none";
	}
	
	div.appendChild(msgDiv);
	div.appendChild(objIframe);

	this.div = div;
	this.iframe = objIframe;
	
	document.body.appendChild(div);
	var poxx = offsetPosition(inputObj);
	with(div.style){
		display = "none";
		position = "absolute";
		margin = "1px 0 0 0";
	}
}



function reFocus(){
	this.foucsIndex = -1;	
	this.listDB = [];
	this.showAll = true;
	
	this.create = function(inputObj,type,theData){
		inputObj = $obj(inputObj);
		
		var fe = new focusEle(inputObj,type,theData);
		var leng = this.listDB.length;
		this.listDB[leng] = {};
		object = this;
		this.listDB[leng].Obj = fe;
		this.listDB[leng].inputObj = inputObj;
		object.listDB[leng].isfocus = 0;
		
		inputObj.autocomplete = "off";
		
		inputObj.onkeydown = function(event){
			object.showAll = false;
			
			e=window.event?window.event:arguments[0];
			var inputKeyCode = e.keyCode || e.which;
			try{
				if(object.listDB[object.focusIndex].Obj.div.style.display == "none"){
					object.initEvent(object.focusIndex);
				}
			}catch(e){
				return false;
			}

			// 监听向上键
			if (inputKeyCode == 38) {	
				object.keysUp();
				return false;
			}
			// 监听向下键
			if (inputKeyCode == 40){
				object.keysDown();
				return false;
			}
			// enter 键
			if (inputKeyCode == 13){
				var childNodes = object.listDB[leng].resultObj.getElementsByTagName("li");
				if (childNodes.length > 0){
					if(object.listDB[object.focusIndex].cursor>-1){						
						var tval=object.listDB[object.focusIndex].Obj.Data.tvalue(object.listDB[object.focusIndex].Obj.type,object.listDB[object.focusIndex].cursor);
						if(tval!="")
							object.listDB[object.focusIndex].inputObj.value = childNodes[object.listDB[object.focusIndex].cursor].innerHTML.replace("---","").trim();
					}
				}
				object.closeShowBox(object.focusIndex);
				return false;
			}
		}

		inputObj.onclick = function(){
			object.showAll = true;
			this.select();
			object.initEvent(leng);
		}
		
		inputObj.onfocus = function(){
			object.initEvent(leng);
		}
	}

	this.initEvent = function(leng){
		object.focusIndex = leng;
		object.checkFocus(leng);

		object.listDB[leng].Obj.div.style.display = "block";
		object.listDB[leng].isfocus = 0;
		object.listDB[leng].isBlur = 0;
		object.listDB[leng].lastValue = "";

		try{
			object.listDB[leng].resultObj = object.listDB[leng].Obj.iframe.contentWindow.getObject();
		}catch (e){
			return false;
		}	
		
		var poxx = offsetPosition(object.listDB[leng].inputObj);
		with(object.listDB[leng].Obj.div.style){
			top = poxx.top + "px";
			left = poxx.left + "px";
			position = "absolute";
			padding = "1px";
			background = "#FFFFFF";
			border = "1px solid #80a66e";
		}

		document.onclick = function (){
			object.listDB[leng].Obj.div.style.display = "block";			
		}
		
		object.listDB[leng].inputObj.onblur = function(){
			object.listDB[leng].isBlur = 1;
			object.checkBlur(leng);
		}

		object.listDB[leng].Obj.div.onmouseover = function(){
			object.listDB[leng].isfocus = 1;
		}

		object.listDB[leng].Obj.div.onmouseout = function(){
			object.listDB[leng].isfocus = 0;
			document.onclick = function(){
				object.listDB[leng].Obj.div.style.display = "none";
			}
		}

/*
**     输入监测区
*/
		if (object.listDB[leng].inputObj.value == ""){
			object.hotCity();
		}
		object.listDB[leng].intervalHandle = window.setInterval(object.filter,100);
	}
	
	this.filter = function (){
		//	alert(this.focusIndex);
		inputValue = object.listDB[object.focusIndex].inputObj.value;
		lastValue = object.listDB[object.focusIndex].lastValue;

		if (inputValue != lastValue){
			list = cElement("ul");
			var cNum = 0;
			var div = cElement("div");
			var inputLeng = inputValue.length;
		
			object.listDB[object.focusIndex].lastValue = inputValue;
			for(var i =0; i<object.listDB[object.focusIndex].Obj.Data.cityArr.length;i++){
				if(object.listDB[object.focusIndex].Obj.Data.cityArr[i].type==object.listDB[object.focusIndex].Obj.type){
					if (object.listDB[object.focusIndex].Obj.Data.cityArr[i].text.substr(0,inputLeng).toLowerCase() == inputValue.toLowerCase() || object.listDB[object.focusIndex].Obj.Data.cityArr[i].value.substr(0,inputLeng).toLowerCase() == inputValue.toLowerCase() || object.showAll){
						var lis = document.createElement("li");
						
						if(object.listDB[object.focusIndex].Obj.Data.cityArr[i].text=="-"){
							lis.innerHTML = "<hr />";
							lis.setAttribute ("va","");
						}else{
							lis.innerHTML = object.listDB[object.focusIndex].Obj.Data.cityArr[i].text;
							lis.setAttribute ("va",object.listDB[object.focusIndex].Obj.Data.cityArr[i].value);
						}
						list.appendChild(lis);
						cNum ++;
					}
				}
			}
			
			if (cNum>0){
				if (object.listDB[object.focusIndex].inputObj.value == ""){
					object.listDB[object.focusIndex].Obj.msgDiv.innerHTML = selectMsg;
					object.hotCity();
				}
				div.appendChild(list);
			
				object.listDB[object.focusIndex].resultObj.innerHTML = div.innerHTML;
				object.listDB[object.focusIndex].cursor = -1;
				object.displayResult();
			}else{
				object.listDB[object.focusIndex].Obj.msgDiv.innerHTML = noRecordMsg;
			}
		}
	}

	this.hotCity = function (){
		list = cElement("ul");
		var cNum = 0;
		var div = cElement("div");

		for(var i =0; i<object.listDB[object.focusIndex].Obj.Data.cityArr.length;i++){
			if(object.listDB[object.focusIndex].Obj.Data.cityArr[i].type==object.listDB[object.focusIndex].Obj.type){
				if (object.listDB[object.focusIndex].Obj.Data.cityArr[i].isHot == 1){
					var lis = document.createElement("li");
					
					if(object.listDB[object.focusIndex].Obj.Data.cityArr[i].text=="-"){
						lis.innerHTML = "<hr />";
						lis.setAttribute ("va","");
					}else{
						lis.innerHTML = object.listDB[object.focusIndex].Obj.Data.cityArr[i].text;
						lis.setAttribute ("va",object.listDB[object.focusIndex].Obj.Data.cityArr[i].value);
					}
					list.appendChild(lis);
					cNum ++;
				}
			}
		}		
		div.appendChild(list);

		object.listDB[object.focusIndex].resultObj.innerHTML = div.innerHTML;
		object.listDB[object.focusIndex].cursor = -1;
		object.displayResult();
	}
	
	this.displayResult = function (){
		var childNodes = object.listDB[object.focusIndex].resultObj.getElementsByTagName("li");
		var defaultIndex=-1;
		for ( var i = 0;i<childNodes.length;i++){
			childNodes[i].setAttribute("idx",i);
			with (childNodes[i].style){
				padding ="2px 0 2px 3px";
				color = "#686868";
			}
			
			childNodes[i].onmouseover = function (){
				var childNodes = object.listDB[object.focusIndex].resultObj.getElementsByTagName("li");
				if(object.listDB[object.focusIndex].cursor>-1)
					childNodes[object.listDB[object.focusIndex].cursor].onmouseout();
				
				object.listDB[object.focusIndex].cursor = this.getAttribute("idx");
				with (this.style){
					backgroundColor = "#FFEAAB";
					color = "#8F2C0F";
				}
			}
			
			childNodes[i].onmouseout = function (){
				with (this.style){
					backgroundColor = "#FFFFFF";
					color = "#686868";
				}
			}
			
			childNodes[i].onclick = function (){
				var tval=object.listDB[object.focusIndex].Obj.Data.tvalue(object.listDB[object.focusIndex].Obj.type,object.listDB[object.focusIndex].cursor);
				if(tval!="")
					object.listDB[object.focusIndex].inputObj.value = this.innerHTML.replace("---","").trim();
				object.closeShowBox(object.focusIndex);
			}
			
			if(object.listDB[object.focusIndex].inputObj.value.trim().toLowerCase()==childNodes[i].innerHTML.replace("---","").trim().toLowerCase()){
				defaultIndex = i;
			}
		}
		if(defaultIndex>-1){			
			object.listDB[object.focusIndex].Obj.iframe.contentWindow.scroll(0,offsetPosition(childNodes[defaultIndex]).top-childNodes[defaultIndex].offsetHeight);
			childNodes[defaultIndex].onmouseover();
		}else{
			childNodes[0].onmouseover();
		}
	}

	this.keysUp = function (){
		var childNodes = object.listDB[object.focusIndex].resultObj.getElementsByTagName("li");
		leng = childNodes.length;
		
		if(object.listDB[object.focusIndex].cursor>-1)
			childNodes[object.listDB[object.focusIndex].cursor].onmouseout();
		
		if(object.listDB[object.focusIndex].cursor> 0){
			object.listDB[object.focusIndex].cursor --;
			childNodes[object.listDB[object.focusIndex].cursor].onmouseover();
		}else{
			object.listDB[object.focusIndex].cursor = leng-1;
			childNodes[object.listDB[object.focusIndex].cursor].onmouseover();
		}
	}

	this.keysDown = function (){
		var childNodes = object.listDB[object.focusIndex].resultObj.getElementsByTagName("li");
		leng = childNodes.length;
		
		if(object.listDB[object.focusIndex].cursor>-1)
			childNodes[object.listDB[object.focusIndex].cursor].onmouseout();
		
		if (object.listDB[object.focusIndex].cursor <leng-1){
			object.listDB[object.focusIndex].cursor ++;
			childNodes[object.listDB[object.focusIndex].cursor].onmouseover();
		}else{
			object.listDB[object.focusIndex].cursor = 0;
			childNodes[object.listDB[object.focusIndex].cursor].onmouseover();
		}
	}

	this.checkFocus = function (leng){
		for(var i = 0;i<this.listDB.length;i++){
			if (i != leng){
				this.closeShowBox(i);
			}
		}
	}

	this.checkBlur = function (leng){
		if(this.listDB[leng].isfocus != 1){
			this.closeShowBox(leng);
		}	
	}
	
	this.closeShowBox  = function (leng){
		window.clearInterval(object.listDB[leng].intervalHandle);
		object.listDB[leng].Obj.div.style.display = "none";
		document.onclick = function (){
			object.listDB[leng].Obj.div.style.display = "none";
		}
		//预订页面验证扩展
		if(object.listDB[leng].inputObj.id=="nation"){
			checkform('nation');
		};
	}
}