//日历控件 单独出来 add by zhineng.zhuang

//  主调用函数是 setday(this,[object])和setday(this)，[object]是控件输出的控件名，举两个例子：
//  一、<input name=txt><input type=button value=setday onclick="setday(this,document.all.txt)">
//  二、<input onfocus="setday(this)">

var bMoveable=true;
var strFrame;

document.writeln('<iframe id=endDateLayer frameborder=0 width=300 height=190 style="position: absolute;  z-index: 9998; display: none"></iframe>');
strFrame='<style>';
strFrame+='INPUT.button{BORDER-RIGHT: #FF9900 1px solid;BORDER-TOP: #FF9900 1px solid;BORDER-LEFT: #FF9900 1px solid;';
strFrame+='BORDER-BOTTOM: #FF9900 1px solid;BACKGROUND-COLOR: #FF9900;font-family:宋体;}';
strFrame+='TD{FONT-SIZE: 12px;font-family:宋体;}';
strFrame+='.tbody{border-color:#FF9900; background-color:#FFFFFF; border-collapse:collapse; }';
strFrame+='</style>';
strFrame+='<scr' + 'ipt>';
strFrame+='var datelayerx,datelayery;';
strFrame+='var bDrag;';
strFrame+='function document.onmousemove()';
strFrame+='{if(bDrag && window.event.button==1)';
strFrame+='	{var DateLayer=parent.document.all.endDateLayer.style;';
strFrame+='		DateLayer.posLeft += window.event.clientX-datelayerx;';
strFrame+='		DateLayer.posTop += window.event.clientY-datelayery;}}';
strFrame+='function DragStart()';
strFrame+='{var DateLayer=parent.document.all.endDateLayer.style;';
strFrame+='	datelayerx=window.event.clientX;';
strFrame+='	datelayery=window.event.clientY;';
strFrame+='	bDrag=true;}';
strFrame+='function DragEnd(){';
strFrame+='	bDrag=false;}';
strFrame+='</scr' + 'ipt>';
strFrame+='<div style="z-index:9999;position: absolute; left:0; top:0;" onselectstart="return false">';
// 第一个月
strFrame+='<table class="tbody" border="1" width=284 height=150><tr><td width=142 ><table class="tbody" width=142>';
strFrame+='    <tr><td width=142 height=21  bgcolor=#FF9900>';
strFrame+='        <table height=21>';
strFrame+='            <tr align=center >';
strFrame+='                <td width=21 align="center"';
strFrame+='					   onclick="parent.meizzPrevM()" title="向前翻 1 月"><img src="/images/iconleft.jpg" width="21" height="20"></td>';
strFrame+='				   <td align="left">';
strFrame+='					   &nbsp;&nbsp;<span id=meizzYearHead></span><span id=meizzMonthHead></span></td>';
strFrame+='			   </tr>';
strFrame+='		   </table></td></tr>';
strFrame+='	   <tr><td width=142 height=18 >';
strFrame+='		   <table bgcolor="#FFFDDA">';
strFrame+='	   <tr><td width=20 align=center>日</td>';
strFrame+='<td width=20 align=center>一</td><td width=20 align=center>二</td>';
strFrame+='<td width=20 align=center>三</td><td width=20 align=center>四</td>';
strFrame+='<td width=20 align=center>五</td><td width=20 align=center>六</td></tr>';
strFrame+='</table></td></tr>';
strFrame+='  <tr ><td width=142 height=120>';
strFrame+='    <table width=142>';
var n=0; for (j=0;j<5;j++){ strFrame+= '<tr>'; for (i=0;i<7;i++){
strFrame+='<td width=20 height=20 align=center  id=meizzDay'+n+' style="font-size:12px;" onclick=parent.meizzDayClick(this.innerText,0)></td>';n++;}
strFrame+='</tr>';}
strFrame+='      <tr align=center>';
for (i=35;i<37;i++)strFrame+='<td width=20 height=20 id=meizzDay'+i+' style="font-size:12px;"  onclick="parent.meizzDayClick(this.innerText,0)"></td>';
strFrame+='        <td colspan=5 align=right></td></tr>';
strFrame+='    </table></td></tr></table></td>';
// 第二个月
strFrame+='   <td width=142><table class="tbody" width=142>';
strFrame+='    <tr><td width=142 height=21  bgcolor=#FF9900>';
strFrame+='        <table width=142 height=21>';
strFrame+='            <tr align=center >';
strFrame+='				   <td width=121 align="right">';
strFrame+='					   <span  id=meizzYearHead2></span><span id=meizzMonthHead2></span>&nbsp;&nbsp;</td>';
strFrame+='                <td width=21 align="center"';
strFrame+='					   onclick="parent.meizzNextM()" title="向后翻 1 月" ><img src="/images/iconright.jpg" width="21" height="20"></td>';
strFrame+='			   </tr>';
strFrame+='		   </table></td></tr>';
strFrame+='	   <tr><td width=142 height=18 >';
strFrame+='		   <table bgcolor="#FFFDDA">';
strFrame+='	   <tr><td width=20 align=center>日</td>';
strFrame+='<td width=20 align=center>一</td><td width=20 align=center>二</td>';
strFrame+='<td width=20 align=center>三</td><td width=20 align=center>四</td>';
strFrame+='<td width=20 align=center>五</td><td width=20 align=center>六</td></tr>';
strFrame+='</table></td></tr>';
strFrame+='  <tr ><td width=142 height=120>';
strFrame+='    <table width=142>';
var n=0; for (j=0;j<5;j++){ strFrame+= ' <tr align=center >'; for (i=0;i<7;i++){
strFrame+='<td width=20 height=20 id=meizzDayT'+n+' style="font-size:12px" onclick=parent.meizzDayTClick(this.innerText,0)></td>';n++;}
strFrame+='</tr>';}
strFrame+='      <tr align=center>';
for (i=35;i<37;i++)strFrame+='<td width=20 height=20 id=meizzDayT'+i+' style="font-size:12px"  onclick="parent.meizzDayTClick(this.innerText,0)"></td>';
strFrame+='        <td colspan=5 align=right style="color:#1478eb"></td></tr>';
strFrame+='    </table></td></tr>';
strFrame+='	</table></td></tr></table></div>';

