
function CheckHotelSearch()
{
      d1=new Date($("startDate" ).value.replace("-","/"));
      d2=new Date($("returnDate" ).value.replace("-","/"));
       if( d1 > d2) 
       {
		alert('对不起，离店日期在入住日期前面去了！');
		var returnDate=g$("returnDate");
        ShowTwoMonthList(returnDate,0);
		return false;
       }
    return true;
}


function g$(fctId){	return document.getElementById(fctId);}
function n$(fctId){	return document.getElementsByName(fctId);}
function c$(fctId,fctClassName){
	var varTempDivObj=g$(fctId);
	if(!varTempDivObj){
		GetPyzyIframe("ifm"+fctId);
		varTempDivObj=document.createElement("div");
		varTempDivObj.id=fctId;
		if(fctClassName && fctClassName!="")varTempDivObj.className=fctClassName;
		document.body.appendChild(varTempDivObj);
	}
	return varTempDivObj;
}

//取得某ID的iframe对象，若不存在该ID的对象则自动创建
function GetPyzyIframe(fctIfmId,fctVisibility,fctTop,fctLeft,fctWidth,fctHeight){
	var varTempIfmObj=g$(fctIfmId);
	if(!varTempIfmObj){
		varTempIfmObj=document.createElement("iframe");
		varTempIfmObj.id=fctIfmId;
		varTempIfmObj.style.position="absolute";
		varTempIfmObj.style.zIndex="1";
		varTempIfmObj.style.visibility="hidden";
		document.body.appendChild(varTempIfmObj);
	}
	if(fctTop)varTempIfmObj.style.top=fctTop+"px";
	if(fctLeft)varTempIfmObj.style.left=fctLeft+"px";
	if(fctWidth)varTempIfmObj.style.width=fctWidth+"px";
	if(fctHeight)varTempIfmObj.style.height=fctHeight+"px";
	if(fctVisibility)varTempIfmObj.style.visibility=(document.all?fctVisibility:"hidden	");
	return varTempIfmObj;
}

//取得某对象的坐标位置、宽、高
function getPosition(obj){
	var top=0;
	var left=0;
	var width=obj.offsetWidth;
	var height=obj.offsetHeight;
	while(obj.offsetParent){
		top+=obj.offsetTop;
		left+=obj.offsetLeft;
		obj=obj.offsetParent;
	}
	return{"top":top,"left":left,"width":width,"height":height};
}

//取得编码存储框对象
function GetValueToInputObj(fctThisObj){
	if(!fctThisObj)return null;
	var varThisObjAutoInput=(fctThisObj.getAttributeNode("value_to_input")?fctThisObj.getAttributeNode("value_to_input").value:"");
	if(varThisObjAutoInput=="")return null;
	return g$(varThisObjAutoInput);
}

//自动触发下一个对象的Act事件
function AutoNextInputAct(fctThisObj,fctAct){
	var varNextInput=fctThisObj.getAttributeNode("nextinput");
	if(varNextInput && varNextInput!=""){
		if(document.all){
			eval("g$('"+varNextInput.value+"')."+fctAct+"()");
		}else{
			var evt = document.createEvent("MouseEvents");
			evt.initEvent(fctAct,true,true);
			g$(varNextInput.value).dispatchEvent(evt);
		}
		g$(varNextInput.value).focus();
	}
}

//给某对象的某事件增加处理函数AddFunToObj(document,"onclick","alert('1');")
function AddFunToObj(fctObj,fctAct,fctFunction){
	if(fctObj.addEventListener){ //!IE
		fctObj.addEventListener(fctAct.replace("on",""),function(e){
			e.cancelBubble=!eval(fctFunction);
		},false);
	}else if(fctObj.attachEvent){ //IE
		fctObj.attachEvent(fctAct,function(){
			return eval(fctFunction);
		});
	}
}

