var months = new Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月");
	var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var days = new Array("日","一", "二", "三", "四", "五", "六");
	var today;
	var tddateold, tddateclass1="tddate", tddateclass2="tddateselected";
	
	document.writeln("<div id=\"divdate\" style=\"position:absolute; visibility:hidden;background-color:#6699cc;\"><iframe src=\"javascript:false\" style=\"position:absolute; visibility:inherit; top:0px; left:0px; width:100px; height:200px; z-index:-1; filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';\"></iframe></div>");
	
	function getDays(month, year)
	{
		//下面的这段代码是判断当前是否是闰年的
		if (1 == month)
			return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28;
		else
			return daysInMonth[month];
	}
	
	function getToday()
	{
		//得到今天的年,月,日
		this.now = new Date();
		this.year = this.now.getFullYear();
		this.month = this.now.getMonth();
		this.day = this.now.getDate();
	}
	
	function getStringDay(str)
	{
		//得到输入框的年,月,日
		var str=str.split("-")
	
		this.now = new Date(parseFloat(str[0]),parseFloat(str[1])-1,parseFloat(str[2]));
		this.year = this.now.getFullYear();
		this.month = this.now.getMonth();
		this.day = this.now.getDate();
	}
	
	function newCalendar()
	{
		var parseYear = parseInt(document.all.Year.options[document.all.Year.selectedIndex].value);
		var newCal = new Date(parseYear, document.all.Month.selectedIndex, 1);
		var day = -1;
		var startDay = newCal.getDay();
		var daily = 0;
	
		if ((today.year == newCal.getFullYear()) &&(today.month == newCal.getMonth()))
			day = today.day;
	
		var tableCal = document.all.tabledate;
		var intDaysInMonth =getDays(newCal.getMonth(), newCal.getFullYear());
		for (var intWeek = 1; intWeek < tableCal.rows.length;intWeek++)
			for (var intDay = 0;intDay < tableCal.rows[intWeek].cells.length;intDay++)
			{
				var cell = tableCal.rows[intWeek].cells[intDay];
				if ((intDay == startDay) && (0 == daily))
					daily = 1;
	
				if(day==daily) //今天，调用今天的Class
				{
					if(tddateold!=null)tddateold.className = tddateclass1;
					tddateold = cell;
					tddateold.className = tddateclass2;
	
				}
				else if(intDay==6) //周六
					cell.style.color='green';
				else if (intDay==0) //周日
					cell.style.color='red';
				if ((daily > 0) && (daily <= intDaysInMonth))
				{
					setInnerText(cell,daily);
					daily++;
				}
				else
				{
					setInnerText(cell," ");
				}
			}
	}
	
	function SelectDate(src, inputboxname)//单击
	{
		if (src.tagName == "TD")
		{
			var num = getInnerText(src)
			if ( num != "")
			{
				if(tddateold!=null)tddateold.className = tddateclass1;
				tddateold = src;
				tddateold.className = tddateclass2;
			}
		}
	}
	
	function ConfirmDate(src, inputboxid)//双击或者点击按钮
	{
		var inputbox = document.getElementById(inputboxid);
		//var inputbox = document.getElementByName(inputboxid);
		var datestr;
		if(src==null)src = tddateold; //直接点确定按钮
		if (src.tagName == "TD")
		{
			var num = getInnerText(src)
			if ( num != "")
			{
				if(tddateold!=null)tddateold.className = tddateclass1;
				tddateold = src;
				tddateold.className = tddateclass2;
				datestr = document.getElementById("Year").value + "-" + document.getElementById("Month").value + "-" + num;
				if(document.getElementById("Hour").value!=""&&document.getElementById("Minute").value!="")
					datestr = datestr + " " + document.getElementById("Hour").value + ":" + document.getElementById("Minute").value
				inputbox.value=datestr;
				HiddenCalendar();
			}
		}
		var aaa = inputbox.value;
		
		var aaas = aaa.split('-');
	
	
		if(aaas[2] == ''){
		   inputbox.value = "";
		}
	
	}
	
	function HiddenCalendar()
	{
		//关闭选择窗口
		document.getElementById('divdate').style.visibility='hidden';
	}
	
	function ShowCalendar(inputboxid)
	{
		var inputbox = document.getElementById(inputboxid);
		//var inputbox = document.getElementByName(inputboxid);
		var x,y,intLoop,intWeeks,intDays;
		var DivContent;
		var year,month,day;
		var thisyear; //真正的今年年份
	
		thisyear=new getToday();
		thisyear=thisyear.year;
	
		today = inputbox.value;
		if(isDate(today))
			today = new getStringDay(today);
		else
			today = new getToday();
	
		//显示的位置
		var o = inputbox;
		x=o.offsetLeft;
		y=o.offsetTop;
		while(o=o.offsetParent)
		{
			x+=o.offsetLeft;
			y+=o.offsetTop;
		}
	   // var temp = $("dateDIV");
		var temp = document.getElementById('dateDIV');
		if(temp != null){
		//alert(temp);
		var hight = temp.scrollTop;
		}
	
		document.getElementById('divdate').style.left=x+2;
	
		if(hight != null)
		document.getElementById('divdate').style.top=y-180-hight;
		else
		document.getElementById('divdate').style.top=y+20;
	
		document.getElementById('divdate').style.visibility="visible";
	
		//下面开始输出日历表格(border-color:#9DBAF7)
		DivContent="\r\n";
		DivContent+="<table border='0' cellspacing='0' class='tabledate'>";
		DivContent+="\r\n";
		DivContent+="<tr>";
		DivContent+="\r\n";
		DivContent+="<td>";
		DivContent+="\r\n";
	
		//年
		DivContent+="<select name='Year' id='Year' onChange='newCalendar()' class='dateoption'>";
		DivContent+="\r\n";
		for (intLoop = 1949; intLoop < 2013; intLoop++)
			DivContent+="<option value= " + intLoop + " " + (today.year == intLoop ? "Selected" : "") + ">" + intLoop + "</option>";
		DivContent+="</select>";
		DivContent+="\r\n";
	
		//月
		DivContent+="<select name='Month' id='Month' onChange='newCalendar()'  class='dateoption'>";
		DivContent+="\r\n";
		for (intLoop = 0; intLoop < months.length; intLoop++)
			DivContent+="<option value= " + (intLoop + 1) + " " + (today.month == intLoop ? "Selected" : "") + ">" + months[intLoop] + "</option>";
		DivContent+="</select>";
		DivContent+="\r\n";
	
		DivContent+="</td>";
		DivContent+="\r\n";
	
		DivContent+="<td class='tdclose' align='center' title='关闭' onClick='javascript:HiddenCalendar()'>关闭</td>";
		DivContent+="\r\n";
		DivContent+="</tr>";
		DivContent+="\r\n";
	
		DivContent+="<tr><td align='center' colspan='2'>";
		DivContent+="\r\n";
		DivContent+="<table id='tabledate' border='0' width='100%'>";
		DivContent+="\r\n";
	
		//星期
		DivContent+="<tr>";
		DivContent+="\r\n";
		for (intLoop = 0; intLoop < days.length; intLoop++)
		{
			DivContent+="<td class='tdweekday' align='center'>" + days[intLoop] + "</td>";
		DivContent+="\r\n";
		}
		DivContent+="</tr>";
		DivContent+="\r\n";
	
		//天
		for (intWeeks = 0; intWeeks < 6; intWeeks++)
		{
			DivContent+="<tr>";
		DivContent+="\r\n";
	
			for (intDays = 0; intDays < days.length; intDays++)
			{
				DivContent+="<td class='"+tddateclass1+"'  align='center' onDblClick='SelectDate(this, \""+inputbox.id+"\")' onClick='ConfirmDate(this, \""+inputbox.id+"\")'>&nbsp;</td>";
			DivContent+="\r\n";
			}
			DivContent+="</tr>";
		DivContent+="\r\n";
		DivContent+="\r\n";
		}
		DivContent+="</table></td></tr>";
		DivContent+="\r\n";
	
		//DivContent+="<tr><td>时间<input type=text name='Hour' id='Hour' size=2 maxlength=2>:<input type=text name='Minute' id='Minute'  size=2 maxlength=2> <input type=button class=datebutton value=确定 onClick='ConfirmDate(null, \""+inputbox.id+"\")'></td></tr>";
		DivContent+="<tr><td><input type=hidden name='Hour' id='Hour' size=2 maxlength=2><input type=hidden name='Minute' id='Minute'  size=2 maxlength=2> <input type=button class=datebutton value=确定 onClick='ConfirmDate(null, \""+inputbox.id+"\")'></td></tr>";
	
		DivContent+="</table>";
	
		document.getElementById('divdate').innerHTML="<body>"+DivContent+"</body><iframe src=\"javascript:false\" style=\"position:absolute; visibility:inherit; top:0px; left:0px; width:162px; height:220px; z-index:-1; filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';\"></iframe>";
		newCalendar();
	}
	
	function isDate(dateStr)
	{
		var datePat = /^(\d{4})(\-)(\d{1,2})(\-)(\d{1,2})$/;
		var matchArray = dateStr.match(datePat);
		if (matchArray == null) return false;
		var month = matchArray[3];
		var day = matchArray[5];
		var year = matchArray[1];
		if (month < 1 || month > 12) return false;
		if (day < 1 || day > 31) return false;
		if ((month==4 || month==6 || month==9 || month==11) && day==31) return false;
		if (month == 2)
		{
			var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
			if (day > 29 || (day==29 && !isleap)) return false;
		}
		return true;
	}
	function ShowListTableCalendar()
	{
		var inputbox = event.srcElement;
		//var inputbox = document.getElementByName(inputboxid);
		var x,y,intLoop,intWeeks,intDays;
		var DivContent;
		var year,month,day;
		var thisyear; //真正的今年年份
	
		thisyear=new getToday();
		thisyear=thisyear.year;
	
		today = inputbox.value;
		if(isDate(today))
			today = new getStringDay(today);
		else
			today = new getToday();
	
		//显示的位置
		var o = inputbox;
		x=o.offsetLeft;
		y=o.offsetTop;
		while(o=o.offsetParent)
		{
			x+=o.offsetLeft;
			y+=o.offsetTop;
		}
	
		document.getElementById('divdate').style.left=x+2;
		document.getElementById('divdate').style.top=y+20;
		document.getElementById('divdate').style.visibility="visible";
	
		//下面开始输出日历表格(border-color:#9DBAF7)
		DivContent="\r\n";
		DivContent+="<table border='0' cellspacing='0' class='tabledate'>";
		DivContent+="\r\n";
		DivContent+="<tr>";
		DivContent+="\r\n";
		DivContent+="<td>";
		DivContent+="\r\n";
	
		//年
		DivContent+="<select name='Year' id='Year' onChange='newCalendar()' class='dateoption'>";
		DivContent+="\r\n";
		for (intLoop = thisyear - 5; intLoop < (thisyear + 5); intLoop++)
			DivContent+="<option value= " + intLoop + " " + (today.year == intLoop ? "Selected" : "") + ">" + intLoop + "</option>";
		DivContent+="</select>";
		DivContent+="\r\n";
	
		//月
		DivContent+="<select name='Month' id='Month' onChange='newCalendar()'  class='dateoption'>";
		DivContent+="\r\n";
		for (intLoop = 0; intLoop < months.length; intLoop++)
			DivContent+="<option value= " + (intLoop + 1) + " " + (today.month == intLoop ? "Selected" : "") + ">" + months[intLoop] + "</option>";
		DivContent+="</select>";
		DivContent+="\r\n";
	
		DivContent+="</td>";
		DivContent+="\r\n";
	
		DivContent+="<td class='tdclose' align='center' title='关闭' onClick='javascript:HiddenCalendar()'>S</td>";
		DivContent+="\r\n";
		DivContent+="</tr>";
		DivContent+="\r\n";
	
		DivContent+="<tr><td align='center' colspan='2'>";
		DivContent+="\r\n";
		DivContent+="<table id='tabledate' border='0' width='100%'>";
		DivContent+="\r\n";
	
		//星期
		DivContent+="<tr>";
		DivContent+="\r\n";
		for (intLoop = 0; intLoop < days.length; intLoop++)
		{
			DivContent+="<td class='tdweekday' align='center'>" + days[intLoop] + "</td>";
		DivContent+="\r\n";
		}
		DivContent+="</tr>";
		DivContent+="\r\n";
	
		//天
		for (intWeeks = 0; intWeeks < 6; intWeeks++)
		{
			DivContent+="<tr>";
		DivContent+="\r\n";
	
			for (intDays = 0; intDays < days.length; intDays++)
			{
				DivContent+="<td class='"+tddateclass1+"'  align='center' onDblClick='SelectDate(this, \""+inputbox.id+"\")' onClick='ConfirmDate(this, \""+inputbox.id+"\")'>&nbsp;</td>";
			DivContent+="\r\n";
			}
			DivContent+="</tr>";
		DivContent+="\r\n";
		DivContent+="\r\n";
		}
		DivContent+="</table></td></tr>";
		DivContent+="\r\n";
	
		DivContent+="<tr><td>时间<input type=text name='Hour' id='Hour' size=2 maxlength=2>:<input type=text name='Minute' id='Minute'  size=2 maxlength=2> <input type=button class=datebutton value=确定 onClick='ConfirmDate(null, \""+inputbox.id+"\")'></td></tr>";
		DivContent+="</table>";
	
		document.getElementById('divdate').innerHTML="<body>"+DivContent+"</body><iframe src=\"javascript:false\" style=\"position:absolute; visibility:inherit; top:0px; left:0px; width:162px; height:220px; z-index:-1; filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';\"></iframe>";
		newCalendar();
	}
	
	
	// getInnerText & setInnerText(el, str) moveto common javascript 2005.7.1
	
	
	function getFormatDate(strDate)
	{
		   var today;
		   if(strDate !=""){
				if(isDate(strDate)){
					today = new getStringDay(strDate);}
				else{
					alert("请填写正确时间");
				}
		   }else{
			   today =  new getToday();
		   }
		   if(today.month<9){
				var m ="0" + (today.month+1);
		   }else{
				var m = today.month+1;
		   }
		   var dt ="";
		   dt =dt.concat(today.year,m,today.day);
		   return dt;
	}
	
	
	function ShowNewCalendar(inputboxid)
	{
		var inputbox = document.getElementById(inputboxid);
		var x,y,intLoop;
		var DivContent;
		var year,month;
		var thisyear; //真正的今年年份
	
		thisyear=new getToday();
		thisyear=thisyear.year;
	
		today = inputbox.value;
		if(isDate(today))
			today = new getStringDay(today);
		else
			today = new getToday();
		//显示的位置
		var o = inputbox;
		x=o.offsetLeft;
		y=o.offsetTop;
		while(o=o.offsetParent)
		{
			x+=o.offsetLeft;
			y+=o.offsetTop;
		}
	
		document.getElementById('divdate').style.left=x+2;
		document.getElementById('divdate').style.top=y+20;
		document.getElementById('divdate').style.visibility="visible";
	
		//下面开始输出日历表格(border-color:#9DBAF7)
		DivContent="\r\n";
		DivContent+="<table border='0' cellspacing='0' class='tabledate'>";
		DivContent+="\r\n";
		DivContent+="<tr>";
		DivContent+="\r\n";
		DivContent+="<td>";
		DivContent+="\r\n";
	
		//年
		DivContent+="<select name='NewYear' id='NewYear'  class='dateoption'>";
		DivContent+="\r\n";
		for (intLoop = thisyear - 5; intLoop < (thisyear + 5); intLoop++)
			DivContent+="<option value= " + intLoop + " " + (today.year == intLoop ? "Selected" : "") + ">" + intLoop + "</option>";
		DivContent+="</select>";
		DivContent+="\r\n";
	
		//月
		DivContent+="<select name='NewMonth' id='NewMonth'  class='dateoption'>";
		DivContent+="\r\n";
		for (intLoop = 0; intLoop < months.length; intLoop++)
			DivContent+="<option value= " + (intLoop + 1) + " " + (today.month == intLoop ? "Selected" : "") + ">" + months[intLoop] + "</option>";
		DivContent+="</select>";
		DivContent+="\r\n";
	
		DivContent+="</td>";
		DivContent+="\r\n";
	
		DivContent+="<td class='tdclose' align='center' title='关闭' onClick='javascript:HiddenCalendar()'>S</td>";
		DivContent+="\r\n";
		DivContent+="</tr>";
		DivContent+="\r\n";
	
		DivContent+="<tr><td align='center' colspan='2'>";
		DivContent+="\r\n";
		DivContent+="<tr><td align= center><input type='button' class='datebutton' value='确定' onClick=ConfirmNewDate('"+ inputboxid +"')></td></tr>";
		DivContent+="</table>";
		document.getElementById('divdate').innerHTML="<body>"+DivContent+"</body><iframe src=\"javascript:false\" style=\"position:absolute; visibility:inherit; top:0px; left:0px; width:162px; height:220px; z-index:-1; filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';\"></iframe>";
	}
	function  ConfirmNewDate(id)
	{
	
	   var obj = document.getElementById(id);
	   var yv =  document.getElementById("NewYear").value;
	   var mv =  document.getElementById("NewMonth").value;
	   if(mv>=1 && mv<=9)
	   {
		   obj.value =yv +"-0" +mv;
	   }else{
		   obj.value =yv +"-" +mv;
	   }
	   HiddenCalendar();
	}
	
	function getInnerText(el) //el可以是tr/td
	{
		//if (ie5) return el.innerText;	//Not needed but it is faster
		var str = "";
		var cs = el.childNodes;
		var l = cs.length;
		for (var i = 0; i < l; i++)
		{
			switch (cs[i].nodeType)
			{
				case 1: //ELEMENT_NODE
					str += getInnerText(cs[i]);
					break;
				case 3:	//TEXT_NODE
					str += cs[i].nodeValue;
					break;
			}
	
		}
		return str;
	}
	
	function setInnerText(el, str) //el只能是td
	{
		var cs = el.childNodes;
		var l = cs.length;
		for (var i = 0; i < l; i++)
		{
			switch (cs[i].nodeType)
			{
				case 1: //ELEMENT_NODE
					setInnerText(cs[i], str);
					break;
				case 3:	//TEXT_NODE
					cs[i].nodeValue = str;
					break;
			}
	
		}
	
	}