var date = new Date();
var currentMonth = date.getMonth();
var currentYear = date.getFullYear();
var currentDate = date.getDate();

var letterDayWeek = new Array("S", "M", "T", "W", "T", "F", "S");
var monthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

function idFromMonthDate(month, dateCount)
{
	return ((month<10) ? "0" + month : month.toString()) + 
	       ((dateCount<10) ? "0" + dateCount : dateCount.toString());
}

function dayClassFromDayIndex (inDay)
// specifying this part of the css class on each day (just to clear:left .sun)
// may not be needed anymore
{
	switch (inDay)
	{
		case 0: return "sun";  break;
		case 1: return "mon";  break;
		case 2: return "tue";  break;
		case 3: return "wed";  break;
		case 4:	return "thur"; break;
		case 5: return "fri";  break;
		case 6: return "sat";  break;
	}
}


function setupWeekHeader() {
	// __BD__ removing this for now, using a graphic to do the week header
	//for (var i = 0; i <= 6; i++) {
		//var theDiv = document.getElementById ("dayofweek" + i);
		//theDiv.innerHTML = letterDayWeek[i];
	//}
}

function fetchIPrefFirstWeekday() {
	// Get the first-day-of-week, 0-based index.
	return 0;
}

// JavaScript's Date.getDay() is Sunday-based, so we have to fix it up.
function getDayFromDate(date) {
	var dayIndex = date.getDay() - fetchIPrefFirstWeekday();
	if (dayIndex < 0)
		dayIndex += 7;
	return dayIndex;
}

function maxDaysForMonth(month, year) {
	var maxDays;
	if((month == 3) || (month == 5) || (month == 8) || (month == 10)){
		maxDays = 30;
  	} else {
		maxDays = 31;
  		if(month == 1) { // Leap year processing
   			if (year/4 - parseInt(year/4) != 0) { maxDays = 28; }
		   	else { maxDays = 29; }
		}
	}
	return maxDays;
}

function setupMonth(month, year) {
	var mo = new Date(year, month, 1);
	var firstDay = getDayFromDate(mo);
	var monthDiv = document.getElementById("calendarDetail");
	var dayOfWeek = 0;
	var dateCount = 1;
	var rowCount = 0;
	
	// show the names of the days of the week
	setupWeekHeader();
	
	// clear out the old
	if (monthDiv.hasChildNodes()) {
		var children = monthDiv.childNodes;
		var count = children.length;
		for(var j=0; j < count; j++) {
			monthDiv.removeChild(children[0]);
		}
	}
	var previousMonth = month-1;
	var previousYear = year;
	if (previousMonth < 0) { previousMonth = 11; previousYear--; }
	var maxPrevMonthDays = maxDaysForMonth(previousMonth, previousYear);
	var maxDays = maxDaysForMonth(month, year);
	var dayNextMonth = 1;
	// create the month
	for(var i=0; i< 42; i++) {
		var dateSpan = document.createElement("span");
		
		if (dateCount < 10) {
                    // Solution for spacing issues on PC and Mac.
                    if ((navigator.appVersion.indexOf("Windows") > 0) && (navigator.appName == "Microsoft Internet Explorer")) {
                        dayThisMonthPrepend = "<img src=\"lib/spacer.gif\" height=\"10\" width=\"5\" alt=\"\" border=\"0\">";
                    } else {
                        dayThisMonthPrepend = "&nbsp;&nbsp;&nbsp;";
                    }
		} else {
			dayThisMonthPrepend = "";
		}
		
		if (dayNextMonth < 10) {
                    // Solution for spacing issues on PC and Mac.
                    if ((navigator.appVersion.indexOf("Windows") > 0) && (navigator.appName == "Microsoft Internet Explorer")) {
                        dayNextMonthPrepend = "<img src=\"lib/spacer.gif\" height=\"10\" width=\"5\" alt=\"\" border=\"0\">";
                    } else {
                        dayNextMonthPrepend = "&nbsp;&nbsp;&nbsp;";
                    }
		} else {
			dayNextMonthPrepend = "";
		}

		if (dayOfWeek < firstDay) {
			var dateSpanContent = "" + maxPrevMonthDays - firstDay + i + 1
			dateSpan.innerHTML = '<span style=\"color:#c2c2c2;\">'+dateSpanContent+'</span>';
		} else {
			if (mo.getMonth() == month && dateCount < 32) {
				dateSpan.innerHTML = '<span>'+dayThisMonthPrepend+dateCount+'</span>';
				dateSpan.setAttribute("id", idFromMonthDate(month, dateCount));
			} else {
				dateSpan.innerHTML = '<span style=\"color:#c2c2c2;\">'+dayNextMonthPrepend+dayNextMonth+'</span>';
				dayNextMonth++;
			}
			dateCount++;
			mo.setDate(dateCount);
		}
		monthDiv.appendChild(dateSpan);

		// increment the number of rows
		if (dayOfWeek%7 == 0) {
			rowCount++;
			}
		
		// stop after the number of days in this month
		if (rowCount == 7) {
			break;
		}

		dayOfWeek++;
	}
	// update the content of the monthInfo area
	var monthInfoDiv = document.getElementById("monthInfo");
	monthInfoDiv.innerHTML = monthNames[month] + "&nbsp;" + year;
}

function hiliteToday() {

	var hilite = document.getElementById("todayHilite");
		
	if ((currentMonth == date.getMonth()) && (currentYear == date.getFullYear())) {
		var c = document.getElementById("calendar");
		var today = document.getElementById(idFromMonthDate(currentMonth, date.getDate()));
		today.setAttribute("class", dayClassFromDayIndex(getDayFromDate(date)) + " today");

		var todaysDate = document.getElementById(idFromMonthDate(currentMonth, date.getDate())).innerHTML;
		var todaysDiv = document.getElementById(idFromMonthDate(currentMonth, date.getDate()));
		todaysDiv.innerHTML = '<font color=#ff0000>'+todaysDate+'</font>';

		//hilite.style.top = (today.offsetTop+c.offsetTop)-1+"px";
		//hilite.style.left = today.offsetLeft+0+"px";
		//hilite.style.visibility = "visible";
	}
	else {
		//hilite.style.visibility = "hidden";
	}
	
}

function goToday() {
	date = new Date();
	currentMonth = date.getMonth();
	currentYear = date.getFullYear();
	setupMonth(currentMonth, currentYear);
	hiliteToday();
}

function changeMonth(moveNext) {
	if (moveNext) {
		currentMonth++;
		if (currentMonth > 11) {
			currentYear++;
			currentMonth = 0;
		}
	} else {
		currentMonth--;
		if (currentMonth < 0) {
			currentYear--;
			currentMonth = 11;
		}
	}
	// create the new calendar
	setupMonth(currentMonth, currentYear);
	
	// set date selections as necessary
	hiliteToday();
}

function displayInitialCalendar() {
	setupMonth(currentMonth, currentYear);
	// set date selections as necessary
	hiliteToday();
}
