/*	
Make a reservations calendar for Seido-en Forest House.
Author: Malcolm Kendall.
*/

// Set or declare variables.
var numMonths = 13;			// Number of months to display.
var i, j;						// Counters. 
var resCursor = 0;			// Array index counters.
var refMonth, thisMonth;	// Stored month.

// Initialize arrays.
// Days of week names.
days = new Array("Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat");
// Month names.
months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");


// Create date object set to the current date but set time to noon. 
// Otherwise, daylight saving time changes will cause this calendar
// to skip April 2 if used between 11pm and 12pm.
dateCursor = new Date();
dateCursor.setHours(12);


// Move resCursor past bygone dates in date object array, reservations[].
while (resCursor < reservations.length && reservations[resCursor].jsDate < dateCursor)
{
	resCursor++
}

// Decrement resCursor if out of range or if the
// previous date in reservations[] is the current date.
// It will point to the last date in the array if all dates are past.
if (resCursor == reservations.length)
{
	resCursor -= 1;
}
else if (reservations[resCursor - 1].jsDate.getDate() == dateCursor.getDate()
         && reservations[resCursor - 1].jsDate.getMonth() == dateCursor.getMonth()
         && reservations[resCursor - 1].jsDate.getYear() == dateCursor.getYear())
{
	resCursor -= 1;
}


// Starting from today, make and display number of months in numMonths.
for (i = 0; i < numMonths; i++)
{
 	// This month.
 	thisMonth = months[dateCursor.getMonth()];
 	noVacancy = 1;

 	// Correct year bug in Date object.
 	year = dateCursor.getYear();
 	if (year < 1000)
 	{
 		year += 1900;
 	}

	// Display month name and year.
	document.write("<p><FONT face='Verdana','Arial' size=+2 color=#7C7140>");
	document.write(thisMonth + " " + year);
	document.write("</FONT>");  
	
	// Start month table.
	document.write("<table cellpadding=5 cellspacing=0 border=0>");

	//display day names in first table row.
	document.write("<tr align=center>");

	for (j = 0; j < 7; j++)
	{
		document.write("<td><FONT face='Verdana','Arial' color=#7C7140>");
		document.write(days[j], "</FONT></td>");
	}

	document.write("</tr>");


	// Make new rows for each week until month changes.
	refMonth = dateCursor.getMonth();
	while (refMonth == dateCursor.getMonth())
	{
		// Start week row.
		document.write("<tr align=center>");		
		
		// Add blank days if the first is not a Sunday.
		for (j = dateCursor.getDay(); j > 0; j--)
		{		
			  document.write("<td></td>");
		}		
		
		// Fill row with date cells until end of week or end of month.
		while (refMonth == dateCursor.getMonth())
		{
			// Make date cells for rest of week.

			backGroundColor = " bgcolor=FFFDE3";
			dayOfMonth = dateCursor.getDate();
			
			// Check for reserved dates if any.
			if (resCursor > 0
			    && resCursor < reservations.length
			    && dateCursor.getDate() == reservations[resCursor].jsDate.getDate()
			    && dateCursor.getMonth() == reservations[resCursor].jsDate.getMonth()
			    && dateCursor.getYear() == reservations[resCursor].jsDate.getYear()) {
				
				backGroundColor = "";
				dayOfMonth = "";
				resCursor++;
			}
			else {
				noVacancy = 0;
			}

			// Print date cell.
			document.write("<td", backGroundColor, ">");
			document.write("<FONT face=Arial color=#7C7140>");
			document.write(dayOfMonth, "</FONT></td>");

			// Increment current date and end week if Sunday.
			dateCursor = new Date(dateCursor.getTime() + 1000 * 60 * 60 * 24);
			if (dateCursor.getDay() == 0)
			{
				break;
			}
		}		
		
		// Add blank days if last day of month is not a Saturday.
		if (dateCursor.getDay() > 0)
		{
			lastDay = dateCursor.getDay();
		
			for (lastDay; lastDay <= 6; lastDay++)
			{				
				document.write("<td></td>");		
			}
		}
		
		// End row.
		document.write("</tr>");
	}	
	
	// End month table.
	document.write("</TABLE>");	
	
	// Display no vacancy message.
	if (noVacancy) {
		document.write("<table cellpadding=2 cellspacing=0 border=0>");
		document.write(" <td align=center bgcolor=FFFDE3>");
		document.write("  <font face='Verdana','Arial' size=-1 color=#7C7140>");
		document.write("  <b>Sorry, " + thisMonth + " is full</b></font>");
		document.write("  <font face='Verdana','Arial' size=-2 color=#7C7140>");
		document.write("  <br>Please ask about cancellations</b></font>");
		document.write(" </td>");
		document.write("</table>");
	}
	else {
		document.write("<img src=images/spcyellw.gif width=20 height=10>");
		document.write("<font face='Verdana','Arial' size=-1 color=#7C7140> = Available</font>");
	}
}
