﻿<!--
function QuickCalendar(ElementId, theDate, nowDate)
{
	this.ParentNodeTagName = function (pntElement, ElementTagName)	{

		ElementTagName = ElementTagName.toUpperCase();

		do
		{
			pntElement = pntElement.parentNode;
		}
		while (pntElement.tagName != ElementTagName)

		return pntElement;
	}

	this.FormatDateTime = function (strDateTime, strFormat)	{

		var iHour = 0;

		strDateTime = strDateTime.toLowerCase();

		while (strDateTime.indexOf("&nbsp;") >= 0)
			strDateTime = strDateTime.replace("&nbsp;", " ");

		strDateTime = strDateTime.replace("-", "/");
		strDateTime = strDateTime.replace("-", "/");
		strDateTime = strDateTime.replace("上午", "");
		strDateTime = strDateTime.replace("am", "");

		if (strDateTime.indexOf("下午") >= 0)
		{
			strDateTime = strDateTime.replace("下午", "");
			iHour = 12;
		}

		if (strDateTime.indexOf("pm") >= 0)
		{
			strDateTime = strDateTime.replace("pm", "");
			iHour = 12;
		}

		strDateTime = new Date(strDateTime);

		if (strDateTime.getTime())
		{
			var strYear, strMonth, strDay, strHour, strMinute, strSecond, strMilliSecond;

			strYear = strDateTime.getFullYear();
			strMonth = "00" + (strDateTime.getMonth() + 1);
			strDay = "00" + strDateTime.getDate();
			strHour = "00" + (strDateTime.getHours() + iHour);
			strMinute = "00" + strDateTime.getMinutes();
			strSecond = "00" + strDateTime.getSeconds();
			strMilliSecond = strDateTime.getMilliseconds() + "000";

			strMonth = strMonth.substr(strMonth.length-2, 2);
			strDay = strDay.substr(strDay.length-2, 2);
			strHour = strHour.substr(strHour.length-2, 2);
			strMinute = strMinute.substr(strMinute.length-2, 2);
			strSecond = strSecond.substr(strSecond.length-2, 2);
			strMilliSecond = strMilliSecond.substr(0, 3);

			if (strHour == "12")
				strHour = "00";
			else if (strHour == "24")
				strHour = "12";

			strFormat = strFormat.replace("yyyy", strYear);
			strFormat = strFormat.replace("yy", strYear);
			strFormat = strFormat.replace("ms", strMilliSecond);
			strFormat = strFormat.replace("m", strMonth);
			strFormat = strFormat.replace("d", strDay);
			strFormat = strFormat.replace("h", strHour);
			strFormat = strFormat.replace("n", strMinute);
			strFormat = strFormat.replace("s", strSecond);

			return strFormat;
		}
		else
		{
			return "";
		}
	}

	this.DateDay = function (nYear, nMonth) {
	
		var aryDateDay = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
		if (((nYear % 4 == 0) && (nYear % 100 != 0)) || (nYear % 400 == 0))
			aryDateDay[2]++;

		return aryDateDay[nMonth];
	}

	var thsElement = document.getElementById(ElementId);
	var thsClass = this;

	theDate = new Date(theDate.toString());
	
	if (!theDate.getTime())
		theDate = new Date();
		
	var theYear  = theDate.getFullYear();
	var theMonth = theDate.getMonth();

	nowDate = new Date(nowDate.toString());

	if (!nowDate.getTime())
		nowDate = new Date();

	thsElement.innerHTML = "<table border='0' cellspacing='0' cellpadding='0'><tr><td><input type='text' value='" + this.FormatDateTime(theDate.toDateString(), "yyyy/m/d") + "' readonly><button type='button'>▼</button></td></tr><tr><td><div style='position: absolute; visibility: hidden;'></div></td></tr></table>";

	var thsClass = this;
	var objElement = thsElement.getElementsByTagName("BUTTON")[0];

	objElement.onclick = function () {

		var pntElement = thsClass.ParentNodeTagName(this, "TABLE");

		pntElement = pntElement.getElementsByTagName("DIV")[0];

		if (pntElement.style.visibility == "hidden")
		{
			pntElement.style.visibility = "visible";

			pntElement.onmouseout = null;
			pntElement.onmouseover = function ()	{

				this.style.visibility = "visible";

				this.onmouseout = function () {

					this.style.visibility = "hidden";
				}
			}
		}
		else
		{
			pntElement.style.visibility = "hidden";
		}
	}

	this.YearCalendar = function (pntElement, add) {
	
		theYear += add;

		pntElement = this.ParentNodeTagName(pntElement, "DIV");

		this.TheCalendar(pntElement);
	}

	this.MonthCalendar = function (pntElement)	{

		theMonth = Number(pntElement.value);

		pntElement = this.ParentNodeTagName(pntElement, "DIV");

		this.TheCalendar(pntElement);
	}

	this.TheCalendar = function (pntElement) {

		var strCalendar, theWeek, EndDay, SumDay, AddDay, theDay, strFg, i;
		var theDay = this.DateDay(theYear, theMonth);

		theDate.setYear(theYear);
		theDate.setMonth(theMonth);
		theDate.setDate(1);
		theWeek = theDate.getDay();
	
		var nowYear = nowDate.getFullYear();
		var nowMonth = nowDate.getMonth();
		var nowDay = nowDate.getDate();
	
		if (nowYear > theYear || (nowYear == theYear && nowMonth > theMonth))
		{
			strFg = "+";
		}
		else if (nowYear == theYear && nowMonth == theMonth)
		{
			strFg = "=";
		}
		else
		{
			strFg = "-";
		}

		var aryMonth = new Array("", "", "", "", "", "", "", "", "", "", "", "");

		aryMonth[theMonth] = " selected ";

		strCalendar =  "<table border='0' cellpadding='0' cellspacing='1' bgcolor='#000000'>";
		strCalendar += "<tr bgcolor='#FFFFFF'>";
		strCalendar += "<td>";
		strCalendar += "<table border='0' cellpadding='2' cellspacing='0'>";
		strCalendar += "<tr>";
		strCalendar += "<td align='center' colspan='7'>";
		strCalendar += "<select size='1'>";
		strCalendar += "<option value='0'" + aryMonth[0] + ">January</option>";
		strCalendar += "<option value='1'" + aryMonth[1] + ">February</option>";
		strCalendar += "<option value='2'" + aryMonth[2] + ">March</option>";
		strCalendar += "<option value='3'" + aryMonth[3] + ">April</option>";
		strCalendar += "<option value='4'" + aryMonth[4] + ">May</option>";
		strCalendar += "<option value='5'" + aryMonth[5] + ">June</option>";
		strCalendar += "<option value='6'" + aryMonth[6] + ">July</option>";
		strCalendar += "<option value='7'" + aryMonth[7] + ">August</option>";
		strCalendar += "<option value='8'" + aryMonth[8] + ">September</option>";
		strCalendar += "<option value='9'" + aryMonth[9] + ">October</option>";
		strCalendar += "<option value='10'" + aryMonth[10] + ">November</option>";
		strCalendar += "<option value='11'" + aryMonth[11] + ">December</option>";
		strCalendar += "</select>&nbsp;<a href='#' name='YearDown'>&lt;</a>&nbsp;" + theYear + "&nbsp;<a href='#' name='YearUp'>&gt;</a>";
		strCalendar += "</td>";
		strCalendar += "</tr>";
		strCalendar += "<tr align='center'>";
		strCalendar += "<td><font color='#FF0000'>日</font></td>";
		strCalendar += "<td>一</td>";
		strCalendar += "<td>二</td>";
		strCalendar += "<td>三</td>";
		strCalendar += "<td>四</td>";
		strCalendar += "<td>五</td>";
		strCalendar += "<td>六</td>";
		strCalendar += "</tr>";
	
		EndDay = theDay + theWeek;
		AddDay = EndDay % 7;
	
		if (AddDay == 0)
			AddDay = 0;
		else
			AddDay = 7 - AddDay;
	
		SumDay = EndDay + AddDay;

		var strMonth = theMonth + 1;

		for (i=1; i<=SumDay; i++)
		{
			if (i <= theWeek || i > EndDay)
			{
				strCalendar += "<td align='center'>&nbsp;</td>";
			}
			else
			{
				theDay = i - theWeek;
	
				if (strFg == "+" || (strFg == "=" && nowDay >= theDay))
					strCalendar += "<td align='center'><a href='#' name='" + theYear + "/" + strMonth + "/" + theDay + "'>" + theDay + "</a></td>";
				else
					strCalendar += "<td align='center'>" + theDay + "</td>";
			}
	
			if ((i+7) % 7 == 0)
				strCalendar += "</tr><tr>";
		}
	
		strCalendar += "</tr>";
		strCalendar += "</table>";
		strCalendar += "</td>";
		strCalendar += "</tr>";
		strCalendar += "</table>";
			
		pntElement.innerHTML = strCalendar;

		var chdElement = pntElement.getElementsByTagName("SELECT")[0];

		chdElement.onchange = function () 	{

			thsClass.MonthCalendar(this);
		}

		if (document.all)
		{
			chdElement.onclick = function () 	{

				var pntElement = thsClass.ParentNodeTagName(this, "TABLE");

				pntElement = thsClass.ParentNodeTagName(pntElement, "TABLE");
				pntElement = thsClass.ParentNodeTagName(pntElement, "TABLE");
				pntElement = pntElement.getElementsByTagName("DIV")[0];
				pntElement.onmouseout = null;
			}
		}

		chdElement = pntElement.getElementsByTagName("A");

		for (i=0; i<chdElement.length; i++)
		{
			switch (chdElement[i].name)
			{
				case "YearDown":
					
					chdElement[i].onclick = function () {

						thsClass.YearCalendar(this, -1);
						
						return false;
					}
					
					break;
				
				case "YearUp":
					
					chdElement[i].onclick = function () {
						
						thsClass.YearCalendar(this, 1);
						
						return false;
					}
					
					break;

				case "":

					break;

				default:

					chdElement[i].onclick = function () {
					
						var divElement = thsClass.ParentNodeTagName(this, "DIV");

						divElement.style.visibility = "hidden";

						divElement = thsClass.ParentNodeTagName(divElement, "TABLE");
						divElement = divElement.getElementsByTagName("INPUT")[0];
						divElement.value = thsClass.FormatDateTime(this.name, "yyyy/m/d");
						divElement.select();

						return false;
					}
					
					break;
			}
		}
	}

	objElement = thsElement.getElementsByTagName("DIV")[0];

	thsClass.TheCalendar(objElement);
}
//-->