window.frames.endDateLayer.document.writeln(strFrame);
window.frames.endDateLayer.document.close();		//解决ie进度条不结束的问题


//==================================================== WEB 页面显示部分 ======================================================
var outObject;
var outButton;		//点击的按钮

var outDate="";		//存放对象的日期
var odatelayer=window.frames.endDateLayer.document.all;		//存放日历对象
var odatelayer=window.endDateLayer.document.all;

function setday(tt,obj) //主调函数
{
	if (arguments.length > 2){alert("对不起！传入本控件的参数太多！");return;}
	if (arguments.length == 0){alert("对不起！您没有传回本控件任何参数！");return;}
	var dads = document.all.endDateLayer.style;
	var th = tt;
	var ttop = tt.offsetTop;	//TT控件的定位点高

	var thei = tt.clientHeight;	//TT控件本身的高
	var tleft = tt.offsetLeft;	//TT控件的定位点宽

	var ttyp = tt.type;	//TT控件的类型

	while (tt = tt.offsetParent){ttop+=tt.offsetTop; tleft+=tt.offsetLeft;}
	dads.top = (ttyp=="image") ? ttop+thei : ttop+thei+6;
	dads.left = tleft;
	// outObject = (arguments.length == 1) ? th : obj;
	// outButton = (arguments.length == 1) ? null : th;	//设定外部点击的按钮
	outObject = th;
	outButton = null;	

	//根据当前输入框的日期显示日历的年月
	var reg = /^(\d+)-(\d{1,2})-(\d{1,2})/;		//不含时间
	var r = (arguments.length == 1) ? outObject.value.match(reg) : obj.value.match(reg);
	if(r!=null){
		r[2]=r[2]-1;
		var d=new Date(r[1],r[2],r[3]);
		if(d.getFullYear()==r[1] && d.getMonth()==r[2] && d.getDate()==r[3])
		{
			outDate=d;
			parent.meizzTheYear = r[1];
			parent.meizzTheMonth = r[2];
			parent.meizzTheDate = r[3];
		}
		else
		{
			outDate="";
		}
		meizzSetDay(r[1],r[2]+1);
	}
	else
	{
		outDate="";
		meizzSetDay(new Date().getFullYear(), new Date().getMonth() + 1);
	}
	dads.display = '';

	meizzWriteHead(meizzTheYear,meizzTheMonth);

	try
	{
		event.returnValue=false;
	}
	catch (e)
	{
		//此处排除错误，错误原因暂未找到。
	}
}

