//============================================================================
//  主调用函数是 setday(this,[object])和setday(this)，[object]是控件输出的控件名，举两个例子：
//  一、<input name=txt><input type=button value=setday onclick="setday(this,document.all.txt)">
//  二、<input onfocus="setday(this)">
//  本日历的年份限制是（1000 - 9999）
//  按ESC键关闭该控件
//  在年和月的显示地方点击时会分别出年与月的下拉框
//  控件外任意点击一点即可关闭该控件
// 说明：受到iframe的限制，如果拖动出日历窗口，则日历会停止移动。

//==================================================== 参数设定部分 =======================================================
var bMoveable=true;		//设置日历是否可以拖动
var _VersionInfo=""	//版本信息

//==================================================== WEB 页面显示部分 =====================================================
var strFrame;		//存放日历层的HTML代码
document.writeln('<iframe id=meizzDateLayer frameborder=0 style="position: absolute; width: 144; height: 211; z-index: 9998; display: none"></iframe>');

strFrame='<style>';
strFrame+='INPUT.button{BORDER-RIGHT: #C6D7F1 1px solid;BORDER-TOP: #C6D7F1 1px solid;BORDER-LEFT: #C6D7F1 1px solid;';
strFrame+='BORDER-BOTTOM: #C6D7F1 1px solid;BACKGROUND-COLOR: #fff8ec;font-family:宋体;}';
strFrame+='TD{FONT-SIZE: 9pt;font-family:宋体;}';
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.meizzDateLayer.style;';
strFrame+='		DateLayer.posLeft += window.event.clientX-datelayerx;/*由于每次移动以后鼠标位置都恢复为初始的位置，因此写法与div中不同*/';
strFrame+='		DateLayer.posTop += window.event.clientY-datelayery;}}';
strFrame+='function DragStart()		/*开始日历拖动*/';
strFrame+='{var DateLayer=parent.document.all.meizzDateLayer.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"><span id=tmpSelectYearLayer style="z-index: 9999;position: absolute;top: 3; left: 19;display: none"></span>';
strFrame+='<span id=tmpSelectMonthLayer style="z-index: 9999;position: absolute;top: 3; left: 78;display: none"></span>';
strFrame+='<table border=1 cellspacing=0 cellpadding=0 width=142 height=160 bordercolor=#C6D7F1 bgcolor=#C6D7F1>';
strFrame+='  <tr><td width=142 height=23 bgcolor=#FFFFFF><table border=0 cellspacing=1 cellpadding=0 width=140 height=23>';
strFrame+='      <tr align=center><td width=16 align=center bgcolor=#C6D7F1 style="font-size:12px;cursor: hand;color: #ffffff" ';
strFrame+='        onclick="parent.meizzPrevM()" title="向前翻 1 月"><b>&lt;</b>';
strFrame+='        </td><td width=60 align=center style="font-size:12px;cursor:default" ';
strFrame+='onmouseover="style.backgroundColor=\'#FFD700\'" onmouseout="style.backgroundColor=\'white\'" ';
strFrame+='onclick="parent.tmpSelectYearInnerHTML(this.innerText.substring(0,4))" title="点击这里选择年份"><span id=meizzYearHead></span></td>';
strFrame+='<td width=48 align=center style="font-size:12px;cursor:default" onmouseover="style.backgroundColor=\'#FFD700\'" ';
strFrame+=' onmouseout="style.backgroundColor=\'white\'" onclick="parent.tmpSelectMonthInnerHTML(this.innerText.length==3?this.innerText.substring(0,1):this.innerText.substring(0,2))"';
strFrame+='        title="点击这里选择月份"><span id=meizzMonthHead></span></td>';
strFrame+='        <td width=16 bgcolor=#C6D7F1 align=center style="font-size:12px;cursor: hand;color: #ffffff" ';
strFrame+='         onclick="parent.meizzNextM()" title="向后翻 1 月"><b>&gt;</b></td></tr>';
strFrame+='    </table></td></tr>';
strFrame+='  <tr><td width=142 height=18>';
strFrame+='<table border=1 cellspacing=0 cellpadding=0 bgcolor=#C6D7F1 ' + (bMoveable? 'onmousedown="DragStart()" onmouseup="DragEnd()"':'');
strFrame+=' BORDERCOLORLIGHT=#C6D7F1 BORDERCOLORDARK=#FFFFFF width=140 height=20 style="cursor:' + (bMoveable ? 'move':'default') + '">';
strFrame+='<tr align=center valign=bottom><td style="font-size:12px;color:#FFFFFF">日</td>';
strFrame+='<td style="font-size:12px;color:#FFFFFF">一</td><td style="font-size:12px;color:#FFFFFF">二</td>';
strFrame+='<td style="font-size:12px;color:#FFFFFF">三</td><td style="font-size:12px;color:#FFFFFF">四</td>';
strFrame+='<td style="font-size:12px;color:#FFFFFF">五</td><td style="font-size:12px;color:#FFFFFF">六</td></tr>';
strFrame+='</table></td></tr>';
strFrame+='  <tr><td width=142 height=120>';
strFrame+='    <table border=1 cellspacing=2 cellpadding=0 BORDERCOLORLIGHT=#C6D7F1 BORDERCOLORDARK=#FFFFFF bgcolor=#fff8ec width=140 height=120>';

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=meizzDay'+n+' style="font-size:12px" onclick=parent.meizzDayClick(this.innerText,0)></td>';n++;}
	strFrame+='</tr>';
}

