﻿function SetDate(objStr,startDayStr) {  
	var obj, startDay;
	//返回的日期串格式为"2009-5-2,2009-5-10,2009-5-30"末尾加上逗号可以避免indexOf('2009-5-1') 在字符串中获得匹配
	obj = (typeof(objStr) == "string" )? document.getElementById(objStr) : objStr;
	startDay = (typeof(startDayStr)!="undefined") ? document.getElementById(startDayStr).value :"thisDay";
	ShowTwoMonthList(obj,0,startDay); 
}

//本代码源自www.pyzy.net
function getElem(fctId){
	return document.getElementById(fctId);
}

//取得某对象，若提供ID下的对象不存在则自动创建
function createElem(fctId,fctClassName){
	var varTempDivObj=getElem(fctId);
	if(!varTempDivObj){
		GetIframe("ifm"+fctId);
		varTempDivObj=document.createElement("div");
		varTempDivObj.id=fctId;
		if(fctClassName && fctClassName!="")varTempDivObj.className=fctClassName;
		document.body.appendChild(varTempDivObj);	
		AddFunToObj(document,"onclick","HideDateBox(event)");
	}
	return varTempDivObj;
}

//取得某ID的iframe对象，若不存在该ID的对象则自动创建
function GetIframe(fctIfmId,fctVisibility,fctTop,fctLeft,fctWidth,fctHeight){
	var dateFrame=getElem(fctIfmId);
	if(!dateFrame){
		dateFrame=document.createElement("iframe");
		dateFrame.id=fctIfmId;
		dateFrame.style.position="absolute";
		dateFrame.style.zIndex="2";
		dateFrame.style.visibility="hidden";
		document.body.appendChild(dateFrame);
	}
	if(fctTop)dateFrame.style.top=fctTop+"px";
	if(fctLeft)dateFrame.style.left=fctLeft+"px";
	if(fctWidth)dateFrame.style.width=fctWidth+"px";
	if(fctHeight)dateFrame.style.height=fctHeight+"px";
	if(fctVisibility)dateFrame.style.visibility=(document.all?fctVisibility:"hidden	");
	return dateFrame;
}

//取得某对象的坐标位置、宽、高
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 getElem(varThisObjAutoInput);
}

//自动触发下一个对象的Act事件
function AutoNextInputAct(fctThisObj,fctAct){
	var varNextInput=fctThisObj.getAttributeNode("nextId");
	if(varNextInput != null && varNextInput.value != ""){
		if(document.all){
			eval("getElem('"+varNextInput.value+"')."+fctAct+"()");
		}
		else{
			var evt = document.createEvent("MouseEvents");
			evt.initEvent(fctAct,true,true);
			getElem(varNextInput.value).dispatchEvent(evt);
		}
		getElem(varNextInput.value).value="";
		getElem(varNextInput.value).focus();
	}
}

//给某对象的某事件增加处理函数AddFunToObj(document,"onclick","alert('1');")
function AddFunToObj(fctObj,fctAct,fctFunction){
	if(fctObj.addEventListener){ //!IE
		fctObj.addEventListener(fctAct.replace("on",""),function(e){
			try {e.cancelBubble=!eval(fctFunction); } catch(e) {}    //FF下第一次执行会找不到event
		},false);
	}
	else if(fctObj.attachEvent){ //IE
		fctObj.attachEvent (fctAct , function(){return eval(fctFunction); });
	}
}
//************通用函数************//

