/*************************************
 * JavaScript "Perpetual Calendar"
 * Copyright 2005, Louis W. G. Barton
 * Re-uses an algorithm attributed to
 * Constantin Kuznetsov, Jr.
 **************************************/

// Dependencies:
// - Requires var ie, ns6, and ns to be already defined.
// - Requires stylesheet "left_menu.css".


// Browser version was set in left_menu.js:
if (ie||ns6) {fShow='visible'; fHide='hidden';}
if (ns) {fShow='show'; fHide='hide';}

var currentDate=new Date();
var currentYear=currentDate.getYear();
if (currentYear < 1000) {
	currentYear+=1900;
}
var currentMonth=currentDate.getMonth()+1;
var currentDay=currentDate.getDate();
var nCurrentYear=0;
var nCurrentMonth=0;
var dWidth=16;
var dHeight=12;

var MonthNames;
var dayLetters;

// Set up the calendar TABLE inline:
// NB: The inclusion of this .js file is position-specific in the HTML document.
function Calendar() {
	document.writeln('<table class="calMain" cellspacing="0" cellpadding="0">');
	document.writeln('<colgroup><col width="12%"><col width="57%"><col width="4%"><col ' +
			'width="27%"><\/colgroup>');
	document.writeln('<tr VAlign="bottom">');
	document.write('<td align="center" class="month">');
	document.write('<a HRef="javascript:resetDate();">');
	document.write('<img width="16" height="16" SRC="./images/date_mini.gif" Border="1" ' +
			'alt="Reset">');
	document.writeln('<\/a><\/td>');
	document.write('<td align="right" class="month">');
	if (ie||ns6) {
		document.write('<div id="main2" style="position=relative;">');
	} else if (ns) {
		document.write('<ilayer id="main2"><layer id="idMonth" top="0" left="0">');
	}
	document.write(MonthNames[currentMonth-1]);
	if (ie||ns6) {
		document.writeln('<\/div>');
	} else if (ns) {
		document.writeln('<\/layer><\/ilayer>');
	}
	document.writeln('<\/td>');
	document.writeln('<td class="month">&nbsp;<\/td>');
	document.write('<td align="left" class="month">');
	if (ie||ns6) {
		document.write('<div id="main" style="position: relative;">');
	} else if (ns) {
		document.write('<ilayer id="main"><layer id="idYear" top="0" left="0">');
	}
	document.write(currentYear);
	if (ie||ns6) {
		document.writeln('<\/div>');
	} else if (ns) {
		document.writeln('<\/layer><\/ilayer>');
	}
	document.writeln('<\/td><\/tr>');

	document.write('<tr style="background-color: #D3D3D3;" height="92px" align="center">');
	document.writeln('<td colspan="4" valign="top">');

	// Inner TABLE:
	document.write('<table style="width: 153px; border: none;" cols="7" cellpadding="2" ' +
			'cellspacing="0">');
	document.writeln('<tr style="background-color: #808080;">');
	for(i=0; i<7; i++) {
		document.write('<td class="daysofweek">' + dayLetters[i] + '<\/td>');
	}
	document.writeln('<\/tr>');
	document.writeln('<tr><td class="days" bgcolor="#D3D3D3" colspan="7">');
	if (ie||ns6) {
		document.write('<div style="position: relative;">');
	} else if (ns) {
		document.write('<ilayer id="idMenuContainer" width="153px" height="77px">');
	}
	for (var date=1; date <= 31; date++) {
		if (ie||ns6) {
			document.write('<div id=\"idDate' + date + '\" val=' + date +
				' style="position: absolute; visibility: hidden;">');
			document.write(date + '<\/div>');
		} else if (ns) {
			document.write('<layer id=\"idDate' + date + '\" val=' + date +
				' visibility="hide">');
			document.write('<layer>' + date + '<\/layer><\/layer>');
		}
	}  // end, for date
	if (ie||ns6) {
		document.writeln('<\/div>');
	} else if (ns) {
		document.writeln('<\/ilayer>');
	}
	document.write('<\/td><\/tr>');
	document.writeln('<\/table>');
	//end, Inner TABLE

	document.write('<\/td><\/tr>');
	document.writeln('<\/table>');
}  // end, Calendar()