strFrame+='      <tr align=center>';

for (i=35;i<39;i++) strFrame+='<td width=20 height=20 id=meizzDay'+i+' style="font-size:12px" onclick="parent.meizzDayClick(this.innerText,0)"></td>';

strFrame+='        <td colspan=3 align=right><span onclick=parent.closeLayer() style="font-size:12px;cursor: hand"';
strFrame+='         title="' + _VersionInfo + '"><u>关闭</u></span>&nbsp;</td></tr>';
strFrame+='    </table></td></tr><tr><td>';
strFrame+='        <table border=0 cellspacing=1 cellpadding=0 width=100% bgcolor=#FFFFFF>';
strFrame+='          <tr><td align=left><input type=button class=button value="<<" title="向前翻 1 年" onclick="parent.meizzPrevY()" ';
strFrame+='             onfocus="this.blur()" style="font-size: 12px; height: 20px"><input class=button title="向前翻 1 月" type=button ';
strFrame+='             value="< " onclick="parent.meizzPrevM()" onfocus="this.blur()" style="font-size: 12px; height: 20px"></td><td ';
strFrame+='             align=center><input type=button class=button value=今天 onclick="parent.meizzToday()" ';
strFrame+='             onfocus="this.blur()" title="当前日期" style="font-size: 12px; height: 20px; cursor:hand">&nbsp;<input type=button class=button value="空" onclick="parent.meizzNull()" onfocus="this.blur()" title="空日期" style="font-size: 12px; height: 20px; cursor:hand" ></td><td ';
strFrame+='             align=right><input type=button class=button value=" >" onclick="parent.meizzNextM()" ';
strFrame+='             onfocus="this.blur()" title="向后翻 1 月" class=button style="font-size: 12px; height: 20px"><input ';
strFrame+='             type=button class=button value=">>" title="向后翻 1 年" onclick="parent.meizzNextY()"';
strFrame+='             onfocus="this.blur()" style="font-size: 12px; height: 20px"></td>';
strFrame+='</tr></table></td></tr></table></div>';

//==================================================== WEB 页面显示部分 ======================================================
var outObject;
var outButton;		//点击的按钮
var outDate="";		//存放对象的日期
var odatelayer = meizzDateLayer.document.all;		//存放日历对象

var __dateCBKFun = null;

window.frames.meizzDateLayer.document.open();
window.frames.meizzDateLayer.document.writeln(strFrame);
window.frames.meizzDateLayer.document.close(); //解决ie进度条不结束的问题



function setday(tt,obj, cbkFun) //主调函数
{
	if (arguments.length > 3)
	{
		alert("对不起！传入本控件的参数太多！");
		return false;
	}
	if (arguments.length == 0)
	{
		alert("对不起！您没有传回本控件任何参数！");
		return false;
	}

	if(typeof(cbkFun) != 'underfined') __dateCBKFun = cbkFun;
	else __dateCBKFun = null;

	var dads  = document.all.meizzDateLayer.style;
	var th = tt;
	var ttop  = tt.offsetTop;     //TT控件的定位点高
	var thei  = tt.clientHeight;  //TT控件本身的高
	var thew  = tt.clientWidth;   //TT控件本身的宽
	var tleft = tt.offsetLeft;    //TT控件的定位点宽
	var ttyp  = tt.type;          //TT控件的类型
	while (tt = tt.offsetParent){ttop+=tt.offsetTop; tleft+=tt.offsetLeft;}

	var t = ttop+thei;
	var l = tleft

	l = document.body.scrollLeft + window.event.x;
	t = document.body.scrollTop + window.event.y;
	if(l + 170 > document.body.scrollLeft+document.body.clientWidth)
		l = document.body.scrollLeft+document.body.clientWidth - 170;
	if(l < 0) l=0;
	if(t + 230 > document.body.scrollTop+document.body.clientHeight)
	t=document.body.scrollTop+document.body.clientHeight-230;
	if(t < 0) t=0;

	dads.top = t;
	dads.left = l;

	outObject = (arguments.length == 1) ? th : obj;
	outButton = (arguments.length == 1) ? null : th;	//设定外部点击的按钮
	//根据当前输入框的日期显示日历的年月
	var reg = /^(\d+)-(\d{1,2})-(\d{1,2})$/; 
	var r = outObject.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;		//保存外部传入的日期
		}
		else outDate="";
			meizzSetDay(r[1],r[2]+1);
	}
	else{
		outDate="";
		meizzSetDay(new Date().getFullYear(), new Date().getMonth() + 1);
	}
	dads.display = '';

	event.returnValue=false;
}

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 meizzWDay=new Array(39);               //定义写日期的数组

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)  //往 head 中写入当前的年与月
{
	var odatelayer = meizzDateLayer.document.all;
	odatelayer.meizzYearHead.innerText  = yy + " 年";
	odatelayer.meizzMonthHead.innerText = mm + " 月";
}

