// 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();

// Events[] is a SPARSE array; Call DefineEvent( ) to populate it
var Events = new Array;
var BDEvents = 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 (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] += "<br><br>&nbsp;&nbsp;&nbsp;" + tmp;
	else
		Events[EventDate] = tmp;

}

function DefineBDEvent(EventDate, EventDescription, EventLink, Image, Width, Height) {
	var tmp;

	// Build the HTML string for this event: image (optional), link (optional), and description
	tmp = "";
	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 (BDEvents[EventDate])
		BDEvents[EventDate] += "<br><br>&nbsp;&nbsp;&nbsp;" + tmp;
	else
		BDEvents[EventDate] = 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");

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

function EventView( ) {
	var curdy, curmo, yr, mo, dy, dayofweek, yearmonth, bgn, lastday, jump;
	var thispage = window.location.pathname;

	// 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;

	document.write("<TABLE BORDER=1 BGCOLOR=#c0c0c0 width=190>");

	ShowThisMonth(yearmonth, mo);

	nym=NextYearMonth(nym);
	nmo=NextMonth(nmo);

	ShowThisMonth(nym, nmo);

	document.write("<tr><td><a href=Events.htm>Click here for more.</a></TD></TR>");
	document.write("</TABLE>");


}

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

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

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


function ShowThisMonth(yrmth, mo) {
 	var nind, ntmp;
 	nym=(yrmth);
	nmo = (mo);


	 	nyr = (nym - nmo) / 100;
 		nlastday = NumDaysIn(nmo,nyr);
		nind = 1;
		ntmp = "";
		while (nind <= nlastday) {
 			ind = (nym * 100) + nind;

			etmp = "";
			if (Events[ind]) {
				etmp += Events[ind] + "<BR>";
			}
 
			btmp = "";
 			ind = (nmo * 100) + nind;
 			if (BDEvents[ind]) {
				btmp += BDEvents[ind] + "<BR>";
			} 
 
			if ((btmp != "" || etmp != "")) {
				if (( ntmp != "" )) {
					ntmp += "<hr>";
				}
				ntmp += "<B>"+nind+". </B>" + etmp + btmp;
			}
				
		 	nind++;
		}
		if (( ntmp != "" )) {
			document.write("<TR><TD ALIGN=CENTER BGCOLOR=#0f0f0f valign=top><FONT COLOR=white><b>Events in "+months[nmo]+" "+nyr+"</B></FONT></TD></TR>");
			document.write("<TR><TD ALIGN=LEFT BGCOLOR=#FFFFFF valign=top>");
			document.write(ntmp);
			document.write("</TD></TR>");
		}


}

// 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;
}

