//  Insert dates of important here.


var months =
[
	"January", "February", "March", "April", "May", "June", "July",
	"August", "September", "October", "November", "December"
];

var listOfDates = new Array ();

for (i = 0; i < calendarDates.length; i++)
{
	calendarDate = calendarDates[i];
	var dateParts = calendarDate[0].split ("/");
	if (dateParts.length == 3)
	{
		theDate = new Date (dateParts[2], dateParts[0] - 1, dateParts[1]);
		if (listOfDates[theDate] == undefined)
		{
			listOfDates[theDate] = "";
		}
		if (calendarDate[2].length > 0)
		{
			//  Opera, for some reason, does not seem to like the string += string in this case
			listOfDates[theDate] = listOfDates[theDate] + "<span style=\"font-size: 7pt;\">- " +
				"<a href=\"" + calendarDate[2] +
				"\">" + calendarDate[1] + "</a></span><br>";		
		}
		else
		{
			listOfDates[theDate] = listOfDates[theDate] + "<span style=\"font-size: 7pt;\">- " + calendarDate[1] + "</span><br>";
		}
	}
}

function getLastDayOfMonth (month, year)
{
	switch (month)
	{
		case 0:
		case 2:
		case 4:
		case 7:
		case 9:
		case 11:
			return 31;

		case 3:
		case 5:
		case 6:
		case 8:
		case 10:			
			return 30;
			
		default:
			dateTrial = new Date (year, month, 29);
			if (dateTrial.getMonth () != month)
			{
				return 28;
			}
			else
			{
				return 29;
			}
			break;
	}
}

function outputCalendar (month, year)
{
	//  Get month and year.
	//  Get day of week of first day.
	//  Get last day of this month.
	//  Get last day of last month.
	
	if (year == 0)
	{
		year = new Date ().getFullYear ();
	}
	if (month == -1)
	{
		month = new Date ().getMonth ();
	}

	///////////////////////////////
	//  Get last day of this month.
	///////////////////////////////
	
	thisLastDay = getLastDayOfMonth (month, year);
	
	if (month == 0)
	{
		prevMonth = 11;
		prevYear = year - 1;
	}
	else
	{
		prevMonth = month - 1;
		prevYear = year;
	}
	
	if (month == 11)
	{
		nextMonth = 0;
		nextYear = year + 1;
	}
	else
	{
		nextMonth = month + 1;
		nextYear = year;
	}				
	
	//  Get first day of week.
	
	firstDayOfWeek = new Date (year, month, 1).getDay ();
	lastDayOfWeek = new Date (year, month, thisLastDay).getDay ();
	
	//  Count number of days back from firstDay
		
	prevLastDay = getLastDayOfMonth (prevMonth, prevYear);
	startLastMonth = prevLastDay - firstDayOfWeek + 1;
	
	weeks = new Array (6);
	weeks[0] = new Array (7);
	weeks[1] = new Array (7);
	weeks[2] = new Array (7);
	weeks[3] = new Array (7);
	weeks[4] = new Array (7);
	weeks[5] = new Array (7);
		
	//  Fill first row.

	curSlot = 0;
	curWeek = 0;
	
	if (firstDayOfWeek != 0)
	{
		for (i = startLastMonth; i <= prevLastDay; i++)
		{
			weeks[0][curSlot] = new Date (prevYear, prevMonth, i);
			curSlot++;
		}
	}
	
	for (j = 1; j <= thisLastDay; j++)
	{
		if (curSlot == 7)
		{
			curSlot = 0;
			curWeek++;
		}
		weeks[curWeek][curSlot] = new Date (year, month, j);
		curSlot++;
	}
	
	nextMonthCount = 1;
	for (j = lastDayOfWeek + 1; j < 7; j++)
	{
		weeks[curWeek][j] = new Date (nextYear, nextMonth, nextMonthCount);
		nextMonthCount++;
	}

	document.writeln ("<h2 align=center>" + months[month] + " " + year + "</h2>");
	document.writeln ("<table align=center class=calendar>");
	document.writeln ("<tr>");
	document.writeln ("<td align=left colspan=2><button onClick=\"window.location='calendar.php?year=" + prevYear + "&month=" +
		prevMonth + "'\">&lt;&lt; Prev Month</button></td>");
	document.writeln ("<td align=center colspan=3><button onClick=\"window.location='calendar.php'\">" +
		"Current Month</button></td>");
	document.writeln ("<td align=right colspan=2><button onClick=\"window.location='calendar.php?year=" + nextYear + "&month=" +
		nextMonth + "'\">Next Month &gt;&gt;</button></td>");
	document.writeln ("</tr>");
	document.writeln ("<tr>");
	document.writeln ("<td class=day_headline>Sun</td>");
	document.writeln ("<td class=day_headline>Mon</td>");
	document.writeln ("<td class=day_headline>Tue</td>");
	document.writeln ("<td class=day_headline>Wed</td>");
	document.writeln ("<td class=day_headline>Thu</td>");
	document.writeln ("<td class=day_headline>Fri</td>");
	document.writeln ("<td class=day_headline>Sat</td>");
	document.writeln ("</tr>");
	
	for (i = 0; i <= curWeek; i++)
	{
		document.write ("<tr>");
		for (j = 0; j < 7; j++)
		{
			if (weeks[i][j].getMonth () != month)
			{
				document.write ("<td class=\"other_month_day\">");
			}
			else
			{
				document.write ("<td class=\"month_day\">");
			}
			listOfEvents = listOfDates[weeks[i][j]];
			document.write (weeks[i][j].getDate ());
			if (listOfEvents != undefined)
			{
				document.write ("<br><span class=\"day_events\">");
				document.write (listOfEvents);
				document.write ("</span>");
			}
			document.write ("</td>");
		}
		document.write ("</tr>");
	}

	document.write ("</table>");
}