function tmpSelectYearInnerHTML(strYear) //年份的下拉框
{
	var odatelayer = meizzDateLayer.document.all;

	if (strYear.match(/\D/)!=null){alert("年份输入参数不是数字！");return;}
  var m = (strYear) ? strYear : new Date().getFullYear();
  if (m < 1000 || m > 9999) {alert("年份值不在 1000 到 9999 之间！");return;}
  var n = m - 30;
  if (n < 1000) n = 1000;
  if (n + 26 > 9999) n = 9974;
  var s = "<select name=tmpSelectYear style='font-size: 12px' "
     s += "onblur='document.all.tmpSelectYearLayer.style.display=\"none\"' "
     s += "onchange='document.all.tmpSelectYearLayer.style.display=\"none\";"
     s += "parent.meizzTheYear = this.value; parent.meizzSetDay(parent.meizzTheYear,parent.meizzTheMonth)'>\r\n";
  var selectInnerHTML = s;
  for (var i = n; i < n + 50; i++)
  {
    if (i == m)
       {selectInnerHTML += "<option value='" + i + "' selected>" + i + "年" + "</option>\r\n";}
    else {selectInnerHTML += "<option value='" + i + "'>" + i + "年" + "</option>\r\n";}
  }
  selectInnerHTML += "</select>";
  odatelayer.tmpSelectYearLayer.style.display="";
  odatelayer.tmpSelectYearLayer.innerHTML = selectInnerHTML;
  odatelayer.tmpSelectYear.focus();
}

function tmpSelectMonthInnerHTML(strMonth) //月份的下拉框
{
	var odatelayer = meizzDateLayer.document.all;

  if (strMonth.match(/\D/)!=null){alert("月份输入参数不是数字！");return;}
  var m = (strMonth) ? strMonth : new Date().getMonth() + 1;
  var s = "<select name=tmpSelectMonth style='font-size: 12px' "
     s += "onblur='document.all.tmpSelectMonthLayer.style.display=\"none\"' "
     s += "onchange='document.all.tmpSelectMonthLayer.style.display=\"none\";"
     s += "parent.meizzTheMonth = this.value; parent.meizzSetDay(parent.meizzTheYear,parent.meizzTheMonth)'>\r\n";
  var selectInnerHTML = s;
  for (var i = 1; i < 13; i++)
  {
    if (i == m)
       {selectInnerHTML += "<option value='"+i+"' selected>"+i+"月"+"</option>\r\n";}
    else {selectInnerHTML += "<option value='"+i+"'>"+i+"月"+"</option>\r\n";}
  }
  selectInnerHTML += "</select>";
  odatelayer.tmpSelectMonthLayer.style.display="";
  odatelayer.tmpSelectMonthLayer.innerHTML = selectInnerHTML;
  odatelayer.tmpSelectMonth.focus();
}

function closeLayer()               //这个层的关闭
{
	if( typeof(document.all.meizzDateLayer) != 'undefined' ) if( typeof(document.all.meizzDateLayer.style) != 'undefined' )  document.all.meizzDateLayer.style.display="none";
}

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 meizzToday()  //Today Button
  {
	var today;
    meizzTheYear = new Date().getFullYear();
    meizzTheMonth = new Date().getMonth()+1;
    today=new Date().getDate();
    //meizzSetDay(meizzTheYear,meizzTheMonth);
    if(outObject){
		outObject.value=meizzTheYear + "-" + meizzTheMonth + "-" + today;
    }
    closeLayer();
}
function meizzNull()  //Null Button
  {
    if(outObject){
		outObject.value="";
    }
    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);
}

