
var gaiMonthDays = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

//
// write out calendar month header line
//
function echoCalMonthHeaders(sMonth) {
	document.write("<br>");
	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 echoCalWeekHeaders(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 echoCalCell(sAction, bLast) {

	if (bLast == true) {
		document.write("<td class='xsmall' height='60' valign='top' ><div align='center'> ");
		document.write(sAction);
		document.write(" </div> </td>");
	} else {
		document.write("<td class='xsmall' ><div align='center'> ");
		document.write(sAction);
		document.write(" </div> </td>");
	}
}

//
// write out a date cell in a calendar with a text color
//
function echoCalColorCell(sAction, sColor, bLast) {

	var sTemp = "<font color='" + sColor + "'> <b> <blink>";

	if (bLast == true) {
		document.write("<td class='xsmall' height='60' valign='top' ><div align='center'> ");
		document.write(sTemp);
		document.write(sAction);
		document.write("</blink></b></font> </div> </td>");
	} else {
		document.write("<td class='xsmall' ><div align='center'> ");
		document.write(sTemp);
		document.write(sAction);
		document.write("</blink></b></font> </div> </td>");
	}
}

//
// write out a date cell with a link on the text
//
function echoCalCellLink(sAction, sURL, bLast) {

	if (bLast == true) {
		document.write("<td class='xsmall' height='60' valign='top' ><div align='center'> ");
		document.write("<a href='"+sURL+"'> "+sAction+" </a>");
		document.write(" </div> </td>");
	} else {
		document.write("<td class='xsmall' ><div align='center'> ");
		document.write("<a href='"+sURL+"'> "+sAction+" </a>");
		document.write(" </div> </td>");
	}
}

//
// 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;
	}
}

//
// 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];
}

//
// 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;
}

//
// 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;
}

//
// output the row of dates for the week starting with tDate
// 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();
	
	setMonthDays(dtDate);
	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 == gaiMonthDays[iMonth]) {
			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();
	
	setMonthDays(dtDate);
	for (var i = 0; i < 7; i++) {
		if (iDay == gaiMonthDays[iMonth]) {
			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(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 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 < gaCalEvents.length; i++) {
		
		dtTmp.setFullYear(gaCalEvents[i][2], (gaCalEvents[i][0] - 1), gaCalEvents[i][1]);
		iTmpMonth = gaCalEvents[i][0] - 1;
		if ((bMonth == true || iTmpMonth == iCurMonth) 
		&& CompareDates(dtNow, dtTmp) < 1 && CompareDates(dtTmp, dtEnd) < 1) {

			// this event is in this week - save index into gaCalEvents 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];
				if (gaCalEvents[k][4].length > 0) {
					echoCalCellLink(gaCalEvents[k][3], gaCalEvents[k][4], bLast);
				} else {
					// if CalEvent had 3rd string it is a text color
					if (gaCalEvents[k][5].length > 0) {
						echoCalColorCell(gaCalEvents[k][3], gaCalEvents[k][5], bLast);
					} else {
						echoCalCell(gaCalEvents[k][3], bLast);
					}
				}
			} else {
				echoCalCell("&nbsp", 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;
	}
}

//
// determine if there are any events to be shown in a given month
//
function MonthHasEvents(iCurMonth) {

	// loop through the gaCalEvents array, return true when we find an event in the current month
	for (var i = 0; i < gaCalEvents.length; i++) {
	
		var iTmpMonth = gaCalEvents[i][0] - 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(iTmpMonth, iDate, iYear) {
	var dtTmp = new Date();
	var iMonth = iTmpMonth - 1;

	// 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;
}