/**********日历选择功能*************/
document.write('\
	<style type="text/css">\
		.DateListBox{float:left;border:solid #BBD9E1 1px;width:147px !important;width:154px;height:168px !important;height:176px;font-size:12px;text-align:center;z-index:99999;}\
		.DateListBox h1{text-align:left; background-color:#F6FFF4;color:#3A7789;font-size:12px;height:20px;font-weight:bold;line-height:20px; margin:0px;}\
		.DateListBox div{float:left;border:solid #F6FFF4 1px;background-color:#7DB6C7;color:#FFFFFF;width:19px !important;width:20px;height:20px;font-size:12px;font-weight:bold;line-height:20px;vertical-align:middle;}\
		.DateListBox a{text-decoration:none;float:left;color:#3A7789;border:solid #ffffff 1px;background-color:#FFFFFF;width:19px !important;width:20px;height:19px !important;height:22px;font-size:12px;line-height:20px;vertical-align:middle;cursor:pointer;}\
		.DateListBox a:hover{border:solid #9DD1E6 1px;background-color:#DEF1F8;cursor:pointer;}\
		.DateListBox .aSelect{cursor:pointer;border:solid #9DD1E6 1px;background-color:#DEF1F8;color:#FF0000;}\
		.DateBox{position:absolute;z-index:99;visibility:hidden;background-color:#FFFFFF;border:solid #BBD9E1 1px;height:170px;width:298px !important;width:312px;}\
	    .DateListBox span { cursor:pointer; display:inline-block; }\
	</style>\
');

//取得日历列表，该函数输出的内容需要根据需求放置到特定的容器中，如浮动层、固定DIV…

function GetMonthHTML(fctStaDate,fctDate){
	if(!fctDate)fctDate=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 today = new Date();
	var todayStr = today.getFullYear() +"-" +  (today.getMonth() + 1)+"-" +today.getDate();

	var varThisDate,varThisWeekDay; 
	var MonthHtml ="<h1>"+varYear+"&nbsp; "+getMonthString(varMonth+1)+"月</h1>"
    MonthHtml+= "<div>S</div><div>M</div><div>T</div><div>W</div><div>T</div><div>F</div><div>S</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++){
				MonthHtml+='<a style="cursor:default;background:none; border:solid #ffffff 1px;"></a>'
			}
		}
		var varThisDateStr = varThisDate.getFullYear()+"-"+(varThisDate.getMonth()+1)+"-"+varThisDate.getDate();
		var instrNum;
		var instrNumToday;
		var dayjudge = (varThisDate <= fctStaDate);

		if(todayStr == varThisDateStr) {
			MonthHtml += '<a ' + ((instrNumToday == -1 || dayjudge) ? 'style="color:#ff0000;cursor:default;background:none;border:solid #ffffff 1px;">' + DayI + '</a>' : 'style="color:#ff0000"  href="javascript:;" onclick="SelectDate(this)" title="' + varThisDateStr  + '" >' + DayI + '</a>');	
		} else {
			MonthHtml += '<a ' + ((fctStaDate && fctStaDate != "" && dayjudge) ? 'style="color:#999;cursor:default;background:none;border:solid #ffffff 1px;">' + DayI + '</a>' : 'style="color:#3A7789"  href="javascript:;" onclick="SelectDate(this)" title="' + varThisDateStr  + '" >' + DayI + '</a>');	
		} 
	}
	return '<div class="DateListBox">'+MonthHtml+'</div>';
}
//选择日期
function SelectDate(fctAObj){
	if(fctAObj.href||fctAObj.className=="aSelect"){
		var InputBox=getElem("divDateBox").Obj;
		var varValueToObj=GetValueToInputObj(InputBox);
		if(varValueToObj)InputBox=varValueToObj;
		if(InputBox.value!=fctAObj.title){
			InputBox.value=fctAObj.title;
		}
		getElem("divDateBox").style.visibility="hidden";
		getElem("divDateBox").bodyclick="";
		GetIframe("ifmdivDateBox","hidden");
		AutoNextInputAct(getElem("divDateBox").Obj,"click");
		try { DateChanged(); } catch(e) {}  //自定义时间改变后的处理
		if(InputBox.id=='txtSDate') SetDate('txtEDate');
		if(InputBox.id=='ArriveDate') SetDate('LeaveDate');
		if(InputBox.id=='CommResv1_d1') SetDate('CommResv1_d2'); 
		if(InputBox.id=='d1') SetDate('d2'); 
	}
}
//隐藏日历
function HideDateBox(e){
    var ev = e || event;  
    var t = ev.target || ev.srcElement; 
    if(t.tagName=="INPUT") return;
	if(getElem("divDateBox")){
		if(getElem("divDateBox").style.visibility!="hidden" && getElem("divDateBox").bodyclick=="1"){
			getElem("divDateBox").style.visibility="hidden";
			getElem("divDateBox").bodyclick="";
			GetIframe("ifmdivDateBox","hidden");
		}
		else getElem("divDateBox").bodyclick="1";
	}
}