/* 主要的写程序 */
function meizzSetDay(yy,mm)
{
	var odatelayer = meizzDateLayer.document.all;

	meizzWriteHead(yy,mm); //设置当前年月的公共变量为传入值

	meizzTheYear=yy;
	meizzTheMonth=mm;

	for (var i = 0; i < 39; i++){meizzWDay[i]=""};  //将显示框的内容全部清空

	var day1 = 1,day2=1,firstday = new Date(yy,mm-1,1).getDay();  //某月第一天的星期几

	for (i=0;i<firstday;i++)meizzWDay[i]=GetMonthCount(mm==1?yy-1:yy,mm==1?12:mm-1)-firstday+i+1	//上个月的最后几天

	for (i = firstday; day1 < GetMonthCount(yy,mm)+1; i++){meizzWDay[i]=day1;day1++;}

	for (i=firstday+GetMonthCount(yy,mm);i<39;i++){meizzWDay[i]=day2;day2++}

	for (i = 0; i < 39; i++)
	{
		var da = eval("odatelayer.meizzDay"+i); //书写新的一个月的日期星期排列
		if (meizzWDay[i]!="")
		{
			//初始化边框
			da.borderColorLight="#C6D7F1";
			da.borderColorDark="#FFFFFF";
			if(i<firstday)		//上个月的部分
			{
				da.innerHTML="<font color=gray>" + meizzWDay[i] + "</font>";
				da.title=(mm==1?12:mm-1) +"月" + meizzWDay[i] + "日";
				da.onclick=Function("meizzDayClick(this.innerText,-1)");
				if(!outDate)
				da.style.backgroundColor = ((mm==1?yy-1:yy) == new Date().getFullYear() && 
				(mm==1?12:mm-1) == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate()) ? "#FFD700":"#e0e0e0";
			else
			{
				da.style.backgroundColor =((mm==1?yy-1:yy)==outDate.getFullYear() && (mm==1?12:mm-1)== outDate.getMonth() + 1 && 
				meizzWDay[i]==outDate.getDate())? "#00ffff" :
				(((mm==1?yy-1:yy) == new Date().getFullYear() && (mm==1?12:mm-1) == new Date().getMonth()+1 && 
				meizzWDay[i] == new Date().getDate()) ? "#FFD700":"#e0e0e0");
				//将选中的日期显示为凹下去
				if((mm==1?yy-1:yy)==outDate.getFullYear() && (mm==1?12:mm-1)== outDate.getMonth() + 1 && 
				meizzWDay[i]==outDate.getDate())
				{
					da.borderColorLight="#FFFFFF";
					da.borderColorDark="#C6D7F1";
				}
			}
		}
		else if (i>=firstday+GetMonthCount(yy,mm))		//下个月的部分
		{
			da.innerHTML="<b><font color=gray>" + meizzWDay[i] + "</font></b>";
			da.title=(mm==12?1:mm+1) +"月" + meizzWDay[i] + "日";
			da.onclick=Function("meizzDayClick(this.innerText,1)");
			if(!outDate)
				da.style.backgroundColor = ((mm==12?yy+1:yy) == new Date().getFullYear() && 
					(mm==12?1:mm+1) == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate()) ?
					 "#FFD700":"#e0e0e0";
			else
			{
				da.style.backgroundColor =((mm==12?yy+1:yy)==outDate.getFullYear() && (mm==12?1:mm+1)== outDate.getMonth() + 1 && 
				meizzWDay[i]==outDate.getDate())? "#00ffff" :
				(((mm==12?yy+1:yy) == new Date().getFullYear() && (mm==12?1:mm+1) == new Date().getMonth()+1 && 
				meizzWDay[i] == new Date().getDate()) ? "#FFD700":"#e0e0e0");
				//将选中的日期显示为凹下去
				if((mm==12?yy+1:yy)==outDate.getFullYear() && (mm==12?1:mm+1)== outDate.getMonth() + 1 && 
				meizzWDay[i]==outDate.getDate())
				{
					da.borderColorLight="#FFFFFF";
					da.borderColorDark="#C6D7F1";
				}
			}
		}
		else //本月的部分
		{
			da.innerHTML="<b>" + meizzWDay[i] + "</b>";
			da.title=mm +"月" + meizzWDay[i] + "日";
			da.onclick=Function("meizzDayClick(this.innerText,0)");		//给td赋予onclick事件的处理
			//如果是当前选择的日期，则显示亮蓝色的背景；如果是当前日期，则显示暗黄色背景
			if(!outDate)
				da.style.backgroundColor = (yy == new Date().getFullYear() && mm == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate())?
					"#FFD700":"#e0e0e0";
			else
			{
				da.style.backgroundColor =(yy==outDate.getFullYear() && mm== outDate.getMonth() + 1 && meizzWDay[i]==outDate.getDate())?
					"#00ffff":((yy == new Date().getFullYear() && mm == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate())?
					"#FFD700":"#e0e0e0");
				//将选中的日期显示为凹下去
				if(yy==outDate.getFullYear() && mm== outDate.getMonth() + 1 && meizzWDay[i]==outDate.getDate())
				{
					da.borderColorLight="#FFFFFF";
					da.borderColorDark="#C6D7F1";
				}
			}
		}
        da.style.cursor="hand";
      }
	else
	{
		da.innerHTML="";
		da.style.backgroundColor="";
		da.style.cursor="default";
	}
  }
}

