//*********************************CALENDAR DRAWING FUNCTIONS********************************************

var events_2007 = new Array();
var events_2008 = new Array();
var events_2009 = new Array();
var events_2010 = new Array();
var events_2011 = new Array();
var events_2012 = new Array();

var eventsForThisMonth = new Array();
var eventIDsForThisMonth = '';

var eventsLoaded_2007 = false;
var eventsLoaded_2008 = false;
var eventsLoaded_2009 = false;
var eventsLoaded_2010 = false;
var eventsLoaded_2011 = false;
var eventsLoaded_2012 = false;

var today = new Date();
var todaysDate = today.getFullYear() + '.' + ((today.getMonth() + 1) < 10?'0' + (today.getMonth() + 1):(today.getMonth() + 1)) + '.' + ((today.getDate())<10?'0' + (today.getDate()):(today.getDate()));

var currYear = today.getFullYear();
var currEventArrayYear = 0;

var eventInfoTarget = '';

function getCurrentDate() {

	var currDate = '';

	var theDate = new Date();
	var monthname = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	currDate +=monthname[theDate.getMonth()] + " ";
	currDate +=theDate.getDate() + ", ";
	currDate +=theDate.getFullYear();

	return(currDate);
}

var theDate = new Date();

var monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

function writeCalendar(whichMonth, dayWidth, dayHeight, year, calDivName, showNav, action, thisEventInfoTarget) {
	return(writeMonth(whichMonth, dayWidth, dayHeight, year, calDivName, showNav, action, thisEventInfoTarget));
}

function writeMonth(whichMonth, dayWidth, dayHeight, year, calDivName, showNav, action, thisEventInfoTarget) {

	if (year != currEventArrayYear) {	
		setEventsArray(year);		
	}

	setMonthInfo(whichMonth, dayWidth, dayHeight, year, calDivName, showNav, action, thisEventInfoTarget);

	eventInfoTarget = thisEventInfoTarget;

	currYear = year;

	theDate = new Date();

	eventsForThisMonth = new Array();
	eventIDsForThisMonth = '';

	var prevMonth = whichMonth-1;
	var prevYear = year;
	var nextMonth = whichMonth+1;
	var nextYear = year;
	if (whichMonth == 0) {
		prevMonth = 11;
		prevYear = year-1;
	}
	if (whichMonth == 11) {
		nextMonth = 0;
		nextYear = year+1;
	}

	var navLinks = '&nbsp;';	

	if (showNav) {
		navLinks =	'<a href="javascript:writePrevMonth(\'show_events\')">< prev</a>' +
					'&nbsp; month &nbsp;' +
					'<a href="javascript:writeNextMonth(\'show_events\')">next ></a>';
	}
	

	var monthML =	'<div style="position:relative;float:left;border:1px solid #000f48;border-bottom:0px;background-color:#4f6aac;font-family:century gothic,arial,helvetica;line-height:' + Math.round(dayHeight*0.50) + 'px;font-size:' + Math.round(dayHeight/2) + 'px;font-weight:' + (Math.round(dayHeight/2) > 12?'bold':'normal') + ';color:#000f48;padding-left:' + Math.round(dayHeight*0.1) + 'px;padding-right:' + Math.round(dayHeight*0.1) + 'px;">' + monthNames[whichMonth] + '&nbsp;\'' + (year+'').substring(2) + '</div>' +
					'<div style="font-size:' +  Math.max(Math.round(dayHeight*0.30), 8) + 'px;line-height:' + Math.round(dayHeight*0.50) + 'px;padding:1px;padding-bottom:0px;text-align:right;font-weight:bold;font-family:century gothic,arial,helvetica;color:#000f48">' + navLinks + '</div>' +
					'<div style="position:relative;float:left;border:1px solid #000f48;padding:0px;padding-right:1px;background-color:#4f6aac;font-family:century gothic,arial,helvetica;">' +
					'<div style="position:relative;width:' + (dayWidth*7 + 7) + 'px;padding-bottom:1px;">';	

	if (year == null || year == '') {
		year = today.getFullYear();
	}

	//get the day this month started on to position the days correctly
	theDate.setFullYear(year, whichMonth, 1);

	//alert(year + ' ' + theDate.getDay());

	var dayContent = '&nbsp;';

	var startDay = theDate.getDay()
	if (startDay != 0) {
		monthML += writeDay('', dayWidth, dayHeight, '', 'firstblank');
		for (var i=0;i<(startDay-1);i++ ) {
			monthML += writeDay('', dayWidth, dayHeight, '', 'blank')
		}
	}
	
	var dCount = getDaysInMonth(whichMonth, year);	

	for (var i=0;i<dCount;i++ ) {		

		monthML += writeDay((i+1), dayWidth, dayHeight, dayContent, 'day', year + '.' + ((whichMonth+1)<10?'0' + (whichMonth+1):(whichMonth+1)) + '.' + ((i+1)<10?'0' + (i+1):(i+1)), action);
	}

	if ((dCount + startDay)%7 > 0) {
		//alert(dCount + ' ' + startDay);
		monthML += writeDay('', dayWidth, dayHeight, '', 'firstblank');
		for (var i=0;i<((7 - (dCount + startDay)%7)-1);i++ ) {
			monthML += writeDay('', dayWidth, dayHeight, '', 'blank');
		}
	}

	monthML +=	'</div></div>';
	monthML +=	'<div style="position:relative;clear:both;"><div style="position:relative;float:left;border:1px solid #000f48;border-top:0px;padding:0px;padding-right:1px;background-color:#4f6aac;font-family:century gothic,arial,helvetica;">';
	
	monthML += writeDay('', dayWidth, Math.max(Math.round(dayHeight*0.35), 8), dayNames[0].substring(0,3), 'firstblank');
	for (var i=1;i<dayNames.length;i++) {
		monthML += writeDay('', dayWidth, Math.max(Math.round(dayHeight*0.35), 8), dayNames[i].substring(0,3), 'dayTab');
	}

	monthML +=	'</div></div>';

	if (action.indexOf('show_events') != -1 || action.indexOf('show_symbol') != -1) {
		setTimeout('waitForEventsAndPopulateCalendar(' + year + ',' + whichMonth + ',\'' + action + '\',\'' + thisEventInfoTarget + '\')', 250);
	}

	return(monthML);

}

function waitForEventsAndPopulateCalendar(year, whichMonth, action, thisEventInfoTarget) {

	if (!eval('eventsLoaded_' + year)) {
		setTimeout('waitForEventsAndPopulateCalendar(' + year + ',' + whichMonth + ',\'' + action + '\',\'' + thisEventInfoTarget + '\')', 100);
	} else {
		eval('eventsArray = events_' + year);
		populateCalendarEvents(year, whichMonth, action, thisEventInfoTarget);
	}

}


var monthInfo = new Object();

