

document.writeln('<div id="calendarDateLayer" height="190" width="182" style="position:absolute; top:98px; left:200px; z-index: 9998; display: none; overflow: visible;"></div>');


var outObject;
var outButton;		//点击的按钮
var outDate="";		//存放对象的日期

var bInitCalendar = false;

function initCalendar(){

	if(bInitCalendar == true){
		return;
	}

	bInitCalendar = true;


	var strFrame;
	strFrame='<style type="text/css">';
	strFrame+='INPUT.button{BORDER-RIGHT: #82CDFF 1px solid;BORDER-TOP: #82CDFF 1px solid;BORDER-LEFT: #82CDFF 1px solid;';
	strFrame+='BORDER-BOTTOM: #82CDFF 1px solid;BACKGROUND-COLOR: #fff8ec;font-family:Arial, Helvetica, sans-serif;}';
	strFrame+='TD{FONT-SIZE: 9pt;font-family:Arial, Helvetica, sans-serif;}';
	strFrame+='</style>';

	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 style="border:1px solid" cellspacing=0 cellpadding=0 width=140 height=150 bordercolor=#82CDFF bgcolor=#82CDFF>';
	strFrame+='  <tr><td width=142 height=23 bgcolor=#FFFFFF><table border=0 cellspacing=1 cellpadding=0 width=180 height=16>';
	strFrame+='      <tr align=center><td width=16 align=center';
	strFrame+='><input Author=calendar  title="向前翻 1 月" type=image src="http://mag.zcom.com/images/calendar_arrowl2.gif" onclick="parent.calendarPrevM()" onfocus="this.blur()" style="font-size: 12px; height: 16px">';
	strFrame+='</td><td align=center style="font-size:12px;cursor:default" Author=calendar ';
	strFrame+='><span Author=calendar id=calendarYearHead></span> ';
	strFrame+='<span id=calendarMonthHead Author=calendar></span></td>';
	strFrame+='        <td width=16 align=center';
	strFrame+='><input Author=calendar type=image src=http://mag.zcom.com/images/calendar_arrowr2.gif  onclick="parent.calendarNextM()"              onfocus="this.blur()" title="向后翻 1 月"  style="font-size: 12px; height: 16px"></td></tr>';
	strFrame+='    </table></td></tr>';
	strFrame+='  <tr><td width=142 height=18>';
	strFrame+='<table border=0 cellspacing=0 cellpadding=0 bgcolor=#82CDFF ';
	strFrame+=' BORDERCOLORLIGHT=#82CDFF BORDERCOLORDARK=#FFFFFF width=180 height=20';
	strFrame+='<tr align=center valign=bottom><td style="font-size:12px;color:#FFFFFF;text-align: center;cursor: default;" Author=calendar>日</td>';
	strFrame+='<td style="font-size:12px;color:#FFFFFF;text-align: center;cursor: default;" Author=calendar>一</td><td style="font-size:12px;color:#FFFFFF;text-align: center;cursor: default;" Author=calendar>二</td>';
	strFrame+='<td style="font-size:12px;color:#FFFFFF;text-align: center;cursor: default;" Author=calendar>三</td><td style="font-size:12px;color:#FFFFFF;text-align: center;cursor: default;" Author=calendar>四</td>';
	strFrame+='<td style="font-size:12px;color:#FFFFFF;text-align: center;cursor: default;" Author=calendar>五</td><td style="font-size:12px;color:#FFFFFF;text-align: center;cursor: default;" Author=calendar>六</td></tr>';
	strFrame+='</table></td></tr><!-- Author:F.R.Huang(calendar) http://www.calendar.com/ mail: calendar@hzcnc.com 2002-10-8 -->';
	strFrame+='  <tr><td width=142 height=120>';
	strFrame+='    <table border=0 cellspacing=1 cellpadding=0 BORDERCOLORLIGHT=#82CDFF BORDERCOLORDARK=#FFFFFF bgcolor=#fff8ec width=180 height=120>';
	var n=0; for (j=0;j<5;j++){ strFrame+= ' <tr align=center>'; for (i=0;i<7;i++){
	strFrame+='<td align=middle width=25 height=20 id=calendarDay'+n+' style="font-size:12px" Author=calendar onclick=parent.calendarDayClick(this.innerText,0)></td>';n++;}
	strFrame+='</tr>';}
	strFrame+='      <tr align=center>';
	for (i=35;i<39;i++)strFrame+='<td width=25 height=20 id=calendarDay'+i+' style="font-size:12px" onclick="parent.calendarDayClick(this.innerText,0)"></td>';
	strFrame+='        <td colspan=3 align=right Author=calendar><span onclick=parent.closeLayer() style="font-size:12px;cursor: hand">';
	strFrame+='         <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 Author=calendar align=left><input Author=calendar type=image src=http://mag.zcom.com/images/calendar_arrowl.gif  title="向前翻 1 年" onclick="parent.calendarPrevY()" ';
	strFrame+='             onfocus="this.blur()" style="font-size: 12px; height: 16px"><input Author=calendar  title="向前翻 1 月" type=image src=http://mag.zcom.com/images/calendar_arrowl2.gif ';
	strFrame+='             onclick="parent.calendarPrevM()" onfocus="this.blur()" style="font-size: 12px; height: 16px"></td><td ';
	strFrame+='             Author=calendar align=center><input Author=calendar type="button" class="butt" value="今天" onclick="parent.calendarToday()" ';
	strFrame+='             onfocus="this.blur()" title="当前日期" style="border: 0px; background-color: #FFF; font-size: 12px; height: 14px; cursor:hand"></td><td ';

	strFrame+='             Author=calendar align=right><input Author=calendar type=image src=http://mag.zcom.com/images/calendar_arrowr2.gif  onclick="parent.calendarNextM()" ';
	strFrame+='             onfocus="this.blur()" title="向后翻 1 月" style="font-size: 12px; height: 16px"><input ';
	strFrame+='             Author=calendar type=image  src=http://mag.zcom.com/images/calendar_arrowr.gif  title="向后翻 1 年" onclick="parent.calendarNextY()"';
	strFrame+='             onfocus="this.blur()" style="font-size: 12px; height: 16px"></td>';
	strFrame+='</tr></table></td></tr></table></div>';

	document.getElementById('calendarDateLayer').innerHTML = strFrame;


}