function meizzDayClick(n,ex)  //点击显示框选取日期，主输入函数*************
{
  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;}
    outObject.value= yy + "-" + mm + "-" + n ; //注：在这里你可以输出改成你想要的格式
    closeLayer(); 

	if(__dateCBKFun != null && typeof(__dateCBKFun) == 'function') setTimeout(function(){__dateCBKFun();}, 500);

  }
  else {closeLayer(); alert("您所要输出的控件对象并不存在！");}
}


/* 主要的写程序 */
function showDate1(dateID, yy,mm, dd)
{
	meizzTheYear=yy;
	meizzTheMonth=mm;

	outDate = new Date(yy, mm, dd);

	for (var i = 0; i < 42; i++){meizzWDay[i]=""};  //将显示框的内容全部清空

	var day1 = 1,day2=1,firstday = new Date(yy,mm-1,1).getDay();  //某月第一天的星期几

	for (i=0;i<firstday;i++) meizzWDay[i] = GetMonthCount(mm==1?yy-1:yy, mm==1?12:mm-1) - firstday + i + 1;	//上个月的最后几天

	for (i = firstday; day1 < GetMonthCount(yy,mm)+1; i++)
	{
		meizzWDay[i]=day1;
		day1++;
	}

	for (i=firstday+GetMonthCount(yy,mm);i<42;i++){meizzWDay[i]=day2;day2++}

	for (i = 0; i < 42; i++)
	{
		var da = eval("document.all."+ dateID +""+i); //书写新的一个月的日期星期排列
		if (meizzWDay[i]!="")
		{
			if(i<firstday)		//上个月的部分
			{
				da.innerHTML="<font color=gray>" + meizzWDay[i] + "</font>";
				da.title=(mm==1?12:mm-1) +"月" + meizzWDay[i] + "日";

				da.onclick=Function("goDay("+yy+", "+(mm-1)+", "+meizzWDay[i]+")");
			}
			else if (i>=firstday+GetMonthCount(yy,mm))		//下个月的部分
			{
				da.innerHTML="<font color=gray>" + meizzWDay[i] + "</font>";
				da.title=(mm==12?1:mm+1) +"月" + meizzWDay[i] + "日";
				da.onclick=Function("goDay("+yy+", "+(mm+1)+", "+meizzWDay[i]+")");
			}
			else //本月的部分
			{
				da.innerHTML="" + meizzWDay[i] + "";
				da.title=mm +"月" + meizzWDay[i] + "日";
				da.onclick=Function("goDay("+yy+", "+mm+", "+meizzWDay[i]+")");		//给td赋予onclick事件的处理

				if(dd==meizzWDay[i])
				{
					da.style.backgroundColor="#00ffff";
				}

				if((new Date()).getYear()==yy && ((new Date()).getMonth()+1)==mm && (new Date()).getDate()==meizzWDay[i])
				{
					da.style.backgroundColor="#FFD700";
				}
			}

			da.style.cursor="hand";
		}
		else
		{
			da.innerHTML="";
			da.style.backgroundColor="";
			da.style.cursor="default";
		}
	}
}

//显示日历程序
function showDateContent(dateID)
{
	var strFrame = "";
	strFrame+='<div onselectstart="return false">';
	strFrame+='<table border=0 cellspacing=1 cellpadding=1 width=180 class=listContent>';
	strFrame+='  <tr><td class=listTitle>';

	strFrame+='<table border=0 cellspacing=1 cellpadding=0 width=100%>';
	strFrame+='<tr align=center valign=bottom>';
	strFrame+='<td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr></table>';

	strFrame+='</td></tr>';

	strFrame+='  <tr><td class=list>';

	strFrame+='    <table border=0 cellspacing=1 cellpadding=1 width=100%>';

	var n=0;
	for (j=0;j<6;j++)
	{
		strFrame+= ' <tr align=center>';

		for (i=0;i<7;i++)
		{
			strFrame+='<td height=16 id='+ dateID +''+n+'></td>';
			n++;
		}
		strFrame+='</tr>';
	}

	strFrame+='  </table></td></tr>';

	strFrame+='</table></div>';

	return strFrame;
}