function setMonthInfo(whichMonth, dayWidth, dayHeight, year, calDivName, showNav, action, thisEventInfoTarget) {
	monthInfo.whichMonth = whichMonth;
	monthInfo.dayWidth = dayWidth;
	monthInfo.dayHeight = dayHeight;
	monthInfo.year = year;
	monthInfo.calDivName = calDivName;
	monthInfo.showNav = showNav;
	monthInfo.action = action;
	monthInfo.thisEventInfoTarget = thisEventInfoTarget;
}

function writeNextMonth(action) {
	
	if (action != null) {
		monthInfo.action = action;
	}

	if (monthInfo.whichMonth == 11) {
		monthInfo.whichMonth = 0;
		monthInfo.year = monthInfo.year+1;
	} else {
		monthInfo.whichMonth++;
	}

	document.getElementById(monthInfo.calDivName).innerHTML = writeMonth(monthInfo.whichMonth, monthInfo.dayWidth, monthInfo.dayHeight, monthInfo.year, monthInfo.calDivName, monthInfo.showNav, monthInfo.action, monthInfo.thisEventInfoTarget);
}

function writePrevMonth(action) {

	if (action != null) {
		monthInfo.action = action;
	}

	if (monthInfo.whichMonth == 0) {
		monthInfo.whichMonth = 11;
		monthInfo.year = monthInfo.year-1;
	} else {
		monthInfo.whichMonth--;
	}

	document.getElementById(monthInfo.calDivName).innerHTML = writeMonth(monthInfo.whichMonth, monthInfo.dayWidth, monthInfo.dayHeight, monthInfo.year, monthInfo.calDivName, monthInfo.showNav, monthInfo.action, monthInfo.thisEventInfoTarget);
}

function writeSpecifiedMonth(specYear, specMonth, action) {

	if (action != null) {
		monthInfo.action = action;
	}

	document.getElementById(monthInfo.calDivName).innerHTML = writeMonth(specMonth, monthInfo.dayWidth, monthInfo.dayHeight, specYear, monthInfo.calDivName, monthInfo.showNav, monthInfo.action, monthInfo.thisEventInfoTarget);

}

function writeDay(dayNum, width, height, content, type, dayID, action) {

	var bgColor1 = '#4f6aac';
	var bgColor2 = '#c0c5fe';
	var fColor = '#c0c5fe';
	var fWeight = 'bold';
	
	if (type == 'day') {
		bgColor1 = '#4f6aac';
		bgColor2 = '#c0c5fe';
	} else if (type == 'blank') {
		bgColor1 = '#c0c5fe';
		bgColor2 = '#4f6aac';
		fColor = '#000f48';
	} else if (type == 'firstblank') {
		bgColor1 = '#4f6aac';
		bgColor2 = '#4f6aac';
		fColor = '#000f48';
	} else if (type == 'event') {
		bgColor1 = '#4f6aac';
		bgColor2 = '#ff9C7D';
	} else if (type == 'dayTab') {
		bgColor1 = '#000f48';
		bgColor2 = '#4f6aac';
		fColor = '#000f48';
	}

	if (dayID != null && dayNum != '' && (dayID == todaysDate)) {
		dayNum = '<span style="color:#660000;font-size:11px;">' + dayNum + '</span>';
	}


	var onClick = '';
	if (action != null & action == 'show_symbol') {
		onClick='document.location.href=\'calendar.html?jumpToDate=' + dayID + '\';';
	} else {
		onClick = 'checkAndShowThisMonthsEvents(\'' + dayID + '\');';
	}

	var dayML = '<div href="#pagebottom" style="position:relative;float:left;border-left:1px solid ' + bgColor1 + ';padding-top:1px;"><div style="position:relative;width:' + width + 'px;overflow:hidden;" onClick="' + onClick + '">' +
							'<div style="position:relative;height:' + height + 'px;background-color:' + bgColor2 + ';line-height:12px;overflow:hidden;"  id="d_' + dayID + '">' +
							(dayNum != ''?'<div style="font-size:12px;font-weight:bold;color:#000f48;line-height:14px;padding:2px;">' + dayNum + '</div>':'') +
							'<div style="line-height:' + height + 'px;padding:' + ((type == 'dayTab' || type == 'firstblank')?0:2) + 'px;padding-top:0px;padding-bottom:' + ((type == 'dayTab' || type == 'firstblank')?Math.round(height*0.1):0) + 'px;font-size:' + Math.max(Math.round(height*0.9), 9) + 'px;text-align:center;color:' + fColor + ';font-weight:' + fWeight + ';" id="d_' + dayID + '_content">' + content + '</div></div></div></div>';

	return(dayML);
}

function getDaysInMonth(month, year) {

theDate = new Date(year,month);//
var intDaysInMonth = 28;//the fewest number of days in a month
var blnDateFound = false;//Set a variable to check on the while loop

while (!blnDateFound)
{
theDate.setDate(intDaysInMonth+1);//create the next possible day
var intNewMonth = theDate.getMonth();//new month date

if (intNewMonth != month)//if the month has changed
  blnDateFound = true;
else
  intDaysInMonth++;
}

return(intDaysInMonth);

}