//显示日历
function ShowTwoMonthList(fctThisObj,Index,fctStaDate){
	if(!Index)Index=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=Index;i<2+Index;i++){
		varShowTwoMonthHTML+=GetMonthHTML((varStaDate?varStaDate:""),new Date(varTheDate.getFullYear(),varTheDate.getMonth()+i,1));
	}

	if(fctThisObj.value!=""){ //对已选日期样式进行改变
		if(/\d{4}|\d{2}-\d{1,2}-\d{1,2}\"/.test(fctThisObj.value+"\""))varShowTwoMonthHTML=varShowTwoMonthHTML.replace(fctThisObj.value,fctThisObj.value+"\" class=\"aSelect\"");
	}
	var varDateBoxObj = createElem("divDateBox","DateBox");
	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;
	GetIframe("ifmdivDateBox","visible",varDateBoxObj.style.top.replace(/px/,""),varDateBoxObj.style.left.replace(/px/,""),varDateBoxObj.offsetWidth,varDateBoxObj.offsetHeight); //取Iframe
    
//    F("divDateBox").onclick = function(e) { stopBubble(e);}
    try { DateListShow(); } catch(e){}   //自定义日期列表显示后的操作
    var head =  F('divDateBox').getElementsByTagName('h1');
    head[1].style.textAlign = 'right'; 
    head[0].innerHTML = '<span style="margin-left:5px" onclick="ShowTwoMonthList(null,'+ (Index-1) +')" title="上个月">&lt;&lt;</span><span style="padding-left:20px">' + head[0].innerHTML + '</span>';
    head[1].innerHTML = '<span style="padding-right:20px">' + head[1].innerHTML + '</span><span style="margin-right:5px" onclick="ShowTwoMonthList(null,'+ (Index+1) +')" title="下个月">&gt;&gt;</span>'; 
}

function stopBubble(e)  //取消事件上冒
{
    if(!document.all)/* 非IE */
    {
        e.stopPropagation();/* 标准W3C的取消冒泡 */
        e.preventDefault(); /* 取消默认行为 */
    }
    else
    {
        event.cancelBubble = true;   /* IE的取消冒泡方式 */
        event.returnValue = false; /* IE的取消默认行为，如<a>的转向地址，类似于return false */
    }
}
/**********日历选择功能 End*************/
function getEnglishMonthString(n){
    var month;
    if(n==1) month = "January";
    if(n==2) month = "February";
    if(n==3) month = "March"; 
    if(n==4) month = "April"; 
    if(n==5) month = "May";
    if(n==6) month = "June";
    if(n==7) month = "July";
    if(n==8) month = "August"; 
    if(n==9) month = "September";
    if(n==10) month = "October";
    if(n==11) month = "November";
    if(n==12) month = "December";  
    return month; 
}

function getMonthString(n){
    var month;
    if(n==1) month = "一";
    if(n==2) month = "二";
    if(n==3) month = "三"; 
    if(n==4) month = "四"; 
    if(n==5) month = "五";
    if(n==6) month = "六";
    if(n==7) month = "七";
    if(n==8) month = "八"; 
    if(n==9) month = "九";
    if(n==10) month = "十";
    if(n==11) month = "十一";
    if(n==12) month = "十二";  
    return month; 
}

