
// Calendar.js // Gene Dumas 01012001
// Main function: openCalendar(monFLDNam, dayFld, formNum)

function daysInFeb(year) {
return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29:28);
}

function makeArray(n) {
this.length = n;
return this;
}

function myGetYear(theDate) {

var iYear, sYear;
// getYear returns the year in (year - 1900), this causes ugly display.
// The comparison with 3 below is for browsers returning
// 100 for year after Dec 99. iYear is returned as 4 digits.
iYear = theDate.getYear();
sYear = new String(iYear); 
if ( sYear.length == 3 ) iYear = iYear + 1900;
return iYear;

}

//
// Global declarations and initialization.
//
var msPERDAY = 24*60*60*1000;
var currYEAR,currMONTH,dispMONTH,currDAY,firstOfMONTH;
var monFLD,dayFLD,yrFLD,frmNUM,htmlBUFFER;
var lFillFORM = true;
var monthNAME = new Array( // Array for drawCalendar().
	"January","February","March","April","May","June",
	"July","August","September","October","November","December");
var calendarWINDOW = new Object;

calendarWINDOW.closed = true;


function init(monFld,dayFld,yrFld,formNum) {

var cookieString,startOfString,countbegin,countend,iYear,iMonth;
var startDate = new Date();

//if (formNum > 1900)
//	iYear = parseInt(formNum);
if (yrFld > 1900) { // Use passed date.
	iYear = parseInt(yrFld);
	iMonth = parseInt(monFld);
	iDay = parseInt(dayFld);
	startDate = new Date(iYear,--iMonth,iDay);
	currDAY = dayFld;
	lFillFORM = false; 
}
else { // Use current date.
	monFLD = monFld;
	dayFLD = dayFld;
	yrFLD = yrFld;
	currDAY = 1; // startDate.getDate();
	startDate = new Date(myGetYear(startDate),startDate.getMonth(),currDAY);
	frmNUM = formNum;
}
currMONTH = startDate.getMonth()+ 1;
currYEAR = myGetYear(startDate);
dispMONTH = currMONTH;
//cookieString = "dispMONTH=" + currMONTH + ";";
//document.cookie = cookieString;
//startOfString = document.cookie.indexOf("dispMONTH");
//countbegin = document.cookie.indexOf("=",startOfString) + 1;
//countend = document.cookie.indexOf(";",countbegin);
//if (countend == -1) countend = document.cookie.length;
//dispMONTH = eval("document.cookie.substring(countbegin,countend)");
firstOfMONTH = new Date(currYEAR,dispMONTH-1,1);

return startDate;

}

//
// Fill the calling forms date and month.
//
function fillDate(filler) {

if (lFillFORM) {
var m = dispMONTH - 1; // 1 - 12
var d = filler - 1;	// filler--> 1 - 31, selectedIndex= 0-30
var y = currYEAR;

currDAY = d + 1;
//alert("frmNUM="+frmNUM+"\nmonFLD="+monFLD+"\ndayFLD="+dayFLD+"\nday="+d);

eval("document.forms[" + frmNUM + "]." + monFLD + ".selectedIndex=" + m);
eval("document.forms[" + frmNUM + "]." + dayFLD + ".selectedIndex=" + d);
eval("document.forms[" + frmNUM + "]." + yrFLD + ".value=" +  y);
}

calendarWINDOW.close();

}

//
// Reset the startdate to get the previous sunday before the
// current date, so that the drawing can begin from a sunday.
//
function firstSunday(fromDate) {

var newTime;
while (fromDate.getDay() != 0) {
	newTime = fromDate.getTime() - msPERDAY;
	fromDate.setTime(newTime);
}
return fromDate;

}