function getEventsForDate(dateString, onlyStar) {		

	var tYear = dateString.split('.')[0];
	var tMonth = dateString.split('.')[1] - 1;
	var tDate = dateString.split('.')[2];

	var dDate = new Date();
	dDate.setFullYear(tYear, tMonth, tDate);

	var thisDay = dDate.getDay();
	var daysInMonth = getDaysInMonth(tMonth, tYear);

	var eventML = '&nbsp;'

	var eventsForToday = new Array();			

	for (var j=0;j<eventsArray.length;j++) {

		thisEvent = new Object();
		thisEvent = eventsArray[j];		

		if (findDateMatch(dateString, eventsArray[j], thisDay, daysInMonth, tDate)) {

			eventsForToday[eventsForToday.length] = new Object();
			eventsForToday[eventsForToday.length - 1] = thisEvent;
			
			//we only add an event if it isn't in the list already
			if (eventIDsForThisMonth.indexOf('|' + j + '|') == -1) {
				eventIDsForThisMonth += '|' + j + '|';
				eventsForThisMonth[eventsForThisMonth.length] = new Object();
				eventsForThisMonth[eventsForThisMonth.length - 1] = thisEvent;
			}

		}

	}

	var thisEventML = '';

	if (eventsForToday.length > 0 && !onlyStar) {				

		if (eventsForToday.length == 1) {

			if (eventsForToday[0].type == 'fieldtrip') {

				thisEventML = '<strong style="color:#660000;">' + getEventTypeName(eventsForToday[0].type) + ':</strong> ' + addElipsis(eventsForToday[0].title, 40);

			} else if (eventsForToday[0].type == 'field_seminar' || eventsForToday[0].type == 'field_seminar') {

				thisEventML = '<strong style="color:#660000;">' + getEventTypeName(eventsForToday[0].type) + ':</strong> ' + addElipsis(eventsForToday[0].title, 34);

			} else if (eventsForToday[0].type == 'volunteer') {

				thisEventML = '<strong style="color:#660000;">' + getEventTypeName(eventsForToday[0].type) + ':</strong> ' + addElipsis(eventsForToday[0].title, 34);

			} else if (eventsForToday[0].type == 'event') {

				thisEventML = '<strong style="color:#660000;">' + getEventTypeName(eventsForToday[0].type) + ':</strong> ' + addElipsis(eventsForToday[0].title, 34);

			}  else if (eventsForToday[0].type == 'canoe') {

				thisEventML = '<strong style="color:#660000;">' + getEventTypeName(eventsForToday[0].type) + ':</strong> ' + addElipsis(eventsForToday[0].title, 34);

			} else if (eventsForToday[0].type == 'opening_closing') {

				thisEventML = '<strong style="color:#660000;">' + getEventTypeName(eventsForToday[0].type) + ':</strong> ' + addElipsis(eventsForToday[0].title, 34);

			} else {

				thisEventML = addElipsis(eventsForToday[0].title, 50)
			}
		} else {
			
			if (eventsForToday.length <= 3) {
			
				thisEventML += '<div style="text-align:left;font-weight:bold;color:#660000;padding-left:0px;">';

				for (var j=0;j<eventsForToday.length;j++) {
					thisEventML += '&#186; ' + getEventTypeName(eventsForToday[j].type) + '<br>';
				}

				thisEventML += '</div>';

			} else if (eventsForToday.length >= 4) {
				
				thisEventML += '<div style="text-align:left;font-weight:bold;color:#660000;padding-left:0px;">';

				var shownCnt = 0;

				for (var j=0;j<eventsForToday.length;j++) {
					if (thisEventML.indexOf('&#186; ' + getEventTypeName(eventsForToday[j].type)) == -1 && shownCnt < 3) {
						shownCnt++;
						thisEventML += '&#186; ' + getEventTypeName(eventsForToday[j].type) + '<br>';
					}
					
				}

				thisEventML += '<span style="font-size:10px;color:#C22D00;">&nbsp; + ' + (eventsForToday.length-shownCnt) + ' more...</span>';

				thisEventML += '</div>';

			}

		}		

		eventML = '<div style="line-height:14px;font-weight:normal;font-size:11px;color:#000;">' + thisEventML + '</div>';
	} else if (eventsForToday.length > 0 && onlyStar) {
		eventML = '<div style="line-height:14px;font-weight:bold;font-size:22px;">*</div>';
	}

	return(eventML);
}

function getEventTypeName(eventType) {

	var eventTypeName = '';

	if (eventType == 'fieldtrip') {

		eventTypeName = 'Naturalist-led';

	} else if (eventType == 'field_seminar' || eventType == 'field_seminar') {

		eventTypeName = 'Seminar';

	} else if (eventType == 'volunteer') {

		eventTypeName = 'Volunteer';

	} else if (eventType == 'event') {

		eventTypeName = 'Regional Event';

	} else if (eventType == 'canoe') {

		eventTypeName = 'Canoe Tours';

	} else if (eventType == 'opening_closing') {

		eventTypeName = 'Opening / Closing';

	}

	return(eventTypeName);
}

function addElipsis(inString, howLong) {

	var elipML = inString + '';

	if (inString.length > howLong) {
		elipML = inString.substring(0, howLong-1) + '...'
	}

	return(elipML);

}

var upComingEventsML = '';

function populateCalendarEvents(thisYear, thisMonth, action, eventInfoDiv) {	

	//we don't want to try and populate a map that has already been refreshed
	if (currYear == thisYear) {
	
		var monthCount = thisMonth+1;
		var thisEvent = new Object();

		var thisDate = new Date();
		
		var isPast = false;		

		var daysInMonth = getDaysInMonth(thisMonth, thisYear);

		for (var i=0;i<daysInMonth;i++ ) {
			
			thisEventDate = thisYear + '.' + (monthCount < 10?'0' + monthCount:monthCount) + '.' + ((i+1)<10?'0' + (i+1):(i+1));			 

			if (thisEventDate < todaysDate) {
				isPast = true;
			} else {
				isPast = false;
			}

			if (!isPast && currMonth == thisMonth) {
				upComingEventsML = getUpcomingEvents(todaysDate);
				if (action.indexOf('show_upcoming_events') != -1) {
					document.getElementById(eventInfoDiv + '_upcoming').innerHTML = upComingEventsML;
					toggleEventInfoOn(monthInfo.thisEventInfoTarget, 'upcoming');
				}
			}

			var thisEventML = '';

			if (action.indexOf('show_symbol') == -1) {
				thisEventML = getEventsForDate(thisEventDate, false);
			} else {				
				thisEventML = getEventsForDate(thisEventDate, true);
			}

			var thisEleID = 'd_' + thisYear + '.' + ((thisMonth+1) < 10?'0' + (thisMonth+1):(thisMonth+1)) + '.' + ((i+1)<10?'0' + (i+1):(i+1));

			if (thisEventML != '&nbsp;') {				
				if (document.getElementById(thisEleID)) {
					document.getElementById(thisEleID).style.cursor = 'pointer';
					document.getElementById(thisEleID).style.backgroundColor = (isPast?'#b4b6b8':'#ffffff');
					document.getElementById(thisEleID + '_content').innerHTML = thisEventML;

				} else {					
					//alert(thisEleID);
				}
			} else if (isPast && document.getElementById(thisEleID)) {
				document.getElementById(thisEleID).style.backgroundColor = '#b4b6b8';
			}
			
		}

		if (action.indexOf('show_date_events') != -1) {
			
			var actionBits = action.split('|');
			var thisEDate = '';
			for (var i=0;i<actionBits.length;i++) {
				if (actionBits[i].indexOf('show_date_events') == 0) {
					thisEDate = actionBits[i].split(':')[1];
				}
			}
			checkAndShowThisMonthsEvents(thisEDate);
			upComingEventsML = getUpcomingEvents(todaysDate);
			document.getElementById('eventsInformationDiv_upcoming').innerHTML = upComingEventsML;
		}

	}

}

