//
// JavaScript Event Calendar
//
// Version: 1.0
// Date: November 15, 1998
// For information: http://members.aol.com/kevinilsen  or kilsen@m-vation.com
//

// Configurable values are set to defaults here; can override them before calling Calendar( ) from the HTML page

var FontSize=5;
var ColorBackground="#E6E6E6";
var ColorToday = "red";
var	ColorHoliday = "#C6C600";
var ColorEvent = "green";

// Initialize the range of the calendar to Jan - Dec of the current year.  Calls to DefineEvent( ) will change this
// as needed; it is also possible to explicitly override the range before calling Calendar( ) from the HTML page.

var today = new Date();
var FirstMonth=GetFullYear(today) * 100 + 1;
var LastMonth=FirstMonth + 11;

// Events[] is a SPARSE array; Call DefineEvent( ) to populate it
var Events = new Array;

// Each event is defined by calling the DefineEvent( ) routine with the following parameters:
//
//   DefineEvent(EventDate, EventDescription, EventLink, Image, Width, Height)
//        EventDate is a numeric value in the format YYYYMMDD
//        EvenDescription is a string that can include embedded HTML tags (e.g., <BR>, <strong>, etc.)
//        EventLink is the URL of the target page if a hyperlink is desired from this event entry
//        Image is the URL of the image if you want to display an image with this event
//        Width is the width of the image in pixels
//        Height is the height of the image in pixels

function DefineEvent(EventDate, EventDescription, EventExtra, EventClass, EventLink, Image, Width, Height, bShowEvents, bHoliday) {
	var tmp;

	// Build the HTML string for this event: image (optional), link (optional), and description
	tmp = "";
	if (!bShowEvents)
	{
		// If an event already exists for this date, append the new event to it.
		if (Events[EventDate])
		{
			rstTemp = Events[EventDate].substring(0, Events[EventDate].length-1);
			Events[EventDate] = rstTemp + "\n_\n\n" + EventDescription + "&nbsp;" + EventExtra;
		}
		else
			Events[EventDate] = EventDescription + "&nbsp;" + EventExtra;
	}
	else
	{
		if (Image != "")
			tmp = tmp + '<img src="' + Image + '"  width="' + Width + '" height="' + Height + '" align="left" valign="top">';
		if (EventLink != "")
			tmp = tmp + '<a href="' + EventLink + '">';
		tmp = tmp + '<label class="' + EventClass + '" style="cursor:hand"';
		if (EventExtra)
			tmp = tmp + " onMouseOver=\"Show('" + escape(EventDescription) + "', '" + escape(EventExtra) + "')\" onMouseOut=\"Hide()\"";
		tmp = tmp + ">" + EventDescription + "</label>";
		if (EventLink != "") tmp = tmp + '</a>';

		// If an event already exists for this date, append the new event to it.
		if (Events[EventDate])
			Events[EventDate] += "<HR>" + tmp;
		else
			Events[EventDate] = tmp;
	}
	if (bHoliday)
	{
		Events[EventDate + "Holiday"] = bHoliday;
	}

	// Adjust the minimum and maximum month & year to include this date
	tmp = Math.floor(EventDate / 100);
	if (tmp < FirstMonth) FirstMonth = tmp;
	if (tmp > LastMonth) LastMonth = tmp;
}

// Utility function to populate an array with values
function arr() {
	for (var n=0;n<arr.arguments.length;n++) {
		this[n+1] = arr.arguments[n];
	}
}

// Create the array of month names (used in various places)
var months = new arr("January","February","March","April","May","June","July","August","September","October","November","December");

// Calendar( ) is the only routine that needs to be called to display the calendar

