
//
// Get the value of giThisYear
//
function getThisYear() {
    return giThisYear;
}

//
// Get the value of giLastYear
//
function getLastYear() {
    return giLastYear;
}

//
// Get the class for an event
//
function getEventClass(aEvent) {
    return aEvent[0];
}

//
// Get the year for an event
//
function getEventYear(aEvent) {

    if (aEvent[1] > 9) {
        return giLastYear;
    } else {
        return giThisYear;
    }
}

//
// get the event month
//
function getEventMonth(aEvent) {
    return aEvent[1];
}
//
// get the event day
//
function getEventDay(aEvent) {
    return aEvent[2];
}

//
// if event has a name, return it.
//
function getEventName(aEvent) {
    return aEvent[3];
}

//
// Get the time for an event
//
function getEventTime(aEvent) {
    return aEvent[4];
}

//
// get whether an event is home or away
//
function getEventLocation(aEvent) {
	if (aEvent[5] == 0) {
		return "Home";
	} else {
		return "Away";
	}
}

//
// return the home street address for this event
//
function getHomeStreet(aEvent) {

    for (var i = 0; i < gaClassAddress.length; i++) {
        if (isEventInClass(getEventClass(aEvent), gaClassAddress[i][0], false)) {
            return gaClassAddress[i][1];
        }
    }
    return "";
}

//
// return the home zip for this event
//
function getHomeZip(aEvent) {

    for (var i = 0; i < gaClassAddress.length; i++) {
        if (isEventInClass(getEventClass(aEvent), gaClassAddress[i][0], false)) {
            return gaClassAddress[i][2];
        }
    }
    return "";
}

//
// return the destination street address for this event
//
function getDestStreet(aEvent) {
    return aEvent[6];
}

//
// return the destination zip for this event
//
function getDestZip(aEvent) {
    return aEvent[7];
}

//
// return if there is a result for this event
//
function getEventResult(aEvent) {
    return aEvent[8];
}

//
// if event has a link, return it.
//
function getEventLink(aEvent) {
    return aEvent[9];
}

//
// if event has a color, return it.
//
function getEventColor(aEvent) {
    return aEvent[10];
}

//
// get the event name without the league and Open/Novice
//
function getNormalizedEventName(aEvent, sLeague, sLevel) {
    var sEventName = getEventName(aEvent);
    var s1 = sEventName.replace(sLeague+" ", "");
    var sRawSite = s1.replace(" "+sLevel, "");
    return sRawSite;
}

//
// Test if an Event is in a Class of events
// if bOther is TRUE, include class=1 events
//
function isEventInClass(iEventClass, iClass, bOther) {

    // note: classes are in ranges of 10
    if ((iEventClass < iClass || (iEventClass - iClass) > 10)
    || (bOther == true && iEventClass == 1)) {
        return false;
    } else {
        return true;
    }
}

//
// test if an event in a class is not a competition event
// used to add time to calendar day or not
//
function isEventACompetition(aEvent) {

    var aClasses = [ 11, 12, 13, 14, 21, 22, 23, 31, 32, 33, 34, 35 ];
   
    for (var i = 0; i < aClasses.length; i++) {
        if (aClasses[i] == getEventClass(aEvent))
            return true;
    }
    return false;
}

//
// determine if an event is on or after a given date
//
function isEventAfterDate(aEvent, iToday) {

    var iEventDate = getEventYear(aEvent)*10000 + (aEvent[1]-1)*100 + aEvent[2];
    
    if (iToday > iEventDate) {
        return false;
    } else {
        return true;
    }
}

//
// Get an array of events in a class of events, from a date forward
// if bOther is TRUE, include class=1 events
//
function getClassEvents(iClass, iStartDate, bOther) {

    var iCnt = 0;
	var aResult = new Array();
    
    for (var i = 0; i < gaAllEvents.length-1; i++) {
        aEvent = gaAllEvents[i];
        if (isEventInClass(aEvent[0], iClass, bOther)) {
            if (iStartDate < 1 || isEventAfterDate(aEvent, iStartDate)) {
                aResult[iCnt] = aEvent;
                iCnt++;
            }
		}
    }
    return aResult;
}