function writeAllFutureEvents() {

	var thisYear = todaysDate.split('.')[0];	

	if (thisYear != currEventArrayYear) {	
		setEventsArray(thisYear);		
	}

	if (!eval('eventsLoaded_' + thisYear)) {				

		setTimeout('writeAllFutureEvents()', 150);
	} else {

		eval('eventsArray = events_' + thisYear + ';');

		var allEventsML = '';

		var futureEvents = getAllFutureEvents(todaysDate);
		
		futureEvents = sortEvents(futureEvents, 'type');

		var oldType = futureEvents[0].type;

		var allEventsMLArray = new Array();

		for (var i=0; i<futureEvents.length; i++) {
			
			var eventType = futureEvents[i].type;

			var eventMLArrayID = 6;

			if (eventType == 'event') {

				eventMLArrayID = 1;

			} else if (eventType == 'field_seminar' || eventType == 'field_seminar') {

				eventMLArrayID = 2;

			} else if (eventType == 'volunteer') {

				eventMLArrayID = 3;

			} else if (eventType == 'fieldtrip') {

				eventMLArrayID = 0;

			} else if (eventType == 'canoe') {

				eventMLArrayID = 4;

			} else if (eventType == 'opening_closing') {

				eventMLArrayID = 5;

			}

			if (allEventsMLArray[eventMLArrayID] == null) {
				allEventsMLArray[eventMLArrayID] = '';
			}
			allEventsMLArray[eventMLArrayID] += getUpcomingEventML(futureEvents[i]);

		}

		var eventTypeNames = new Array('Naturalist-led Activities','Regional Events','Field Seminars','Volunteer Events','Mono Lake Canoe Tours','Openings and Closings','Additional Events');

		var jumpLinksML = '';

		for (var j=0;j<allEventsMLArray.length ;j++ ) {
			if (allEventsMLArray[j] != null && allEventsMLArray[j].length != 0) {
				jumpLinksML += '<a style="font-family:century gothic,arial,helvetica;font-weight:bold;font-size:12px;" href="#events_' + j + '">' + eventTypeNames[j] + ' &#187;</a><br>';
				allEventsML += '<div id="events_' + j + '" style="position:relative;clear:both;color:#c0c5fe;font-weight:bold;background-color:#4f6aac;padding-left:3px;padding-right:3px;font-size:18px;line-height:26px;font-family:century gothic,arial,helvetica;margin-bottom:10px;margin-top:10px;">' + (allEventsML!=''?'<div style="position:relative;float:right;"><a href="#pageTop" style="color:#c0c5fe;font-weight:bold;font-size:12px;">back to top</a></div>':'') + eventTypeNames[j] + '</div>' + allEventsMLArray[j]; 						
			}
		}

		document.getElementById('futureEventsListingsDiv').innerHTML = jumpLinksML + allEventsML;
	}

}


function getAllFutureEvents(dateOfToday) {

	var futureEventsArray = new Array();

	var addedEventIDs = '';

	for (var i=0; i<eventsArray.length; i++) {

		var thisEvent = eventsArray[i];

		var thisEDate = thisEvent.date;
		if (thisEDate.indexOf(',')!=-1 ) {

			var thesedates = thisEDate.split(',');

			for (var k=0; k<thesedates.length; k++) {
				if (thesedates[k].indexOf('recurring')!= -1 || thesedates[k] >= todaysDate) {

					if (addedEventIDs.indexOf('|' + i + '|') == -1) {
						addedEventIDs += '|' + i + '|';
						futureEventsArray[futureEventsArray.length] = new Object();
						futureEventsArray[futureEventsArray.length - 1] = thisEvent;
					}

				}

			}

		} else if (thisEDate >= todaysDate|| thisEDate.indexOf('recurring') != -1) {
			if (addedEventIDs.indexOf('|' + i + '|') == -1) {
				addedEventIDs += '|' + i + '|';
				futureEventsArray[futureEventsArray.length] = new Object();
				futureEventsArray[futureEventsArray.length - 1] = thisEvent;
			}
		}

	}


	return(futureEventsArray);

}

function getUpcomingEvents(fromDate) {

	var uEventsML = '';

	var cnt = 0;
	while (cnt < eventsArray.length && eventsArray[cnt].date < fromDate) {
		cnt++;
	}

	if (cnt < eventsArray.length) {
		for (var j=cnt;j<cnt+5 ;j++ ) {
			if (eventsArray[j] != null) {
				uEventsML += getEventML(eventsArray[j], eventsArray[j].date.split(',')[0], 'small', 'checkAndShowUpcomingEvents(\'' + eventsArray[j].date.split(',')[0] + '\')');
			}
		}

	}
	
	if (uEventsML != '') {
		uEventsML = '<div style="position:relative;color:#000f48;font-size:20px;font-weight:bold;font-family:century gothic,arial,helvetica;margin-top:6px;"><a style="position:relative;top:3px;float:right;font-size:14px;line-height:24px;" href="all_upcoming_events">show all &#187;</a>Coming Soon</div>' + uEventsML;
	} else {
		uEventsML = '<div style="position:relative;color:#660000;font-size:20px;font-weight:bold;font-family:century gothic,arial,helvetica;margin-top:6px;"><a style="position:relative;top:3px;float:right;font-size:14px;line-height:24px;" href="all_upcoming_events">show all &#187;</a>Coming Soon</div>' + '<p>No upcoming events are currently scheduled.</p>'
	}

	return(uEventsML);

}

function checkAndShowThisMonthsEvents(dayID) {

	var tYear = dayID.split('.')[0];
	var tMonth = dayID.split('.')[1] - 1;
	var tDate = dayID.split('.')[2];

	var dDate = new Date();
	dDate.setFullYear(tYear, tMonth, tDate);

	var thisDay = dDate.getDay();
	var daysInMonth = getDaysInMonth(tMonth, tYear);

	var eventsInfoML = '';

	var theseEvents = new Array();

	for (var i=0;i<eventsForThisMonth.length;i++ ) {

		if (findDateMatch(dayID, eventsForThisMonth[i], thisDay, daysInMonth, tDate)) {
			theseEvents[theseEvents.length] = eventsForThisMonth[i];
		} 
	}

	theseEvents = sortEvents(theseEvents, 'timeStart');

	for (var i=0;i<theseEvents.length;i++ ) {
		eventsInfoML += getEventML(theseEvents[i], dayID);
	}	

	if (document.getElementById(monthInfo.thisEventInfoTarget + '_eventInfo') && eventsInfoML != '' && eventsInfoML != '&nbsp;') {
		showEventsInfo(eventsInfoML);
	}

}

function toggleEventInfoOn(eventInfoTarget, which) {
	if (which == 'eventInfo') {
		document.getElementById(eventInfoTarget + '_upcoming').style.display = 'none';		
		document.getElementById(eventInfoTarget + '_' + which).style.display = 'block';		
	} else {
		document.getElementById(eventInfoTarget + '_eventInfo').style.display = 'none';		
		document.getElementById(eventInfoTarget + '_' + which).style.display = 'block';	
	}

}

