// SmallBizWebs.com JavaScript Document
// Copyright SmallBizWebs.com, A Division of The JBI Company, Inc.
// All Rights Reserved, contact SmallBizWebs.com for licensing information.
//
// ===================================
// Custom Code for Ultrasound Calendar
// ===================================
//
// Global Variables
//
var theDays = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat'); 
var theMonths = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var theCurrentDate = new Date(); // Lock in current date at page load
var theDueDate;  // Set as Global Variable for access across functions
//
// Main Function, call with 'init' for initial display
//
function SBW_CreateCalendar(parmDueDate) {
	var thisHTML = '';
	var thisCode = '';
// Execute initial or date-selected code as required
	if (parmDueDate == 'init') {
		document.getElementById("SBW_CalInst").style.display='block'; // Display instructions
		theDueDate = new Date(theCurrentDate);
		theDueDate.setMonth(theDueDate.getMonth() - 12); // Set Due Date off the calendar
	} else {
		document.getElementById("SBW_CalInst").style.display='none'; // Hide instructions
		document.getElementById("SBW_CalLegend").style.display='block'; // Display legend
		theDueDate = new Date(parmDueDate); // Selected Due Date
	}
// Create the calendar grid and months
	thisHTML = '<!-- Copyright SmallBizWebs.com -->';
	for (idxRow = 0; idxRow < 3; idxRow++) {
		thisHTML += '<div class="SBW_CalRow">';
		for (idxCol = 1; idxCol < 4; idxCol++) {
			thisHTML += '<div class="SBW_CalCol' + idxCol + '">' + SBW_CreateMonth(idxRow * 3 + idxCol) + '</div>';
		}
		thisHTML += '</div>';
	}
	document.getElementById("SBW_Calendar").innerHTML = thisHTML;
}
//
// Sub-Function to display each month, call with # as relative month within calendar (1 for current month)
//
function SBW_CreateMonth(parmMonthNum) {
	var thisHTML = '';
	var thisFullDate = new Date(theCurrentDate);
	var tempFullDate;
	var thisYear;
	var thisMonth = thisFullDate.getMonth() + parmMonthNum - 1;
	var thisDate;
	var thisWeekDay;
// Adjust date for selected month
	thisFullDate.setDate(1);
	thisFullDate.setMonth(thisMonth);
	thisMonth = thisFullDate.getMonth();
	thisYear = thisFullDate.getFullYear();
// Start table and build headings
	thisHTML = '<table border="0" cellspacing="0" cellpadding="0" class="SBW_CalMonth">';
	thisHTML += '<tr><td align="center" colspan="7" class="SBW_CalMonthTitle">' + theMonths[thisMonth]  + '&nbsp;&nbsp;' + thisYear + '</td></tr>';
	thisHTML += '<tr>';
	for (idxWeekday=0; idxWeekday < 7; idxWeekday++) {
		thisHTML += '<td align="center" class="SBW_CalDayHdg">' + theDays[idxWeekday] + '</td>';
	}
	thisHTML += '</tr>';
// Pad for blank days at start of month
	thisWeekday = thisFullDate.getDay();
	if (thisWeekday > 0) {
		thisHTML += '<tr class="SBW_CalDateRow">';
		for (idxWeekday=0; idxWeekday < thisWeekday; idxWeekday++) {
			thisHTML += '<td align="center" class="SBW_CalDateBlank">&nbsp;</td>';
		}
	}
// Build month full of days
	tempFullDate = new Date(thisFullDate);
	tempFullDate.setMonth(tempFullDate.getMonth() + 1);
	tempFullDate.setDate(0);
	thisDaysInMonth = tempFullDate.getDate();
	for (idxDate=0; idxDate < thisDaysInMonth; idxDate++) {
		thisDate = thisFullDate.getDate();
		thisWeekday = thisFullDate.getDay();
		if (thisWeekday == 0) {
			thisHTML += '<tr class="SBW_CalDateRow">';
		}
		thisHTML += "<td align='center' class='SBW_CalDate" + SBW_SetStyle(thisFullDate, theDueDate) + "' onclick='SBW_CreateCalendar(\"" + thisFullDate + "\");'";
		if (parmMonthNum == 1 & thisFullDate.toDateString() == theCurrentDate.toDateString()) { // Ignore time differences
			thisHTML += " id='SBW_CalDateToday'";
		}
		thisHTML += ">" + thisDate + "</td>";
		if (thisWeekday == 6) {
			thisHTML += '</tr>';
		}
		thisFullDate.setDate(thisDate+1);
	}
// Pad for blank days at end of month
	thisWeekday = thisFullDate.getDay();
	if (thisWeekday > 0) {
		for (idxWeekday=thisWeekday; idxWeekday < 7; idxWeekday++) {
			thisHTML += '<td align="center" class="SBW_CalDateBlank">&nbsp;</td>';
		}
		thisHTML += '</tr>';
	}
// Close table and return
	thisHTML += '</table>';
	return thisHTML;
}
//
// Sub-Sub-Function to assign Style class suffix for each date
//
function SBW_SetStyle(parmFullDate, parmDueDate) {
	var styleSuffix = '';
	var diffDate = parmDueDate - parmFullDate;
	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 >= 20 & numWeek <= 25) { styleSuffix = 'Gender'; }
	else {
		if (numWeek >= 26 & numWeek <= 28) { styleSuffix = 'Best'; }
		else {
			if (numWeek >= 29 & numWeek <= 32) { styleSuffix = 'Good'; }
			else {
				if (numWeek >= 33 & numWeek <= 37) { styleSuffix = 'Poor'; }
			}
		}
	}
	return styleSuffix;
}