var MonHead = new Array(12);	//定义阳历中每个月的最大天数

MonHead[0] = 31; MonHead[1] = 28; MonHead[2] = 31; MonHead[3] = 30; MonHead[4]  = 31; MonHead[5]  = 30;
MonHead[6] = 31; MonHead[7] = 31; MonHead[8] = 30; MonHead[9] = 31; MonHead[10] = 30; MonHead[11] = 31;

var meizzTheYear=new Date().getFullYear(); //定义年的变量的初始值

var meizzTheMonth=new Date().getMonth()+1; //定义月的变量的初始值

var meizzTheDate=new Date().getDate();	//定义日的变量的初始值

var meizzWDay=new Array(37);	//定义写日期的数组

var meizzWDayT=new Array(37);	//定义写日期的数组 第二个月

function document.onclick() //任意点击时关闭该控件	//ie6的情况可以由下面的切换焦点处理代替
{
	with(window.event)
	{
		if (srcElement != outObject && srcElement != outButton)
		closeLayer();
	}
}

function document.onkeyup()		//按Esc键关闭，切换焦点关闭
{
	if (window.event.keyCode==27){
		if(outObject)outObject.blur();
		closeLayer();
	}
	else if(document.activeElement)
	{
		if(document.activeElement != outObject && document.activeElement != outButton)
		{
			closeLayer();
		}
	}
}

function meizzWriteHead(yy,mm,ss)	//往 head 中写入当前的年与月
{
	odatelayer.meizzYearHead.innerText	= yy + ".";
	odatelayer.meizzMonthHead.innerText	= format(mm);
	if(mm==12){
	   var y2=parseInt(yy)+1;
	   odatelayer.meizzYearHead2.innerText	= y2 + ".";
	   odatelayer.meizzMonthHead2.innerText	=  1 ;
	}
	else{
		odatelayer.meizzYearHead2.innerText	= yy + ".";
		odatelayer.meizzMonthHead2.innerText = format(mm+1) ;
	}
}

function closeLayer()	//这个层的关闭
{
	var o = document.getElementById("endDateLayer");
	if (o != null)
	{
		o.style.display="none";
	}
}

function showLayer()	//这个层
{
	document.all.endDateLayer.style.display="";
}

function IsPinYear(year)	//判断是否闰平年
{
	if (0==year%4&&((year%100!=0)||(year%400==0))) return true;else return false;
}

function GetMonthCount(year,month)	//闰年二月为29天
{
	var c=MonHead[month-1];if((month==2)&&IsPinYear(year)) c++;return c;
}

function GetDOW(day,month,year)	//求某天的星期几
{
	var dt=new Date(year,month-1,day).getDay()/7; return dt;
}

