//
// 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 SpecialDay=0;	// 1=Sunday, 2=Monday, . . . 7=Saturday
var FontSize=1;
var ColorBackground="ffffff";
var ColorSpecialDay = "red";
var ColorToday = "green";
var ColorEvent = "blue";

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

// 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="top">';
	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>" + tmp;
	else
		Events[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");

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

function Calendar( ) {
	var curdy, curmo, yr, mo, dy, dayofweek, yearmonth, bgn, lastday, jump, r, c, rowswritten;
	var weekdays = new arr("Su","Mo","Tu","We","Th","Fr","Sa");


	// 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><tr><td><font color=#6f3737>Key:&nbsp;</font></td>");
document.write("<td bgcolor=#0000ff><font  color=#ffffff>Booked</FONT></td>");
document.write("<td bgcolor=#c0c0c0><font color=#000000 >Available</font></td>");
document.write("<td bgcolor=#c0c0c0><font color=#ff0000 >Change over</FONT></td></ROW></tr></table>");
				

// Create a table of 4 x 3 months
document.write("<TABLE BORDER=3 bordercolor=#6f3737 bgcolor=#ffffff cellspacing=4 cellpadding=4 Width=95% Align=center>");
for (r=0;r<3;r++){
   document.write("<tr>");
     for (c=0;c<4;c++)
	 {
	 document.write("<td width=25% height=33% align=center valign=top>");
	 	  
	// Create a date 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 class=toolBar id=");

	document.write(months[mo]);

	document.write(" BORDER=0 cellspacing=4 cellpadding=4 BGCOLOR=#c0c0c0");

	document.write("><TR height=12%><TD BGCOLOR=#0000ff ALIGN=CENTER COLSPAN=7>");

	document.write("<FONT COLOR=#ffff00 SIZE=");

	document.write(FontSize);

	document.write("><B>"+months[mo]+" "+yr+"</B></FONT></TD></TR><TR height=12%>");

	for (var i=1;i<=7;i++){
		document.write("<TD width=14% ALIGN=CENTER><FONT SIZE=1>"+weekdays[i]+"</FONT></TD>");
	}
	document.write("</TR><TR height=12%>");
	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 BGCOLOR=#c0c0c0 valign=top>&nbsp;</TD>");
		}
		// Otherwise, write date and the event,
		// if any, in this cell of the table.
		else {
			ShowDate(yr,mo,dy,i,curmo,curdy);
			dy++;
		}
	}
	rowswritten=1;
	document.write("</TR><TR height=12%>");
	// Rest of the weeks . . .

	while ( (rowswritten < 6)) {
		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 BGCOLOR=#c0c0c0 valign=top>&nbsp;</TD>");
			}
			// Otherwise, write date and the event,
			// if any, in this cell of the table.
			else {
				ShowDate(yr,mo,dy,i,curmo,curdy);
				dy++;
			}
		}

		document.write("</TR><TR height=12%>");
		rowswritten++;
	}

	document.write("</TD></TR></TABLE>");

	document.write("</td>");
	yearmonth=NextYearMonth(yearmonth);
	mo=yearmonth%100;
	yr=(yearmonth-mo)/100;
	
	}
	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) {
	var ind, HighlightEvent, bgcol, fgcol, tmp;

	ind = (((yr * 100) + mo) * 100) + dy;
	bgcol="#c0c0c0";
	fgcol="#000000";
	tmp="";
	
	if (Events[ind]) {
		tmp = Events[ind];
	}

	if(tmp == 1 || tmp == 3) {
		   bgcol="#c0c0c0";
		   fgcol="#ff0000";	
	}	
	if(tmp == 2) {
		   bgcol="#000088";
		   dy="";
		   ind="";	
	}	
	if ((mo == currentmonth) && (dy < currentday)) {
	   document.write("<TD ALIGN=CENTER bgcolor=#000088 VALIGN=TOP><FONT SIZE="+FontSize+">");
	   tmp="&nbsp;";
	   dy="&nbsp;";
	} else 
	if(tmp == 2)
	{
 document.write("<TD ALIGN=CENTER VALIGN=TOP bgcolor="+bgcol+">");
 dy="&nbsp;";
	}else
	{
	  document.write("<TD class=coolButton id=&quot;"+ind+"&quot; ");
	  document.write("ALIGN=CENTER VALIGN=TOP><FONT SIZE="+FontSize+" color="+fgcol+">");
	}
	document.write("<B>"+dy+"</B><br></FONT></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);
}




