// ===============================================
// Define GLOBAL Variables
// ===============================================
var swUpDone = "n" ;	// are upcoming events displayed
// ===============================================
// Arrays to hold Month Names
// ===============================================
var mNames = new Array ("January","February","March","April","May","June","July","August","September","October","November","December")
var mNamesShort = new Array ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")

// ===============================================
// Arrays to hold Day Names
// ===============================================
var dNames = new Array ("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var dNamesShort = new Array ("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
var mNbrOfDays= new Array (31,0,31,30,31,30,31,31,30,31,30,31)

// ===============================================
// iEvents will hold the total number of events loaded
// aEvents will hold the actual Event data
//  **  CAUTION **
//  The fCalBldEventsTbl function MUST be the ONLY place where this is updated
//   All other functions MUST ONLY read this value
// ===============================================
var iEvents = -1;       // MUST BE -1 for everything to work correctly
var EventArrayLoaded = "n";		// Are the Event Arrays loaded??
var vEntityFound = "n";	// Let me know if a valid entity requested
var aEvents = new Array;

// ============
// The following function assigns the properties
//	to the aEvents array
// ============
function fEventsSet(pDate, pSname, pDesc, pIcon, pGraphic, pGraphicW, pGraphicH) 
{
this.date = pDate;
this.sname = pSname;
this.desc = pDesc;
this.icon = pIcon;
this.graphic = pGraphic;
this.graphicw = pGraphicW;
this.graphich = pGraphicH;
return
}



// ============
// The following function MUST be called only once and from the <BODY> tag
// ============
function fCalSetup()
{
// first, make sure the events table is loaded
if(EventArrayLoaded=="n"){fCalBldEventsTbl()}	// If event arrays aren't loaded - load them
// now check if display upcoming events
if ( swUpDone == "n" ) { fDspUp() ; swUpDone = "y" ; } ;	// display the upcoming events
wD = new Date() ;	// get today's date
wD.y = wD.getFullYear() ;	// get the year
wD.m = wD.getMonth() + 1 ;	// get the month (getMonth is relative to 0
fCalDspMonth(wD.m, wD.y) ;	// now - display the calendar for this month
}



// ========
// The following function builds a text string
//	for any events for a requested date
// ========
function fCalGetEvents(pY, pM, pD) 
{
i1=iEvents // Get the total events available
var t = ""
var br = "<br /><br />" ;
var w1 = ""
var w2 = ""
var i2 = 0
w1 = ((pM<10)) ? 0 + '' + pM : pM;
w2 = ((pD<10)) ? 0 + '' + pD : pD;
var zKey1 = pY + '' + w1 + ''  + w2
var zKey2 = '1000' + '' + w1 + ''  + w2
// ========
// Remember - the calBldEvents function MUST be called before this can work
//          it loads the events in the requisite tables
//          and set variable i1 = to the total events
// ========
for (i2=0; i2<=iEvents; i2++)
{
if(aEvents[i2].date==zKey1 || aEvents[i2].date==zKey2)
	{
// ========
// Set the attributes pending which event
// ========
	switch(aEvents[i2].sname)
		{
		case "Holiday" :
			br = "<br />" ;
			tmp = "<span class=\"calholiday\">" ;
			tmp += aEvents[i2].desc ;
			tmp += "</span>" ;
			break;    
		default:
			tmp = br + "<img src=\"" + vicondir + aEvents[i2].icon + "\" align=\"left\">&nbsp;" ;
			br = "<br />" ;
			tmp += "<span class=\"calbody\">" ;
			tmp += aEvents[i2].sname ;
			tmp += "</span><span class=\"callist\">" ;
			tmp += " - " + aEvents[i2].desc ;
			tmp += "</span>" ;
		}
	if ( aEvents[i2].graphic != "" )
		{
		tmp += "<br /><center><img src=\"" + vgraphicdir + aEvents[i2].graphic + "\"" ;
		if ( aEvents[i2].graphicw != "" && aEvents[i2].graphicw != 0 )
			{
			tmp += " width=\"" + aEvents[i2].graphicw + "\" height=\"" + aEvents[i2].graphich + "\"" ;
			}
		tmp += " ></center>" ;
		}
	t += tmp ;
  t += "<hr />" ;
	}
}
return t
}



// =====================================================
// The following function builds the actual calendar
// =====================================================
function fCalDspMonth(pM, pY)
{
// alert("fCalDspMonth m=" + pM + " y=" + pY ) ;
var x = document.getElementById("calDspMonth")
x.innerHTML = fCalBldMth(pM, pY)
return 
}



// =====================================================
// Build Monthly Calendar Header
// =====================================================
function fCalBldMthHdr(pM, pY)
{
mCur='';yCur='';t=''
zHdrL='<td class="calhdL" width="30%" >'
zHdrC='<td class="calhdC" width="40%" >'
zHdrR='<td class="calhdR" width="30%" >'
// First - setup some variables
//  NOTE:  Becuse the Month Names are in an array
//         the month number - m - must be adjusted to 0 for Jan, NOT 1 for Jan
mCur = mNames[pM-1]
yCur = pY
// Define calendar Header table
t+='<center><table class="cal" width="98%" cols="3"><tr>'
// Define Left Cell
t+=zHdrL
t+="</td>"
// Define Center
t+=zHdrC
t+=mCur + ' ' + yCur + '</td>'
// Define Right Cell
t+=zHdrR
//if(yCur=='2008'){t+='200th Anniversary of Freemasonry in Ohio'}
t+='</td>'
t+='</tr></table></center><br />'
return t
}



// =====================================================
// The following function builds the actual calendar
// =====================================================
function fCalBldMth(pM, pY)
{
t = '' ; // Variable where the actual text string being built is returned to the caller
// ---------------------------------------------------------------  
// Build Monthly Calendar Header
// ---------------------------------------------------------------
t=fCalBldMthHdr(pM, pY)
// ---------------------------------------------------------------  
// Begin Month Calendar Header
// ---------------------------------------------------------------  
t+='<center><table class="cal" width="98%" cols="7">'

// Define Day Name Row
t+='<tr align="center">';

// Put Day Names into table cells
for(s=0;s<7;s++) { t += '<th class="cal" >' + dNames[s] + '</th>' }

// end Day Names Row
t+='</tr>';
// ---------------------------------------------------------------  
// Define variables used in this function
var d = 0
tdHdr = '<td height="80" class="cal" >' 
tdEnd = '</td>'
bHdrnan = '<span class="calnan">&nbsp;'
bHdrdom = '<span class="caldom">&nbsp;'
bHdrtdy = '<span class="caltdy">&nbsp;'
bEnd = '&nbsp;</span>&nbsp;'
cD = new Date() ;	// get todays date
cD.y = cD.getFullYear() ;
cD.m = cD.getMonth() + 1 ;
cD.d = cD.getDate() ;
// ---------------------------------------------------------------  
// Set Feb nbrofdays based on the requested year being a leap year.
//		Ask for march of year minus 1 day = last day of feb for rquested year.
feb = new Date(pY,2,0) ;
mNbrOfDays[1] = feb.getDate() ;

// Get a standard formatted date for the 1st of the month and year passed
//		pM is passed relative to 1, need it relative to 0
var wD = new Date(pY, pM-1, 1) ;

// Get the day of week the 1st of the month falls on
wD.dow1st=wD.getDay()+1 ; // get dow of week for the first of the month


q = "loop date values=" ;	// debug display area


// ---------------------------------------------------------------  
// Begin Month Calendar 
// ---------------------------------------------------------------  
// Outer loop handles the max number of weeks in a month - 6
for (k=0; k<6; k++)
	{
	t += '<tr align="left">'

// ---------------------------------------------------------------  
// Inner loop handles the number of days in a week - 7
	for (j=0; j<7; j++)
		{
		i = k*7 + j + 1;
//		Set x = the day or nbsp NOTE: wD.dow1st is the day of week for the 1st of the month
//		do NOT change the nbsp value without changing the if statement at the end of the
//		  outer loop as well
// since everything is relative to 0, to display the actual date correctly,
//		the next statement adjusts date relative to 1 if within the month
		x = ((i-wD.dow1st >= 0) && (i-wD.dow1st < (mNbrOfDays[pM-1]) )) ? i-wD.dow1st+1 : '&nbsp;';
		x = x
		d = x
		t+=tdHdr
// the following will set the correct css class.  
//		If x is not a number use the "no display" class
//		else, if it is a number, and the requested date is today - use the today class
//		else, use the not today class
		if ( isNaN(x) )
			{ t += bHdrnan ; }
			 else
			{ t += (cD.y == pY && cD.m == pM && cD.d == d) ? bHdrtdy : bHdrdom ; }
		t += x + bEnd + fCalGetEvents(pY, pM, d) + tdEnd ;	// display the day and any events with it.
		}

// Inner Loop ends here
// ---------------------------------------------------------------  

// if here - have finished a week - end the table row
	t += '</tr>'

// now check to see if we are done with month (x will be the nbsp character)
// -or- if the last day of the month is on a Sat
// if so, kill the loop
//  this is put in to not display an empty week.
// The outer loop is killed by setting the counter above the termination value

	if(x == "&nbsp;" || x == (mNbrOfDays[pM-1]) ) { k=10 ; }
	}
// Outer Loop ends here
// ---------------------------------------------------------------  

// finish table
t+='</table></center><br />'

// return the text string defining the table
return t
}



// =====================================================
// This function will build and display the upcomming events
// the Parms are first date and last date to display
// It skips holidays.
// The events table is sorted - holidays first, then events in date sequence
// =====================================================
function fDspUp()
{
// first - calculate the first and last dates to display in the upcoming events division
d = new Date() ;

frst = d ;
frstYYYY = frst.getFullYear() ;
tmp = frst.getMonth() + 1 ;
frstMM = (tmp < 10 ) ? 0 + '' + tmp : tmp ;
tmp = frst.getDate() ;
frstDD = (tmp < 10 ) ? 0 + '' + tmp : tmp ;
pFrst = frstYYYY + '' + frstMM + '' + frstDD ;

last = d ;
last.setFullYear(d.getFullYear(), d.getMonth()+2, 0)
lastYYYY = last.getFullYear() ;
tmp = last.getMonth() + 1 ;
lastMM = (tmp < 10 ) ? 0 + '' + tmp : tmp ;
tmp = last.getDate() ;
lastDD = (tmp < 10 ) ? 0 + '' + tmp : tmp ;
pLast = lastYYYY + '' + lastMM + '' + lastDD ;

t = "<table width=\"99%\"><tr><td colspan=\"2\"><center><hr /><h5>Upcoming Events</h5></center><hr /></td></tr>" ;
vDate = "00000000" ;
for (i1 = 0; i1 <= iEvents; i1++)
	{
	if ( aEvents[i1].sname != "Holiday"
	  && aEvents[i1].date >= pFrst
	  && aEvents[i1].date <= pLast )
		{
		if ( vDate != aEvents[i1].date )
			{
			tDate = aEvents[i1].date ;
			t += "<tr><td colspan=\"2\"><hr /><h5>" ;
			t += tDate.substr(0,4) + "-" ;
			t += tDate.substr(4,2) + "-" ;
			t += tDate.substr(6,2) ;
			t += "</h5></td></tr>" ;
			vDate = aEvents[i1].date ; 
			}
		t += "<tr><td width=\"20%\" valign=\"top\" ><img src=\"" ;
		t += vicondir + aEvents[i1].icon + "\" align=\"left\"></td>" ;
		t += "<td width=\"80%\">" ; 
		t += "<span class=\"calbody\">"  + aEvents[i1].sname + "</span>" ;
		t += " - <span class=\"callist\">" + aEvents[i1].desc ;
		t += "</span></td></tr>" ;
		}
	}
t += "</table>" ;
var x = document.getElementById("calDspUp") ;
if ( x ) { x.innerHTML = t ; }
return ;
}