// Variables
//
var Days = new Array('Su','Mo','Tu','We','Th','Fr','Sa'); 
var Months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var Today = new Date(); // Today's Date when page loads
var DueDate;  // DueDate
//
// 'init'
//
function CreateCalendar(paramDueDate) {
	var HTML = '';
	var Code = '';
// Execute initial or date-selected code as required
	if (paramDueDate == 'init') {
		//document.getElementById("Inst").style.display='block'; // Display instructions
		DueDate = new Date(Today);
		DueDate.setMonth(DueDate.getMonth() - 12); // Set Due Date off the calendar
	} else {
		//document.getElementById("Inst").style.display='none'; // Hide instructions
		//document.getElementById("Legend").style.display='block'; // Display legend
		DueDate = new Date(paramDueDate); // Selected Due Date
	}
// Create the calendar
	for (idxRow = 0; idxRow < 3; idxRow++) {
		HTML += '<div class="Row">';
		for (idxCol = 1; idxCol < 4; idxCol++) {
			HTML += '<div class="Col' + idxCol + '">' + CreateMonth(idxRow * 3 + idxCol) + '</div>';
		}
		HTML += '</div>';
	}
	document.getElementById("Calendar").innerHTML = HTML;
}
//
// Sub-Function to display each month, call with # as relative month within calendar (1 for current month)
//
function CreateMonth(paramMonthNum) {
	var HTML = '';
	var FullDate = new Date(Today);
	var tempFullDate;
	var Year;
	var Month = FullDate.getMonth() + paramMonthNum - 1;
	var aDate;
	var WeekDay;
// Adjust date for selected month
	FullDate.setDate(1);
	FullDate.setMonth(Month);
	Month = FullDate.getMonth();
	Year = FullDate.getFullYear();
// Start table and build headings
	HTML = '<table id="CalMonth" border="0" cellspacing="0" cellpadding="0" class="Month">';
	HTML += '<tr><td id="CalMonthTitle" align="center" colspan="7" class="MonthTitle">' + Months[Month]  + '&nbsp;&nbsp;' + Year + '</td></tr>';
	HTML += '<tr>';
	for (idxWeekday=0; idxWeekday < 7; idxWeekday++) {
		HTML += '<td align="center" class="DayHdg">' + Days[idxWeekday] + '</td>';
	}
	HTML += '</tr>';
// Pad for blank days at start of month
	Weekday = FullDate.getDay();
	if (Weekday > 0) {
		HTML += '<tr class="DateRow">';
		for (idxWeekday=0; idxWeekday < Weekday; idxWeekday++) {
			HTML += '<td align="center" class="DateBlank">&nbsp;</td>';
		}
	}
// Build month full of days
	tempFullDate = new Date(FullDate);
	tempFullDate.setMonth(tempFullDate.getMonth() + 1);
	tempFullDate.setDate(0);
	DaysInMonth = tempFullDate.getDate();
	for (idxDate=0; idxDate < DaysInMonth; idxDate++) {
		aDate = FullDate.getDate();
		Weekday = FullDate.getDay();
		if (Weekday == 0) {
			HTML += '<tr class="DateRow">';
		}
		HTML += "<td align='center' class='Date" + SetStyle(FullDate, DueDate) + "' onclick='CreateCalendar(\"" + FullDate + "\");'";
		if (paramMonthNum == 1 & FullDate.toDateString() == Today.toDateString()) { // Ignore time differences
			HTML += " id='DateToday'";
		}
		HTML += ">" + aDate + "</td>";
		if (Weekday == 6) {
			HTML += '</tr>';
		}
		FullDate.setDate(aDate+1);
	}
// Pad for blank days at end of month
	Weekday = FullDate.getDay();
	if (Weekday > 0) {
		for (idxWeekday=Weekday; idxWeekday < 7; idxWeekday++) {
			HTML += '<td align="center" class="DateBlank">&nbsp;</td>';
		}
		HTML += '</tr>';
	}
// Close table and return
	HTML += '</table>';
	return HTML;
}
//
// Sub-Sub-Function to assign Style class suffix for each date
//
function SetStyle(paramFullDate, paramDueDate) {
	var styleSuffix = '';
	var diffDate = paramDueDate - paramFullDate;
	var dayInMsecs = 24 * 60 * 60 * 1000;
	var diffDays = Math.round(diffDate / dayInMsecs);
// Past Due Date
	if (diffDays < 0) {
		return styleSuffix;
	}
// Is Due Date
	if (diffDays == 0) {
		styleSuffix = 'Due';
		return styleSuffix;
	}
// Before Due Date, check ranges
	var numWeek = 39 - (parseInt(diffDays/7));
	if (numWeek >= 16 & numWeek <= 25) { styleSuffix = 'Development'; }
	else {
		if (numWeek >= 26 & numWeek <= 36) { styleSuffix = 'Features'; }
		else {
			if (numWeek >= 37 & numWeek <= 39) { styleSuffix = 'Crowded'; }
		}
	}
	return styleSuffix;
}
