// calendar.js
// JavaScript Event Calendar
//
// Version: 1.0, rev: CSS
// Date: November 15, 1998
// For information: http://home.comcast.net/~kilsen/Calendar/index.html
// Revised by LMP to use CSS & to output valid XHTML 1.1
// To do:
//	1. Rewrite <form> to validate;

// 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, EventLink, Image, Width, Height) {
	var tmp;

	// Build the HTML string for this event: image (optional), link (optional), and description
	tmp = '';
	if (Image != '')
		tmp = tmp + '<img src="' + Image + '"  width="' + Width + '" height="' + Height + '" align="left" valign="bottom" alt="' + Image + '" />';
	if (EventLink != '')
		tmp = tmp + '<a href="' + EventLink + '">';
	tmp = tmp + EventDescription;
	if (EventLink != '') tmp = tmp + '</a>';

	// If an event already exists for this date, append the new event to it.
	if (Events[EventDate])
		Events[EventDate] += '</p><p class="cal_event_text">' + tmp;
	else
		Events[EventDate] = tmp;

	// 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() {
	var current_day, current_month, year, month, day_number, dayofweek, yearmonth, beginning, lastday, jump;
	var weekdays = new arr('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
	var thispage = window.location.pathname;

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

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

	// If querystring parameter is present, get the month/year ("calendar.htm?YYYYMM")
	if (location.search.length > 1) {
		yearmonth = parseInt(location.search.substring(1,location.search.length));
		if (('' + yearmonth).length == 6) {
			month = yearmonth % 100;
			year = (yearmonth - month) / 100;
		}
	}

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

	// Create a datae object for the first day of the desired month
	beginning = new Date(months[month] + " 1," + year);
	// Get the day-of-week of the first day, and the # days in the month
	dayofweek = beginning.getDay();
	lastday = NumDaysIn(month, year);
	document.write('<table class="cal_table"><tr><td colspan="7" class="cal_month_name">' + months[month] + ' ' + year + '</td></tr><tr>');
	for (var i=1;i<=7;i++){
		document.write('<th class="cal_day_name">' + weekdays[i] + '</th>');
	}
	document.write('</tr><tr>');
	day_number = 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 class="cal_day_number">&nbsp;</td>');
		}
		// Otherwise, write date and the event,
		// if any, in this cell of the table.
		else {
			ShowDate(year,month,day_number,i,current_month,current_day);
			day_number++;
		}
	}
	document.write("</tr><tr>");
	// Rest of the weeks . . .
	while (day_number <= 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 (day_number > lastday) {
				document.write("<td class=\"cal_day_number\">&nbsp;</td>");
			}
			// Otherwise, write date and the event,
			// if any, in this cell of the table.
			else {
				ShowDate(year,month,day_number,i,current_month,current_day);
				day_number++;
			}
		}
		document.write("</tr><tr>");
	}

	jump = "";
	if (yearmonth > FirstMonth)
		jump += '<a href="' + thispage + '?' + PrevYearMonth(yearmonth) + '">&lt;&mdash; View ' + months[PrevMonth(month)] + '</a>';
	if ((yearmonth > FirstMonth) && (yearmonth < LastMonth))
		jump += ' &nbsp; | &nbsp; ';
	if (yearmonth < LastMonth)
		jump += '<a href="' + thispage + '?' + NextYearMonth(yearmonth) + '">View ' + months[NextMonth(month)] + ' &mdash;&gt;</a>';
	document.write('<td colspan="7" class="cal_nav_text">' + jump + '</td></tr>');

// LMP: this does *not* write valid xhtml 1.1! (but it does write *functional* code...)
	document.write('<tr><td colspan="7" class="cal_nav_text"><form>Jump to month:&nbsp;&nbsp;');
	BuildSelectionList(yearmonth, thispage);
	document.write('</form></td></tr></table>');

// LMP: this works exactly once, only on initial load of the page, & not on subsequent tries or on re-loads.
//	document.write('<tr><td colspan="7" class="cal_nav_text"><label for="jumpmonth">Jump to month:&nbsp;&nbsp;</label><br />');
//	BuildSelectionList(yearmonth, thispage);
//	document.write('</td></tr></table>');
}

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

function ShowDate(year, month, day_number, dayofweek, currentmonth, currentday) {
	var ind, HighlightEvent, ChangedText, tmp;

	HighlightEvent = true;
	WroteClass = false
	if ((month == currentmonth) && (day_number == currentday)) {
		document.write('<td class="cal_today_number"');
		HighlightEvent = false; WroteClass=true;
	}
	ind = (((year * 100) + month) * 100) + day_number;
	if (Events[ind]) {
		tmp = Events[ind];
		if (HighlightEvent) {
			document.write('<td class="cal_event_number"');
			WroteClass=true;
		}
	} else tmp="&nbsp;<br />&nbsp;";
	if (WroteClass == false) {document.write('<td class="cal_day_number"');}
	document.write('><p>' + day_number + '</p><p class="cal_event_text">' + tmp + '</p></td>');
}


// Remaining routines are utilities used above

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

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

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

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

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 month, year, yearmonth;

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