var CrrDate = new Date();
//今天的日期
var ToDay;
//事件数组
var ActionArg = new Array();
ActionArg[0] = new Array();

//清空单元格所有的内容
function GridClear(){
	var i;
	var j;
	for (i = 1; i < 7; i++ ) {
		for (j = 1; j < 8; j++ ) {
			//alert("D" + i + j);
			document.getElementById("D" + i + j).innerHTML = "";
		}
	}
	document.getElementById("CalendarTitle").innerHTML = "&nbsp;"
}

//显示当月的日历
//参数为某年某月1日
function DrawCalendar(mDate) {
	var FirstDayWeekDay;//一日的星期树
	var i;
	var j;
	var k;
	var mTmpDate;
	var strDay;

	var week = new Date().getDay();
	switch (week) {
		case 1: week = "星期一"; break;
    	case 2: week = "星期二"; break;
    	case 3: week = "星期三"; break;
    	case 4: week = "星期四"; break;
    	case 5: week = "星期五"; break;
    	case 6: week = "星期六"; break;
    	case 0: week = "星期天"; break;
	}

	CrrDate = new Date(mDate.getFullYear(),mDate.getMonth(),1);
	document.getElementById("CalendarTitle").innerHTML = "" +  mDate.getFullYear() ;
	document.getElementById("monthSpan").innerHTML = "<font style='font-size:26px'>" + (mDate.getMonth() + 1) + "</font><font style='font-size:13px'>&nbsp;</font>月";
	var wk = document.getElementById("weekSpan");
	if (wk != undefined) {
		wk.innerHTML = week;
	}
	FirstDayWeekDay = mDate.getDay() + 1;
	//alert(FirstDayWeekDay);
	mTmpDate = new Date(mDate.getFullYear(),mDate.getMonth(),mDate.getDate());
//	alert(mDate.getFullYear() + "==================" + mDate.getMonth()+ "==================" + mDate.getDate());
	
	while (mDate.getMonth() == mTmpDate.getMonth()) {
		
		i = mTmpDate.getDate() + FirstDayWeekDay - 2;
		
		i = parseInt(i / 7) + 1;

		j = mTmpDate.getDay() + 1;
		//alert(mTmpDate.getDay());
		//alert(j);
		var colorStr;
		if (j == 1) {
			colorStr = "#E74848";
		} else {
			colorStr = "#FFFFFF";
		}
		
		strDay = "<font color="+colorStr+">" + mTmpDate.getDate() + "</font>";
		//k = FindAction(mTmpDate);
		//If k>0 Then
			happyTime = getTimeStr(mTmpDate);
			strDay = "<A HREF='./showPage.html?method=showInfo&happyTime=" + happyTime + "' TARGET='_parent' style='text-decoration:none;'><FONT COLOR="+colorStr+">" + mTmpDate.getDate() + "</FONT></A>";
		//End If 
		//alert(mTmpDate.getTime() + "=====" + ToDay.getTime());
		if (mTmpDate.getTime() == ToDay.getTime()) {
			
			//strDay = "<span STYLE=""width: 19; height: 15;border: 1 outset"">" + strDay + "</span>"
			strDay = "<table border=0 width=100% cellSpacing=0 cellPadding=0  style='background-color:#66B4FF;'><tr><td align='center' style='font-size:12px;'>" +  "<A HREF='./showPage.html?method=showInfo&happyTime=" + happyTime + "' TARGET='_parent' style='text-decoration:none;color:#FFF;'><FONT COLOR='white'>" + mTmpDate.getDate() + "</FONT></A>" + "</td></tr></table>";
		}
		//alert("D" + i + j);
		document.getElementById("D" + i + j).innerHTML = strDay;
		mTmpDate = new Date(mTmpDate.getFullYear(),mTmpDate.getMonth(),mTmpDate.getDate() + 1);
	}
}

function getTimeStr(mTmpDate) {
	var year = mTmpDate.getFullYear();
	var month = mTmpDate.getMonth() + 1;
	var day = mTmpDate.getDate();

	if (month < 10) {
		var month = "0" + month;
	}
	if (day < 10) {
		var day = "0" + day;
	}

	return year + "-" + month + "-" + day;
}

function DateLeft_onclick(){
	CrrDate = new Date(CrrDate.getFullYear(),CrrDate.getMonth() - 1,CrrDate.getDate());
	GridClear();
	DrawCalendar(CrrDate);
}

function DateRight_onclick(){
	CrrDate = new Date(CrrDate.getFullYear(),CrrDate.getMonth()+1,CrrDate.getDate());
	
	GridClear();
	DrawCalendar(CrrDate);
}


function FindAction(mDate){
	var i;
	var rtn = -1;
	for (i = 1; i < ActionArg[0].length; i++) {
		if (ActionArg[0][i] == mDate) {
			rtn = ActionArg[1][i];
			break;
		}
	}
	return rtn;
}

function calendarInit(){
	
	var ViewDate = document.getElementById("ViewDate");
	
	var dtArr = ViewDate.value.split("-");
	CrrDate = new Date(dtArr[0] + "/" + dtArr[1] + "/1");

	var ToDayDate = document.getElementById("ToDayDate");
	var dtArr1 = ToDayDate.value.split("-");
	ToDay = new Date(dtArr1[0] + "/" + dtArr1[1] + "/" + dtArr1[2]);
	
	GridClear();
	DrawCalendar(CrrDate);
	
}