function matchCityItems(cityDataSplitI, objValue) {
    var cityArr = cityDataSplitI.split("|"); 
    for (var i = 0; i < cityArr.length; i++) {       
        if (cityArr[i].toUpperCase().substr(0, objValue.length) == objValue.toUpperCase())
            return true;
    }
    return false;
}
var varPageId=0;
//生成并显示出城市下拉菜单
function GetCityList(fctThisObj,address,pos,even){
/*
fctThisObj 自动完成对象；
pos 指定定位方式，默认与表单对象左侧对齐，若为'R'则基于表单对象右侧对齐
even 指定触发方式，若为'click'，则清空input值

modifyed by youhua@staff Tel:5947 @ 090707
**/


	var varMenuObj=c$("divAddressMenu");
	var varThisObj=fctThisObj;
	var EventType = even;
	if(varThisObj.id=="menuPageS"||varThisObj.id=="menuPageE"){
		varThisObj=varMenuObj.obj;
	}else{
		varPageId=0;
	}
	//清除已选城市Value
	var varThisObjAutoInput=GetValueToInputObj(varThisObj); //取得城市编码值存储对象
	if(EventType=='click'){varThisObjAutoInput.value="";varThisObj.value=""};//清除输入框值by youhuata 090629
	//取得城市数据并拆解为数组
	var varObjValue=varThisObj.value;
	var varThisObjAdd=(varThisObj.getAttributeNode("mod_address_suggest")?varThisObj.getAttributeNode("mod_address_suggest").value:"");
	var varData=(varObjValue==""?(varThisObjAdd==""?(address=="varAddress"?varAddress:cityAddress):varThisObjAdd):(address=="varAddress"?varAddress:cityAddress));
	var varHtmlStr="",varCityDataSplit=varData.split("@"),varCityDataSplitI,varCityDataSplitIu,varNextPageStr="";
	//存储当前操作对象
	varMenuObj.obj=varThisObj;
	var varPageRCount=(varThisObj.getAttributeNode("pagecount")?parseInt(varThisObj.getAttributeNode("pagecount").value,10):18);
	var varThisPageI = 0; 
	for(var i=1;i<varCityDataSplit.length-1;i++){
	    varCityDataSplitI = varCityDataSplit[i];
	    
		if (matchCityItems(varCityDataSplitI, varObjValue) || varObjValue == "" || i == varObjValue) { // || varCityDataSplitI.toLowerCase().indexOf(varObjValue.toLowerCase())>=0
			varThisPageI+=1;
			if(varThisPageI>varPageId*varPageRCount && varThisPageI<=(varPageId+1)*varPageRCount){
				varCityDataSplitISplit=varCityDataSplitI.split("|");
               if (address=="varAddress")
                {	
            		varHtmlStr+="<a href='javascript:;' onclick='WriteCity("+varThisPageI+")' id='menuA"+varThisPageI+"' title='"+varCityDataSplitI+"'><span>"+varCityDataSplitISplit[1]+"("+varCityDataSplitISplit[3]+")</span><em>"+varCityDataSplitISplit[0]+"</em></a>";
                }
                else
                {
            		varHtmlStr+="<a href='javascript:;' onclick='WriteCity("+varThisPageI+")' id='menuA"+varThisPageI+"' title='"+varCityDataSplitI+"'><span>"+varCityDataSplitISplit[1]+"("+varCityDataSplitISplit[2]+")</span><em>"+varCityDataSplitISplit[0]+"</em></a>";
                 }
			}
		}
	}
	if(varThisPageI>varPageRCount){
		varNextPageStr="&nbsp;<b id=menuPageS style="+(varPageId>0?"cursor:pointer;":"color:#666666;")+">&lt;&lt;&lt;&nbsp;上一页</b>　"
		varNextPageStr+="<b id=menuPageE style="+(varThisPageI>(varPageId+1)*varPageRCount?"cursor:pointer;":"color:#666666;")+">下一页&nbsp;&gt;&gt;&gt;</b>";
	}
	var varThisObjPosition=getPosition(varThisObj); //取得事件发生处控件坐标
	with(varMenuObj){
		style.top=varThisObjPosition.top+varThisObjPosition.height+"px";
		style.left=varThisObjPosition.left+"px";
		style.visibility="hidden";
		innerHTML="<div><h4>可输拼音/汉字/首字母</h4>"+(varHtmlStr==""?"<nobr>没有找到您查的城市‘"+varObjValue+"’。</nobr>":varHtmlStr+varNextPageStr)+"</div>";
	}
	var MenuWidth = getPosition(varMenuObj).width;
	var MenuL = varThisObjPosition.left;
	if(pos=='R'){
			varMenuObj.style.left=varThisObjPosition.left-(MenuWidth-varThisObjPosition.width)+'px';
			MenuL = varThisObjPosition.left-(MenuWidth-varThisObjPosition.width);
		}
	varMenuObj.style.visibility="visible";	
	GetPyzyIframe("ifm"+varMenuObj.id,"visible",(varThisObjPosition.top+varThisObjPosition.height),MenuL,varMenuObj.offsetWidth,varMenuObj.offsetHeight); //取Iframe
	return false;
}
//选择某城市
function WriteCity(fctI){
	var varMenuObj=c$("divAddressMenu");
	var varThisObj=varMenuObj.obj;
	var varMenuValue=g$("menuA"+fctI).title;
	varMenuValue=varMenuValue.split("|");
	varThisObj.value=varMenuValue[1];
	
	var varThisObjAutoInput=GetValueToInputObj(varThisObj); //取得城市编码值存储对象
	if(!varThisObjAutoInput)varThisObjAutoInput=varThisObj;
	varThisObjAutoInput.value=varMenuValue[2];
	
	if(typeof(fctI)=="number")AutoNextInputAct(varThisObj,"click");
	//_Hidden("1");
}
//隐藏城市列表
function _Hidden(e){
	e=e?e:event;
	var varMenuObj=c$("divAddressMenu");
	var varThisObj=varMenuObj.obj;
	if(varMenuObj.style.visibility!="hidden"){
		if(e){
			var EventOBJ=(e.srcElement?e.srcElement:e.target);
			if(EventOBJ.id=="menuPageS" && EventOBJ.style.color==""){ //如果点的是“上一页”则向上翻页
				varPageId=varPageId-1;
				GetCityList(EventOBJ);
			}
			if(EventOBJ.id=="menuPageE" && EventOBJ.style.color==""){ //如果点的是“下一页”则向下翻页
				varPageId=varPageId+1;
				GetCityList(EventOBJ);
			}
			if(varThisObj==EventOBJ || EventOBJ.id.indexOf("menuPage")==0 || EventOBJ.id.indexOf("divAddressMenu")==0) return false;
		}
		
		var varThisObjAutoInput=GetValueToInputObj(varThisObj); //取得城市编码值存储对象
		if(g$("menuA1")){ //如果下拉菜单中存在第一个城市
			if(!varThisObjAutoInput)varThisObjAutoInput=varThisObj;
			if(varThisObjAutoInput.value=="" || varThisObjAutoInput==varThisObj){ //编码框中无值
				WriteCity("1");
			}
		}else if(EventOBJ.id.indexOf("menuA")<0){ //如果下拉菜单中不存在满足条件的城市
			if(varThisObj)varThisObj.value="";
		}
		varMenuObj.style.visibility="hidden";
		GetPyzyIframe("ifm"+varMenuObj.id,"hidden","400","100");
	}
}
 