function meizzPrevY()	//往前翻 Year
{
	if(meizzTheYear > 999 && meizzTheYear <10000){meizzTheYear--;}
	else{alert("年份超出范围（1000-9999）！");}
	meizzSetDay(meizzTheYear,meizzTheMonth);
}
function meizzNextY()	//往后翻 Year
{
	if(meizzTheYear > 999 && meizzTheYear <10000){meizzTheYear++;}
	else{alert("年份超出范围（1000-9999）！");}
	meizzSetDay(meizzTheYear,meizzTheMonth);
}
function setNull()
{
	outObject.value = '';
	closeLayer();
}
function meizzToday()	//Today Button
{
	parent.meizzTheYear		= new Date().getFullYear();
	parent.meizzTheMonth	= new Date().getMonth()+1;
	parent.meizzTheDate		= new Date().getDate();

	if (meizzTheMonth<10 && meizzTheMonth.length<2)	//格式化成两位数字
	{
		parent.meizzTheMonth="0"+parent.meizzTheMonth;
	}
	if (parent.meizzTheDate<10 && parent.meizzTheDate.length<2)	//格式化成两位数字
	{
		parent.meizzTheDate="0"+parent.meizzTheDate;
	}
	//meizzSetDay(meizzTheYear,meizzTheMonth);
	if(outObject)
	{
		outObject.value= parent.meizzTheYear + "-" + format( parent.meizzTheMonth) + "-" +
							format(parent.meizzTheDate); //注：在这里你可以输出改成你想要的格式

	}
	closeLayer();
}
function meizzPrevM()	//往前翻月份
{
	if(meizzTheMonth>1){meizzTheMonth--}else{meizzTheYear--;meizzTheMonth=12;}
	meizzSetDay(meizzTheYear,meizzTheMonth);
}
function meizzNextM()	//往后翻月份
{
	if(meizzTheMonth==12){meizzTheYear++;meizzTheMonth=1}else{meizzTheMonth++}
	meizzSetDay(meizzTheYear,meizzTheMonth);
}