function setday(ttId, clickId) //主调函数
{
	
	initCalendar();
	
	var tt = document.getElementById(ttId);
	var oClick = document.getElementById(clickId);
	var obj = tt;
	var dads  = document.all.calendarDateLayer.style;
	var th = tt;
	var ttop  = tt.offsetTop;     //TT控件的定位点高
	var thei  = tt.clientHeight;  //TT控件本身的高
	var tleft = tt.offsetLeft-94;    //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 = oClick;	//设定外部点击的按钮

	//更新当前日期
	if(outObject.value == "选择日期"){
		var yy,mm,dd;
		var nowDate = new Date();
		
		yy = nowDate.getYear();

		mm = nowDate.getMonth() + 1;
		mm = "" + mm;
		if(mm.length == 1){
			mm = "0" + mm;
		}
		dd = "" + nowDate.getDate();
		if(dd.length == 1){
			dd = "0" + dd;
		}
		var localtime = yy + "-" + mm + "-" + dd ;
		outObject.value = localtime;
	}


	//根据当前输入框的日期显示日历的年月
	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="";
			calendarSetDay(r[1],r[2]+1);
	}
	else{
		outDate="";
		calendarSetDay(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 calendarTheYear=new Date().getFullYear(); //定义年的变量的初始值
var calendarTheMonth=new Date().getMonth()+1; //定义月的变量的初始值
var calendarWDay=new Array(39);               //定义写日期的数组

function document.onclick() //任意点击时关闭该控件	//ie6的情况可以由下面的切换焦点处理代替
{
  with(window.event)
  { if (srcElement.getAttribute("Author")==null && 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.getAttribute("Author")==null && document.activeElement != outObject && document.activeElement != outButton)
		{
			closeLayer();
		}
  }

function calendarWriteHead(yy,mm)  //往 head 中写入当前的年与月
  {
	document.getElementById('calendarYearHead').innerText  = yy + " 年 ";
    document.getElementById('calendarMonthHead').innerText = mm + " 月";
  }

function closeLayer()               //这个层的关闭
  {
    document.getElementById('calendarDateLayer').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 calendarPrevY()  //往前翻 Year
  {
    if(calendarTheYear > 999 && calendarTheYear <10000){calendarTheYear--;}
    else{alert("年份超出范围（1000-9999）！");}
    calendarSetDay(calendarTheYear,calendarTheMonth);
  }
function calendarNextY()  //往后翻 Year
  {
    if(calendarTheYear > 999 && calendarTheYear <10000){calendarTheYear++;}
    else{alert("年份超出范围（1000-9999）！");}
    calendarSetDay(calendarTheYear,calendarTheMonth);
  }
function calendarToday()  //Today Button
  {
	var today;
    calendarTheYear = new Date().getFullYear();
    calendarTheMonth = new Date().getMonth()+1;
    today=new Date().getDate();
    //calendarSetDay(calendarTheYear,calendarTheMonth);
    if(outObject){
		outObject.value=calendarTheYear + "-" + calendarTheMonth + "-" + today;
    }
    closeLayer();
	document.forms.search_date.action = "http://search.mag.zcom.com/date/"+outObject.value;
	document.forms.search_date.submit();
  }
function calendarclr()  //Today Button
  {
    if(outObject){
		outObject.value="";
    }
    closeLayer();
  }

function calendarPrevM()  //往前翻月份
  {
    if(calendarTheMonth>1){calendarTheMonth--}else{calendarTheYear--;calendarTheMonth=12;}
    calendarSetDay(calendarTheYear,calendarTheMonth);
  }
function calendarNextM()  //往后翻月份
  {
    if(calendarTheMonth==12){calendarTheYear++;calendarTheMonth=1}else{calendarTheMonth++}
    calendarSetDay(calendarTheYear,calendarTheMonth);
  }

function calendarSetDay(yy,mm)   //主要的写程序**********
{
  calendarWriteHead(yy,mm);
  //设置当前年月的公共变量为传入值
  calendarTheYear=yy;
  calendarTheMonth=mm;

  for (var i = 0; i < 39; i++){calendarWDay[i]=""};  //将显示框的内容全部清空
  var day1 = 1,day2=1,firstday = new Date(yy,mm-1,1).getDay();  //某月第一天的星期几
  for (i=0;i<firstday;i++)calendarWDay[i]=GetMonthCount(mm==1?yy-1:yy,mm==1?12:mm-1)-firstday+i+1	//上个月的最后几天
  for (i = firstday; day1 < GetMonthCount(yy,mm)+1; i++){calendarWDay[i]=day1;day1++;}
  for (i=firstday+GetMonthCount(yy,mm);i<39;i++){calendarWDay[i]=day2;day2++}
  for (i = 0; i < 39; i++)
  { var da = eval("document.getElementById('calendarDay"+i+"')")     //书写新的一个月的日期星期排列
    if (calendarWDay[i]!="")
      {
		//初始化边框
		da.borderColorLight="#82CDFF";
		da.borderColorDark="#FFFFFF";
		if(i<firstday)		//上个月的部分
		{
			da.innerHTML="<b><font color=gray>" + calendarWDay[i] + "</font></b>";
			da.title=(mm==1?12:mm-1) +"月" + calendarWDay[i] + "日";
			da.onclick=Function("calendarDayClick(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 && calendarWDay[i] == new Date().getDate()) ?
					 "#82CDFF":"#f2f2f2";
			else
			{
				da.style.backgroundColor =((mm==1?yy-1:yy)==outDate.getFullYear() && (mm==1?12:mm-1)== outDate.getMonth() + 1 &&
				calendarWDay[i]==outDate.getDate())? "#00ffff" :
				(((mm==1?yy-1:yy) == new Date().getFullYear() && (mm==1?12:mm-1) == new Date().getMonth()+1 &&
				calendarWDay[i] == new Date().getDate()) ? "#82CDFF":"#f2f2f2");
				//将选中的日期显示为凹下去
				if((mm==1?yy-1:yy)==outDate.getFullYear() && (mm==1?12:mm-1)== outDate.getMonth() + 1 &&
				calendarWDay[i]==outDate.getDate())
				{
					da.borderColorLight="#FFFFFF";
					da.borderColorDark="#82CDFF";
				}
			}
		}
		else if (i>=firstday+GetMonthCount(yy,mm))		//下个月的部分
		{
			da.innerHTML="<b><font color=gray>" + calendarWDay[i] + "</font></b>";
			da.title=(mm==12?1:mm+1) +"月" + calendarWDay[i] + "日";
			da.onclick=Function("calendarDayClick(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 && calendarWDay[i] == new Date().getDate()) ?
					 "#82CDFF":"#f2f2f2";
			else
			{
				da.style.backgroundColor =(yy==outDate.getFullYear() && mm== outDate.getMonth() + 1 && calendarWDay[i]==outDate.getDate())?
					"#00ffff":((yy == new Date().getFullYear() && mm == new Date().getMonth()+1 && calendarWDay[i] == new Date().getDate())?
					"#82CDFF":"#f2f2f2");
				//将选中的日期显示为凹下去
				if((mm==12?yy+1:yy)==outDate.getFullYear() && (mm==12?1:mm+1)== outDate.getMonth() + 1 &&
				calendarWDay[i]==outDate.getDate())
				{
					da.borderColorLight="#FFFFFF";
					da.borderColorDark="#82CDFF";
				}
			}
		}
		else		//本月的部分
		{
			da.innerHTML="<b>" + calendarWDay[i] + "</b>";
			da.title=mm +"月" + calendarWDay[i] + "日";
			da.onclick=Function("calendarDayClick(this.innerText,0)");		//给td赋予onclick事件的处理
			//如果是当前选择的日期，则显示亮蓝色的背景；如果是当前日期，则显示暗黄色背景
			if(!outDate)
				da.style.backgroundColor = (yy == new Date().getFullYear() && mm == new Date().getMonth()+1 && calendarWDay[i] == new Date().getDate())?
					"#82CDFF":"#f2f2f2";
			else
			{
				(yy==outDate.getFullYear() && mm== outDate.getMonth() +1 && calendarWDay[i]==outDate.getDate())?
					da.style.backgroundImage="url(http://mag.zcom.com/images/calendar_sel.gif)":((yy == new Date().getFullYear() && mm == new Date().getMonth()+1 && calendarWDay[i] == new Date().getDate())?
					(da.style.backgroundColor="#82CDFF",da.style.backgroundImage=""):(da.style.backgroundColor="#f2f2f2",da.style.backgroundImage=""));
				//将选中的日期显示为凹下去
				if(yy==outDate.getFullYear() && mm== outDate.getMonth() + 1 && calendarWDay[i]==outDate.getDate())
				{
					da.borderColorLight="#FFFFFF";
					da.borderColorDark="#82CDFF";
				}
			}
		}
        da.style.cursor="hand"
      }
    else{da.innerHTML="";da.style.backgroundColor="";da.style.cursor="default"}
  }
}

function calendarDayClick(n,ex)  //点击显示框选取日期，主输入函数*************
{
  var yy=calendarTheYear;
  var mm = parseInt(calendarTheMonth)+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;}

    var localtime = yy + "-" + mm + "-" + n ;
	var action_url = "http://search.mag.zcom.com/date/";
	action_url = action_url + localtime ;
	outObject.value=localtime;
	document.forms.search_date.action = action_url;
	closeLayer();
	document.forms.search_date.submit();

  }
  else {closeLayer(); alert("您所要输出的控件对象并不存在！");}
}