function showEventsInfo(eventsInfoML) {
	document.getElementById(monthInfo.thisEventInfoTarget + '_eventInfo').innerHTML = '<div style="position:relative;color:#000f48;font-size:20px;font-weight:bold;font-family:century gothic,arial,helvetica;margin-top:6px;"><a style="position:relative;top:3px;float:right;font-size:14px;line-height:24px;" href="javascript:toggleEventInfoOn(eventInfoTarget, \'upcoming\');">show coming soon &#187;</a>Events for this Date</div>';
	document.getElementById(monthInfo.thisEventInfoTarget + '_eventInfo').innerHTML += eventsInfoML;
	toggleEventInfoOn(monthInfo.thisEventInfoTarget, 'eventInfo');
}


function checkAndShowEvents(dayID) {

	var tYear = dayID.split('.')[0];
	var tMonth = dayID.split('.')[1] - 1;
	var tDate = dayID.split('.')[2];

	var dDate = new Date();
	dDate.setFullYear(tYear, tMonth, tDate);

	var thisDay = dDate.getDay();
	var daysInMonth = getDaysInMonth(tMonth, tYear);


	var eventsInfoML = '';

	var theseEvents = new Array();

	for (var i=0;i<eventsArray.length;i++ ) {

		if (findDateMatch(dayID, eventsArray[i], thisDay, daysInMonth, tDate)) {
			theseEvents[theseEvents.length] = new Object();
			theseEvents[theseEvents.length - 1] = eventsArray[i];
		}

	}

	for (var i=0;i<theseEvents.length;i++ ) {
			eventsInfoML += getEventML(theseEvents[i], dayID);
	}

	if (document.getElementById(monthInfo.thisEventInfoTarget + '_eventInfo') && eventsInfoML != '' && eventsInfoML != '&nbsp;') {
		showEventsInfo(eventsInfoML);

		if ((dayID.split('.')[1]-1) != monthInfo.whichMonth || dayID.split('.')[0] != monthInfo.year) {

			writeSpecifiedMonth(dayID.split('.')[0], dayID.split('.')[1] - 1, 'show_events');
		}	

	}	

}


function checkAndShowUpcomingEvents(dayID) {

	var tYear = dayID.split('.')[0];
	var tMonth = dayID.split('.')[1] - 1;
	var tDate = dayID.split('.')[2];

	var dDate = new Date();
	dDate.setFullYear(tYear, tMonth, tDate);

	var thisDay = dDate.getDay();
	var daysInMonth = getDaysInMonth(tMonth, tYear);


	var eventsInfoML = '';

	var cnt = 0;

	while (eventsArray[cnt].date < dayID) {
		cnt++;
	}

	var theseEvents = new Array();

	for (var i=cnt;i<eventsArray.length;i++ ) {

		if (findDateMatch(dayID, eventsArray[i], thisDay, daysInMonth, tDate)) {
			theseEvents[theseEvents.length] = new Object();
			theseEvents[theseEvents.length - 1] = eventsArray[i];
		}

	}

	for (var i=0;i<theseEvents.length;i++ ) {
			eventsInfoML += getEventML(theseEvents[i], dayID);
	}

	if (document.getElementById(monthInfo.thisEventInfoTarget + '_eventInfo') && eventsInfoML != '' && eventsInfoML != '&nbsp;') {
		showEventsInfo(eventsInfoML);

		if ((dayID.split('.')[1]-1) != monthInfo.whichMonth || dayID.split('.')[0] != monthInfo.year) {

			writeSpecifiedMonth(dayID.split('.')[0], dayID.split('.')[1] - 1, 'show_events');
		}	

	}	

}

function findDateMatch(dayID, tEvent, thisDay, daysInMonth, tDate) {	

	if (tEvent.date.indexOf(dayID) != -1) {
		return(true);
	} else {

		var dateValue = 'recurring|' + thisDay;

		//we need to get which day of week this is
		//and which day-of-week in month this is			
		if (tEvent.date == dateValue) {
			
			return(true);

		//check for basic recurring mutiple dates such as: recurring|3,recurring|6
		} else if (tEvent.date.indexOf(dateValue) != -1 && tEvent.date.charAt(tEvent.date.indexOf(dateValue) + dateValue.length) != '.') {
		
			return(true);			

		} else {
			var dayOrdinal = 0;

			if (tDate < 8) {
				dayOrdinal = 1;
			} else if (tDate < 15) {
				dayOrdinal = 2;
			} else if (tDate < 22) {
				dayOrdinal = 3;
			} else if (tDate < 29) {
				dayOrdinal = 4;
				if (daysInMonth == 28 || (daysInMonth - tDate) < 7) {
					dayOrdinal = 'last';
				}
			} else if (tDate >= 29) {
				dayOrdinal = 'last';
			}

			//now look for this string in the date value			

			if (dayOrdinal != 0) { 	

				dateValue = 'recurring|' + thisDay + '.' + dayOrdinal;

				if (tEvent.date == dateValue) {				
					return(true);
				//first through #positions-1 positions
				} else if (tEvent.date.indexOf(dateValue + ',') != -1 ) {				
					return(true);
				//and last position
				} else if (tEvent.date.indexOf(dateValue) == (tEvent.date.length - dateValue.length)) {				
					return(true);
				}

			}

		}
	}

	return(false);

}

function getEventML(thisEvent, thisdate, size, onClick) {
		
	var thisEventML = '';

	thisEventML += '<div style="position:relative;margin-top:10px;font-family:century gothic,arial,helvetica;' + ((onClick != null && onClick != '')?'cursor:hand;cursor:pointer;" onClick="' + onClick + ';"':'"') + '>' +
					'<div style="position:relative;border: 1px solid #4f6aac;">' + 
						'<div style="position:relative;display:block;color:#c0c5fe;font-weight:bold;background-color:#4f6aac;padding-left:3px;padding-right:3px;font-size:16px;line-height:22px;">' + getDateInWords(thisdate) + '</div>' +
						'<div style="position:relative;display:block;color:#660000;font-weight:bold;padding-left:5px;font-size:10px;line-height:22px;">' + getTimeInWords(thisEvent) + '</div>' +
							'<div style="position:relative;display:block;clear:both;padding-top:5px;padding-left:10px;padding-right:10px;">'  +
								'<div style="font-size:18px;color:#000f48;font-weight:bold;">' +
									getEventTitle(thisEvent) +
								'</div>';

	if (size != 'small') {

		thisEventML +=				'<div style="position:relative;display:block;top:-2px;height:1px;overflow:hidden;line-height:1px;background-color:#c0c5fe;">&nbsp;</div>';

		thisEventML += getEventSpeakers(thisEvent);

		thisEventML += getEventLocation(thisEvent);

		thisEventML += getEventContacts(thisEvent);

		thisEventML += getEventDescription(thisEvent);

		thisEventML += getEventSponsors(thisEvent);

		thisEventML += getEventDirections(thisEvent);		

	}

	thisEventML +=			'</div>' +
					'</div>' +
				   '</div>';

	return(thisEventML);

}


