// Source Code By : Alias May อัจฉราพร เมทินีพิศาลกุล
// Created Date : 23/07/2003
// E-mail : aliasmay@hotmail.com

// ---------- ประกาศตัวแปร ----------// 
var now   = new Date();
var currentday   = now.getDate();
var currentmonth = now.getMonth();
var currentyear  = now.getYear();
var selectmomth = currentmonth;
var selectyear = currentyear;

var monthLabel = new Array();
monthLabel[0] = "January";
monthLabel[1] = "February";
monthLabel[2] = "March";
monthLabel[3] = "April";
monthLabel[4] = "May";
monthLabel[5] = "June";
monthLabel[6] = "July";
monthLabel[7] = "August";
monthLabel[8] = "September";
monthLabel[9] = "October";
monthLabel[10] = "November";
monthLabel[11] = "December";

calendarFontType = "Tahoma, MS Sans Serif, sans-serif";  //กำหนด font family ตัวอักษรที่ใ้ช้ในแสดงผลปฎิทิน
calendarFontSize = "10px"; //กำหนดขนาดตัวอักษรที่ใ้ช้ในแสดงผลปฎิทิน
calendarFontColor = "black"; //กำหนดค่าสีตัวอักษรที่ใ้ช้ในแสดงผลปฎิทิน
calendarBGColor = "#CCCCCC";  //กำหนดค่าสี Bg และเส้นขอบของตารางปฎิทิน
todayColor = "white";  //กำหนดค่าสีตัวอักษร ของวันที่ปัจจุบัน ที่ต้องการเน้น
todayBGColor = "#009900"; //กำหนดค่าสี Bg ของวันที่ปัจจุบัน ที่ต้องการเน้น
dayBGColor = "#EEEEEE"; //กำหนดค่าสี Bg บริเวณที่มีการแสดงวันที่
dayNonBGColor = "white"; //กำหนดค่าสี Bg บริเวณที่ไม่มีการแสดงวันที่


// ---------- ประกาศฟังก์ชั่น ----------// 
function displayCalender(m) { 
// ใช้เรียกเพิ่อแสดงผลปฏิทิน โดยรับ input เป็นเดือนที่ต้องการ

	calMonthYear(m);

	// แสดงเดือน, ปี ของปฏิทิน
	dinamicMonth.innerHTML = monthLabel[selectmonth] + " " + selectyear;

	var i = 0;
	var days = getDaysInMonth(selectmonth);
	var firstOfMonth = new Date (selectyear, selectmonth, 1);
	var startPosition = firstOfMonth.getDay() + 1;

	// ขั้นตอนสร้างตารางและวันที่ของปฏิทิน
	var temp = "";
	temp += "<table width=100% border=0 cellpadding=1 cellspacing=1 bgcolor=" +calendarBGColor+ " style='font-family: "+calendarFontType+";text-decoration: none;font-size:"+calendarFontSize+";color:"+calendarFontColor+"'>";
	temp += "<tr align=center valign=middle>";
	temp += "<td width=20><strong>Su </strong></td> <td width=20><strong>M </strong></td> <td width=20><strong>Tu </strong></td> <td width=20><strong>W </strong></td> <td width=20><strong>Th </strong></td>  <td width=20><strong>F </strong></td> <td width=20><strong>Sa </strong></td> </tr>";
	temp += "<tr align=center valign=middle bgcolor="+dayNonBGColor+">";

	for (i = 1; i < startPosition; i++)
	{
		temp += "<td width=20>&nbsp;</td>";
	}
	
	for (i = startPosition; i-startPosition < days; i++) 
	{	
		var date_ = i-startPosition+1;
		var link_ = "http://www.thaiag.net:2003/calendar/calendar.jsp?date=" + selectyear +"-"+(selectmonth+1)+"-"+date_;
		if ( date_ == currentday && selectmonth == currentmonth && selectyear == currentyear)
		{
			// กำหนดการแสดงผล เพื่อเน้น วันที่ปัจจุบัน
			date_ = "<a href="+link_+" target='_blank'><font color=" + todayColor +"><b>" + date_ + " </b></font></a>";
			temp += "<td bgcolor=" +todayBGColor+ " width=20>" + date_ + "</td>";
		}
		else
		{	
			temp += "<td bgcolor=" +dayBGColor +" width=20><a href="+link_+" target='_blank'>" + date_ + "</a></td>";
		}
		if ( i%7 == 0 ) { temp+= "</tr><tr align=center valign=middle bgcolor="+dayNonBGColor+">";  }
	}

	if( i < 37 ) { i = i +7;}
	for (  ; i <= 42; i++)
	{
		temp+= "<td width=20>&nbsp;</td> ";
		if ( i%7 == 0 ) temp+= "</tr>";
	}

	temp += "</table>";

	// แสดงวันที่ทั้งหมดที่สร้างขึ้น
	dinamicDate.innerHTML = temp;
}

// ---------- ประกาศฟังก์ชั่น ----------// 
// ใช้เพิ่อคำนวณและตั้งค่าที่ถูกต้องให้กับ selectmonth และ selectyear
function calMonthYear(m) { 
	if(m >= 0 && m < 12) { selectmonth = m; }
	else 
	{
		if (m > 11)
		{
			selectmonth = m%12;
			selectyear++;
		}
		else
		{
			selectmonth = (m+12)%12;
			selectyear--;
		}
	}
}

// ---------- ประกาศฟังก์ชั่น ----------// 
function getDaysInMonth(m)  {
// ใช้เพิ่อหาจำนวนวันที่มีทั้งหมดในเดือนที่ต้องการ โดยรับ input ค่าเดือน และได้ output เป็น จำนวนวันทั้งหมด

	var numOfDays;
	m++;

	if (m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12) { numOfDays = 31; }
	else if (m==4 || m==6 || m==9 || m==11) { numOfDays = 30; }
	else if (m==2)  
	{
		if (isLeapYear(selectyear)) { numOfDays = 29; }
		else numOfDays = 28;
	}
	return (numOfDays);
}

// ---------- ประกาศฟังก์ชั่น ----------// 
// This Function By :  Rob Patrick (rpatrick@mit.edu)
// From url=(0077)http://www.aspchapter.com/Tip_Technic/javascript/calenders/calendermaker_.htm 
function isLeapYear (y) {
	if (((y % 4)==0) && ((y% 100)!=0) || ((y % 400)==0)) { return (true); } 
	else { return (false); }
}