var theDate = new Date();
// Months range from 0-11
var currentMonth = theDate.getMonth();
// All four digits of the year
var currentYear = theDate.getFullYear();

function init() {
    makeCalendar('', '');
}

function selectCurrentDay() {
    document.getElementById(theDate.getDate).style = "font-weight:bold";
}

function previousMonth() {
    currentMonth = currentMonth - 1;
    //Check if the current month is less than 0.  If it is, set it to last year, month 11.
    if (currentMonth < 0) {
        currentMonth = 11;
        currentYear = currentYear - 1;
    }
    makeCalendar(currentMonth, currentYear);
}

function nextMonth() {
    currentMonth = currentMonth + 1;
    //Check if the current month is greater than 11.  If it is, set it to next year, month 0.
    if (currentMonth > 11) {
        currentMonth = 0;
        currentYear = currentYear + 1;
    }
    makeCalendar(currentMonth, currentYear);
}


function makeCalendar(aMonth, aYear) {
    if (aMonth == "" || aYear == "") {
        aMonth = currentMonth;
        aYear = currentYear;
    }
    var numDays = new Date(aYear, aMonth + 1, 0).getDate();
    var monthArray = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    var daysArray = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
    theHTML = "<table border=\"0\" class=\"calendar\" cellpadding=\"0\" cellspacing=\"0\"><tr><td class=\"prevArrow\"><a href=\"javascript:previousMonth()\">&lt;&lt;</a></td><td colspan=\"5\" class=\"monthHeader\">" + monthArray[aMonth] + " " + aYear + "</td><td class=\"nextArrow\"><a href=\"javascript:nextMonth()\">&gt;&gt;</a></td></tr>";
    for (var i = 0; i < 7; i++) {
	theHTML += "<td class=\"daysHeader\">" + daysArray[i] + "</td>";
    }
    theHTML += "</tr><tr>";
    for (var j = 1; j <= numDays; j++) {
        theDate.setDate(j);
        var aDay = theDate.getDay();
        if (j == 1) {
            for (var k = 0; k < aDay; k++) {
                theHTML += "<td>&nbsp;</td>";
            }
        }
        if (aDay == 0 && j != 1) {
            theHTML += "<tr>";
        }
        theHTML += "<td class=\"days\"><a href=\"#\">" + j + "</a></td>";
	if (aDay == 6 || j == numDays) {
            theHTML += "</tr>";
        }
    }
    theHTML += "</table>";
    var theCalendarItem = document.getElementById("Calendar");
    theCalendarItem.innerHTML = theHTML;
    return false;
}
