﻿function CalendarClassEvent(sDate, strDate)
{
	var theDate = new Date( sDate + "/1" );
	var EndDay, SumDay, AddDay, eDate, nxtLink;
	var nowDate = new Date( nowDateTime.toDateString() );
	var theYear  = theDate.getFullYear();
	var theMonth = theDate.getMonth();
	var theWeek = theDate.getDay();
	var theDay = DateDay(theYear, theMonth);

	theMonth += 1;

	nowDate.setDate(1);

	if (theDate <= nowDate)
	{
		nxtLink = "";

		theDate.setMonth((theDate.getMonth()+1));
	}
	else
	{
		theDate.setMonth((theDate.getMonth()-1));

		nxtLink = " href='http://www.cnyes.com/club/class/page01.asp?yy=" + theDate.getFullYear() + "&mm=" + (theDate.getMonth()+1) + "'";

		theDate.setMonth((theDate.getMonth()+2));
	}

	var strHTML = "<div align='center'>";

	strHTML += "<table width='100%' border='0' cellpadding='3' cellspacing='0' class='Calendar'>";
	strHTML += "<tr>";
	strHTML += "<td height='30' align='center'>";
	strHTML += "<a " + nxtLink + ">&lt;&lt;</a>";
	strHTML += "&nbsp;<a href='http://www.cnyes.com/club/class/page01.asp?yy=" + theYear + "&mm=" + theMonth + "'>";

	switch (theMonth)
	{
		case 1:
			strHTML += "January";
			break;
		case 2:
			strHTML += "February";
			break;
		case 3:
			strHTML += "March";
			break;
		case 4:
			strHTML += "April";
			break;
		case 5:
			strHTML += "May";
			break;
		case 6:
			strHTML += "June";
			break;
		case 7:
			strHTML += "July";
			break;
		case 8:
			strHTML += "August";
			break;
		case 9:
			strHTML += "September";
			break;
		case 10:
			strHTML += "October";
			break;
		case 11:
			strHTML += "November";
			break;
		case 12:
			strHTML += "December";
			break;
		default:
			break;
	}

	strHTML += "&nbsp;" + theYear + "</a>&nbsp;<a href='http://www.cnyes.com/club/class/page01.asp?yy=" + theDate.getFullYear() + "&mm=" + (theDate.getMonth()+1) + "'>&gt;&gt;</a>";
	strHTML += "</td>";
	strHTML += "</tr>";
	strHTML += "<tr>";
	strHTML += "<td>";
	strHTML += "<table width='100%' border='0' cellpadding='2' cellspacing='1'>";
	strHTML += "<tr>";
	strHTML += "<td align='center'><div>日</div></td>";
	strHTML += "<td align='center'><div>一</div></td>";
	strHTML += "<td align='center'><div>二</div></td>";
	strHTML += "<td align='center'><div>三</div></td>";
	strHTML += "<td align='center'><div>四</div></td>";
	strHTML += "<td align='center'><div>五</div></td>";
	strHTML += "<td align='center'><div>六</div></td>";
	strHTML += "</tr>";
	strHTML += "<tr>";

	EndDay = theDay + theWeek;
	AddDay = EndDay % 7;
			
	if (AddDay == 0)
		AddDay = 0;
	else
		AddDay = 7 - AddDay;
			
	SumDay = EndDay + AddDay;
		
	for (var i=1; i<=SumDay; i++)
	{
		if (i <= theWeek || i > EndDay)
		{
			strHTML += "<td align='center'>&nbsp;</td>";
		}
		else
		{
			eDate = i - theWeek;

			if (strDate.indexOf( sDate + "/" + eDate ) >= 0)
				strHTML += "<td align='center'><a href='http://www.cnyes.com/club/class/page01.asp?yy=" + theYear + "&mm=" + theMonth + "&dd=" + eDate + "'>" + eDate + "</a></td>";
			else
				strHTML += "<td align='center'>" + eDate + "</td>";
		}
		
		if ((i+7) % 7 == 0)
			strHTML += "</tr><tr>";
	}

	strHTML += "</tr>";
	strHTML += "</table>";
	strHTML += "</td>";
	strHTML += "</tr>";
	strHTML += "</table>";
	strHTML += "</div>";

	return strHTML;
}

function RequestClassCalendar(thsElement)
{
	var objElement = ParentNodeTagName(thsElement, "TD");
	var httpRequest = AjaxRequest();
	var theDate = new Date( nowDateTime.toDateString() );

	if (objElement && httpRequest)
	{
		objElement.title = "";

		var sDate = theDate.getFullYear() + "/" + (theDate.getMonth() + 1);

		httpRequest.onreadystatechange = function() {

			if (httpRequest.readyState == 4 && httpRequest.status == 200)
			{
				var xmldoc = httpRequest.responseXML;

				if (xmldoc)
				{
					var rowsElement = xmldoc.getElementsByTagName("rows");
					var strDate = ",";

					for (i=0; i<rowsElement.length; i++)
						strDate += rowsElement[i].childNodes[0].firstChild.data + ",";

					objElement.innerHTML = CalendarClassEvent(sDate, strDate);
				}
			}
		}

		var url = "ClubClass.aspx?sDate=" + sDate + "&rnd=" + Rnd();

		httpRequest.open("GET", url, true);
		httpRequest.send(null);
	}
}

