var LunCalendar = function(){

	this.DaysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31,30, 31, 30, 31);
	this.ArrMonth = new Array("1", "2", "3","4", "5", "6", "7","8", "9","10","11", "12");
	this.ArrMonthName = new Array("01", "02", "03","04", "05", "06", "07","08", "09","10","11", "12");
	this.now = new Date();
	this.nowYear = this.now.getFullYear();
	this.nowMonth = this.now.getMonth()+1;
	this.nowDay = this.now.getDate();
	this.nowDate = this.nowYear + "-" + ((this.nowMonth < 10) ? ("0" + this.nowMonth) : this.nowMonth) + "-" + ((this.nowDay < 10) ? ("0" + this.nowDay) : this.nowDay);
	this.strCal = "";
}

LunCalendar.prototype.getDaysInMonth = function(year, month){
	if (month == 2){
		return (((year % 4 == 0) && ((year % 100) != 0)) ||(year % 400 == 0)) ? 29 : 28;
	}else{
		return this.DaysInMonth[month-1];
	}
}

LunCalendar.prototype.InitLunCalYear = function(){
	var ii = 0;
	var StarYear = this.nowYear + 10;
	var EndYear = this.nowYear - 10;
	for(var i = StarYear;i >= EndYear;i--){
		document.getElementById("CalLunYear").options[ii] = new Option(i,i);
		if (document.getElementById("CalLunYear").options[ii].value == this.nowYear){
			document.getElementById("CalLunYear").options[ii].selected = true;
		}
		ii++;
	}
}

LunCalendar.prototype.InitLunCalMonth = function(){
	for(var i = 0;i < 12;i++){
		document.getElementById("CalLunMonth").options[i] = new Option(this.ArrMonthName[i], this.ArrMonth[i]);
		if (document.getElementById("CalLunMonth").options[i].value == this.nowMonth){
			document.getElementById("CalLunMonth").options[i].selected = true;
		}
	}
}

LunCalendar.prototype.buttonOver = function(id){
	oldclassname = document.getElementById(id).className;
	document.getElementById(id).className = "DayOver";
}

LunCalendar.prototype.buttonOut = function(id){
	document.getElementById(id).className = oldclassname;
}

LunCalendar.prototype.ChgLunYear = function(id){
	var Year = id.options[id.selectedIndex].value;
	var MonthIndex = document.getElementById("CalLunMonth").selectedIndex;
	var Month = document.getElementById("CalLunMonth").options[MonthIndex].value;
	this.InitLunCalendar(Year, Month);
}

LunCalendar.prototype.ChgLunMonth = function(id){
	var Month = id.options[id.selectedIndex].value;
	var YearIndex = document.getElementById("CalLunYear").selectedIndex;
	var Year = document.getElementById("CalLunYear").options[YearIndex].value;
	this.InitLunCalendar(Year, Month);
}

LunCalendar.prototype.InitLunCalendar = function(Year, Month){
	if (!Year && !Month){
		Year = this.nowYear;
		Month = this.nowMonth;
	}

	var DayInMonth = this.getDaysInMonth(Year,Month);
	var ThisMonthWeek = new Date(Year,parseInt(Month)-1,1);
	Week = ThisMonthWeek.getDay();

	strCal = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"1\" class=\"caltab\">\n";
	strCal += "<tr>";
	strCal += "<th>S</th>";
	strCal += "<th>M</th>";
	strCal += "<th>T</th>";
	strCal += "<th>W</th>";
	strCal += "<th>T</th>";
	strCal += "<th>F</th>";
	strCal += "<th>S</th>";
	strCal += "</tr>\n";
	strCal += "<tr>\n";
	for(var i=0;i<Week;i++){
		strCal += "<td width=\"16\" height=\"16\" align=\"center\" valign=\"middle\">&nbsp;</td>\n";
	}

	var intWeek = i;
	for(var j=1;j<=DayInMonth;j++){
		var strDate = Year + "-" + ((Month < 10) ? ("0" + Month) : Month) + "-" + ((j < 10) ? ("0" + j) : j);
		var tmpMonth = (Month < 10) ? ("0" + Month) : Month;
		var tmpDay = (j < 10) ? ("0" + j) : j;
		if (strDate == this.nowDate){
			strCal += "<td id=\"day_" + intWeek + "_" + j + "\" width=\"16\" height=\"16\" onMouseover=\"javascript:buttonOver(this.id);\" onMouseOut=\"javascript:buttonOut(this.id);\" style=\"cursor:hand;\"><a href=\"http://mindcity.sina.com.tw/east/MC-lunar/daily.php?year=" + Year + "&month=" + tmpMonth + "&mday=" + tmpDay + "\" target=\"_blank\"><em>" + j + "</em></a></td>\n";
		}else{
			strCal += "<td id=\"day_" + intWeek + "_" + j + "\" width=\"16\" height=\"16\" onMouseover=\"javascript:buttonOver(this.id);\" onMouseOut=\"javascript:buttonOut(this.id);\" style=\"cursor:hand;\"><a href=\"http://mindcity.sina.com.tw/east/MC-lunar/daily.php?year=" + Year + "&month=" + tmpMonth + "&mday=" + tmpDay + "\" target=\"_blank\">" + j + "</a></td>\n";
		}
		if (intWeek == 6 && j < DayInMonth){
			intWeek = 0;
			strCal += "</tr>\n";
			strCal += "<tr>\n";
		}else{
			intWeek++;
		}
	}

	for(k=intWeek;k<=6;k++){
		strCal += "<td width=\"16\" height=\"16\" align=\"center\" valign=\"middle\" >&nbsp;</td>\n";
	}

	strCal += "</tr>\n";
	strCal += "</table>\n";

	document.getElementById("CalLunBody").innerHTML = strCal;
}