//
// get all events from a date forward, irrespective of date.
//
function getAllEventsByDate(iStartDate) {

    var iCnt = 0;
	var aResult = new Array();
    
    for (var i = 0; i < gaAllEvents.length-1; i++) {
        aEvent = gaAllEvents[i];
        if (iStartDate < 1 || isEventAfterDate(aEvent, iStartDate)) {
        
            aResult[iCnt] = aEvent;
            iCnt++;
		}
    }
    return aResult;
}

//
// get all events from a date forward, whose type matches an array of values
//
function getEventsByType(aTypes, iStartDate) {

    var iCnt = 0;
	var aResult = new Array();
    
    for (var i = 0; i < gaAllEvents.length-1; i++) {
        aEvent = gaAllEvents[i];
        if (iStartDate < 1 || isEventAfterDate(aEvent, iStartDate)) {
        
            for (var j = 0; j < aTypes.length; j++) {
                if (aTypes[j] == aEvent[0]) {
                    aResult[iCnt] = aEvent;
                    iCnt++;
                    break;
                }
            }
		}
    }
    return aResult;
}

//
// return the home street address for this type of events
//
function getHomeStreetbyClass(iEventClass) {

    for (var i = 0; i < gaClassAddress.length; i++) {
        if (isEventInClass(iEventClass, gaClassAddress[i][0], false)) {
            return gaClassAddress[i][1];
        }
    }
    return "";
}

//
// return the home zip for this class of events
//
function getHomeZipbyClass(iEventClass) {

    for (var i = 0; i < gaClassAddress.length; i++) {
        if (isEventInClass(iEventClass, gaClassAddress[i][0], false)) {
            return gaClassAddress[i][2];
        }
    }
    return "";
}

//
// get an array of all BHS types
//
function getBHSEventTypes(bOther) {

    if (bOther == true) {
        var aTypes = [ 1, 11, 12, 13, 14, 15, 16 ];
    } else {
        var aTypes = [ 11, 12, 13, 14, 15, 16 ];
    }
    return aTypes;
}

//
// get an array of BHS Varsity A event types
//
function getBHSVarsityAEventTypes() {

    var aTypes = [ 11, 12, 15, 16 ];
    return aTypes;
}

//
// get an array of BHS Varsity A event types
//
function getBHSVarsityBEventTypes() {

    var aTypes = [ 11, 13, 15, 16 ];
    return aTypes;
}

//
// get an array of BHS Varsity A event types
//
function getBHSJVEventTypes() {

    var aTypes = [ 11, 14, 15, 16 ];
    return aTypes;
}

//
// get an array of BHS Varsity A event types
//
function getScrantonEventTypes(bOther) {

    if (bOther == true) {
        var aTypes = [ 1, 21, 22, 23, 24, 25];
    } else {
        var aTypes = [ 21, 22, 23, 24, 25 ];
    }
    return aTypes;
}

//
// get an array of Club event types
//
function getClubEventTypes(bOther) {

    if (bOther == true) {
        var aTypes = [ 1, 31, 32, 33, 34, 35, 36, 37, 38 ];
    } else {
        var aTypes = [ 31, 32, 33, 34, 35, 36, 37, 38 ];
    }
    return aTypes;
}

//
// get the calendar update date
//
function getEventsUpdateDate() {
    return gsUpdateDate;
}

//
// get the last event
//
function getLastEvent(aEvents) {
    return aEvents[aEvents.length-1];
}

//
// get first event
//
function getFirstEvent(aEvents) {
    return aEvents[0];
}

//
// determine if there are any events to be shown in a given month
//
function getMonthHasEvents(aEvents, iCurMonth) {

	// loop through the aEvents array, return true when we find an event in the current month
	for (var i = 0; i < aEvents.length; i++) {
	
		var iTmpMonth = aEvents[i][1] - 1;
		if (iTmpMonth == iCurMonth) {
			// found one
			return true;
		}
	}
	return false;
}