// 社團最新發表

function RequestClubDocument(thsElement)
{
	var objElement = ParentNodeTagName(thsElement, "TD");
	var httpRequest = AjaxRequest();
	var Request = new aspRequest(document.URL);

	if (httpRequest)
	{
		httpRequest.onreadystatechange = function() {

			if (httpRequest.readyState == 4 && httpRequest.status == 200)
			{
				var oJSON = eval("(" + httpRequest.responseText + ")");
				var strDocumentId, strTitle, strMy, j;
				var strHTML = "<table width='100%' border='0' cellspacing='0' cellpadding='2'>";

				for (var i=1; i<oJSON.DOCUMENT.length; i++)
				{
					strDocumentId = oJSON.DOCUMENT[i].ID;
					strMy = oJSON.DOCUMENT[i].CN;
					strTitle = oJSON.DOCUMENT[i].TITLE;

					strHTML += "<tr>";
					strHTML += "<td align='center'>" + i + "</td>";
					strHTML += "<td width='100%'><a href='../../My/" + encodeURIComponent(strMy) + "/Content.htm?DocumentId=" + strDocumentId + "' target='_blank'>" + HTMLEncode(strTitle) + "</a></td>";
					strHTML += "</tr>";
				}

				strHTML += "</table>";

				objElement.innerHTML = strHTML;
			}
		}

		var url = "ClubDocument.aspx?rnd=" + Rnd();

		httpRequest.open("GET", url, true);
		httpRequest.send(null);
	}
}

// 社團最新發表

function RequestClubHit(thsElement)
{
	var objElement = ParentNodeTagName(thsElement, "TD");
	var httpRequest = AjaxRequest();
	var Request = new aspRequest(document.URL);

	if (httpRequest)
	{
		var strMy = "ycblog";

		httpRequest.onreadystatechange = function() {

			if (httpRequest.readyState == 4 && httpRequest.status == 200)
			{
				var oJSON = eval("(" + httpRequest.responseText + ")");
				var strDocumentId, strHit, strTotal, strTitle, strDateTime, strQuestion, strAnswer, j;

				var strHTML = "<table width='100%' border='0' cellPadding='2' cellSpacing='0' bgcolor='#EEEEEE' class='Group-Search'>";

				strHTML += "<tr>";
				strHTML += "<td width='20%' height='21' align='center' valign='bottom' bgcolor='#ECF2FF'>最新更新日</td>";
				strHTML += "<td width='14%' height='21' align='center' valign='bottom' bgcolor='#ECF2FF'>回應篇數</td>";
				strHTML += "<td width='56%' height='21' align='center' valign='bottom' bgcolor='#ECF2FF'>主題</td>";
				strHTML += "<td width='10%' height='21' align='center' valign='bottom' bgcolor='#ECF2FF'>人數</td>";
				strHTML += "</tr>";

				strMy = encodeURIComponent(strMy);

				for (var i=1; i<oJSON.DOCUMENT.length; i++)
				{
					strDocumentId = oJSON.DOCUMENT[i].ID;
					strTitle = oJSON.DOCUMENT[i].TITLE;
					strHit = oJSON.DOCUMENT[i].HIT;
					strTotal = oJSON.DOCUMENT[i].TOTAL;
					strDateTime = oJSON.DOCUMENT[i].DATETIME;
					strQuestion = oJSON.DOCUMENT[i].QUESTION;
					strAnswer = oJSON.DOCUMENT[i].ANSWER;

					strDateTime = FormatDateTime(strDateTime, "yyyy/m/d");

					if (strQuestion != "")
					{
						strQuestion = FormatDateTime(strQuestion, "yyyy/m/d");
						strQuestion = new Date(strQuestion);

						if (strDateTime < strQuestion)
							strDateTime = strQuestion;
					}

					if (strAnswer != "")
					{
						strAnswer = FormatDateTime(strAnswer, "yyyy/m/d");
						strAnswer = new Date(strAnswer);

						if (strDateTime < strAnswer)
							strDateTime = strAnswer;
					}
				

/*
					strHTML += "<tr>";
					strHTML += "<td align='center'>" + i + "</td>";
					strHTML += "<td width='100%'><a href='../../My/" + strMy + "/Content.htm?DocumentId=" + strDocumentId + "' target='_blank'>" + HTMLEncode(strTitle) + "</a></td>";
					strHTML += "</tr>";
*/
					strHTML += "<tr>";
					strHTML += "<td bgcolor='#FFFFFF' align='center'>" + FormatDateTime(strDateTime, "yyyy/m/d") + "</td>";
					strHTML += "<td bgcolor='#FFFFFF' align='center'>" + strTotal + "</td>";
					strHTML += "<td bgcolor='#FFFFFF' align='left'><a href='../../My/" + strMy + "/Content.htm?DocumentId=" + strDocumentId + "' target='_blank'>" + HTMLEncode(strTitle) + "</a></td>";
					strHTML += "<td bgcolor='#FFFFFF' align='center'>" + strHit + "</td>";
					strHTML += "</tr>";
				}

				strHTML += "</table>";

				objElement.innerHTML = strHTML;
			}
		}

		var url = "ClubHit.aspx?CN=" + strMy + "&rnd=" + Rnd();

		httpRequest.open("GET", url, true);
		httpRequest.send(null);
	}
}