function getUpcomingEventML(thisEvent, size, onClick) {
		
	var thisEventML = '';

	thisEventML += '<div style="position:relative;margin-top:10px;font-family:century gothic,arial,helvetica;' + ((onClick != null && onClick != '')?'cursor:hand;cursor:pointer;" onClick="' + onClick + ';"':'"') + '>' +
					'<div style="position:relative;border: 1px solid #4f6aac;">' + 
							'<div style="position:relative;display:block;clear:both;padding-top:5px;padding-left:10px;padding-right:10px;">'  +
								'<div style="font-size:20px;color:#000f48;font-weight:bold;">' +
									getEventTitle(thisEvent) +
								'</div>';

	if (size != 'small') {

		thisEventML +=				'<div style="position:relative;display:block;top:-2px;height:1px;overflow:hidden;line-height:1px;background-color:#c0c5fe;">&nbsp;</div>';

		thisEventML += getUpcomingEventDates(thisEvent, false);

		thisEventML += getEventTimes(thisEvent);
		
		thisEventML += getEventSpeakers(thisEvent);

		thisEventML += getEventLocation(thisEvent);

		thisEventML += getEventContacts(thisEvent);

		thisEventML += getEventDescription(thisEvent);

		thisEventML += getEventSponsors(thisEvent);

		thisEventML += getEventDirections(thisEvent);		

	}

	thisEventML +=			'</div>' +
					'</div>' +
				   '</div>';

	return(thisEventML);

}

function getFeaturedEventML(thisEvent, size) {
		
	var thisEventML = '';

	thisEventML += '<div style="position:relative;margin-top:10px;font-family:century gothic,arial,helvetica;>' +
							'<div style="position:relative;display:block;clear:both;padding-top:5px;padding-left:10px;padding-right:10px;">'  +
								'<div style="font-size:26px;color:#000f48;font-weight:bold;">' +
									'<span style="color:#660000">Featured Event:</span> ' + thisEvent.title +
								'</div>';

	if (size != 'small') {

		thisEventML +=				'<div style="position:relative;display:block;top:-2px;height:1px;overflow:hidden;line-height:1px;background-color:#c0c5fe;">&nbsp;</div>';

		thisEventML += getUpcomingEventDates(thisEvent, false);

		thisEventML += getEventTimes(thisEvent);
		
		thisEventML += getEventSpeakers(thisEvent);

		thisEventML += getEventLocation(thisEvent);

		thisEventML += getEventContacts(thisEvent);

		thisEventML += getEventDescription(thisEvent);

		thisEventML += getEventSponsors(thisEvent);

		thisEventML += getEventDirections(thisEvent);		

	}

	thisEventML +=			'</div>' +
				   '</div>';

	return(thisEventML);

}

function getEventByTitle(title) {
	
	var rEvent = null;

	for (var i=0;i<eventsArray.length;i++ ) {
		if (eventsArray[i].title == title) {
			rEvent = new Object();
			rEvent = eventsArray[i];
			break;
		}
	}

	return(rEvent);
}


function getEventTitle(thisEvent) {

	var titleML = '';

	var eType = thisEvent.type;

	if (eType == 'field_seminar' || eType == 'field_seminar') {
		titleML = '<span style="color:#660000;">Seminar:</span> ' + thisEvent.title + '<br>';
	} else if (eType == 'fieldtrip') {
		titleML = '<span style="color:#660000;">Naturalist-led:</span> ' + thisEvent.title + '<br>';
	} else if (eType == 'volunteer') {
		titleML = '<span style="color:#660000;">Volunteer:</span> ' + thisEvent.title + '<br>';
	} else if (eType == 'event') {
		titleML = '<span style="color:#660000;">Regional Event:</span> ' + thisEvent.title + '<br>';
	} else if (eType == 'canoe') {
		titleML = '<span style="color:#660000;">Canoe Tours:</span> ' + thisEvent.title + '<br>';
	} else if (eType == 'opening_closing') {
		titleML = '<span style="color:#660000;">Openings and Closings:</span> ' + thisEvent.title + '<br>';
	} else {
		titleML = thisEvent.title + '<br>';
	}

	return(titleML);
}

function getUpcomingEventDates(thisEvent, isOnCalPage) {

	var dateML = '';

	var eventDate = thisEvent.date;

	var eventDateML = '';

	var dateString = 'Date';

	if (eventDate.indexOf(',') != -1) {

		dateString += 's';

		var eDates = eventDate.split(',');

		for (var k=0;k<eDates.length;k++ ) {
			
			if (eDates[k] >= todaysDate) {
			
				if (eDates[k].indexOf('recurring') == -1 && eDates[k].indexOf('variable') == -1) {
					if (isOnCalPage) {
						eventDateML += (eventDateML != ''?'<span style="color:#000f48;">Dates: </span>':'') + '<a onClick="checkAndShowEvents(\'' + eDates[k] + '\')">' + getDateInWords(eDates[k]) + (k != eDates.length-1?'</a><br>':'</a>');
					} else {
						eventDateML += (eventDateML != ''?'<span style="color:#000f48;">Dates: </span>':'') + '<a href="calendar.html?jumpToDate=' + eDates[k] + '">' + getDateInWords(eDates[k]) + (k != eDates.length-1?'</a><br>':'</a>');
					}
				} else {
					eventDateML += (eventDateML != ''?'<span style="color:#000f48;">Dates: </span>':'') + getDateInWords(eDates[k]) + (k != eDates.length-1?'<br>':'');
				}

			}
			
		}		
	} else {

		if (eventDate.indexOf('recurring') == -1 && eventDate.indexOf('variable') == -1) {
			if (isOnCalPage) {
				eventDateML += '<a onClick="checkAndShowEvents(\'' + eventDate + '\')">' + getDateInWords(eventDate) + '</a>';
			} else {
				eventDateML += '<a href="calendar.html?jumpToDate=' + eventDate + '">' + getDateInWords(eventDate) + '</a>';
			}
		} else {
			eventDateML += getDateInWords(eventDate) + '<br>';
		}
		
	}

	if (eventDate != '') {

		dateML +=				'<div style="position:relative;display:block;font-weight:bold;color:#000f48;font-size:12px;">' +
										'<span style="color:#000f48;">' + dateString + ': </span>' + eventDateML +
									'</div>';
	}

	return(dateML);
}


function getEventTimes(thisEvent) {

	var timeML = '';

	if (thisEvent.timeStart != '') {

		timeML +=				'<div style="position:relative;display:block;font-weight:bold;color:#000f48;font-size:12px;">' +
										'<span style="color:#000f48;">Time: </span>' + getTimeInWords(thisEvent) +
									'</div>';
	}

	return(timeML);
}

function getEventDescription(thisEvent) {

	var descML = '';
	if (thisEvent.description != '') {
		descML += '<div style="position:relative;display:block;font-size:12px;color:#000;"><p style="margin-top:5px;margin-bottom:5px;">' + replace(thisEvent.description, '|' , '</p><p style="margin-top:5px;margin-bottom:5px;">') + '</p></div>';
	}	
	return(descML);
	
}


