
/***************************************************************************************
	JavaScript Calendar - Digital Christian Design
	//Script featured on and available at JavaScript Kit: http://www.javascriptkit.com
	// 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 wordMonth = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
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 numbDays = 0;
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 changedate(buttonpressed) {
	if (buttonpressed == "prevmo") monthNum--;
	else if (buttonpressed == "nextmo") monthNum++;
	else  if (buttonpressed == "return") { 
		monthNum = todaysMonth;
		yearNum = todaysYear;
	}

	if (monthNum == 0) {
		monthNum = 12;
		yearNum--;
	}
	else if (monthNum == 13) {
		monthNum = 1;
		yearNum++
	}

	lastDate = new Date(String(monthNum+1)+"/0/"+String(yearNum));
	numbDays = lastDate.getDate();
	firstDate = new Date(String(monthNum)+"/1/"+String(yearNum));
	firstDay = firstDate.getDay() + 1;
	createCalendar();
	return;
}

function createCalendar() {
	calendarString = '';
	var daycounter = 0;

	calendarString += '<table border="0" cellpadding="0" cellspacing="0" style="width:300px; background-color:#f6f6f6;">';
	calendarString += '<tr><td colspan="3" style="width:300px; height:10px; background-color:#ffffff;"><img src="spacer.gif" width="300" height="10" alt="" /></td></tr>';
	calendarString += '<tr><td colspan="3" style="width:300px; height:25px; background-color:#ffffff;"><table border="0" cellpadding="0" cellspacing="0" style="width:300px; height:25px;"><tr><td style="width:50px; height:25px;"><a href="javascript:changedate(\'prevmo\');"><div align="left"><img src="calendar_prev.jpg" width="32" height="25" alt="" /></div></a></td><td style="width:200px; height:25px; vertical-align:middle;"><div align="center"><b>' + wordMonth[monthNum-1] + '&nbsp;' + yearNum + '</b></div></td><td style="width:50px; height:25px;"><a href="javascript:changedate(\'nextmo\');"><div align="right"><img src="calendar_next.jpg" width="32" height="25" alt="" /></div></a></td></tr></table></td></tr>';
	calendarString += '<tr><td colspan="3" style="width:300px; height:6px; background-color:#ffffff;"><img src="spacer.gif" width="300" height="6" alt="" /></td></tr>';
	calendarString += '<tr><td colspan="3" style="width:300px; height:45px;"><img src="calendar_top.jpg" width="300" height="45" alt="" /></td></tr>';
	calendarString += '<tr><td style="width:10px;"><img src="spacer.gif" width="10" height="100" alt="" /></td><td style="width:280px;"><div align="center">';
	calendarString += '<table cellpadding="0" cellspacing="0" style="width:280px;">';

	thisDate == 1;

	for (var i = 1; i <= 6; i++) {
		calendarString += '<tr>';
		for (var x = 1; x <= 7; x++) {
			daycounter = (thisDate - firstDay)+1;
			thisDate++;
			if ((daycounter > numbDays) || (daycounter < 1)) {
				calendarString += '<td class=\"calendar_blankspace\">&nbsp;<\/td>';
			} else {
				if (checkevents(daycounter,monthNum,yearNum,i,x) || ((todaysDay == x) && (todaysDate == daycounter) && (todaysMonth == monthNum))){
					if ((todaysDay == x) && (todaysDate == daycounter) && (todaysMonth == monthNum)) {
						calendarString += '<td class=\"calendar_today\"><a href=\"javascript:showevents(' + daycounter + ',' + monthNum + ',' + yearNum + ',' + i + ',' + x + ');\" class=\"calendar_link_today\">' + daycounter + '<\/a><\/td>';
					}
 					else	calendarString += '<td class=\"calendar_event\"><a href=\"javascript:showevents(' + daycounter + ',' + monthNum + ',' + yearNum + ',' + i + ',' + x + ');\" class=\"calendar_link_event\">' + daycounter + '<\/a><\/td>';
				} else {
					calendarString += '<td class=\"calendar_noevent\">' + daycounter + '<\/td>';
				}
			}
		}
		calendarString += '<\/tr>';
	}

	calendarString += '<\/table></div></td>';
	calendarString += '<td style="width:10px;"><img src="spacer.gif" width="10" height="100" alt="" /></td></tr>';
	calendarString += '<tr><td colspan="3" style="width:300px; height:10px;"><img src="spacer.gif" width="300" height="10" alt="" /></td></tr>';
	calendarString += '<tr><td colspan="3" style="width:300px; height:20px; background-color:#ffffff; vertical-align:bottom;"><div align="right" style="padding-right:10px;"><a href="javascript:changedate(\'return\');" style="text-decoration:none; font-size:12px;"><img src="calendar_today.jpg" width="60" height="15" alt="" /></a></div></td></tr>';
	calendarString += '</table>';

	var object=document.getElementById('calendar');
	object.innerHTML= calendarString;
	thisDate = 1;
}

function checkevents(day,month,year,week,dayofweek) {
var numevents = 0;
var floater = 0;

	for (var i = 0; i < events.length; i++) {
		if (events[i][0] == "W") {
			if ((events[i][2] == dayofweek)) numevents++;
		}
		else if (events[i][0] == "Y") {
			if ((events[i][2] == day) && (events[i][1] == month)) numevents++;
		}
		else if (events[i][0] == "F") {
			if ((events[i][1] == 3) && (events[i][2] == 0) && (events[i][3] == 0) ) {
				easter(year);
				if (easterday == day && eastermonth == month) numevents++;
			} else {
				floater = floatingholiday(year,events[i][1],events[i][2],events[i][3]);
				if ((month == 5) && (events[i][1] == 5) && (events[i][2] == 4) && (events[i][3] == 2)) {
					if ((floater + 7 <= 31) && (day == floater + 7)) {
						numevents++;
					} else if ((floater + 7 > 31) && (day == floater)) numevents++;
				} else if ((events[i][1] == month) && (floater == day)) numevents++;
			}
		}
		else if ((events[i][2] == day) && (events[i][1] == month) && (events[i][3] == year)) {
			numevents++;
		}
	}

	if (numevents == 0) {
		return false;
	} else {
		return true;
	}
}

function showevents(day,month,year,week,dayofweek) {
var theevent = '' + wordMonth[monthNum-1] + ' ' + day + '\n\n';
var floater = 0;
var today = 1;

	for (var i = 0; i < events.length; i++) {
		// First we'll process recurring events (if any):
		if (events[i][0] != "") {
			if (events[i][0] == "D") {
			}
			if (events[i][0] == "W") {
				if ((events[i][2] == dayofweek)) {
				theevent += '' + events[i][6] + '\n';
				theevent += '' + events[i][4] + ' - ' + events[i][7] + '\n\n';
				today = 0;
				}
			}
			if (events[i][0] == "M") {
			}
			if (events[i][0] == "Y") {
				if ((events[i][2] == day) && (events[i][1] == month)) {
				theevent += '' + events[i][6] + '\n\n';
				today = 0;
				}
			}
			if (events[i][0] == "F") {
				if ((events[i][1] == 3) && (events[i][2] == 0) && (events[i][3] == 0) ) {
					if (easterday == day && eastermonth == month) {
						theevent += '' + events[i][6] + '\n\n';
						today = 0;
					} 
				} else {
					floater = floatingholiday(year,events[i][1],events[i][2],events[i][3]);

					if ((month == 5) && (events[i][1] == 5) && (events[i][2] == 4) && (events[i][3] == 2)) {
						if ((floater + 7 <= 31) && (day == floater + 7)) {
							theevent += '' + events[i][6] + '\n';
							theevent += '' + events[i][4] + ' - ' + events[i][7] + '\n\n';
							today = 0;

						} else if ((floater + 7 > 31) && (day == floater)) {
							theevent += '' + events[i][6] + '\n';
							theevent += '' + events[i][4] + ' - ' + events[i][7] + '\n\n';
							today = 0;
						}
					} else if ((events[i][1] == month) && (floater == day)) {
						theevent += '' + events[i][6] + '\n';
						theevent += '' + events[i][4] + ' - ' + events[i][7] + '\n\n';
						today = 0;
					}
				}
		}
		}
		// Now we'll process any One Time events happening on the matching month, day, year:
		else if ((events[i][2] == day) && (events[i][1] == month) && (events[i][3] == year)) {
			theevent += '' + events[i][6] + '\n';
			theevent += 'Start: ' + events[i][4] + '\n';
			theevent += '' + events[i][7] + '\n\n';
			today = 0;
		}
	}
	if (today) {
	theevent += 'There are no events to show for today...';
	}

	theevent = theevent.substring(0,((theevent.length)-2));
	alert(theevent);

}

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;
}