function showYearMonth() {
	var date = new Date(nCurrentYear, nCurrentMonth-1, 1);
	var nWeek = 1;  // new date, week number
	var nDate;  // new date, day number

	if (ie||ns6) {
		var cross_obj = ns6? document.getElementById('main') : document.all['main'];
		var cross_obj2 = ns6? document.getElementById('main2') : document.all['main2'];
		cross_obj.innerHTML = nCurrentYear;
		cross_obj2.innerHTML = MonthNames[nCurrentMonth-1];
		while (date.getMonth() == nCurrentMonth-1) {
			nDate = date.getDate();
			var posDay = date.getDay();
			var posLeft = posDay*(dWidth+5)+5;
			var posTop  = (nWeek-1)*dHeight;
			var cross_obj3 = ns6? document.getElementById('idDate'+nDate).style : document.all['idDate'+nDate].style;
			cross_obj3.left = posLeft;
			cross_obj3.top = posTop;
			if (posDay == 0 || posDay == 6) {
				cross_obj3.color = 'red';
			} else {
				cross_obj3.color = 'black';
			}
			if (nCurrentYear == currentYear && nCurrentMonth == currentMonth && nDate == currentDay) {
				cross_obj3.background = 'yellow';
			} else {
				cross_obj3.background = '#D3D3D3';
			}
			cross_obj3.visibility = 'visible';
			date = new Date(nCurrentYear, date.getMonth(), date.getDate()+1);
			if (posDay == 6) nWeek++;
		}  //end, while
		for (++nDate; nDate <= 31; nDate++) {
			cross_obj3 = ns6? document.getElementById('idDate'+nDate).style : document.all['idDate'+nDate].style;
			cross_obj3.visibility = 'hidden';
		}
	//end, if (ie||ns6)
	} else if (ns) {
		document.main.document.idYear.document.open();
		document.main.document.idYear.document.write(nCurrentYear);
		document.main.document.idYear.document.close();

		document.main2.document.idMonth.document.open();
		document.main2.document.idMonth.document.write(MonthNames[nCurrentMonth-1]);
		document.main2.document.idMonth.document.close();

		while (date.getMonth() == nCurrentMonth-1) {
			nDate = date.getDate();
			var posDay = date.getDay();
			var posLeft = posDay*(dWidth+5)+5;
			var posTop  = (nWeek-1)*dHeight;

			document.layers['idMenuContainer'].document.layers['idDate'+nDate].left = posLeft;
			document.layers['idMenuContainer'].document.layers['idDate'+nDate].top = posTop;
			if (posDay == 0 || posDay == 6) {
				document.layers['idMenuContainer'].document.layers['idDate'+nDate].color = 'red';
			} else {
				document.layers['idMenuContainer'].document.layers['idDate'+nDate].color = 'black';
			}
			if (nCurrentYear == currentYear && nCurrentMonth == currentMonth && nDate == currentDay) {
				document.layers['idMenuContainer'].document.layers['idDate'+nDate].background = 'yellow';
			} else {
				document.layers['idMenuContainer'].document.layers['idDate'+nDate].background = '#D3D3D3';
			}
			document.layers['idMenuContainer'].document.layers['idDate'+nDate].visibility = 'visible';
			date = new Date(nCurrentYear, date.getMonth(), date.getDate()+1);
			if (posDay == 6) nWeek++;
		}  //end, while
		for (++nDate; nDate <= 31; nDate++) {
			document.layers['idMenuContainer'].document.layers['idDate'+nDate].visibility = 'hidden';
		}
	}  //end, if (ns)
}  //end, showYearMonth()

function nextMonth() {
	nCurrentMonth++;
	if (nCurrentMonth > 12) {
		nCurrentMonth -= 12;
		nextYear();
	} else {
		showYearMonth();
	}
}

function prevMonth() {
	nCurrentMonth--;
	if (nCurrentMonth < 1) {
		nCurrentMonth += 12;
		prevYear();
	} else {
		showYearMonth();
	}
}

function nextYear() {
	nCurrentYear++;
	showYearMonth();
}

function prevYear() {
	nCurrentYear--;
	showYearMonth();
}

function resetDate() {
	currentDate = new Date();
	currentYear = currentDate.getYear();
	if (currentYear < 1000) {
		currentYear += 1900;
	}
	currentMonth = currentDate.getMonth()+1;
	currentDay = currentDate.getDate();
	nCurrentYear = currentYear;
	nCurrentMonth = currentMonth;
	showYearMonth();
}