function getEventLocation(thisEvent) {

	var locML = '';

	if (thisEvent.title != '' && thisEvent.type != 'field_seminar') {

		locML +=				'<div style="position:relative;display:block;color:#000f48;font-size:12px;">' +
										'<span style="font-weight:bold;color:#000f48;">Location: </span>' + thisEvent.place +
									'</div>';
	}

	return(locML);
}

function getEventDirections(thisEvent) {
	var dirML = '';
	
	if (thisEvent.directions != '') {
		dirML += '<div style="position:relative;margin-top:5px;margin-bottom:5px;padding:5px;background-color:#c0c5fe;"><span style="color:#000f48;font-weight:bold;font-size:12px;">Directions:</span><br><ul style="margin-top:0px;margin-bottom:0px;font-size:12px;marker-offset:4px;padding-left:0px;list-style-position:inside;"><li style="margin-left:3px;margin-right:0px;">' + replace(thisEvent.directions, '|', '</li><li  style="margin-left:3px;margin-right:0px;">') + '</li></ul></div>';
	}

	return(dirML);

}


function getEventContacts(thisEvent) {

	var contactML = '';

	if (thisEvent.contact != '') {

			var theseConts = '';

			theseConts = thisEvent.contact.split(',');

			for (var j=0;j< theseConts.length;j++ ) {
				
				var contInfo = theseConts[j].split('|');			

				contactML +=				'<div style="position:relative;display:block;color:#000f48;font-size:12px;">' +
												'<span style="font-weight:bold;color:#000f48;">Contact: </span>';
				if (contInfo[0] != 'none') {
					contactML +=				' &nbsp; ' + contInfo[0];
				}

				if (contInfo[1] != 'none') {
					if (contInfo[1].indexOf('@') == -1) {
						contactML +=				' &nbsp; ' + '<a href="' + (contInfo[1].indexOf('http://')==-1?'http://' + contInfo[1]:contInfo[1]) + '" target="top">' + contInfo[1] + '</a>';
					} else {
						contactML +=				' &nbsp; ' + '<a href="mailto:' + contInfo[1] + '">' + contInfo[1] + '</a>';
					}
				}

				if (contInfo[2] != 'none') {
					contactML +=				' &nbsp; ' + contInfo[2];
				}

				contactML +=				'</div>';

			}
			
		}

	return(contactML);
}

function getEventSpeakers(thisEvent) {

	var speakerML = '';

	if (thisEvent.speakers != '') {
			
			var speakerTitle = 'Lead by';

			if (thisEvent.type == 'fieldtrip') {
				speakerTitle = 'Guide';
				if (thisEvent.speakers.indexOf('|') != -1) {
					speakerTitle += 's';
				}
			} else if (thisEvent.type == 'event') {
				speakerTitle = 'Presenters';
				if (thisEvent.speakers.indexOf('|') != -1) {
					speakerTitle += 's';
				}
			}

			speakerML +=				'<div style="position:relative;display:block;color:#000f48;font-size:12px;">' +
											'<span style="font-weight:bold;color:#000f48;">' + speakerTitle + ': </span>' + replace(thisEvent.speakers, '|', ' & ') +
										'</div>';
		}

	return(speakerML);

}


function getEventSponsors(thisEvent) {

	var sponsorML = '';

	if (thisEvent.sponsors != '') {
			
		var sponsorTitle = 'Sponsors';

		if (thisEvent.type == 'canoe') {
			sponsorTitle = 'Sponsored by';
		} else if (thisEvent.type == 'event') {
			sponsorTitle = 'Organizers';
			if (thisEvent.speakers.indexOf('|') != -1) {
				sponsorTitle += 's';
			}
		}

		sponsorML +=				'<div style="position:relative;display:block;color:#000f48;font-size:12px;">' +
											'<span style="font-weight:bold;color:#000f48;">' + sponsorTitle + ': </span>' + replace(thisEvent.sponsors, '|', ' & ') +
										'</div>';
	}

	return(sponsorML);

}


function getDateInWords(thisDate) {

	var dateWords = thisDate;

	if (dateWords == 'variable') {
		
		dateWords = 'Variable dates - call for more info.';

	} else if (dateWords.indexOf('recurring') == -1) {

		var newDate = new Date();
		newDate.setFullYear(thisDate.split('.')[0], thisDate.split('.')[1]-1, thisDate.split('.')[2]);

		dateWords = dayNames[newDate.getDay()] + ', ' + monthNames[(thisDate.split('.')[1]-1)] + ' ' + thisDate.split('.')[2] + ', ' + thisDate.split('.')[0];
	} else {
		var rDatesWords = '';

		var theDay = dayNames[thisDate.split('|')[1].split('.')[0]];

		var recurenceVal = thisDate.split('|')[1].split('.')[1];

		var recurringAt = '';

		if (recurenceVal == null) {

			rDatesWords = 'Every ' + theDay;
			
		} else {

			if (recurenceVal == 1) {
				recurringAt = 'First';
			} else if (recurenceVal == 2) {
				recurringAt = 'Second';
			} else if (recurenceVal == 3) {
				recurringAt = 'Third';
			} else if (recurenceVal == 4) {
				recurringAt = 'Fourth';
			} else if (recurenceVal == 'last') {
				recurringAt = 'Last';
			}

			rDatesWords = recurringAt + ' ' + theDay + ' of every month';

		}

		dateWords = rDatesWords;
	}

	return(dateWords);
}


function getTimeInWords(thisEvent) {

	var timeWords = '';

	var tStart = thisEvent.timeStart;
	var tStop = thisEvent.timeStop;

	if ((tStart) == '') {
		timeWords = 'pre-arranged time';
	} else if ((tStart-0) < 1000) {
		timeWords += tStart.substring(0,1) + ':' + tStart.substring(1) + 'am';
	} else if ((tStart-0) < 1200) {
		timeWords += tStart.substring(0,2) + ':' + tStart.substring(2) + 'am';
	} else if ((tStart-0) == 1200) {
		timeWords += '12:00pm';
	} else if ((tStart-0) < 1300) {
		timeWords += tStart.substring(0,2) + ':' + tStart.substring(2) + 'pm';
	} else {
		timeWords += (tStart.substring(0,2) - 12) + ':' + tStart.substring(2) + 'pm';
	}	

	if (tStop == '') {
		timeWords = 'starting at ' + timeWords;
	} else {

		timeWords += ' to ';
		if ((tStop-0) < 1000) {
			timeWords += tStop.substring(0,1) + ':' + tStop.substring(1) + 'am';
		} else if ((tStop-0) < 1200) {
			timeWords += tStop.substring(0,2) + ':' + tStop.substring(2) + 'am';
		}  else if ((tStop-0) == 1200) {
			timeWords += '12:00pm';
		} else if ((tStop-0) < 1300) {
			timeWords += tStop.substring(0,2) + ':' + tStop.substring(2) + 'pm';
		} else {
			timeWords += (tStop.substring(0,2) - 12) + ':' + tStop.substring(2) + 'pm';
		}	
	}

	return(timeWords);
}