//
// Determine the first monday on or before the date given
// note iMonth is 1-12, not 0-11
//
function getCalStartDate(aEvent) {
	var dtTmp = new Date();
	var iMonth = getEventMonth(aEvent) - 1;
    var iYear = getEventYear(aEvent);
    var iDate = getEventDay(aEvent);

	// let store the date and determine the day of week
	dtTmp.setFullYear(iYear, iMonth, iDate);
	setMonthDays(dtTmp);
	var iDay = dtTmp.getDay();
	while (iDay != 1) {
		// back up a day
		iDate = dtTmp.getDate() - 1;
		if (iDate == 0) {
			// go to last day of  previous month
			iMonth = dtTmp.getMonth() - 1;
			if (iMonth < 0) {
				// go to previous year
				iMonth = 11; // December
				iYear--;
				iDate = gaiMonthDays[iMonth];
			} else {
				iDate = gaiMonthDays[iMonth];
			}
		}
		dtTmp.setFullYear(iYear, iMonth, iDate);
		iDay = dtTmp.getDay();
	}
	return dtTmp;
}

//
// Determine the first monday on or before today's date
// note iMonth is 1-12, not 0-11
//
function getDateToStartCalendar() {
	var dtTmp = new Date();
	var iMonth = dtTmp.getMonth() - 1;
    var iYear = dtTmp.getFullYear();
    var iDate = dtTmp.getDay();

	setMonthDays(dtTmp);
	var iDay = dtTmp.getDay();
	while (iDay != 1) {
		// back up a day
		iDate = dtTmp.getDate() - 1;
		if (iDate == 0) {
			// go to last day of  previous month
			iMonth = dtTmp.getMonth() - 1;
			if (iMonth < 0) {
				// go to previous year
				iMonth = 11; // December
				iYear--;
				iDate = gaiMonthDays[iMonth];
			} else {
				iDate = gaiMonthDays[iMonth];
			}
		}
		dtTmp.setFullYear(iYear, iMonth, iDate);
		iDay = dtTmp.getDay();
	}
	return dtTmp;
}

//
// given a Monday date, advance to the next Sunday
//
function getEndOfWeek(dtDate) {
	var iYear = dtDate.getFullYear();
	var iDay = dtDate.getDate();
	var iMonth = dtDate.getMonth();
	var dtEnd = new Date();

	setMonthDays(dtDate);
	for (i = 0; i < 6; i++) {
		if (iDay == gaiMonthDays[iMonth]) {
			iDay = 0;
			iMonth++;
			if (iMonth > 11) {
				iMonth = 0;
				iYear++;
			}
		}
		iDay++;
	}
	dtEnd.setFullYear(iYear, iMonth, iDay);
	return dtEnd;
}

//
// Determine if a week needs to be displayed in the calendar
//
function isWeekNeeded(dtVal) {
	var iDate = dtVal.getFullYear()*10000 + dtVal.getMonth()*100 + dtVal.getDate();
	var dtToday = new Date();
	var iToday = dtToday.getFullYear()*10000 + dtToday.getMonth()*100 + dtToday.getDate();

	if (iDate < iToday) {
		return false;
	} else {
		return true;
	}
}

//
// getMonthName - return name of a month
//
function getMonthName(iMonth) {
	saMonths = [	"January", "February", "March", "April", "May", "June",
					"July", "August", "September", "October", "November", "December" ];
	return saMonths[iMonth];
}

//
// getMonthString - convert date to "month yyyy" string
//
function getMonthString(iMonth, dtDate) {
	var sMonth = getMonthName(iMonth);
	var iYear = dtDate.getFullYear();
	var sTemp = sMonth + " " + iYear;
	return sTemp;
}

//
// Determine if a month needs to be displayed in the calendar
//
function isMonthNeeded(dtVal) {
	var iDate = dtVal.getFullYear()*10000 + dtVal.getMonth()*100 + dtVal.getDate();
	var dtToday = new Date();
	var iToday = dtToday.getFullYear()*10000 + dtToday.getMonth()*100 + dtToday.getDate();
	if (iDate < iToday) {
		return false;
	} else {
		return true;
	}
}