function Calendar(width, height, bShowEvents) {
	var curdy, curmo, yr, mo, dy, dayofweek, yearmonth, bgn, lastday, jump;
	var weekdays = new arr("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
	var thispage = window.location.href;

	// Save current day and month for comparison
	curdy = today.getDate();
	curmo = today.getMonth()+1;

	// Default to current month and year
	mo = curmo;
	yr = GetFullYear(today);
	yearmonth = (yr * 100) + mo;

	// If querystring parameter is present, get the month/year ("calendar.htm?YYYYMM")
	if (location.search.length > 1)
	{
//debugger;
		var	raData = (location.search.substring(1,location.search.length)).split("&");
		if (raData[raData.length -1].indexOf("=") == -1)
		{
			iTemp = parseInt(raData[raData.length -1], 10);
			if (iTemp)
			{
				yearmonth = iTemp;
				if ((""+yearmonth).length == 6) 
				{
					mo = yearmonth % 100;
					yr = (yearmonth - mo) / 100;
				}
			}
			delete raData[raData.length -1];
			thispage = thispage.split("?")[0] + "?" + raData.join("&");
			thispage = thispage.substring(0, thispage.length-1);
		}
		else
		{
			thispage = window.location.href;
		}
	}
	if (thispage.indexOf("?") == -1)
		thispage = thispage + "?";
	else
		thispage = thispage + "&";
/*
	if (location.search.length > 1) 
	{
		var iTemp = parseInt(location.search.substring(location.search.1,location.search.length), 10);
		if (iTemp)
		{
			yearmonth = iTemp;
			if ((""+yearmonth).length == 6) 
			{
				mo = yearmonth % 100;
				yr = (yearmonth - mo) / 100;
			}
		}
	}
*/

	// Constrain to the range of months with events
	if (yearmonth < FirstMonth) {
		mo = FirstMonth % 100;
		yr = (FirstMonth - mo) / 100;
		yearmonth = FirstMonth;
	}
	if (yearmonth > LastMonth) {
		mo = LastMonth % 100;
		yr = (LastMonth - mo) / 100;
		yearmonth = LastMonth;
	}

	// Create a datae object for the first day of the desired month
	bgn = new Date(months[mo] + " 1," + yr);
	// Get the day-of-week of the first day, and the # days in the month
	dayofweek = bgn.getDay();
	lastday = NumDaysIn(mo,yr);
	document.write("<TABLE");
	if (!width)
		width="100%";
	document.write(" width=\"" + width + "\"");
	if (height)
		document.write(" height=\"" + height + "\"");
		
	document.write(" BORDER=2 BGCOLOR="+ColorBackground+
		"><TR><TD BGCOLOR=\"#E6E6E6\" COLSPAN=7><TABLE width=\"100%\"><TR>");
	if (yearmonth > FirstMonth)
		document.write("<td align=\"left\"><a href=\"" + thispage + PrevYearMonth(yearmonth) + "\">" + months[PrevMonth(mo)] + "</a></td>");
//		document.write("<td align=\"left\"><a href=\"" + thispage + PrevYearMonth(yearmonth) + "\"><img src=\"images/arrow_left.gif\" width=\"16\" height=\"16\"></a></td>");
	else
		document.write("<td align=\"left\">&nbsp;</td>");		
	document.write("<TD width=\"100%\" align=\"center\"><FORM>");
	BuildSelectionList(yearmonth, thispage);
	document.write("</FORM></TD>");
	if (yearmonth < LastMonth)
		document.write("<td align=\"right\"><a href=\"" + thispage + NextYearMonth(yearmonth) + "\">" + months[NextMonth(mo)] + "</a></td>");
	else
		document.write("<td align=\"right\">&nbsp;</td");		
	document.write("</TR></TABLE></TD></TR><TR>");
	for (var i=1;i<=7;i++){
		document.write("<TD BGCOLOR=\"#808080\" ALIGN=CENTER WIDTH=14%><FONT SIZE=2>"+weekdays[i]+"</FONT></TD>");
	}
	document.write("</TR><TR>");
	dy = 1;
	// Special handling for the first week of the month
	for (var i=1;i<=7;i++) {
		// If the day is less than the day of the
		// week determined to be the first day
		// of the month, print a space in
		// this cell of the table.
		if (i <= dayofweek){
			document.write("<TD ALIGN=CENTER><FONT SIZE="+FontSize+">&nbsp;</FONT></TD>");
		}
		// Otherwise, write date and the event,
		// if any, in this cell of the table.
		else {
			ShowDate(yr,mo,dy,i,curmo,curdy,bShowEvents);
			dy++;
		}
	}
	document.write("</TR><TR>");
	// Rest of the weeks . . .
	while (dy <= lastday) {
		for (var i=1;i<=7;i++) {
			// If the day is greater than the last
			// day of the month, print a space in
			// this cell of the table.
			if (dy > lastday) {
				document.write("<TD ALIGN=CENTER>&nbsp;</TD>");
			}
			// Otherwise, write date and the event,
			// if any, in this cell of the table.
			else {
				ShowDate(yr,mo,dy,i,curmo,curdy,bShowEvents);
				dy++;
			}
		}
		document.write("</TR>");
	}
	document.write("</TABLE>");
}

// Display a date in the appropriate color, with events (if there are any)

function ShowDate(yr, mo, dy, dayofweek, currentmonth, currentday, bShowEvents) {
	var ind, HighlightEvent, tmp;

	document.write("<TD ALIGN=CENTER VALIGN=TOP");
	if ((mo == currentmonth) && (dy == currentday)) 
	{
		document.write(" BGCOLOR=" + ColorToday);
	}
	document.write(">");
	if (bShowEvents)
		document.write("<P ALIGN=RIGHT>");
	document.write("<FONT SIZE="+FontSize);
	HighlightEvent = true;
	if ((mo == currentmonth) && (dy == currentday)) {
		document.write(" COLOR=white");
		HighlightEvent = false;
	}
	ind = (((yr * 100) + mo) * 100) + dy;
	if (Events[ind]) {
		tmp = Events[ind];
		if (HighlightEvent) 
		{
			if ((typeof(Events[ind + "Holiday"]) != "undefined") &&
				(Events[ind + "Holiday"] == true))
			 document.write(" COLOR=" + ColorHoliday);
			else
			 document.write(" COLOR=" + ColorEvent);
		}
	} 
	else tmp="&nbsp;<BR>&nbsp;";
	document.write(">");
	if (!bShowEvents && Events[ind])
	{
		var	rstTemp = tmp.replace(/\n_\n\n/gi, "<HR>");
		document.write("<label style=\"cursor:hand\" onMouseOver=\"Show('" + dy + "', '" + escape(rstTemp) + "')\" onMouseOut=\"Hide()\">" + dy + "</label>");
	}
	else
		document.write(dy);
	document.write("</ALIGN></FONT>");
	if (bShowEvents)
		document.write("</P><FONT SIZE=1>" + tmp);
	document.write("</TD>");
}


// Remaining routines are utilities used above

function NumDaysIn(mo,yr) {
	if (mo==4 || mo==6 || mo==9 || mo==11) return 30;
	else if ((mo==2) && LeapYear(yr)) return 29;
	else if (mo==2) return 28;
	else return 31;
}

function LeapYear(yr) {
	if (((yr % 4 == 0) && yr % 100 != 0) || yr % 400 == 0) return true;
	else return false;
}

// fixes a Netscape 2 and 3 bug
function GetFullYear(d) { // d is a date object
	var yr;

	yr = d.getYear();
	if (yr < 1000)
	yr +=1900;
	return yr;
}

function PrevMonth(mth) {
	if (mth == 1) return 12;
	else return (mth-1);
}

function NextMonth(mth) {
	if (mth == 12) return 1;
	else return (mth+1);
}

function PrevYearMonth(yrmth) {
	if ((yrmth % 100) == 1) return ((yrmth-100)+11);
	else return (yrmth-1);
}

function NextYearMonth(yrmth) {
	if ((yrmth % 100) == 12) return ((yrmth-11)+100);
	else return (yrmth+1);
}

function JumpTo(calendar, thispage) {
	var sel, yrmo;

	sel = calendar.selectedIndex;
	yrmo = calendar.form.jumpmonth[sel].value;
	document.location = thispage + yrmo;
}

function BuildSelectionList(current,thispage) {
	var mo, yr, yearmonth;

	yearmonth = FirstMonth;
	document.write("<select name=\"jumpmonth\" size=1 onchange=\"JumpTo(this,'" + thispage + "')\">");
	while (yearmonth <= LastMonth) {
		mo = yearmonth % 100;
		yr = (yearmonth - mo) / 100;
		document.write("<option value=");
		document.write(yearmonth);
		if (yearmonth == current) document.write(" selected");
		document.write(">");
		document.write(months[mo]+" "+yr);
		yearmonth = NextYearMonth(yearmonth);
	}
	document.write("</select>");
}
