/* DHTML_Calendar.js $Revision: 1.1.1.1 $ ($Date: 2005/05/06 07:32:52 $)
* Copyright 2005 Eric Pretorious (eric@pretorious.net)
* Distributed under the terms of the GNU General Public License (http://www.gnu.org/copyleft/gpl.html)
*/

var now;
var currYear,currMonth;
var i,j,currDateObj,firstDay,daysInMonth,sevenDayRows;;
var DHTMLC,prev,curr,next,moniker;
var months	= new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var days	= new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

/*
1. Set currMonth & currYear to now.
2. CBE will automatically call windowOnLoad() which, in turn, will...
 a. Create the CrossBrowserElement objects.
 b. Call Calendar() which will...
  i. Calculate the calendar info: firstDay, daysInMonth, sevenDayRows.
  ii. Determine nextMonth, nextYear, prevMonth, & prevYear.
  iii. Call cbeTraverseTree() with a reference to drawCalendar() which will...
   * Populate the innerHTML for each object in the CBE Object Tree.
*/

// Use the present date to set the global variables (used to generate the calender)
now		= new Date();
currYear	= now.getYear();
currMonth	= now.getMonth();

// Some browsers may not be Y2K compliant...
if (currYear < 1900) currYear += 1900;

function Calendar(Month,Year) {
	currDateObj	= new Date(Year,Month,1);

	// Update the global variables (used to generate the calender)
	currMonth	= Month;
	currYear	= Year;
	
	// Perform the basic calculations necessary to generate a calendar
	firstDay	= currDateObj.getDay();	// The day-of-the-week of the first day of the month;
	daysInMonth	= 32 - new Date(Year,Month,32).getDate();
	sevenDayRows	= Math.ceil((firstDay + daysInMonth) / 7);
	
	switch (Month) {
	case 0:
		nextMonth	= Month + 1;
		nextYear	= Year;
		prevMonth	= 11;
		prevYear	= Year - 1;
		break;
	
	case 11:
		nextMonth	= 0;
		nextYear	= Year + 1;
		prevMonth	= Month - 1;
		prevYear	= Year;
		break;
	
	default:
		nextMonth	= Month + 1;
		nextYear	= Year;
		prevMonth	= Month - 1;
		prevYear	= Year;
		break;
	}
	
	i = 0; j = 1;
	cbeTraverseTree('preorder',DHTMLC,drawCalendar);

	DHTMLC.resizeTo(230, sevenDayRows * 20 + 60);
	moniker.top(sevenDayRows * 20 + 50);
	prev.innerHtml('<A HREF="javascript:Calendar(prevMonth,prevYear);">' + months[prevMonth].substr(0,3) + '</A>');
	curr.innerHtml(months[Month] + ', ' + Year);
	next.innerHtml('<A HREF="javascript:Calendar(nextMonth,nextYear);">' + months[nextMonth].substr(0,3) + '</A>');
}

function drawCalendar(node,level,branch) {
	var HREF;
	if (level == 2 && branch > 2) {
		if (i >= firstDay && j <= daysInMonth) {
			HREF = 'javascript:void(document.forms[0].elements[0].value = \'' + (currMonth + 1) + '/' + j + '/' + currYear +'\')';
			node.innerHtml('<A HREF="' + HREF + '">' + j++ + '</A>');
			node.innerHtml();node.show();
		} else {
			node.hide();
		}
		i++;
	}
	return true;
}

function windowOnload() {
	DHTMLC	= cbeGetElementById('DHTMLC').cbe;
	prev	= cbeGetElementById('prev').cbe;
	curr	= cbeGetElementById('curr').cbe;
	next	= cbeGetElementById('next').cbe;
	moniker	= cbeGetElementById('moniker').cbe;

	Calendar(currMonth,currYear);
	DHTMLC.clip('auto');
	DHTMLC.show();
	DHTMLC.addEventListener('drag');
}