//
// set the number of days in each month based on a date to handle leap year
//
function setMonthDays(dtDate) {
	var iYear = dtDate.getFullYear();
	var bLeapYear = ( iYear % 4 == 0) || ((iYear % 100 == 0) && (iYear % 400 == 0));
	if (bLeapYear == true) {
		gaiMonthDays[1] = 29;
	} else {
		gaiMonthDays[1] = 28;
	}
}

//
// get the last date of a month given a date in that month
//
function getEndOfMonth(dtDate) {
	var iYear = dtDate.getFullYear();
	var dtEnd = new Date();
	
	setMonthDays(dtDate);
	dtEnd.setFullYear(iYear, dtDate.getMonth(), gaiMonthDays[dtDate.getMonth()]);
	return dtEnd;
}

//
// getDayOfWeekAbbr - return day of the week abbreviation
//
function getDayOfWeekAbbr(iMonth, iDay, iYear) {
	saDayAbbrs = [	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ];
	var dtVal = new Date();
	dtVal.setFullYear(iYear, iMonth, iDay);
	return saDayAbbrs[dtVal.getDay()];
}

//
// getMonthAbbr - return abbreviated name of a month
//
function getMonthAbbr(iMonth) {
	saAbbrs = [	"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" ];
	return saAbbrs[iMonth];
}

//
// get the number of days in a month
//
function getMonthDays(iMonth, dtDate) {

    setMonthDays(dtDate);
    return gaiMonthDays[iMonth];
}

//
// output a simple sched row field
//
function outputSchedRowField(sVal, sAlign) {
	document.write("<td nowrap align='" + sAlign + "'>" + sVal + "</td>");
}

//
// output the header rows for a sched table
//
function outputSchedHeaders(sTitle, sType) {
   
    var sCols = [
        [ "Club",   "Day" ],
        [ "Club",   "Date" ],
        [ "Club",   "League" ],
        [ "Club",   "Open/<br />Novice" ],
        [ "Club",   "Event" ],
        [ "Club",   "Sign Up?" ],
        [ "Club",   "Who's<br />Signed<br />Up?" ],
        [ "Duals",  "Day" ],
        [ "Duals",  "Date" ],
        [ "Duals",  "Event" ],
        [ "Duals",  "Start Time" ],
        [ "Duals",  "Place" ],
        [ "Default",   "Day" ],
        [ "Default",   "Date" ],
        [ "Default",   "Location" ],
        [ "Default",   "Event" ],
        [ "Default",   "Start Time" ]
    ];
    // Count the columns we need.
    var iCols = 0;
    for (var i = 0; i < sCols.length; i++) {
        if (sType == sCols[i][0]) {
            iCols++;
        }
    }
    
    // Begin the table
	document.write("<table width='900' align='center' border='4' cellspacing='0' cellpadding='4'>");

    // Output the table header
	document.write("<tr bgcolor='#000000'> ");
		document.write("<td colspan='");
        document.write(iCols);
        document.write("' align='center' valign='bottom'><b><font color=\"#FF9900\"><h2>");
        document.write(sTitle); 
        document.write("</font></h2></b></div></td>");
	document.write("</tr>");

    // Output a row of column headers
	document.write("<tr bgcolor='#000000'> ");
    
        for (var i = 0; i < sCols.length; i++) {
            if (sType == sCols[i][0]) {
                document.write("<td class='xsmall' valign='bottom'> <div align='center'><b><font color='#FF9900'>");
                document.write(sCols[i][1]);
                document.write("</font></b></div></td>");
            }
        }
	document.write("</tr>");
}

//
// write out a directions call for a calendar event
//
function outputDirections(aCalEvent) {

    var sAddress = getHomeStreet(aCalEvent);
    var sZip = getHomeZip(aCalEvent);
    var sDestAddr = getDestStreet(aCalEvent);
    var sDestZip = getDestZip(aCalEvent);

	if (sAddress.length > 0
	&& sZip.length > 0
	&& sDestAddr.length > 0  
	&& sDestZip.length > 0) {   
		document.write(" <a href=\"http://www.mapquest.com/maps?&1a=");
		document.write(sAddress);
		document.write("&1z=")
		document.write(sZip);
		document.write("&1s=MI&1y=US&2a=");
		document.write(sDestAddr);
		document.write("&2z=")
		document.write(sDestZip);
		document.write("&2s=MI&2y=US");
		document.write("\" target=_blank >(Directions)</a>");
	}
}

