// Pedantic Calendar
var xmlFile = "events.xml";

/* --- DO NOT ALTER ANYTHING BELOW THIS LINE --- */

var GlobalDate = new Date();
var TableObj = null;
var xmlhttp = false;
var xmlObj = null;
var xmlLastMod;

function refreshEvents()
{
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if(!xmlhttp)
	{
		alert("We're sorry. Due to a limitation in your browser, we are unable to retrieve upcomming event information.");
	}
	else
	{
		if (xmlhttp.overrideMimeType)
			xmlhttp.overrideMimeType('text/xml')
		xmlhttp.open("GET",xmlFile,true);
		xmlhttp.onreadystatechange=function() // Request XML file header
		{
			if (xmlhttp.readyState==4)
			{
				if(xmlhttp.status > 300)
				{
					alert("Can not find list of events. Error: " + xmlhttp.status);
				}
				else
				{
					xmlLastMod = xmlhttp.getResponseHeader("Last-Modified");
					xmlObj = xmlhttp.responseXML.documentElement;
					drawCalendar();
				}
			}
		}
		xmlhttp.send(null)
	}
}
function prevMonth()
{
	GlobalDate.setMonth( GlobalDate.getMonth() - 1 );
	drawCalendar();
}
function nextMonth()
{
	GlobalDate.setMonth( GlobalDate.getMonth() + 1 );
	drawCalendar();
}
function drawCalendar()
{
	if( TableObj != null )
	{
		TableObj.parentNode.removeChild(TableObj);
	}
	var calObj = document.getElementById("calendar");
	var Months= new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	var Weekdays = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
	var today = new Date();
	var dateIndex = new Date(0);
	var evntDate = new Date(0);
	var tb = null; // Table Body
	var tr = null; // Table Row
	var td = null; // Table Cell
	var e = null; // Element
	var i = null; // Iterator
	dateIndex.setTime(GlobalDate.valueOf());
	dateIndex.setDate(1);
	var month = dateIndex.getMonth();
	
	
	TableObj = document.createElement('table');
	calObj.appendChild(TableObj);
	tb = document.createElement('tbody');
	TableObj.appendChild(tb);
	tr = document.createElement('tr');
	tb.appendChild(tr);
	td = document.createElement('th');
	td.innerHTML = "<img src=\"img/cal_prev.png\" onclick=\"prevMonth();\"/>"
	tr.appendChild(td);
	td = document.createElement('th');
	td.colSpan = 5;
	tr.appendChild(td);
	e = document.createElement('h3');
	e.innerHTML = Months[month] + " " + dateIndex.getFullYear();
	td.appendChild(e);
	td = document.createElement('th');
	td.innerHTML = "<img src=\"img/cal_next.png\" onclick=\"nextMonth();\"/>"
	tr.appendChild(td);
	
	tr = document.createElement('tr');
	tb.appendChild(tr);
	
	for( i=0; i <= 6; i++)
	{
		td = document.createElement('th');
		td.innerHTML = Weekdays[i];
		tr.appendChild(td);
	}
	
	dateIndex.setDate(dateIndex.getDate() - dateIndex.getDay());
	dateIndex.setHours(0);
	dateIndex.setMinutes(0);
	dateIndex.setSeconds(0);
	dateIndex.setMilliseconds(0);
	do {
		tr = document.createElement('tr'); // New Row
		tb.appendChild(tr);
		for( i=0; i <= 6; i++)
		{
			td = document.createElement('td');
			if( dateIndex.getMonth() != month )
			{
				td.className = "OffMonth";
			}
			else
			{
				td.className = "InMonth";
				if(dateIndex.getDate()==today.getDate())
					if(dateIndex.getMonth()==today.getMonth())
						if(dateIndex.getFullYear()==today.getFullYear())
							td.className = "Today";
			}
			td.innerHTML = "<p>" + dateIndex.getDate() + "</p>";
			/* Check for Event */
			if( xmlObj != null )
			{
				/* First Obtain Holidays */
				var evnt = xmlObj.getElementsByTagName("holiday");
				var n = 0;
				do
				{
					var ch = evnt[n].firstChild;
					var evntIs = false;
					var evntTitle = null;
					var evntDesc = null;
					var evntURL = null;
					do
					{
						if(ch.nodeName == "date")
						{
							evntDate.setTime(Date.parse(ch.firstChild.nodeValue));
							if( isNaN(evntDate) )
							{
								alert("Error parsing the date: " + ch.firstChild.nodeValue);
							}
							if( evntDate.valueOf() == dateIndex.valueOf() )
							{
								evntIs = true;
							}
						}
						if((ch.nodeName == "title") && evntIs )
						{
							evntTitle = ch.firstChild.nodeValue;
						}
						if((ch.nodeName == "description") && evntIs )
						{
							evntDesc = ch.firstChild.nodeValue;
						}
						if((ch.nodeName == "url") && evntIs )
						{
							evntURL = ch.firstChild.nodeValue;
						}
						ch = ch.nextSibling;
					} while( ch != null )
					if( evntIs )
					{
						if( evntURL == null )
						{
							td.innerHTML += "<div class=\"holiday\" alt=\"" + evntDesc + "\" title=\"" + evntDesc + "\">" + evntTitle + "</div>";
						}
						else
						{
							td.innerHTML += "<div class=\"holiday\" alt=\"" + evntDesc + "\" title=\"" + evntDesc + "\">" + "<a href=\"" + evntURL + "\">" + evntTitle + "</a></div>";
						}
					}
					n++;
				} while (n < evnt.length);

				/* Now Check For Events */
				var evnt = xmlObj.getElementsByTagName("event");
				var n = 0;
				do
				{
					var ch = evnt[n].firstChild;
					var evntIs = false;
					var evntTitle = null;
					var evntDesc = null;
					var evntURL = null;
					do
					{
						if(ch.nodeName == "date")
						{
							evntDate.setTime(Date.parse(ch.firstChild.nodeValue));
							if( isNaN(evntDate) )
							{
								alert("Error parsing the date: " + ch.firstChild.nodeValue);
							}
							if( evntDate.valueOf() == dateIndex.valueOf() )
							{
								evntIs = true;
							}
						}
						if((ch.nodeName == "title") && evntIs )
						{
							evntTitle = ch.firstChild.nodeValue;
						}
						if((ch.nodeName == "description") && evntIs )
						{
							evntDesc = ch.firstChild.nodeValue;
						}
						if((ch.nodeName == "url") && evntIs )
						{
							evntURL = ch.firstChild.nodeValue;
						}
						ch = ch.nextSibling;
					} while( ch != null )
					if( evntIs )
					{
						if( evntURL == null )
						{
							td.innerHTML += "<div class=\"event\" alt=\"" + evntDesc + "\" title=\"" + evntDesc + "\">" + evntTitle + "</div>";
						}
						else
						{
							td.innerHTML += "<div class=\"event\" alt=\"" + evntDesc + "\" title=\"" + evntDesc + "\">" + "<a href=\"" + evntURL + "\">" + evntTitle + "</a></div>";
						}
					}
					n++;
				} while (n < evnt.length);
			}
			tr.appendChild(td);
			dateIndex.setDate(dateIndex.getDate() + 1);
		}
	} while( dateIndex.getMonth() == month );
}

refreshEvents();