function showDate(dateID)
{
	var strFrame = "";
	strFrame+='<div onselectstart="return false">';
	strFrame+='<table border=0 cellspacing=1 cellpadding=1 width=180 class=listContent>';
	strFrame+='  <tr><td class=listTitle>';

	strFrame+='<table border=0 cellspacing=1 cellpadding=0 width=100%>';
	strFrame+='<tr align=center valign=bottom>';
	strFrame+='<td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr></table>';

	strFrame+='</td></tr>';

	strFrame+='  <tr><td class=list>';

	strFrame+='    <table border=0 cellspacing=1 cellpadding=1 width=100%>';

	var n=0;
	for (j=0;j<6;j++)
	{
		strFrame+= ' <tr align=center>';

		for (i=0;i<7;i++)
		{
			strFrame+='<td height=16 id='+ dateID +''+n+'></td>';
			n++;
		}
		strFrame+='</tr>';
	}

	strFrame+='  </table></td></tr>';

	strFrame+='</table></div>';

	document.write(strFrame);
}


function Calendar ()
{
	this.Today = new Date();

	this.isToday    = false;

	this.detail = new Array();

	////////////////////////////////////////////////////////////
	// 日期资料
	/////////////////////////////////////////////////////////////
	this._lunarInfo = new Array(0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2,0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977,0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970,0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950,0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557,0x06ca0,0x0b550,0x15355,0x04da0,0x0a5d0,0x14573,0x052d0,0x0a9a8,0x0e950,0x06aa0,0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0,0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b5a0,0x195a6,0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570,0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5,0x092e0,0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0,0x0cab5,0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930,0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530,0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45,0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0);
	this._solarMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	this._Gan=new Array("甲","乙","丙","丁","戊","己","庚","辛","壬","癸");
	this._Zhi=new Array("子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥");
	this._Animals = new Array("鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪");
	this._solarTerm = new Array("小寒","大寒","立春","雨水","惊蛰","春分","清明","谷雨","立夏","小满","芒种","夏至","小暑","大暑","立秋","处暑","白露","秋分","寒露","霜降","立冬","小雪","大雪","冬至");
	this._sTermInfo = new Array(0,21208,42467,63836,85337,107014,128867,150921,173149,195551,218072,240693,263343,285989,308563,331033,353350,375494,397447,419210,440795,462224,483532,504758);
	this._nStr1 = new Array('日','一','二','三','四','五','六','七','八','九','十');
	this._nStr2 = new Array('初','十','廿','卅','　');
	this._monthName = new Array("1 月","2 月","3 月","4 月","5 月","6 月","7 月","8 月","9 月","10 月","11 月","12 月");
	this._sFtv = new Array("0101*元旦","0214 情人节","0308 妇女节","0312 植树节","0314 国际警察日","0315 消费者权益日","0323 世界气象日","0401 愚人节","0407 世界卫生日","0501*劳动节","0504 青年节","0508 红十字日","0512 护士节","0515 国际家庭日","0517 世界电信日","0519 全国助残日","0531 世界无烟日","0601 儿童节","0605 世界环境日","0606 全国爱眼日","0623 奥林匹克日","0625 全国土地日","0626 反毒品日","0701 建党节","0707 抗战纪念日","0711 世界人口日","0801 建军节","0908 国际扫盲日","0909 毛泽东逝世纪念","0910 教师节","0917 国际和平日","0920 国际爱牙日","0922 国际聋人节","0927 世界旅游日","0928 孔子诞辰","1001*国庆节","1004 世界动物日","1006 老人节","1007 国际住房日","1009 世界邮政日","1015 国际盲人节","1016 世界粮食日","1024 联合国日","1031 万圣节","1108 中国记者日","1109 消防宣传日","1112 孙中山诞辰","1114 世界糖尿病日","1117 国际大学生节","1128 感恩节","1201 世界艾滋病日","1203 世界残疾人日","1209 世界足球日","1220 澳门回归","1225 圣诞节","1226 毛泽东诞辰");
	this._lFtv = new Array("0101*春节","0102*初二","0103*初三","0104*初四","0105*初五","0106*初六","0107*初七","0115 元宵节","0505 端午节","0707 七夕情人节","0815 中秋节","0909 重阳节","1208 腊八节","1223 小年","0100*除夕");
	this._wFtv = new Array("0520 母亲节"); //某月的第几个星期几

	/*****************************************************************************
										  日期计算
	*****************************************************************************/
	// 传回农历 y年的总天数
	this._nYearDays = function (y)
	{
		var i, sum = 348;
		for(i=0x8000; i>0x8; i>>=1) sum += (this._lunarInfo[y-1900] & i)? 1: 0;
		return(sum+this._leapDays(y));
	};

	// 传回农历 y年闰月的天数
	this._leapDays = function (y)
	{
		if(this._leapMonth(y))  return((this._lunarInfo[y-1900] & 0x10000)? 30: 29);
		else return(0);
	};

	// 传回农历 y年闰哪个月 1-12 , 没闰传回 0
	this._leapMonth = function (y)
	{
		return(this._lunarInfo[y-1900] & 0xf);
	}

	// 传回农历 y年m月的总天数
	this._monthDays = function (y,m)
	{
		return( (this._lunarInfo[y-1900] & (0x10000>>m))? 30: 29 );
	}

	this.getLunarYear = function (y)
	{
		return this._cyclical(y-1900+36)
	};

	this.getAnimal = function (y)
	{
		return this._Animals[(y-4)%12];
	};

	this.dayCyl;this.monCyl;this.yearCyl;this.isLeap;this.month;this.year;this.day;

	// 算出农历, 传入日期物件, 传回农历日期物件
	// 该物件属性有 .year .month .day .isLeap .yearCyl .dayCyl .monCyl
	this._Lunar = function (objDate)
	{
		var i, leap=0, temp=0;
		var baseDate = new Date(1900,0,31);
		var offset   = (objDate - baseDate)/86400000;

		this.dayCyl = offset + 40;
		this.monCyl = 14;

		for(i=1900; i<2050 && offset>0; i++)
		{
			temp = this._nYearDays(i);
			offset -= temp;
			this.monCyl += 12;
		}

		if(offset<0)
		{
			offset += temp;
			i--;
			this.monCyl -= 12;
		}

		this.year = i
		this.yearCyl = i-1864

		leap = this._leapMonth(i) //闰哪个月
		this.isLeap = false

		for(i=1; i<13 && offset>0; i++)
		{
			//闰月
			if(leap>0 && i==(leap+1) && this.isLeap==false)
			{ --i; this.isLeap = true; temp = this._leapDays(this.year); }
			else
			{ temp = this._monthDays(this.year, i); }

			//解除闰月
			if(this.isLeap==true && i==(leap+1)) this.isLeap = false

			offset -= temp
			if(this.isLeap == false) this.monCyl ++
		}

		if(offset==0 && leap>0 && i==leap+1)
			if(this.isLeap)
			{ this.isLeap = false; }
			else
			{ this.isLeap = true; --i; --this.monCyl;}

		if(offset<0){ offset += temp; --i; --this.monCyl; }

		this.month = i;
		this.day = offset + 1;
	};

	//====传回国历 y年某m+1月的天数
	this._solarDays = function (y,m)
	{
		if(m==1)
			return (((y%4 == 0) && (y%100 != 0) || (y%400 == 0))? 29: 28);
		else
			return (this._solarMonth[m]);
	}

	//==== 传入 offset 传回干支, 0=甲子
	this._cyclical = function(num)
	{
		return (this._Gan[num%10]+this._Zhi[num%12]);
	}

	//===== 某年的第n个节气为几日(从0小寒起算)
	this._sTerm = function sTerm(y,n)
	{
		var offDate = new Date( ( 31556925974.7*(y-1900) + this._sTermInfo[n]*60000  ) + Date.UTC(1900,0,6,2,5) );
		return (offDate.getUTCDate());
	};

	//====================== 中文日期
	this._cDay = function (d)
	{
		var s;
		switch (d)
		{
			case 10:
				s = '初十'; break;
			case 20:
				s = '二十'; break;
			case 30:
				s = '三十'; break;
			default :
				s = this._nStr2[Math.floor(d/10)];
				s += this._nStr1[d%10];
		}
		return(s);
	};

	//==== 传回月历物件 (y年,m+1月)
	this.calendar = function(y, m)
	{
		var sDObj, lY, lM, lD=1, lL, lX=0, tmp1, tmp2;
		var lDPOS = new Array(3);
		var n = 0;
		var firstLM = 0;

		sDObj = new Date(y, m, 1); //当月一日日期

		this.length    = this._solarDays(y,m); //国历当月天数
		this.firstWeek = sDObj.getDay(); //国历当月1日星期几

		for(var i=0;i<this.length;i++)
		{
			if(lD>lX)
			{
				sDObj = new Date(y, m, i+1)    //当月一日日期
				this._Lunar(sDObj)     //农历
				lY    = this.year           //农历年
				lM    = this.month          //农历月
				lD    = this.day            //农历日
				lL    = this.isLeap         //农历是否闰月
				lX    = lL? this._leapDays(lY): this._monthDays(lY,lM) //农历当月最後一天

				if(n==0) firstLM = lM
				lDPOS[n++] = i-lD+1
			}

			this.detail[i] = new _CalendarDetail(y, m+1, i+1, this._nStr1[(i+this.firstWeek)%7], lY, lM, lD++, lL, this._cyclical(this.yearCyl) ,this._cyclical(this.monCyl), this._cyclical(this.dayCyl++) );

			//if((i+this.firstWeek)%7==0)   this[i].color = 'red'  //周日颜色
			//if((i+this.firstWeek)%14==13) this[i].color = 'red'  //周休二日颜色
		}

		//节气
		tmp1=this._sTerm(y, m*2  )-1;
		tmp2=this._sTerm(y, m*2+1)-1;

		this.detail[tmp1].solarTerms = this._solarTerm[m*2];
		this.detail[tmp2].solarTerms = this._solarTerm[m*2+1];

		if(m==3) this.detail[tmp1].color = 'red'; //清明颜色

		//国历节日
		for(var i=0; i<this._sFtv.length; i++)
		{
			if( this._sFtv[i].match(/^(\d{2})(\d{2})([\s\*])(.+)$/) )
			{
				if(Number(RegExp.$1)==(m+1))
				{
					this.detail[Number(RegExp.$2)-1].solarFestival += RegExp.$4 + ' '
					if(RegExp.$3=='*') this.detail[Number(RegExp.$2)-1].color = 'red'
				}
			}
		}

		//月周节日
		for(var i=0; i<this._wFtv.length; i++)
		{
			if(this._wFtv[i].match(/^(\d{2})(\d)(\d)([\s\*])(.+)$/))
			{
				if(Number(RegExp.$1)==(m+1))
				{
					tmp1=Number(RegExp.$2)
					tmp2=Number(RegExp.$3)
					this.detail[((this.firstWeek>tmp2)?7:0) + 7*(tmp1-1) + tmp2 - this.firstWeek].solarFestival += RegExp.$5 + ' '
				}
			}
		}

		//农历节日
		for(var i=0; i<this._lFtv.length; i++)
			if(this._lFtv[i].match(/^(\d{2})(.{2})([\s\*])(.+)$/))
			{
				tmp1=Number(RegExp.$1)-firstLM
				if(tmp1==-11) tmp1=1
				if(tmp1 >=0 && tmp1<n)
				{
					tmp2 = lDPOS[tmp1] + Number(RegExp.$2) -1
					if( tmp2 >= 0 && tmp2<this.length)
					{
						this.detail[tmp2].lunarFestival += RegExp.$4 + ' '
						if(RegExp.$3=='*') this.detail[tmp2].color = 'red'
					}
				}
			}

		//黑色星期五
		//if((this.firstWeek+12)%7==5) this[12].solarFestival += '黑色星期五 '

		//今日
		//if(y==tY && m==tM) this[tD-1].isToday = true;
	};
}