//
// write the html to start a schedule row
//
function outputSchedRowStart(sColor) {
	document.write("<tr bgcolor=\"#" + sColor + "\" valign=\"top\">");
}

//
// output a simple schedule row
//
function outputSchedRow(iRowCount, aEvent) {

	var sColor;
    var sEvent = getEventName(aEvent);
	var iYear = getEventYear(aEvent);
	var sWhere = getEventLocation(aEvent);
    
	if ((iRowCount % 2) == 1) {
		sColor = "FFFFFF";
	} else {
		sColor = "FF9900";
	}
	
	iRowCount++;

    outputSchedRowStart(sColor);

	var adate = new Date();
	adate.setFullYear(iYear, (aEvent[1]-1), aEvent[2]);
	outputSchedRowField(getDayOfWeek(adate), "center");

	outputSchedRowField(aEvent[1] + "/" + aEvent[2] + "/" + iYear, "right");
	outputSchedRowField(sWhere, "center");
	
	// if date is past, output results entry for this event
    //sEvent = getEventName(aEvent);
	var sResults = sEvent.replace(/ |,|'|\./g, "");
	var bDateIsPassed = isDatePassed(aEvent[1], aEvent[2], iYear);

	if (getEventResult(aEvent) == 1 && bDateIsPassed == true) {
		document.write("<td class='xsmall' nowrap> <div align='left'> ")
		document.write("<a href=\"");
		document.write(sResults);
		document.write(".htm\" onclick=\"return popUpSized('");
		document.write(sResults);
		document.write(".htm', 400, 400)\"> ");
		document.write(sEvent);
		document.write(" </a> ");
		document.write(" </div> </td>");
	} else {
		document.write("<td class='xsmall' nowrap> <div align='left'> ")
		document.write(sEvent);

        if (sWhere == "Away") {
            outputDirections(aEvent);
        }
        
		document.write("</div> </td>");
	}
	outputSchedRowField(getEventTime(aEvent), "right");

	document.write("</tr>");
	return iRowCount;
}

//
// write out calendar month header line
//
function outputCalMonthHeaders(sMonth) {
	document.write("<table width='100%'>");
	document.write("<td colspan='7' align='center' valign='top' > <h2> ");
	document.write(sMonth);
	document.write(" </h2> </td>");
}

//
// write out the days of the week above a month
//
function outputCalWeekHeaders(bWantDays) {

	document.write("<table width='100%' border='4' cellspacing='0' cellpadding='2'>");
	document.write("<colgroup span='7' width='14%'>");
	if (bWantDays == true) {
		document.write("<tr bgcolor='#000000'> ");
		document.write("<td class='xsmall' > <div align='center'><b><font color='#FF9900'> Monday </font></b></div></td>");
		document.write("<td class='xsmall' > <div align='center'><b><font color='#FF9900'> Tuesday </font></b></div></td>");
		document.write("<td class='xsmall' > <div align='center'><b><font color='#FF9900'> Wednesday </font></b></div></td>");
		document.write("<td class='xsmall' > <div align='center'><b><font color='#FF9900'> Thursday </font></b></div></td>");
		document.write("<td class='xsmall' > <div align='center'><b><font color='#FF9900'> Friday </font></b></div></td>");
		document.write("<td class='xsmall' > <div align='center'><b><font color='#FF9900'> Saturday </font></b></div></td>");
		document.write("<td class='xsmall' > <div align='center'><b><font color='#FF9900'> Sunday </font></b></div></td>");
		document.write("</tr>");
	}
}

//
// write out a date cell in a calendar
//
function outputCalCell(aCalEvent, bLast) {

    var sColorPre = "";
    var sColorPost = "";
    var sLink = getEventLink(aCalEvent);
    var sLinkPre = "";
    var sLinkPost = "";
    var sColor = getEventColor(aCalEvent);
    var sName;
   
    // if we have a link, the color will be the default link color, so ignore the color value
    // so this test is if we have no link and a color, set the color strings 
	if (sLink.length < 1 && sColor.length > 0) {
        sColorPre = "<font color='" + sColor + "'> <b> <blink>";
        sColorPost = "</blink></b></font>";
    }
    if (sLink.length > 0) {
        sLinkPre = "<a href='" + getEventLink(aCalEvent) + "'> ";
        sLinkPost = " </a>";
    }
    
    if (isEventACompetition(aCalEvent) == false) {
        sName = getEventName(aCalEvent) + " " + getEventTime(aCalEvent);
    } else {
        sName = getEventName(aCalEvent);
    }

	if (bLast == true) {
		document.write("<td class='xsmall' height='60' valign='top' ><div align='center'> ");
    } else {
        document.write("<td class='xsmall' ><div align='center'> ");
    }
    document.write(sLinkPre);
    document.write(sColorPre);
    document.write(sName);
    document.write(sColorPost);
    document.write(sLinkPost);

    // if we have a destination address, output directions call
    outputDirections(aCalEvent);

    document.write(" </div> </td>");
}

//
// write out a blank date cell in a calendar
//
function outputBlankCalCell(bLast) {

	if (bLast == true) {
		document.write("<td class='xsmall' height='60' valign='top' ><div align='center'> &nbsp </div> </td>");
    } else {
        document.write("<td class='xsmall' ><div align='center'> &nbsp </div> </td>");
    }
}

//
// output the row of dates for the week starting with dtDate
// note that if the date is not in the current month, output no date 
// for that day of the week
//
function outputCalDateCells(iCurMonth, dtDate, bMonth) {
	var iYear = dtDate.getFullYear();
	var iDay = dtDate.getDate();
	var iMonth = dtDate.getMonth();
	
	if (bMonth == true) {
		document.write("<tr bgcolor='#000000'> ");
	} else {
		document.write("<tr  bgcolor='#BEBEBE'> ");
	}
	for (var i = 0; i < 7; i++) {
		document.write("<td class='xsmall' > <div align='right'><b> ");
		if (bMonth == true) {
			document.write("<font color='#FF9900'> ");
			document.write(getDayOfWeekAbbr(iMonth, iDay, iYear));
			document.write(" ");
			document.write(getMonthAbbr(iMonth));
			document.write(" ");
			document.write(iDay);
			document.write("</font>");
		} else {
			if (iMonth == iCurMonth) {
				document.write(iDay);
			} else {
				document.write("&nbsp");
			}
		}
		document.write(" </b></div></td>");
		if (iDay == getMonthDays(iMonth, dtDate)) {
			iDay = 0;
			iMonth++;
			if (iMonth > 11) {
				iMonth = 0;
				iYear++;
			}
		}
		iDay++;
	}
	document.write("</tr>");
}

//
// Given a date, return the date 7 days forward
//
function updateDateToNextWeek(dtDate) {
	var iYear = dtDate.getFullYear();
	var iDay = dtDate.getDate();
	var iMonth = dtDate.getMonth();
	var dtDateNext = new Date();
	
	for (var i = 0; i < 7; i++) {
		if (iDay == getMonthDays(iMonth, dtDate)) {
			iDay = 0;
			iMonth++;
			if (iMonth > 11) {
				iMonth = 0;
				iYear++;
			}
		}
		iDay++;
	}
	dtDateNext.setFullYear(iYear, iMonth, iDay);
	return dtDateNext;
}

//
// getMyDay - get days of week where 0 = Monday through 6 = Sunday
//
function getMyDay(dtDate) {
	var aiVals = [ 6, 0, 1, 2, 3, 4, 5 ];
	return aiVals[dtDate.getDay()];
}

//
// output any events for this week of days from the events list
//
function outputCalDayCells(aEvents, iCurMonth, dtNow, dtEnd, bMonth) {

	var dtTmp = new Date();
	var iTmpMonth;
	var aiEvtCounts = [ 0, 0, 0, 0, 0, 0, 0 ];
	var iMax = 0;
	var iDay;
	var bLast;
    var sLink;
	var i;
	var j;
	var k;
	var asEvtList = [
		[ -1, -1, -1 ],		// Monday - up to 3 events on a single week day
		[ -1, -1, -1 ],		// Tuesday
		[ -1, -1, -1 ],		// Wednesday
		[ -1, -1, -1 ],		// Thursday
		[ -1, -1, -1 ],		// Friday
		[ -1, -1, -1 ],		// Saturday
		[ -1, -1, -1 ]		// Sunday
	];

	// loop through the gaCalEvents array, gather all events for this week
	for (i = 0; i < aEvents.length; i++) {
		
		dtTmp.setFullYear(getEventYear(aEvents[i]), (aEvents[i][1] - 1), aEvents[i][2]);
		iTmpMonth = aEvents[i][1] - 1;
		if ((bMonth == true || iTmpMonth == iCurMonth) 
		&& CompareDates(dtNow, dtTmp) < 1 && CompareDates(dtTmp, dtEnd) < 1) {

			// this event is in this week - save index into aEvents array
			iDay = getMyDay(dtTmp);
			asEvtList[iDay][aiEvtCounts[iDay]] = i;
			aiEvtCounts[iDay]++;
			if (iMax < aiEvtCounts[iDay]) {
				iMax = aiEvtCounts[iDay];
			}
		}
	}

	// now lets print out the days' events a row at a time.  
	// the last row is special
	// note: if max is 0 we need max to be 1 at least to put out a row of blank cells
	if (iMax < 1) { 
		iMax = 1; 
	}
	for (j = 0; j < iMax; j++) {
		if (j == (iMax - 1)) { 
			bLast = bMonth == false;
		} else {
			bLast = false;
		}
		document.write("<tr > ");
		for (i = 0; i < 7; i++) {
			if (asEvtList[i][j] > -1) {
				// if CalEvent has 2nd string it is a link
				k = asEvtList[i][j];
				outputCalCell(aEvents[k], bLast);
			} else {
				// output special blank event
				outputBlankCalCell(bLast);
			}
		}
		document.write("</tr>");
	}		
}

//
// compare the month, day, year of 2 dates, returning:
// -1 if date1 is before date2
//  0 if date1 is the same as date2
// +1 if date1 is after date2
// this function is needed to avoid problems with the time portion of the date object
//
function CompareDates(dtOne, dtTwo) {

	var iOne = dtOne.getFullYear()*10000 + dtOne.getMonth()*100 + dtOne.getDate();
	var iTwo = dtTwo.getFullYear()*10000 + dtTwo.getMonth()*100 + dtTwo.getDate();
	if (iOne < iTwo) {
		return -1;
	} else if (iOne == iTwo) {
		return 0;
	} else {
		// iOne must be > iTwo
		return 1;
	}
}

//
// get club tournament level from class
//
function getClubTournamentLevel(aEvent) {
    var iClass = getEventClass(aEvent);
    
	if (iClass == 31 || iClass == 33) {
		return "Open";
	} 
    if (iClass == 32 || iClass == 34) {
		return "Novice";
	}
}

//
// get club tournament level from class
//
function getClubTournamentLeague(aEvent) {
    var iClass = getEventClass(aEvent);

	if (iClass == 31 || iClass == 32) {
		return "MMWA";
	} 
    if (iClass == 33 || iClass == 34) {
		return "MYWA";
	}
}

//
// determine if we need the next month
//
function isMonthNeeded(aEvents, dtEndOfWeek, iCurrentMonth, iWeeks, iWeeksShown) {

    if (iWeeks > 0) {
        if (isWeekNeeded(dtEndOfWeek) == true && iWeeksShown < iWeeks) {
            return true;
        } else {
            return false;
        }
    } else {
        if (isWeekNeeded(dtEndOfWeek) == true 
        && getMonthHasEvents(aEvents, iCurrentMonth) == true) {
            return true;
        } else {
            return false;
        }
    }
}

//
// Output a page break 
//
function outputPageBreak() {

    document.write("<span style='font-size:12.0pt;font-family:\"Times New Roman\",\"serif\"'><br");
    document.write("clear=all style='page-break-before:always'>");
    document.write("</span>");
}

//
// Produce a calendar 
//
function buildCalendar(aEvents, iWeeks, sTitle, bNewPage) {

    var bNotFirstPage = false;
    var bLimited = new Boolean(iWeeks > 0);
    var aCalEvent;
    var aCalLastEvent;
    var dtToday = new Date();
    var dtNow = new Date();
    var dtNextWeek = new Date();
    var dtLast = new Date();
    var dtEndOfWeek = new Date();
    var iCurrentMonth;
    var bNeedCurrentMonthHdrs = true;
	var iWeeksShown = 0;
    var sCalSeasonYearString = getLastYear() + "-" + getThisYear();

	// first see if there are any events to show at all
	aCalLastEvent = getLastEvent(aEvents);
	dtLast.setFullYear(getEventYear(aCalLastEvent), getEventMonth(aCalLastEvent), getEventDay(aCalLastEvent));
	
	if (CompareDates(dtToday, dtLast) > 0) {
		// no dates in calendar to show
        if (sTitle.length > 0) {
            document.write("<p align='center'><h3><b>Last Updated ");
            document.write(getEventsUpdateDate());
            document.write("</h3></b></p>");
            document.write("<p align='left'> The season calendar for ");
            document.write(sTitle);
            document.write(" for ");
            document.write(sCalSeasonYearString);
            document.write(" will be updated in the fall of 2011.  See you then!");
            document.write("</center></p>");
        }
	} else {
		// we have events to show
		// determine first monday we want to show from first event in the list
		// note that current month is month of first event in the list, not necessarily the first monday
		aCalEvent = getFirstEvent(aEvents);
		iCurrentMonth = getEventMonth(aCalEvent) - 1;
		dtNow = getCalStartDate(aCalEvent);

		// produce headers
        if (sTitle.length > 0) {
            document.write("<P><center><h3><b>Last Updated ");
            document.write(getEventsUpdateDate());
            document.write("</h3></b></center>");
            document.write("<P><center><h2><b> Season Calendar for ");
            document.write(sTitle);
            document.write(" for ");
            document.write(sCalSeasonYearString);
            document.write("</h2></b></center></p>");
        }

		while (CompareDates(dtNow, dtLast) < 1) {

			// find end of month for start date
			dtEndOfWeek = getEndOfWeek(dtNow);
			
			// if we need this month, 
            if (isMonthNeeded(aEvents, dtEndOfWeek, iCurrentMonth, iWeeks, iWeeksShown)) {
            
				if (bNeedCurrentMonthHdrs == true) {
                    if (bLimited == true) {
                        outputCalWeekHeaders(false);
                    } else {
                        if (bNewPage == true) {
                            if (bNotFirstPage == true) {
                                outputPageBreak();
                            }
                            bNotFirstPage = true;
                        }
                        outputCalMonthHeaders(getMonthString(iCurrentMonth, dtEndOfWeek));
                        outputCalWeekHeaders(true);
                    }
					bNeedCurrentMonthHdrs = false;
				}

				if (isWeekNeeded(dtEndOfWeek) == true) {
			
					// output the values in the list for this week, that are within this month.
                    outputCalDateCells(iCurrentMonth, dtNow, bLimited);

					// output any events for this week of dates.
					outputCalDayCells(aEvents, iCurrentMonth, dtNow, dtEndOfWeek, bLimited);

                    // increment weeks show so we only show the next 2 weeks
                    iWeeksShown++;
				}	
			}
			
			// update date by 7 days, if still in current month
			dtNextWeek = updateDateToNextWeek(dtNow);
			if (iCurrentMonth == dtNextWeek.getMonth() || bLimited == true) {
				dtNow = dtNextWeek;
			} else {
				// if whole week is in next month, update to dtNextWeek,
				if (iCurrentMonth == dtEndOfWeek.getMonth()) {
					dtNow = dtNextWeek;
				}
				// do part of this week that is in the next month by only updating month
				iCurrentMonth++;
				document.write("</table>");
				document.write("</table>");
				bNeedCurrentMonthHdrs = true;
                document.write("<br>");
				if (iCurrentMonth > 11) {
					// is month is past December, loop over to start of next month
					iCurrentMonth = 0;
				}
			}
		}
	}
	document.write("</table>");
}
