// Created by Rebecca Chesin. Invaluable consultation provided by Andre Guirard.// Also uses re-worked elements from a script by Nick Korosi.// This script will create the current month's calendar and enter any events, announcements, and holidays in it.// The previous 2 months and following 9 months are also available from a drop-down input, making an entire// year available for viewing.var currentDate=new Date();var currentMonth=currentDate.getMonth()+1;var currentYear=currentDate.getYear();var Month=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];var Weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];var path=location.href;// Convert a string with leading zeros into a decimal number (not octal!).function strToInt(x) {	while (x.charAt(0)=="0") x=x.substr(1);	return(x==""?0:parseInt(x));}function isToday(y, m, d){	return (y == currentYear && m == currentMonth && d == currentDate.getDate());}// List any announcements for selected month.function announcements() {	var announceList="";	for (var i = 0; i < upcomingEvents.length; i++) {		if (upcomingEvents[i][0] == newMonth) {			announceList+=upcomingEvents[i][1] + "<br>" + upcomingEvents[i][2] + "<p>";		}	}	if (announceList!="") {		document.write("<div class='announce'>Upcoming Activities (will appear on calendar when scheduled): <p>" + announceList + "</div> <p><p>");	}}// Display the month selection field.function selectMonth() {	document.write("<select name=\"month\" onchange=\"changeMonth(this)\">")	for (var i=-12; i<=12; i++){		var nextMonth=newMonth+i;		var nextYear=newYear;		while (nextMonth<1){			nextMonth+=12;			nextYear--;		}		while (nextMonth>12) {     		nextMonth-=12;			nextYear++;		}		var alias= nextYear + "-" + nextMonth;		var userValue=Month[nextMonth-1]+ " " + nextYear;		document.write ("<option value=\""+alias+"\""+((nextMonth==newMonth && nextYear == newYear)?" selected":"")+">"+userValue);	}	document.write("<\/select>")}// Figure out how many days are in a specified month (0-11).function getDaysInMonth(iMonth, iYear) {	var prevDate = new Date(iYear, iMonth, 0);	return prevDate.getDate();}// Create a two-dimensional array that contains the month's dates 'mapped out'.function fBuildCal(iYear, iMonth) {//alert("fBuildCal(" + iYear + ", " + iMonth + ")"); // @~@	var aMonth = new Array();	aMonth[0] = new Array(7);	aMonth[1] = new Array(7);	aMonth[2] = new Array(7);	aMonth[3] = new Array(7);	aMonth[4] = new Array(7);	aMonth[5] = new Array(7);	aMonth[6] = new Array(7);	var calDate = new Date(iYear, iMonth-1, 1);	var iDayOfFirst = calDate.getDay();	var iDaysInMonth = getDaysInMonth(iMonth, iYear);	var iVarDate = 1;	var i, d, w;	// Fill in the day names on the heading row.	for (i = 0; i < 7; i++)	aMonth[0][i] = Weekdays[i];	// Fill in the day numbers in cells of the first row that should have contents.	for (d = iDayOfFirst; d < 7; d++) {		aMonth[1][d] = iVarDate;		iVarDate++;	}	// fill in the day numbers in the remaining rows, up to the number of days in the month.	// The nested loop below scans thru each row of the aMonth array, and each column of the row,	// counting up as it processes each cell until it counts past the last day of the month.	for (w = 2; w < 7; w++) {		for (d = 0; d < 7; d++) {			if (iVarDate <= iDaysInMonth) {				aMonth[w][d] = iVarDate;				iVarDate++;			}		}	}	return aMonth;}// Write out the html for the actual calendar.function fDrawCal(iYear, iMonth) {	var myMonth;	// massageData();// alert("fDrawCal(" + iYear + ", " + iMonth + ")"); // @~@	myMonth = fBuildCal(iYear, iMonth);	document.write("<table width='100%' class='calendar' border=0 cellpadding=0 cellspacing=0>")	document.write("<tr>");	for (var i = 0; i < 7; i++) {		document.write("<th width='14.3%' height='6' class='dayOfWeek'>" + myMonth[0][i] + "</th>");	}	document.write("</tr>");	// Now, write out the remaining rows of the calendar.	for (w = 1; w < 7 && (w==1 || !isNaN(myMonth[w][0])); w++) {		document.write("<tr>")		for (d = 0; d < 7; d++) {			// Fill in  cell contents. If cell contains a number it's a day of the month else cell should be blank.			if (!isNaN(myMonth[w][d])) {				// Insert month number along with the HTML tags for formatting a month number.				// In addition, call cellContents to obtain any other text that should appear in the cell.				var strClass = isToday(iYear, iMonth, myMonth[w][d]) ? "calToday" : "calCell";				document.write("<td class='" + strClass 					+ "' align='left' valign='top'>"					+ "<div class='date'>" + myMonth[w][d] + "</div>"					+ cellContents(iYear, iMonth, myMonth[w][d]));			} else {				document.write("<td class='calEmpty'>&nbsp;");			}			document.write("</td>")		}		document.write("</tr>");	}	document.write("</table>")}// Convert a date in the form mm-dd-yy hh:mm into a javascript date & time object.function StrToDate(ds) {	var yr, mo, da, hr, mn;	var datetime = ds.split(" ");	var dateparts = datetime[0].split("/");	yr = dateparts.length < 3 ? currentYear : strToInt(dateparts[2]);	if (yr < 100) yr += 2000;	mo = strToInt(dateparts[0]) - 1;	da = strToInt(dateparts[1]);	var retval;	if (datetime.length > 1 && datetime[1] != "") {		var timeparts = datetime[1].split(":");		hr = strToInt(timeparts[0]);		mn = strToInt(timeparts[1]);		retval = new Date(yr, mo, da, hr, mn, 0);	} else {		retval = new Date(yr, mo, da);	}	return retval;}// Check the events list for any entries that happen on the given day.function cellContents(year, month, day) {	var retval = "";	var i;	var date = new Date(year, month-1, day);	var dayName=Weekdays[date.getDay()];	for (i = 0; i < events.length; i++) {		if (day == events[i][0]) {			retval += eventCellHTML(events[i], "E"+i);		}	}	// Check the weekly events list for any entries that happen on the given day.	for (i = 0; i < weeklyEvents.length; i++) {		if (dayName == weeklyEvents[i][0]) {			retval += eventCellHTML(weeklyEvents[i], "W"+i);		}	}	return retval; // == "" ? "" : ("<ul class='eventList'>" + retval + "</ul>");}// Fill in events & holidays for each date.function eventCellHTML(entry, anchor) {	var strRet = "";	var strDesc = "";	var link = true;	if(entry[1].charAt(0) == "*") {		// entry is a holiday		strRet = "<p class='holiday'>";		strDesc = entry[1].substr(1);		link = holidayLinks;	} else {		strRet = "<p class='event'>";		strDesc = entry[1];	}	if (link)		strRet += "<a href=\"" + entryPath + "/" + entry[2] + "?OpenDocument\">" + strDesc + "</a>";	else		strRet += strDesc;	if (entry[3] != "")		strRet += "<br>" + entry[3];	//alert(strRet);	return strRet;}// Display the month the user selects.function changeMonth(monthfield) {	// var monthfield=document.forms[0].month;	var newURL=entryPath +"?OpenView&"+monthfield.options[monthfield.selectedIndex].value;	location.replace(newURL);}// Display month, year, and image.function monthHead() {	var image;	var monthNYear;	image="<img src=\"" + images[newMonth-1] + "\">";	monthNYear = Month[newMonth-1] + " " + newYear;	if (newMonth % 2) {		document.write("<p class='month'>" + image + "&nbsp; &nbsp;" + monthNYear + "</p>");	} else {		document.write("<p class='month'>" + monthNYear + "&nbsp; &nbsp;" + image + "</p>");	}}
