/***************************************************************************************
	JavaScript Calendar - Digital Christian Design
	//Script featured on and available at JavaScript Kit: http://www.javascriptkit.com
	// Functions
		changedate(): Moves to next or previous month or year, or current month depending on the button clicked.
		createCalendar(): Renders the calander into the page with links for each to fill the date form filds above.
			
***************************************************************************************/
var thisDate = 1;								// Tracks current date being written in calendar
var monthLength = new Array(31,31,28,31,30,31,30,31,31,30,31,30,31);
var today = new Date();							// Date object to store the current date
var todaysDay = today.getDay() + 1;				// Stores the current day number 1-7
var todaysDate = today.getDate();				// Stores the current numeric date within the month
var todaysMonth = today.getUTCMonth() + 1;		// Stores the current month 1-12
var todaysYear = today.getFullYear();			// Stores the current year
var monthNum = todaysMonth;						// Tracks the current month being displayed
var yearNum = todaysYear;						// Tracks the current year being displayed
var firstDate = new Date(String(monthNum)+"/1/"+String(yearNum));	// Object Storing the first day of the current month
var firstDay = firstDate.getUTCDay();					// Tracks the day number 1-7 of the first day of the current month
var lastDate = new Date(String(monthNum+1)+"/0/"+String(yearNum));	// Tracks the last date of the current month
var calendarString = "";
var eastermonth = 0;
var easterday = 0;

function easter(year) {
// feed in the year it returns the month and day of Easter using two GLOBAL variables: eastermonth and easterday
var a = year % 19;
var b = Math.floor(year/100);
var c = year % 100;
var d = Math.floor(b/4);
var e = b % 4;
var f = Math.floor((b+8) / 25);
var g = Math.floor((b-f+1) / 3);
var h = (19*a + b - d - g + 15) % 30;
var i = Math.floor(c/4);
var j = c % 4;
var k = (32 + 2*e + 2*i - h - j) % 7;
var m = Math.floor((a + 11*h + 22*k) / 451);
var month = Math.floor((h + k - 7*m + 114) / 31);
var day = ((h + k - 7*m +114) % 31) + 1;
eastermonth = month;
easterday = day;
}

function checkevents(day,month,year,week,dayofweek) {
var numevents = 0;
var floater = 0;
var index =-1;
var enumerator = 0;
var leftover = 0;
var lastday = 0;
var days = monthLength[month - 1];
	if ((month == 3) && (year % 4 == 0))
		days ++;

	events[0][2] = todaysMonth;
	events[0][3] = todaysDate;
	events[0][4] = todaysYear;
	
	for (var i = 0; i < events.length; i++) {
		if (events[i][0] == "W") {
			if ((events[i][3] == dayofweek)) { numevents++; index = i; }
		}
		else if (events[i][0] == "Y") {
			if ((events[i][3] == day) && (events[i][2] == month)) { numevents++;  index = i;  }
		}
		else if (events[i][0] == "F") {
			if ((events[i][2] == 3) && (events[i][3] == 0) && (events[i][4] == 0) ) {
				easter(year);
				if (easterday == day && eastermonth == month) { numevents++;  index = i;  }
			} else {
				floater = floatingholiday(year,events[i][2],events[i][3],events[i][4]);
				if ((month == 5) && (events[i][2] == 5) && (events[i][3] == 4) && (events[i][4] == 2)) {
					if ((floater + 7 <= 31) && (day == floater + 7)) {
						numevents++; 
						index = i;  
					} else if ((floater + 7 > 31) && (day == floater)) { numevents++;  index = i; }
				} else if ((events[i][2] == month) && (floater == day)) { numevents++;  index = i;  }
			}
		}
		// 
		// Check for consecutive days in type, if so handle multi-day event
		//
		else if (events[i][0] == "C") {
			enumerator = events[i][1]; 
			var lastday = events[i][3];
			lastday += enumerator - 1;
			
			if ((day >= events[i][3]) && (day <= lastday) && (events[i][2] == month) && (events[i][4] == year)) {
				numevents++; 
				index = i;  
			}
			//
			// check to see if we just crossed a month boundary
			//
			else if ((day <= enumerator) && (events[i][2] == month - 1) 
							&& ((events[i][2] + enumerator) > days) &&  (events[i][4] == year)){
				//
				// we crossed the line, figure out what's left and see if we are still in the event
				//
				leftover = events[i][3];
				leftover += enumerator;			 // add the enumerator
				leftover -= days;				 // and subract the number of days in the month

				if (day < leftover) {
					numevents++; 
					index = i;  
				}
			}
		}
		else if ((events[i][3] == day) && (events[i][2] == month) && (events[i][4] == year)) {
			 numevents++; 
			 index = i;  
		}
	}
	
	return index;
}

function geteventname(index) {
var label = events[index][6]; 
	return label;
}

function geteventtype(index) {
var type = events[index][5]; 
	return type;
}

function geteventlink(index) {
var type = events[index][7]; 
	return type;
}

function floatingholiday(targetyr,targetmo,cardinaloccurrence,targetday) {
// Floating holidays/events of the events.js file uses:
//	the Month field for the Month (here it becomes the targetmo field)
//	the Day field as the Cardinal Occurrence  (here it becomes the cardinaloccurrence field)
//		1=1st, 2=2nd, 3=3rd, 4=4th, 5=5th, 6=6th occurrence of the day listed next
//	the Year field as the Day of the week the event/holiday falls on  (here it becomes the targetday field)
//		1=Sunday, 2=Monday, 3=Tuesday, 4=Wednesday, 5=Thurday, 6=Friday, 7=Saturday
//	example: "F",	"1",	"3",	"2", = Floating holiday in January on the 3rd Monday of that month.
//
// In our code below:
// 	targetyr is the active year
// 	targetmo is the active month (1-12)
// 	cardinaloccurrence is the xth occurrence of the targetday (1-6)
// 	targetday is the day of the week the floating holiday is on
//		0=Sun; 1=Mon; 2=Tue; 3=Wed; 4=Thu; 5=Fri; 6=Sat
//		Note: subtract 1 from the targetday field if the info comes from the events.js file
//
// Note:
//	If Memorial Day falls on the 22nd, 23rd, or 24th, then we add 7 to the dayofmonth to the result.
//
// Example: targetyr = 2052; targetmo = 5; cardinaloccurrence = 4; targetday = 1
//	This is the same as saying our floating holiday in the year 2052, is during May, on the 4th Monday
//
var firstdate = new Date(String(targetmo)+"/1/"+String(targetyr));	// Object Storing the first day of the current month.
var firstday = firstdate.getUTCDay();	// The first day (0-6) of the target month.
var dayofmonth = 0;	// zero out our calendar day variable.

	targetday = targetday - 1;

	if (targetday >= firstday) {
		cardinaloccurrence--;	// Subtract 1 from cardinal day.
		dayofmonth = (cardinaloccurrence * 7) + ((targetday - firstday)+1);
	} else {
		dayofmonth = (cardinaloccurrence * 7) + ((targetday - firstday)+1);
	}
return dayofmonth;
}

