var DaysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31,30, 31, 30, 31);
var ArrMonth = new Array("1", "2", "3","4", "5", "6", "7","8", "9","10","11", "12");
//var ArrMonthName = new Array("Jan", "Feb", "Mar","Apr", "May", "Jun", "Jul","Aug", "Sep","Oct","Nov", "Dec");
var ArrMonthName = new Array("01", "02", "03","04", "05", "06", "07","08", "09","10","11", "12");

now = new Date();
nowYear = now.getFullYear();
nowMonth = now.getMonth()+1;
nowDay = now.getDate();
nowDate = nowYear + "-" + ((nowMonth < 10) ? ("0" + nowMonth) : nowMonth) + "-" + ((nowDay < 10) ? ("0" + nowDay) : nowDay);
var strCal = "";

function getDaysInMonth(year,month){
	if (month == 2){
		return (((year % 4 == 0) && ((year % 100) != 0)) ||(year % 400 == 0)) ? 29 : 28;
	}else{
		return DaysInMonth[month-1];
	}
}

function InitCalYear(){
	var ii = 0;
	var StarYear = nowYear + 10;
	var EndYear = nowYear - 10;
	for(var i = StarYear;i >= EndYear;i--){
		document.getElementById("CalYear").options[ii] = new Option(i,i);
		if (document.getElementById("CalYear").options[ii].value == nowYear){
			document.getElementById("CalYear").options[ii].selected = true;
		}
		ii++;
	}
}

function InitCalMonth(){
	for(var i = 0;i < 12;i++){
		document.getElementById("CalMonth").options[i] = new Option(ArrMonthName[i],ArrMonth[i]);
		if (document.getElementById("CalMonth").options[i].value == nowMonth){
			document.getElementById("CalMonth").options[i].selected = true;
		}
	}
}

function buttonOver(id){
	oldclassname = document.getElementById(id).className;
	document.getElementById(id).className = "DayOver";
}

function buttonOut(id){
	document.getElementById(id).className = oldclassname;
}

function ChgYear(id){
	var Year = id.options[id.selectedIndex].value;
	var MonthIndex = document.getElementById("CalMonth").selectedIndex;
	var Month = document.getElementById("CalMonth").options[MonthIndex].value;
	InitCalendar(Year,Month);
}

function ChgMonth(id){
	var Month = id.options[id.selectedIndex].value;
	var YearIndex = document.getElementById("CalYear").selectedIndex;
	var Year = document.getElementById("CalYear").options[YearIndex].value;
	InitCalendar(Year,Month);
}

function InitCalendar(Year,Month){
	if (!Year && !Month){
		Year = nowYear;
		Month = nowMonth;
	}

	var DayInMonth = getDaysInMonth(Year,Month);
	var ThisMonthWeek = new Date(Year,parseInt(Month)-1,1);
	Week = ThisMonthWeek.getDay();

	strCal = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"1\">\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\" >&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);
		if (strDate == nowDate){
			//var ClassName = "DayNow";
			strCal += "<td id=\"day_" + intWeek + "_" + j + "\" width=\"16\" height=\"16\" onMouseover=\"javascript:buttonOver(this.id);\" onMouseOut=\"javascript:buttonOut(this.id);\" onClick=\"javascript:doClick(" + Year + "," + Month + "," + j + ");\" style=\"cursor:hand;\"><em>" + j + "</em></td>\n";
		//}else if(ArticleInDay.indexOf(strDate) > -1){
			//var ClassName = "ArticleInDay";
		//	var ClassName = "DayNow";
		//}else if (intWeek == 0){
		//	var ClassName = "DaySun";
		//}else if (intWeek == 6){
		//	var ClassName = "DaySat";
		}else{
			//var ClassName = "Day";
			strCal += "<td id=\"day_" + intWeek + "_" + j + "\" width=\"16\" height=\"16\" onMouseover=\"javascript:buttonOver(this.id);\" onMouseOut=\"javascript:buttonOut(this.id);\" onClick=\"javascript:doClick(" + Year + "," + Month + "," + j + ");\" style=\"cursor:hand;\">" + j + "</td>\n";
		}
		if (intWeek == 6){
			intWeek = 0;
			strCal += "</tr>\n";
			strCal += "<tr>\n";
		}else{
			intWeek++;
		}
	}

	for(k=intWeek;k<=6;k++){
		strCal += "<td width=\"16\" height=\"16\" >&nbsp;</td>\n";
	}

	strCal += "</tr>\n";
	strCal += "</table>\n";

	document.getElementById("CalBody").innerHTML = strCal;
}
