// Calendar
// Copyright 2004 Creature Works Labs
// Author Dale Hughes

// This file builds the actual calendar by writing into the divs created by a calling page.
// All the usefullness has been taken out of this version. There are no event reading utilities.

//******************** Some Globals ***************************
// Start off with the first calendar being active
var activeCalendar = 1;

// The RealXXXX variables never change.
var Now = new Date();
var RealYear = Now.getYear();
if (RealYear < 1000) {
	RealYear += 1900;
}
var RealMonth = Now.getMonth();
var RealDate = Now.getDate();
Now = null;
// These curXXXXX are changed at will to draw different days, months, years.
// Start off with the current date (if the variables were not set before this file was read.)
if (typeof(curYear) == "undefined") {
	var curYear = RealYear;
}
if (typeof(curMonth) == "undefined") {
	var curMonth = RealMonth;
}
if (typeof(curDate) == "undefined") {
	var curDate = RealDate;
}
//***********************************************************

//******************** Start some calendar building utilities **********

function leapYear(year) {
        if (year % 4 == 0) { // basic rule
                return true; // is leap year
	}
        else {
                return false; // is not leap year
	}
}

function getDays(month, year) {
        // create an array to hold number of days in each month
        var ar = new Array(12);
        ar[0] = 31; // January
        ar[1] = (leapYear(year)) ? 29 : 28; // February
        ar[2] = 31; // March
        ar[3] = 30; // April
        ar[4] = 31; // May
        ar[5] = 30; // June
        ar[6] = 31; // July
        ar[7] = 31; // August
        ar[8] = 30; // September
        ar[9] = 31; // October
        ar[10] = 30; // November
        ar[11] = 31; // December

        // return number of days in the specified month (parameter)
        return ar[month];
}

function getMonthName(month) {
        // create array to hold name of each month
        var ar = new Array(12);
        ar[0] = "January";
        ar[1] = "February";
        ar[2] = "March";
        ar[3] = "April";
        ar[4] = "May";
        ar[5] = "June";
        ar[6] = "July";
        ar[7] = "August";
        ar[8] = "September";
        ar[9] = "October";
        ar[10] = "November";
        ar[11] = "December";

        // return name of specified month (parameter)
        return ar[month];
}
function getDayName(date) {
        var dayInstance = new Date(curYear, curMonth, date);
        var thisDay = dayInstance.getDay();
        dayInstance = null;
        // Array of abbreviated day names
        var dayNames = new Array(7);
        dayNames[0] = "Sunday";
        dayNames[1] = "Monday";
        dayNames[2] = "Tuesday";
        dayNames[3] = "Wednesday";
        dayNames[4] = "Thursday";
        dayNames[5] = "Friday";
        dayNames[6] = "Saturday";
	return dayNames[thisDay];
}

//******************** End calendar building utilities **********


// *************** Draw the calendar *******************

function drawCal(divId,whichCalendar) {

	// Set some local variables to the global values
	var date=curDate;
	var month=curMonth;
	var year=curYear;

	// Get the name of the month
        var monthName = getMonthName(month)

        // Find out what day of the week the 1st is on.
        var firstDayInstance = new Date(year, month, 1)
        var firstDay = firstDayInstance.getDay()
	firstDay += 1;
        firstDayInstance = null

        // number of days in current month
        var lastDate = getDays(month, year)

	// We should have every thing we need to know to build a calendar

        // build a calendar 
        var text = ""
        text += "<center>"
        text += "<table width=100% height=100% border=0 cellpadding=0 cellspacing=1>"
	text +=     "<tr>"
        text +=         "<td bgcolor='#ffffff' colspan=8 height='20' align='center'>"
        text +=             "<a href='#' class='monthArrows' onclick='changeMonth(-1);buildCalendar();'><<</a>"
        text +=                 "<span class='calendarMonthText'>&nbsp;&nbsp;&nbsp;"
        text +=                         monthName + " " + year 
        text +=                 "&nbsp;&nbsp;&nbsp;</span>"
        text +=             "<a href='#' class='monthArrows' onclick='changeMonth(1);buildCalendar();'>>></a>"
        text +=         "</td>"
	text +=     "</tr>"

        // Array of abbreviated day names
        var weekDay = new Array(7)
        weekDay[0] = "Su"
        weekDay[1] = "Mo"
        weekDay[2] = "Tu"
        weekDay[3] = "We"
        weekDay[4] = "Th"
        weekDay[5] = "Fr"
        weekDay[6] = "Sa"

        // Do the names of the days of the week
        text += "<tr align='center' valign='center'>"
        text += "<td width='2%' height='16' class='dayNames'>&nbsp;</td>"
        for (var dayNum = 0; dayNum < 7; ++dayNum) {
                  //text += "<td bgcolor='#efefef' width='14%' height='25' class='dayNames'>" + weekDay[dayNum] + "</td>"
                  text += "<td width='14%' height='16' class='dayNames'>" + weekDay[dayNum] + "</td>"
        }
        text += "</tr>"

        // declaration and initialization of two variables to help with tables
        var digit = 1
        var curCell = 1

	var classDIVtype ='';
	var classTDtype ='';
	// Make the day boxes with the numbers in them.
        for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
                text += '<TR ALIGN="right" VALIGN="top">'
        	text += "<td width='2%'  class='dayNames'>></td>"
                for (var col = 1; col <= 7; ++col) {
                        if (digit > lastDate)
                                break
                        if (curCell < firstDay) {
                                text += '<TD></TD>';
                                curCell++
                        } else {
                                if (curYear == RealYear && curMonth == RealMonth && digit == RealDate) { 
				    // current cell represent today's date
				    classDIVtype = "class='calCellDivToday'";
				    classTDtype = "class='calCellTDToday'";
				}
                                else if (digit == curDate && whichCalendar == activeCalendar) { 
				    // current cell represent date we are displaying
				    classDIVtype = "class='calCellDivDisplay'";
				    classTDtype = "class='calCellTDDisplay'";
				}
				else {
				    classDIVtype = "class='calCellDiv'";
				    classTDtype = "class='calCellTD'";
				}
			        var timevars;
				text += "<TD "+classTDtype+">";
				text += "<img align='left' src='../../images/spacer.gif' width=1 height=14>";
				text += "<div "+classDIVtype+" >";

				text += digit+"<br></div></TD>";
                                digit++
                        }
                }
                text += '</TR>'
        }

        text += "</table>"
        text += "</center>"

	divId.innerHTML = text;
}
function changeMonth(howmuch) {
	curMonth += howmuch;
	if (curMonth > 11) {
		curMonth -= 12;
		curYear += 1;
	}
	else if (curMonth < 0) {
		curMonth += 12;
		curYear -= 1;
	}
}
function changeDay(howmuch) {
	curDate += howmuch;
	if (curDate > getDays(curMonth,curYear)) {
		curDate = 1;
		changeMonth(1);
	}
	else if (curDate < 1) {
		changeMonth(-1);
		curDate = getDays(curMonth,curYear);
	}
}
//*************** end of calendar *********

function buildCalendar() {
        var calendarId;
	//buildHeader();
	calendarId = document.getElementById("calendar1");
	drawCal(calendarId,1); // The 2nd arg tells which calendar to draw.
}