//取得日历列表，该函数输出的内容需要根据需求放置到特定的容器中，如浮动层、固定DIV…
function GetMonthHTML(fctStaDate,fctDate){
	if(!fctDate)fctDate=new Date(); //默认为当前日期
	var todaydate=new Date();
	var varYear=fctDate.getFullYear();
	var varMonth=fctDate.getMonth();
	var varNextMonth=new Date(varYear,varMonth+1,1);
	var varThisMonthButDay = new Date(varNextMonth-86400000); //本月最后一天

	var varThisDate,varThisWeekDay,varThisMonthHTML="";
	varThisMonthHTML+="<h1>"+varYear+"年"+(varMonth+1)+"月</h1>"
	varThisMonthHTML+="<div>日</div><div>一</div><div>二</div><div>三</div><div>四</div><div>五</div><div>六</div>"
	for(var DayI=1;DayI<=varThisMonthButDay.getDate();DayI++){
		varThisDate=new Date(varYear,varMonth,DayI);
		varThisWeekDay=varThisDate.getDay();
		if(DayI==1){
			for(var spcI=0;spcI<varThisWeekDay;spcI++){
				varThisMonthHTML+="<a></a>"
			}
		}
		addvarThisDate=new Date(varYear,varMonth,DayI+1);
       if( todaydate<addvarThisDate) 
       {
         varThisMonthHTML+="<a "+((fctStaDate && fctStaDate!="")?(varThisDate<fctStaDate?"old":""):"")+" href=javascript:; onclick='SelectDate(this)' title='"+varYear+"-"+(varMonth+1)+"-"+DayI+"'>"+DayI+"</a>";
	   }
	   else
	   {
        varThisMonthHTML+="<a>"+DayI+"</a>";
	    }
	}
	return '<div class="DateListBox">'+varThisMonthHTML+'</div>';
}
//选择日期
function SelectDate(fctAObj){
	if(fctAObj.href||fctAObj.className=="aSelect"){
		var varValueObj=g$("divPyzyDateBox").Obj;
		var returnDate=g$("returnDate");
		var varValueToObj=GetValueToInputObj(varValueObj);
		if(varValueToObj)varValueObj=varValueToObj;
			varValueObj.value=fctAObj.title;
		g$("divPyzyDateBox").style.visibility="hidden";
		g$("divPyzyDateBox").bodyclick="";
		GetPyzyIframe("ifmdivPyzyDateBox","hidden");
        if (varValueObj.id=="startDate")ShowTwoMonthList(returnDate,0);
        if (varValueObj.id=="returnDate")CheckHotelSearch();
	}
}
//隐藏日历
function HiddenDateBox(){
	if(g$("divPyzyDateBox")){
		if(g$("divPyzyDateBox").style.visibility!="hidden" && g$("divPyzyDateBox").bodyclick=="1"){
			g$("divPyzyDateBox").style.visibility="hidden";
			g$("divPyzyDateBox").bodyclick="";
			GetPyzyIframe("ifmdivPyzyDateBox","hidden");
		}else{
			g$("divPyzyDateBox").bodyclick="1";
		}
	}
}
//显示日历
function ShowTwoMonthList(fctThisObj,fctJNum,fctStaDate){
	if(!fctJNum)fctJNum=0;
	if(!fctThisObj)fctThisObj="";
	var varStaDate=null,varTheDate=new Date();
	if(fctStaDate||fctStaDate==""){
		varStaDate=new Date(new Date()-86400000); //如果没有定义具体可使用的开始时间则自定义为今天
		var varStaDateSplit=fctStaDate.split("-");
		if(varStaDateSplit.length==3){
			varStaDate=new Date(varStaDateSplit[0],parseInt(varStaDateSplit[1],10)-1,varStaDateSplit[2]);//fctStaDate=new Date();
		}
		varTheDate=varStaDate;
		if(fctStaDate=="")fctStaDate=varStaDate.getFullYear()+"-"+(varStaDate.getMonth()+1)+"-"+varStaDate.getDate();
	}
	var varShowTwoMonthHTML="";
	for(var i=0+fctJNum;i<2+fctJNum;i++){ // here to change the display monthes
		varShowTwoMonthHTML+=GetMonthHTML((varStaDate?varStaDate:""),new Date(varTheDate.getFullYear(),varTheDate.getMonth()+i,1));
	}
	if(varStaDate)varShowTwoMonthHTML=varShowTwoMonthHTML.replace(/old href/g,"style=color:#999 old");
	if(fctThisObj.value!=""){ //对已选日期样式进行改变
		if(/^((\d{4})|(\d{2}))-(\d{1,2})-(\d{1,2})$/g.test(fctThisObj.value))varShowTwoMonthHTML=varShowTwoMonthHTML.replace(fctThisObj.value,fctThisObj.value+"' class='aSelect");
	}
	var varDateBoxObj = c$("divPyzyDateBox","PyzyDateBox");
	varDateBoxObj.bodyclick="";
	if(fctThisObj!=""){ //调整坐标到合适位置
		var varThisObjPosition=getPosition(fctThisObj);
		varDateBoxObj.style.top=(varThisObjPosition.top+varThisObjPosition.height)+"px";
		varDateBoxObj.style.left=varThisObjPosition.left+"px";
		varDateBoxObj.style.visibility="visible"; //hidden
		varDateBoxObj.Obj=fctThisObj;
	}
	varDateBoxObj.innerHTML=varShowTwoMonthHTML+'<div class="ahsh" style="margin-left:'+(document.all?'-288':'-1')+'px;">&nbsp;<span style="padding-right:'+(document.all?'280':'232')+'px;cursor:pointer;" onclick="ShowTwoMonthList(null,'+(fctJNum-1)+(fctStaDate?',\''+fctStaDate+'\'':'')+')" title="上月"><<</span><span style="cursor:pointer;" onclick="ShowTwoMonthList(null,'+(fctJNum+1)+(fctStaDate?',\''+fctStaDate+'\'':'')+')" title="下月">>></span></div>';
	GetPyzyIframe("ifmdivPyzyDateBox","visible",(varThisObjPosition.top+varThisObjPosition.height),varThisObjPosition.left,varDateBoxObj.offsetWidth,varDateBoxObj.offsetHeight); //取Iframe
}
AddFunToObj(window,"onload","AddFunToObj(document,'onclick','_Hidden("+(document.all?"":"e")+");HiddenDateBox();');");