// TODO: 整理代码
function meizzSetDay(yy,mm)	//主要的写程序**********
{
	meizzWriteHead(yy,mm);
	//设置当前年月的公共变量为传入值

	meizzTheYear=yy;
	meizzTheMonth=mm;
    //第一个月
	for (var i = 0; i < 37; i++){meizzWDay[i]=""};	//将显示框的内容全部清空

	var day1 = 1,day2=1,firstday = new Date(yy,mm-1,1).getDay();	//某月第一天的星期几


	for (i = firstday; day1 < GetMonthCount(yy,mm)+1; i++) { meizzWDay[i]=day1;day1++; }

	for (i = 0; i < 37; i++)
	{
		var da = eval("odatelayer.meizzDay"+i)	//书写新的一个月的日期星期排列

		//初始化
		da.style.backgroundColor="#ffffff";
		da.onmouseover=Function("onMouseOut(this)");
		da.onmouseout=Function("onMouseOut(this)");

		if (meizzWDay[i]!="")
		{
		//初始化边框
			da.style.color="#FF9900";

		//本月的部分
			da.innerHTML="<b>" + meizzWDay[i] + "</b>";
			da.title=mm +"月" + meizzWDay[i] + "日";
			da.onclick=Function("meizzDayClick(this.innerText,0)");		//给td赋予onclick事件的处理

			//如果是当前选择的日期，则显示亮蓝色的背景；如果是当前日期，则显示暗黄色背景
			if(!outDate){
				if((yy < new Date().getFullYear())||(yy == new Date().getFullYear() && mm < new Date().getMonth()+1 )||(yy == new    Date().getFullYear() && mm == new Date().getMonth()+1 && meizzWDay[i] < new Date().getDate())){
					da.style.color="#999999";
				}
			    else if((yy == new Date().getFullYear() && mm == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate())){
					da.style.backgroundColor="#FFF4D2";
					da.onmouseover=Function("onMouseOver(this)");
					da.onmouseout=Function("onMouseOutToday(this)");
				}
				else {
					da.onmouseover=Function("onMouseOver(this)");
				}
			}
			else
			{
				if((yy < new Date().getFullYear())||(yy == new Date().getFullYear() && mm < new Date().getMonth()+1 )||(yy == new    Date().getFullYear() && mm == new Date().getMonth()+1 && meizzWDay[i] < new Date().getDate())){
					da.style.color="#999999";
				}
				else if (yy == new Date().getFullYear() && mm == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate()){
					da.style.backgroundColor="#FFF4D2";
					da.onmouseover=Function("onMouseOver(this)");
					da.onmouseout=Function("onMouseOutToday(this)");
				}
				else if(yy==outDate.getFullYear() && mm== outDate.getMonth() + 1 && meizzWDay[i]==outDate.getDate()){
					da.style.backgroundColor="#FFCC66";
					da.onmouseover=Function("onMouseOver(this)");
					da.onmouseout=Function("onMouseOutSelected(this)");

				}
				else{
					da.onmouseover=Function("onMouseOver(this)");
				}
			}
			da.style.cursor="hand";
		}
		else { da.innerHTML="";da.style.backgroundColor="";da.style.cursor="default";}
	}
	//第二个月
	for (i = 0; i < 37; i++){meizzWDayT[i]=""};	//将显示框的内容全部清空

	day1 = 1,day2=1,firstday = new Date(yy,mm,1).getDay();	//某月第一天的星期几

    if(mm==12){
	  yy++;mm=1;
    }

	else{
	  mm++;
	}

	for (i = firstday; day1 < GetMonthCount(yy,mm)+1; i++) { meizzWDayT[i]=day1;day1++; }

	for (i = 0; i < 37; i++)
	{
		var da2 = eval("odatelayer.meizzDayT"+i)	//书写新的一个月的日期星期排列

		//初始化
		da2.style.backgroundColor="#ffffff";
		da2.onmouseover=Function("onMouseOut(this)");
		da2.onmouseout=Function("onMouseOut(this)");

		if (meizzWDayT[i]!="")
		{
		//初始化边框
			da2.style.color="#FF9900";

		//本月的部分
			da2.innerHTML="<b>" + meizzWDayT[i] + "</b>";
			da2.title=mm +"月" + meizzWDayT[i] + "日";
			da2.onclick=Function("meizzDayTClick(this.innerText,0)");		//给td赋予onclick事件的处理

			//如果是当前选择的日期，则显示亮蓝色的背景；如果是当前日期，则显示暗黄色背景
			if(!outDate){
				if((yy < new Date().getFullYear())||(yy == new Date().getFullYear() && mm < new Date().getMonth()+1 )||(yy == new    Date().getFullYear() && mm == new Date().getMonth()+1 && meizzWDayT[i] < new Date().getDate())){
					da2.style.color="#999999";
				}
				else if((yy == new Date().getFullYear() && mm == new Date().getMonth()+1 && meizzWDayT[i] == new Date().getDate())){
					da2.style.backgroundColor="#FFF4D2";
					da2.onmouseover=Function("onMouseOver(this)");
					da2.onmouseout=Function("onMouseOutToday(this)");
				}
				else {
					da2.onmouseover=Function("onMouseOver(this)");
				}
			}
			else
			{
				if((yy < new Date().getFullYear())||(yy == new Date().getFullYear() && mm < new Date().getMonth()+1 )||(yy == new    Date().getFullYear() && mm == new Date().getMonth()+1 && meizzWDayT[i] < new Date().getDate())){
					da2.style.color="#999999";
				}
				else if (yy == new Date().getFullYear() && mm == new Date().getMonth()+1 && meizzWDayT[i] == new Date().getDate()){
					da2.style.backgroundColor="#FFF4D2";
					da2.onmouseover=Function("onMouseOver(this)");
					da2.onmouseout=Function("onMouseOutToday(this)");
				}
				else if(yy==outDate.getFullYear() && mm== outDate.getMonth() + 1 && meizzWDayT[i]==outDate.getDate()){
					da2.style.backgroundColor="#FFCC66";
					da2.onmouseover=Function("onMouseOver(this)");
					da2.onmouseout=Function("onMouseOutSelected(this)");

				}
				else{
					da2.onmouseover=Function("onMouseOver(this)");
				}
			}
			da2.style.cursor="hand";
		}
		else { da2.innerHTML="";da2.style.backgroundColor="";da2.style.cursor="default"; }
	}
}

