

//  Calendar.js  JavaScript function by Scriptus Web Design
// ª 2009 Scriptus Web Design:  www.scriptus.co.nz 


// Inserts a calendar with todays date highlighted & the day written out underneath

 function calendar(date) {

// If no parameter is passed use the current date.

    if(date == null)
    date = new Date();

    day = date.getDate();
    month = date.getMonth();
    year = date.getFullYear();
    
    months = new Array('January','February','March','April','May','June','July','August','September','October','November','December'); 

    dayNames = new Array('Saturday','Sunday','Monday','Tuesday','Wednesday','Thursday','Friday');

    this_month = new Date(year, month, 1);
    next_month = new Date(year, month + 1, 1);

// Find out when this month starts and ends.

    first_week_day = this_month.getDay();

    days_in_this_month = Math.round((next_month.getTime() - this_month.getTime()) / (1000 * 60 * 60 * 24));

    calendar_html = '<table style="color:#559; width:220px">';

    calendar_html += '<tr ><td colspan="7" style="border: 1px solid #A8E0FF;  color:#000; text-align: center; height:30px;"><h2>' + months[month] + ' ' + year + '</h2></></>';

    calendar_html += '<tr style="color:#559; text-align: center;">'

// fill in the day names header row

    calendar_html += '<td style="width:30px;">Su</><td style="width:30px;">Mo</><td style="width:30px;">Tu</><td style="width:30px;">We</><td style="width:30px;">Th</><td style="width:30px;">Fr</><td style="width:30px;">Sa</></>';

    calendar_html += '<tr>';

// Fill the first week of the month with the appropriate number of blanks.

    for(week_day = 0; week_day < first_week_day; week_day++) {

        calendar_html += '<td style="color:#559;"> </>';

    }

    week_day = first_week_day;

    for(day_counter = 1; day_counter <= days_in_this_month; day_counter++) 
    {
        week_day %= 7;

        if(week_day == 0)

            calendar_html += '</><tr>';

// Format today differently from the rest.

        if(day == day_counter)

            calendar_html += '<td style="text-align: center; border:1px solid #A8E0FF; color: #0000BF;" title="todays date"><b>' + day_counter + '</></>';

        else

            calendar_html += '<td style="color: #559; text-align: center;"> ' + day_counter + ' </>';

        week_day++;
    }


    calendar_html += '</></table>';

// Write the Calendar to the parent document.

    document.write(calendar_html);

}       // End function