//
// Generate the string for calendar body.
//
function drawBody(theDate) {

var w,d,thisMonth,iDay,newTime;

thisMonth = theDate.getMonth();
theDate = firstSunday(theDate);

for (w=0; w<6; w++) {
	htmlBUFFER += "<TR>";
	for (d=0; d<7; d++) {
		iDay = theDate.getDate();
		htmlBUFFER += "<TD ALIGN=CENTER>";
		// Skip previous month
		if (theDate.getMonth() != thisMonth) {
			htmlBUFFER += "<BR>";
		} else {
			htmlBUFFER += "<INPUT TYPE=BUTTON NAME=";
			htmlBUFFER += iDay + " VALUE=";
			if ( iDay < 10 ) htmlBUFFER += "0";
			htmlBUFFER += iDay;
			htmlBUFFER += " onClick=callingForm.fillDate(" + iDay + ")>";
		}
		// Increment the date
		newTime = theDate.getTime() + msPERDAY;
		theDate.setTime(newTime);
		// check for DST 
		if (theDate.getHours() != 23) theDate.setTime(newTime + 3600000);
	} // End for days
	htmlBUFFER += "</TR>";
} // End for weeks
}

//
// Generate string for calendar document.
//
function drawCalendar(theDate) {

var iYear,monthNum;

monthNum = theDate.getMonth();
iYear = myGetYear(theDate);
htmlBUFFER = "<HTML><HEAD><TITLE>Calendar</TITLE></HEAD>";
// htmlBUFFER += "<BODY BGCOLOR=#FFFFFF><FORM NAME=\"callingForm\">";
htmlBUFFER += "<BODY BGCOLOR=#FFFFFF><FORM>";
htmlBUFFER += "<TABLE BORDER=2 WIDTH=50%><TR><TH COLSPAN=2>";
htmlBUFFER += "<INPUT TYPE=BUTTON NAME=monthDn VALUE=\"<<\" onClick=callingForm.changeMonth(-1)>";
htmlBUFFER += "<TH COLSPAN=3> " + monthNAME[monthNum];
htmlBUFFER += " " + iYear + "<TH COLSPAN=2>";
htmlBUFFER += "<INPUT TYPE=BUTTON NAME=monthUp VALUE=\">>\" onClick=callingForm.changeMonth(1)></TR>";
htmlBUFFER += "<TR><TH>S<TH>M<TH>T<TH>W<TH>T<TH>F<TH>S</TR>";

drawBody(theDate);

htmlBUFFER += "</FORM></TABLE></BODY></HTML>";

}

//
// Repaint the calendar
//
function reDraw(theDate) {

calendarWINDOW.callingForm = this;
calendarWINDOW.document.open();
drawCalendar(theDate);
calendarWINDOW.document.write(htmlBUFFER);
calendarWINDOW.document.close();
calendarWINDOW.callingForm = this;
calendarWINDOW.focus();

}

//
// Set the month.
//
function changeMonth(increment) {

var nextMonth = dispMONTH;
var newDate;

if (increment == 1) {
	nextMonth++;
	if (nextMonth == 13) {
		nextMonth = 1;
		currYEAR++;
	}
} else {
	nextMonth-- ;
	if (nextMonth == 0) {
		nextMonth = 12;
		currYEAR--;
	}
}
// Limit to current month and out to 1 year.
//if ((nextMonth-currMONTH >= 13) || (nextMonth < currMONTH)) {
//	nextMonth = currMONTH;
//}

dispMONTH = nextMonth;
newDate = new Date(currYEAR, dispMONTH-1, 1);
//document.cookie="dispMONTH=" + nextMonth;
reDraw(newDate);

}

//
// Main Entry Point
//
function openCalendar(monFLDNam, dayFld, yrFld, formNum) {

var startDate,windowOptions;

startDate = init(monFLDNam, dayFld, yrFld, formNum);
windowOptions = "toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=yes,width=221,height=290,top=20,left=100";
if (!calendarWINDOW.closed) calendarWINDOW.close();
calendarWINDOW = this.open("","calendarWINDOW",windowOptions);
//calendarWINDOW = window.open("","calendarWINDOW",windowOptions); // Same as line above.
calendarWINDOW.callingForm = this;
reDraw(startDate);

return new Date(currYEAR,dispMONTH-1,currDAY);
}
// Eof: calendar.js // No right way to do a wrong thing.//