//************************EVENT CREATION FUNCTIONS

var event = null;

function newEvent() {

	//event.date is in YYYY.MM.DD format
	//event.time is in 0-23 military format

	event = new Object();
	event.date = null;
	event.timeStart = null;
	event.timeStop = null;
	event.title = null;
	event.descriptionML = null;
	event.shortTitle = null;
	event.shortDescription = null;
	event.speakers = null;
	event.sponsors = null;
	event.type = null;
	event.contact = null;
	event.place = null;
	event.directions = null;

}

var currMonth = today.getMonth();

function addEvent(year) {

	if (!exists(year)) {
		
		if (event.date.indexOf('20') == 0) {
			year = event.date.split('.')[0];
		} else {
			year = currYear;
		}
	}

	if (event.date == null) {
		event.date = '';
	} 

	if (event.timeStart == null) {
		event.timeStart = '';
	} 

	if (event.timeStop == null) {
		event.timeStop = '';
	} 

	if (event.title == null) {
		event.title = '';
	} 

	if (event.descriptionML == null) {
		event.descriptionML = '';
	} 

	if (event.shortTitle == null) {
		event.shortTitle = '';
	} 

	if (event.shortDescription == null) {
		event.shortDescription = '';
	} 

	if (event.speakers == null) {
		event.speakers = '';
	} 

	if (event.sponsors == null) {
		event.sponsors = '';
	} 

	if (event.type == null) {
		event.type = '';
	} 

	if (event.contact == null) {
		event.contact = '';
	} 

	if (event.place == null) {
		event.place = '';
	} 

	if (event.directions == null) {
		event.directions = '';
	}

	eval('events_' + year + '[events_' + year + '.length] = new Object(); events_' + year + '[events_' + year + '.length - 1] = event;');
}

function findEventByDate(eDate) {

	var eventID = -1;

	//add code to catch recurring events

	for (var i=0;i<events.length;i++ ) {
		if (events[i].date.indexOf(eDate) != -1) {
			return(i);
		}
	}

	return(eventID);

}


function sortEvents(theseEventsArray, byWhich) {

	var lowestInd = null;
	var thisOne = null; 
	
	var sortedEvents = new Array();

	var theseEvents = new Array();
	theseEvents = theseEventsArray;

	var sortedList = new Array();
	sortedList = theseEventsArray;

	for (var i=0;i<theseEvents.length ;i++ ) {
		
		if (theseEvents[i]!=null) {

			thisOne = theseEvents[i];

			for (var j=0;j<theseEvents.length ;j++ ) {

				if (theseEvents[j]!=null) {
	
					var currIData = '';
					var currJData = '';

					if (byWhich == 'date') {
							
						currIData = thisOne.date;
						currJData = theseEvents[j].date;
						
					} else if (byWhich == 'title') {
						
						currIData = thisOne.title;
						currJData = theseEvents[j].title;

					} else if (byWhich == 'timeStart') {
						
						currIData = thisOne.timeStart - 0;
						currJData = theseEvents[j].timeStart - 0;

					} else if (byWhich == 'type') {
						
						currIData = thisOne.type;
						currJData = theseEvents[j].type;

					}
					
					if (currJData < currIData) {
						thisOne = theseEvents[j];
						lowestInd = j;
					}
				}


			}

			if (thisOne != theseEvents[i]) {				
				i=0;				
				sortedEvents[sortedEvents.length] = thisOne;
				theseEvents[lowestInd] = null;
			}			
		}

		
	}

	//and get the last one left

	for (var i=0;i<theseEvents.length ;i++ ) {
		if (theseEvents[i] != null) {
			sortedEvents[sortedEvents.length] = theseEvents[i];
		}
	}

	sortedList = sortedEvents;	

	return(sortedList);

}


function setEventsArray(year) {
	eventsArray = new Array();

	if (year > 2006) {

		if (!eval('eventsLoaded_' + year)) {
			//we need to load the calendar data for this year then
			var headID = document.getElementsByTagName("head")[0];  
			var widgetScript = document.createElement('script');
			widgetScript.type = 'text/javascript';
			
			widgetScript.src = 'cal_' + year + '.js?' + getSecondsParm();
			headID.appendChild(widgetScript);
		}		

	}
	
	currEventArrayYear = year;
}

function getSecondsParm() {
	var thisTime = new Date();
	return(thisTime.getTime());
}



//---begin accessory funcs------------------------------------
//these are utility functions that are called in rendering the calendar. They are useful aside fro the cal
//so they could also be kept in a general utils file and included as needed

rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
        rnd.seed = (rnd.seed*9301+49297) % 233280;
        return rnd.seed/(233280.0);
};

function rand(number) {
        return Math.ceil(rnd()*number);
};


	function replace(string,text,by) {
		// Replaces text with by in string
		var strLength = string.length, txtLength = text.length;
		if ((strLength == 0) || (txtLength == 0)) return string;

		var i = string.indexOf(text);
		if ((!i) && (text != string.substring(0,txtLength))) return string;
		if (i == -1) return string;

		var newstr = string.substring(0,i) + by;

		if (i+txtLength < strLength)
			newstr += replace(string.substring(i+txtLength,strLength),text,by);

		return newstr;
	}


	function navOver(thisObj, bgColor) {
		thisObj.style.backgroundColor = bgColor;
		if (document.getElementById(thisObj.id + '_line')) {
			document.getElementById(thisObj.id + '_line').style.backgroundColor = '#000f48';
		}
	}

	function navOff(thisObj, bgColor) {
		if (document.getElementById(thisObj.id + '_line')) {
			document.getElementById(thisObj.id + '_line').style.backgroundColor = '#000f48';
		}
		thisObj.style.backgroundColor = bgColor;
	}


	function exists(thing) {

	if (thing != null) {
		if ((thing + '') == '0') {
			return(true);
		} else if (thing) {
			return(true);
		}
	}

	return(false);
}



function getCurrentDate() {

	var currDate = '';

	var theDate = new Date();
	var monthname = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	currDate +=monthname[theDate.getMonth()] + " ";
	currDate +=theDate.getDate() + ", ";
	currDate +=theDate.getFullYear();

	return(currDate);
}

function Trim(TRIM_VALUE){
if(TRIM_VALUE.length < 1){
return"";
}
TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);
if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
} //End Function

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function

//---end accessory funcs------------------------------------

document.write('<scr' + 'ipt type="text/javascript" src="cal_' + currYear + '.js?' + getSecondsParm() + '"><' + '/sc' + 'ript>');