var meizzNextPopUpID = ""; // 如果非空串, 则设置完后自动弹出该ID对应的控件的日历
function meizzDayClick(n,ex)	//点击显示框选取日期，主输入函数*************
{
	parent.meizzTheDate=n;
	var yy=meizzTheYear;
	var mm = parseInt(meizzTheMonth)+ex;	//ex表示偏移量，用于选择上个月份和下个月份的日期
	//判断月份，并进行对应的处理

	if(mm<1){
		yy--;
		mm=12+mm;
	}
	else if(mm>12){
		yy++;
		mm=mm-12;
	}

	if (mm < 10)	{mm = "0" + mm;}

	if (outObject)
	{
		if (!n) {	//outObject.value="";
			return;}
		if ( n < 10){n = "0" + n;}

		WriteDateTo(yy,mm,n);

		var obj = null; 
		if(meizzNextPopUpID != null && meizzNextPopUpID != "") {
		  obj = document.getElementById(meizzNextPopUpID);
		  meizzNextPopUpID = "";
		}	

		closeLayer();
		
	    if(obj != null) {
	    	setday(obj, outObject);
	    }			
	}
	else {closeLayer(); alert("您所要输出的控件对象并不存在！");}
}



function meizzDayTClick(n,ex)	//点击显示框选取日期，主输入函数*************
{
	parent.meizzTheDate=n;
	var yy;
	var mm;
	if(meizzTheMonth==12){yy=parseInt(meizzTheYear)+1;mm=1;}
	else{yy=meizzTheYear;mm = parseInt(meizzTheMonth)+1+ex;}
	//判断月份，并进行对应的处理

	if(mm<1){
		yy--;
		mm=12+mm;
	}
	else if(mm>12){
		yy++;
		mm=mm-12;
	}

	if (mm < 10)	{mm = "0" + mm;}

	if (outObject)
	{
		if (!n) {	//outObject.value="";
			return;}
		if ( n < 10){n = "0" + n;}

		WriteDateTo(yy,mm,n);
		
		var obj = null; 
		if(meizzNextPopUpID != null && meizzNextPopUpID != "") {
		  obj = document.getElementById(meizzNextPopUpID);
		  meizzNextPopUpID = "";
		}		

		closeLayer();
		
	    if(obj != null) {
	    	setday(obj, outObject);
	    }			
	}
	else {closeLayer(); alert("您所要输出的控件对象并不存在！");}
}

function format(n)	//格式化数字为两位字符表示
{
	var m=new String();
	var tmp=new String(n);
	if (n<10 && tmp.length<2)
	{
		m="0"+n;
	}
	else
	{
		m=n;
	}
	return m;
}

function WriteDateTo(yy,mm,n)
{
	outObject.style.color="#000000";
	outObject.value= yy + "-" + format(mm) + "-" + format(n); //注：在这里你可以输出改成你想要的格式

}


function onMouseOver(obj){
	obj.style.backgroundColor="#FFCC66";
}

function onMouseOut(obj){
	obj.style.backgroundColor="#ffffff";
}

function onMouseOutToday(obj){
	obj.style.backgroundColor="#FFF4D2";
}
function onMouseOutSelected(obj){
	obj.style.backgroundColor="#FFCC66";
}

function showHint(obj,type,idName){

    if(type && type < 10){
        if(obj.value==""){
			obj.style.color="#C1C1C1";
			obj.value="";
		}
    }else{
        if(obj.value==""){
			obj.style.color="#000000";
            obj.value="";
		}
		if(type && type == 10) {
			if(arguments.length < 3) {
				meizzNextPopUpID = "outDate";
			} else {
				meizzNextPopUpID = idName;
			}
		} else {
			meizzNextPopUpID = "";
		}		
		setday(obj);
    }
}