function _CalendarDetail(sYear, sMonth, sDay, week, nYear, nMonth, nDay, isLeap, cYear, cMonth, cDay)
{
	this.isToday    = false;
	//国历
	this.sYear      = sYear;
	this.sMonth     = sMonth;
	this.sDay       = sDay;
	this.week       = week;
	//农历
	this.nYear      = nYear;
	this.nMonth     = nMonth;
	this.nDay       = nDay;
	this.isLeap     = isLeap;

	//干支
	this.cYear      = cYear;
	this.cMonth     = cMonth;
	this.cDay       = cDay;

	this.color      = '';
	this.lunarFestival = ''; //农历节日
	this.solarFestival = ''; //国历节日
	this.solarTerms    = ''; //节气

	this.nYearC; //中文农历显示
	this.nMonthC; //中文月显示
	this.nDayC; //中文日期显示

	this._constructor = function()
	{
		var _nStr1 = new Array('日','一','二','三','四','五','六','七','八','九','十');
		var _nStr2 = new Array('初','十','廿','卅','　');
		var _monthName = new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");

		///////////////////////
		this.nMonthC = _monthName[(this.nMonth-1)];

		///////////////////////
		var d = '';
		switch (this.nDay)
		{
			case 10:
				d = '初十'; break;
			case 20:
				d = '二十'; break;
			case 30:
				d = '三十'; break;
			default :
				d = _nStr2[Math.floor(this.nDay/10)] +""+ _nStr1[this.nDay%10];
				break;
		}

		this.nDayC = d;
	};

	this._constructor();
};

// begin cal
function getDateCalendar(y, m, d)
{
	if(typeof(m) == 'undefined' || typeof(d) == 'undefined')
	{
		var arr = y.split('-');
		y = arr[0];
		m = arr[1];
		d = arr[2];
	}

	m = m - 1;
	d = d - 1;

	var cld = new Calendar(y, m);
	cld.calendar(y, m);

	//var temp = cld.detail[d].sYear +" 年 "+ cld.detail[d].sMonth +" 月 "+ cld.detail[d].sDay +" 日 ";
	//temp += "<br>";
	//temp += "星期"+ cld.detail[d].week;
	//temp += "<br>";

	//temp += " 农历 "+ cld.detail[d].nMonth +" 月 "+ cld.detail[d].nDay +" 日 ";
	//temp += "<br>";

	//temp += cld.detail[d].cYear +" "+ cld.detail[d].cMonth +" "+ cld.detail[d].cDay +" ";

	//temp += "<br><b>"+ cld.detail[d].lunarFestival +" "+ cld.detail[d].solarFestival +" "+ cld.detail[d].solarTerms +"</b>";

	return cld.detail[d];
}
// end cal