var callback;
var now,thisYear,thisMonth,thisDay,today,aryMonth;
var bgclrTitle,clrTitle,bgclrYMD,clrYMD,bgclrWeek,bgclrCell,bgclrToday,clrToday,clrMarked,clrBtn;
var myDiv,myTable,objText,btnToday,cmb_year,cmb_month;
var date_lessB,date_less,date_biggerB,date_bigger,date_equalB=[],date_equal=[];
var mark_mode = true, mark_close = true;//標記模式、標記停/啟用(true/false)
if (document.body) main();
function main()
{
	initArguments();
	aryMonth = ["1月 Jan.","2月 Feb.","3月 Mar.","4月 Apr.","5月 May","6月 June","7月 July","8月 Aug.","9月 Sep.","10月 Oct.","11月 Nov.","12月 Dec."];
	bgclrTitle = "#a15a20" ; clrTitle = "white";//標題列背景、文字色彩
	bgclrYMD = "#FFCC00" ; clrYMD = "black";//年月列色彩
	bgclrWeek = "#FFFFFF" ; bgclrCell = "#fff7ed";//星期列背景、一般日背景
	bgclrToday = "lightskyblue"; clrToday = "white";//今日色彩
	clrMarked = "gray", clrBtn = "black";//標記顏色,標記按鈕顏色
	document.body.appendChild(myDiv = document.createElement("div"));
	myDiv.appendChild(myTable = document.createElement("table"));
	toCreateTable();
	btnToday = document.getElementById("btnToday");
	cmb_year = document.getElementById("cmb_year");
	cmb_month = document.getElementById("cmb_month");
	initLoadPage();
}
function toCreateTable()
{
	var tbody,tr,td;
	myDiv.style.visibility = "hidden";
	myDiv.style.position = "absolute";
	myDiv.style.border = "1px solid black";
	myDiv.style.top = myTable.cellSpacing = myTable.cellPadding = 0;
	myTable.style.fontSize = "9pt";
	myTable.appendChild(tbody = document.createElement("tbody"));
	tbody.appendChild(tr = document.createElement("tr"));
	tr.bgColor = bgclrTitle;
	tr.style.color = clrTitle;
	tr.align = "center";
	tr.appendChild(td = document.createElement("td"));
	td.setAttribute("colSpan","6");
	td.innerHTML = "請選擇日期";
	tr.appendChild(td = document.createElement("td"));
	td.bgColor = bgclrTitle;
	td.innerHTML = "<input type='button' onclick='toHideDiv()' value='X' style='font-weight:bold;border:none;background-color:" +
		bgclrTitle + ";color:" + clrBtn + ";width:100%;' />";
	tbody.appendChild(tr = document.createElement("tr"));
	tr.align = "center";
	tr.bgColor = bgclrYMD;
	tr.style.color = clrYMD;
	tr.appendChild(td = document.createElement("td"));
	td.setAttribute("colSpan","7");
	td.innerHTML = "<input type='button' value='Today' id='btnToday' style='border:none;background-color:" + bgclrYMD +
		";color:" + clrBtn + ";' onClick=\"onClickDay()\">";
	td.innerHTML += "<input type='button' value='<' style='border:none;width:22px;font-weight:bold;background-color:" +
		bgclrYMD + ";color:" + clrBtn + ";' onclick='onMonthStep(-1)'>";
	td.innerHTML += "<select id='cmb_year' onChange='onYearChange()' style='border:none;background-color:" + bgclrYMD +
		";color:" + clrYMD + ";'></select>";
	td.innerHTML += "<select id='cmb_month' onChange='ontheDateChange()' style='border:none;background-color:" + bgclrYMD +
		";color:" + clrYMD + ";'></select>";
	td.innerHTML += "<input type='button' value='>' style='border:none;width:22px;font-weight:bold;background-color:" +
		bgclrYMD + ";color:" + clrBtn + ";' onclick='onMonthStep(1)'>";
	td.innerHTML += " <input type='button' value='Clear' style='border:none;background-color:" + bgclrYMD + ";color:" +
		clrBtn + ";' onClick=\"onClearDay()\">";
	tbody.appendChild(tr = document.createElement("tr"));
	tr.height = 24;
	tr.align = "center";
	tr.bgColor = bgclrWeek;
	tr.style.fontWeight = "bold";
	var dayName = ["日<br>Sun","一<br>Mon","二<br>Tue","三<br>Wed","四<br>Thr","五<br>Fri","六<br>Sat"];
	for (var i = 0 ; i < dayName.length ; i++)
	{
		tr.appendChild(td = document.createElement("td"));
		td.style.color = getWeekdayColor(i);
		td.width = 35;
		td.innerHTML = dayName[i];
	}
}
function getWeekdayColor(weekday)//星期色彩
{
	switch (Number(weekday))
	{
	case 0: return "red";
	case 1:
	case 2:
	case 3:
	case 4:
	case 5: return "blue";
	case 6: return "green";
	default: return "black";
	}
}
//以下為月曆產生、選取
function initArguments()
{
	now = new Date();
	thisYear = now.getFullYear();
	thisMonth = now.getMonth();
	thisDay = now.getDate();
	today = new Date(thisYear,thisMonth,thisDay);
}
function initLoadPage()
{
	initYear(Math.floor(thisYear / 10) * 10);
	cmb_year.value = thisYear;
	initMonth();
}
function delTableRows()
{
	while(myTable.rows.length > 3)
		myTable.deleteRow(-1);
}
function initYear(iFirst)
{
	cmb_year.options.length = 0;
	if (iFirst > 1900) cmb_year.options.add(new Option("<<<","<<<"));
	var iLast = iFirst + 9;
	for (var i = iFirst; i <= iLast ; i++)
		cmb_year.options.add(new Option(i.toString(),i.toString()));
	if (iLast < 2099) cmb_year.options.add(new Option(">>>",">>>"));
}
function initMonth()
{
	cmb_month.options.length = 0;
	for (var i = 0; i < 12 ; i++)
		cmb_month.options.add(new Option(aryMonth[i], i));
	cmb_month.value = thisMonth;
}
function initTable(iYear,iMonth)
{
	delTableRows();
	var tmpDate,day_color;
	if (iYear == null || isNaN(iYear)) return;
	var i = 1;
	var theDay = new Date(iYear,iMonth,i);
	var tbRow, tbCell;
	while (theDay.getMonth() == iMonth)
	{
		tbRow = myTable.insertRow(myTable.rows.length);
		tbRow.bgColor = bgclrCell;
		for ( var j = 0; j < 7 ; j++)
		{
			tbCell = tbRow.insertCell(j);
			if (theDay.getDay()==j && theDay.getMonth()==iMonth)
			{
				day_color = getWeekdayColor(j);
				day_bgcolor = bgclrCell;
				tmpDate = iYear.toString() + "/" + (Number(iMonth) + 1).toString() + "/" + i.toString();
				if (today.valueOf() == theDay.valueOf())
				{
					day_color = clrToday;
					day_bgcolor = bgclrToday;
				}
				tbCell.innerHTML = '<input type="button" name="btnDay" id="btn' + theDay.valueOf() +
					'" style="border:none;font-weight:bold;color:' + day_color + ';background-color:' +
					day_bgcolor + ';width:100%" onclick="onClickDay(\'' + tmpDate +'\')" value="' + i + '"/>';
				theDay.setDate(++i);
			}
			else
				tbCell.innerHTML = "&nbsp;";
		}
	}
	btnToday.disabled = false;
	if (mark_mode) toDisabledBtn();
}
function onMonthStep(step)
{
	switch (cmb_month.selectedIndex)
	{
	case 0://一月
		if (step == -1)
		{
			cmb_month.selectedIndex = 11;
			cmb_year.selectedIndex -= 1;
		}
		else
			cmb_month.selectedIndex += 1;
		break;
	case 11://十二月
		if (step == 1)
		{
			cmb_month.selectedIndex = 0;
			cmb_year.selectedIndex += 1;
		}
		else
			cmb_month.selectedIndex -= 1;
		break;
	default:
		cmb_month.selectedIndex += step;
		break;
	}
	onYearChange();
}
function onYearChange()
{
	var iFirst;
	switch (cmb_year.value)
	{
	case "<<<":
		iFirst = Number(cmb_year.options[1].value) - 10;
		if (iFirst < 1900) iFirst = 1900;
		initYear(iFirst);
		cmb_year.selectedIndex = isNaN(cmb_year.options[0].value) ? 1 : 0;
		break;
	case ">>>":
		var iLast = Number(cmb_year.options[cmb_year.options.length-2].value) + 10;
		if (iLast > 2099) iLast = 2099;
		iFirst = iLast - 9;
		initYear(iFirst);
		cmb_year.selectedIndex = isNaN(cmb_year.options[0].value) ? 1 : 0;
		break;
	}
	ontheDateChange();
}
function ontheDateChange(){	initTable(cmb_year.value,cmb_month.value); }
function onClickDay(sDate)
{
	if (sDate == null) sDate = thisYear.toString() + "/" + (thisMonth+1).toString() + "/" + thisDay.toString();
	if (objText != null)
	{
		objText.value = sDate;
		toHideDiv();
		objText = null;
		if (callback != null) callback(sDate);
	}
}
function onClearDay()
{
	if (objText != null)
	{
		objText.value = "";
		toHideDiv();
		objText = null;
		if (callback != null) callback(null);
	}
}
function setBeforeDate(sDate) {	date_equalB.push(date_lessB = new Date(sDate)); }
function setAfterDate(sDate) { date_equalB.push(date_biggerB = new Date(sDate)); }
function addEqualDate(sDate) { date_equalB.push(new Date(sDate)); }
function cleanLimitDate()
{
	date_less = date_bigger = null;
	date_equal = new Array();
}
function toDisabledBtn()
{
	var btnDayAry = document.getElementsByName("btnDay");
	var tmpDate = new Date(Number(cmb_year.value),Number(cmb_month.value),1);
	var tmpBtn,marked = false;
	if (date_less != null) marked |= (today.valueOf() < date_less.valueOf());
	if (date_bigger != null) marked |= (today.valueOf() > date_bigger.valueOf());
	btnToday.disabled = mark_close ? marked : !marked;
	for (var i = 0 ; i < btnDayAry.length ; i++)
	{
		marked = false;
		tmpDate.setDate(Number(btnDayAry[i].value));
		if (date_less != null) marked |= (tmpDate.valueOf() < date_less.valueOf());
		if (date_bigger != null) marked |= (tmpDate.valueOf() > date_bigger.valueOf());
		btnDayAry[i].disabled = mark_close ? marked : !marked;
		btnDayAry[i].style.color = btnDayAry[i].disabled ? clrMarked : getWeekdayColor(tmpDate.getDay());
	}
	for (var i = 0 ; i < date_equal.length ; i++)
	{
		if (date_equal[i].valueOf() == today.valueOf()) btnToday.disabled = mark_close ? true : false;
		tmpBtn = document.getElementById("btn" + date_equal[i].valueOf().toString());
		if (tmpBtn != null)
		{
			tmpBtn.disabled = mark_close ? true : false;
			tmpBtn.style.color = mark_close ? clrMarked : getWeekdayColor(tmpDate.getDay());
		}
	}
}
function toHideDiv(){	myDiv.style.visibility = "hidden";}
function toShowDiv(obj,mode)
{
	if (!document.body) return;
	mark_mode = mode;
	ontheDateChange();
	objText = obj;
	var iLeft = obj.offsetLeft;
	var iTop = obj.offsetTop + obj.offsetHeight;
	while(obj = obj.offsetParent)
	{
		iLeft += obj.offsetLeft;
		iTop += obj.offsetTop;
	}
	myDiv.style.left = iLeft.toString() + "px";
	myDiv.style.top = iTop.toString() + "px";
	myDiv.style.visibility = "visible";
}
function toShow(obj,mode,option,func)
{
	cleanLimitDate();
	initArguments();
	switch (option)//compare with today
	{
	case 0://limit mode
		date_equal = date_equalB;
		date_less = date_lessB;
		date_bigger = date_biggerB;
		break;
	case 1://mark under
		date_equal.push(date_bigger = new Date(today));
		break;
	case 2://mark before
		date_bigger = new Date(today);
		break;
	case 3://mark after
		date_less = new Date(today);
		break;
	case 4://mark over
		date_equal.push(date_less = new Date(today));
		break;
	default://no mark
		break;
	}
	callback = func;
	toShowDiv(obj,mode);
}
function toShow_Calendar(obj,func){ toShow(obj,false,null,func); }
function toShow_Birthday(obj,func){ toShow_BeforeToday(obj,func); }
function toShow_BeforeToday(obj,func) { toShow(obj,true,2,func); }
function toShow_UnderToday(obj,func) { toShow(obj,true,1,func); }
function toShow_AfterToday(obj,func) { toShow(obj,true,3,func); }
function toShow_OverToday(obj,func) { toShow(obj,true,4,func); }
function toShow_LimitMode(obj,func) { toShow(obj,true,0,func); }
/*
write by wyvern
support Firefox
ver 20080916
*/