// eventcal.js // Gene Dumas 07/09/2001, ver 1.0 07/26/2001

// --- Functions for eventcal.htm or calstd5.htm -- //
// Global vars defined in eventlst.js:
// sCALTITLE, sTITLEBGCOLOR, sCALBGCOLOR, sCALFONT
// iEVENTS num of aEVENTS[1-4] arrays. Used by getevents();

function myGetYear(theDate) {

var iYear, sYear;
// getYear() returns the year in (year - 1900).
// 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;
}


function findElem(aElem,sElem) {

var iElem = -1;
var i, k;
//if (sElem==102) alert("aElem="+aElem+"\nElem="+sElem);

for ( i=0; i < aElem.length; i++ ) {
//	k = aElem[i].indexOf(sElem);
//	if ( k > -1 ) iElem = i;
	if ( aElem[i] == sElem ) {
		iElem = i;
		i = aElem.length;
	}
}
return iElem;
}


function createDate(iYear,sMDW) {

var numWeeks, DayCounter, WeekCounter, DayOfWeekCounter, theDay, iDoW;
var sMon, iMon, sDay, iTheDoW, cWeek, iWeek, iBeginDoW, theDate;
var newTime, iLastDoM;
var aMonthNames = new Array(
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var aDays = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");

// sMDW = "Mon-DoW #1|2|3|4|L][,year][:color][;text]"
//	     01234567890 // Apr-Sun #2;
sMon = sMDW.substring(0,3);
iMon = findElem(aMonthNames,sMon);
sDay = sMDW.substring(4,7);
iTheDoW = findElem(aDays,sDay);
cWeek = sMDW.substring(9,10);
theDate = new Date(iYear, iMon, 1); // 1st dayof mon.
iBeginDoW = theDate.getDay(); // get Day of the week the mon begins on;
//alert("yy="+iYear+"\nmm="+iMon+"\nDate="+theDate+"\niBeginDoW="+iBeginDoW);

theDate = new Date(iYear,iMon+1,1); // 1st dayof next mon.
newTime = theDate.getTime() - msPERDAY; // Last dayof mon.
theDate.setTime(newTime);
iLastDoM = theDate.getDate(); // Last dayof mon.
theDay = 0;
numWeeks = 5;	// Determine number of weeks for the month.
if (((iBeginDoW == 5) && (iLastDoM == 31)) ||
	((iBeginDoW == 6) && (iLastDoM > 29)))
	numWeeks = 6;

if (cWeek == "L") { iWeek = 31; //numWeeks;
}else iWeek = parseInt(cWeek);

DayCounter = 1;
WeekCounter = 1;
DayOfWeekCounter = iBeginDoW;
iDoW = 0;

while ( DayCounter < (iLastDoM + 1) ) {

  while ( DayOfWeekCounter < 7 ) {
	if (DayOfWeekCounter == iTheDoW) {
		iDoW++;
		if ( (iDoW == iWeek)||((cWeek=="L")&&(numWeeks == WeekCounter)) ) {
//		if ( (iDoW == iWeek)||(numWeeks == WeekCounter) ) {
			theDay = DayCounter;	// We got it.
			DayOfWeekCounter = 8;	// Now exit.
			DayCounter = iLastDoM;
		}
	}
	DayOfWeekCounter++;
	DayCounter++;
  } // end while DayOfWeekCounter

	DayOfWeekCounter = 0;
	WeekCounter++;

} // end while DayCounter != last day of mon.

if (cWeek=="L") { // if numWeeks = 6 theDay is missed by 7;
 //	alert(sMDW+"\nYear="+iYear+"\nMon="+iMon+"\nDay="+iTheDoW
// +"\niWeek="+iWeek+
//	"\niDoW="+iDoW+"\niBeginDoW="+iBeginDoW+"\nnumWeeks="
// +numWeeks+"\ntheDay="+theDay);
	if (theDay > iLastDoM) theDay = theDay - 7;
}// endif cWeek=="L"

return theDay;
}

function getEaster(theYear) {

var iDay=0, iMon;
var v1 = 0, v2 = 0, v3 = 0, v4 = 0, v5 = 0, v6 = 0, v7 = 0;
var easterDay;

if ((theYear > 1699) || (theYear < 1800)) {
	v6 = 23;
	v7 = 4;
}
if ((theYear > 1799) && (theYear < 1900)) {
	v6 = 23;
	v7 = 4;
}
if ((theYear > 1899) && (theYear < 2100)) {
	v6 = 24;
	v7 = 5;
}
if ((theYear > 2099) && (theYear < 2200)) {
	v6 = 24;
	v7 = 6;
}

if((theYear > 2899 ) || (theYear < 1800))
	alert("Easter Day may be wrong for the year " + theYear + "!");

v1 = theYear % 19;
v2 = theYear % 4;
v3 = theYear % 7;
v4 = ((19 * v1) + v6) % 30;
v5 = ((2 * v2)+(4 * v3)+(6 * v4) + v7) % 7;
iDay = parseInt(22 + v4 + v5);
iMon = 3;
if (iDay > 31) {
	iDay = (v4 + v5 - 9);
	iMon = 4;
	if (iDay > 24) {
		if (iDay == 26) iDay = 19;
		if ((iDay == 25) && (v4 == 28) && (v1 > 10)) iDay = 18;
	}
}

easterDay = new Date(theYear,iMon-1,iDay);

return easterDay;

}


function parseArray(aParse,iYear) { // Main 

var iPos1, iPos2, iPos3, iPos4, iPos5, sMon, iMon, iDay;
var iTxtLen, bgColor, theYear, easterDate;
var aMonthNames = new Array(
 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var aParsed = new Array();
var j = 0;

// aParse[i]--> "=Mon [DoM|DoW #1|2|3|4|L][,year][:color][;text]"

for ( var i=0; i < aParse.length; i++ ) {
	iPos1 = aParse[i].indexOf("=");	// Valid line
	iPos2 = aParse[i].indexOf("-");	// Contains month, day & week #
	iPos3 = aParse[i].indexOf(",");	// Year
	iPos4 = aParse[i].indexOf(":");	// BGColor string
	iPos5 = aParse[i].indexOf(";");	// Begin day text label
	sMon = "";
	iMon = -1;
	iDay = -1;
	theYear = iYear;
	bgColor = "";
	sText = "";
	iTxtLen = aParse[i].length;
	if (iPos1 > -1) { // Valid line.
		sMon = aParse[i].substring(iPos1+1,iPos1+4);
		if ((iPos3 > 1) && (iPos3 < iPos5))
			theYear = aParse[i].substring(iPos3+2,iPos3+6);
		if (sMon == "EAS") {
			easterDate = getEaster(theYear);
			iMon = easterDate.getMonth();
			iDay = easterDate.getDate();
		}
		else {
			iMon = findElem(aMonthNames,sMon);
			iDay = aParse[i].substring(iPos1+5,iPos1+7);
		}
		if (iPos5 > 0) sText = aParse[i].substring(iPos5+1,iTxtLen);
		if ((iPos4 > 0) && (iPos4 < iPos5))
			bgColor = aParse[i].substring(iPos4+1,iPos5);
		// Find day of month using week #.
		if ((iPos2 > 1) && (iPos2 < iPos5))
		  iDay = createDate(theYear,aParse[i].substring(iPos1+1,iPos5+1));

//alert(aParse[i]+"\nsM="+sMon +"\nM=" +iMon +"\nD="+ iDay+"\nY="+ theYear
// +"\nC="+ bgColor +"\nT="+ sText);

	} // endif (iPos1 > -1)

	if (iMon > -1) {
	 aParsed[j] = new Array(5);
	 aParsed[j][0] = theYear;
	 aParsed[j][1] = iMon;
	 aParsed[j][2] = iDay;
	 aParsed[j][3] = bgColor;
	 aParsed[j++][4] = sText;
	}
} //endfor

return aParsed;
}

function GetEvents(iYear,iMonth,numMonths) {

var aEvent = new Array();

if (iEVENTS > 0) aEvent = parseArray(aEVENTS1, iYear);
if (iEVENTS > 1) aEvent = aEvent.concat(parseArray(aEVENTS2, iYear));
if (iEVENTS > 2) aEvent = aEvent.concat(parseArray(aEVENTS3, iYear));
if (iEVENTS > 3) aEvent = aEvent.concat(parseArray(aEVENTS4, iYear));

aEvent = aEvent.concat(parseArray(aHOLIDAY, iYear));

while ((11 - iMonth) < numMonths) {
	iYear++;
//alert(11-iMonth+" |iMonth="+iMonth+"\niYear="+iYear+"\nnumMonths="+numMonths);

	aEvent = aEvent.concat(parseArray(aHOLIDAY, iYear));

	numMonths = numMonths - 12;
} //endwhile

return aEvent; 
}

function eventcal(iYear, iMonth, numMonths) { // Main Calendar Procedure.

// Global vars in eventlst.js:
//  sCALTITLE, sTITLEBGCOLOR, sCALBGCOLOR, sCALFONT
var msPERDAY = 24*60*60*1000;
var MonthNAME = new Array("January","February","March","April","May",
"June","July","August","September","October","November","December");
var htmlBuffer,tmpDate,bwdom,DayOfWeekCounter;
var i,k,len,mc,sText;
var nextMon,newTime,iLastDoM,NumWeeks,DayCounter,WeekCounter,thisMonth;
var today = new Date();
var aEvents = new Array();
var sNotes = "", sTitle = "Events Calendar", sTitleBgColor = "";
var thisMonth = 0;
var aIndexes = new Array();
var j = 0;
aIndexes[j] = "-1";

if ((iYear < 1753) || (iYear > 9999)) {
	if (iYear > -1) alert("Invalid Year, using current!");
	iYear = myGetYear(today);
}
if ((iMonth < 0) || (iMonth > 11)) {
	thisMonth = today.getMonth();
}else {
	thisMonth = parseInt(iMonth*10/10); // if iMonth=09 then parseInt=0;
}// endif
iYEAR = iYear;
iMONTH = iMonth;

// alert("iYEAR = "+iYEAR+"\niMONTH = "+ iMONTH + "\niMonth = "+iMonth +
//	 "\nthisMonth = " + thisMonth);

aEvents = GetEvents(iYear,thisMonth,numMonths);
//aEvents.sort();

//document.open();

if (-1) // (aEvents[0][2] == 99)
{
	sTitle = aEvents[0][4];
	if (aEvents[0][3] != "")
	 sTitleBgColor = ' bgcolor="' + aEvents[0][3] + '"';
}

htmlBuffer = '';
htmlBuffer += '<font ' + sCALFONT + '>';
htmlBuffer += '<table align="center" bgcolor="' + sCALBGCOLOR;
htmlBuffer += '" border="1" cellspacing="0" cellpadding="2" width="100%">';
// <!-- bgcolor="#0080FF"  #FFE4C4 -->

htmlBuffer += '<tr><th colspan="7"' + ' bgcolor="' + sTITLEBGCOLOR + '">';
htmlBuffer += sCALTITLE + "</th></tr><tr>";
htmlBuffer += "<td align=\"middle\"><strong>Sun.</strong></td>";
htmlBuffer += "<td align=\"middle\"><strong>Mon.</strong></td>";
htmlBuffer += "<td align=\"middle\"><strong>Tue.</strong></td>";
htmlBuffer += "<td align=\"middle\"><strong>Wed.</strong></td>";
htmlBuffer += "<td align=\"middle\"><strong>Thu.</strong></td>";
htmlBuffer += "<td align=\"middle\"><strong>Fri.</strong></td>";
htmlBuffer += "<td align=\"middle\"><strong>Sat.</strong></td></tr>";

iMonth = 0;
len = aEvents.length;

for (mc = thisMonth; iMonth < numMonths; mc++) {

	if ( mc == 12 ) {
		mc = 0;
		iYear++;
	}
	iMonth++;
	htmlBuffer += '<tr><td align="bottom" colspan="7"';
	htmlBuffer += ' bgcolor="' + sTITLEBGCOLOR + '"><strong>' + MonthNAME[mc];
	htmlBuffer += ' &nbsp; &nbsp;' + iYear + '</strong></td></tr>';

// Determine day of the week the month begins on.
tmpDate = new Date(iYear, mc, 1);

bwdom = tmpDate.getDay();

// Determine last day of month.
if (mc < 12) nextMon = new Date(iYear,mc+1,1);
else 	nextMon = new Date(iYear,1,1);

newTime = nextMon.getTime() - msPERDAY;
nextMon.setTime(newTime);
iLastDoM = nextMon.getDate();

NumWeeks = 5;	// Determine number of weeks.
if (((bwdom == 5) && (iLastDoM == 31)) || ((bwdom == 6) && (iLastDoM > 29)))
	NumWeeks = 6;

//The "week" has 7 "days or cells"
DayOfWeekCounter = 0;

htmlBuffer += "<tr>";
// Create cells for leading blank days.
while ( DayOfWeekCounter++ < bwdom ){
	htmlBuffer += "<td></td>";
}

sNotes = "";
DayCounter = 1;
WeekCounter = 1;
while (WeekCounter < NumWeeks + 1) {

while (DayOfWeekCounter < 8) {

	// IF statements for events, holidays and anniversaries start here.
	sBgColor = "";
	i = 0;
	sText = "";
	while (i < len) { // while not end of aEvents
	  if ((iYear == aEvents[i][0]) && (mc == aEvents[i][1]) ) {
		if (aEvents[i][2] == DayCounter) {
			if (aEvents[i][3] != "") {
				if (sBgColor == "")
					sBgColor = ' bgcolor="' + aEvents[i][3] + '"';
			}
			sText += "<br>&nbsp;" + aEvents[i][4];
		} else {
			if (aEvents[i][2] == 33) {
			// If not already used then add note.
				if ( (findElem(aIndexes,i))==(-1) ) {
					aIndexes[ j++ ] = "" + i + "";
					sNotes = sNotes + aEvents[i][4] + "<br>";
				//+WeekCounter+":"+DayOfWeekCounter+":"+ i + ": " +
				}
			}
		} //endif dayCounter ==
	  } //endif iYear ==
	  i++;
	} //endwhile i < len

	if (DayCounter < iLastDoM + 1) {
		htmlBuffer += '<td align="left" valign="top"' + sBgColor; 
		htmlBuffer += "><strong>" + DayCounter + "</strong>";
		htmlBuffer += '<font face="Courier New, Courier" size="-1"';
		htmlBuffer += ' color="#0000FF">' + sText + '</font></td>';
		//
	} // endif (DayCounter < iLastDoM + 1)
	DayOfWeekCounter++;
	DayCounter++;
  } // endwhile DayOfWeekCounter < 8
  DayOfWeekCounter = 1;
  WeekCounter++;
  htmlBuffer += "</tr><tr>";
} // endwhile WeekCounter < NumWeeks + 1

htmlBuffer += '</tr><tr><td colspan="7">';
htmlBuffer += '<font face="Courier New, Courier" size="-1">';
if (sNotes != "") sNotes = "Notes: " + sNotes;
htmlBuffer +=  sNotes + '&nbsp; &nbsp;</font></td></tr>';
htmlBuffer += '<tr><td colspan="7">&nbsp;</td></tr>';
document.writeln(htmlBuffer);	// Write one month.
htmlBuffer = "";

} // end for loop: number of months

htmlBuffer = '</table>';
htmlBuffer += '</font><br />';
//onClick="window.location = '#TOP';return false;">;
document.writeln(htmlBuffer);

//document.close();

}// end main()

function saveThisStuff(){
htmlBuffer = '<center><form name="change">' +
'Enter a year from 1753 to 9999: &nbsp;' +
'<input size=6 maxlength=4 name="fYear" value="2002"> &nbsp;' +
'Enter Start Month: <select name="fMon" value="Jan">' +
'<option>Jan</option><option>Feb</option><option>Mar</option>' +
'<option>Apr</option><option>May</option><option>Jun</option>' +
'<option>Jul</option><option>Aug</option><option>Sep</option>' +
'<option>Oct</option><option>Nov</option><option>Dec</option></select>'
// +
'<br />Number of Display Months: ' +
'<input type="Text" name="fNumMons" size="3" value="13">' +
' <input type="button" value="Change" onClick="changeDate(this.form);'
// + 'history.go(0);"></form></center>';
}

// eof.
