// Calendar View Script

// REQUIRED PAGE INSERTED INTO WEBPAGE BEFORE THIS PAGE...
// - CalData

// Variables for Calendar Display...
var ND = 0; 
var FD = 0; 
var ID = 0; 
var WD = 0; 
var OD = 0; 
var TD = 0; 
var BD = 0; 
var TR = 0; 
var IR = 0; 
var BR = 0; 
var NR = 0; 

// Calendar Heading...
var prevMonth;
var prevMonthDate = new Date();
var thisMonth;
var nextMonth;
var nextMonthDate = new Date();
var thisYear;
var todayDate = new Date();
var dayViewDate;

// CSS Class Name Holder...
var cssClass = "";

// For Types Display
var typeLabelIndicator = 0;

// Functions...
function InitializeCalendar(){
	//Get date of the first day of selected month and year...
	var d = new Date();
	d.setFullYear(selectedYear,selectedMonth,1);
	FD = d.getDay(); 	// (Sunday=0, Monday=1, etc)
	
	//Get number od days in selected Month
	ND = GetNumberOfDays(selectedMonth,selectedYear); 
	
	//Contants...
	ID = 21; 	// Number of "Inner Days" displayed on a typical calendar (there's always atleast 21 days displayed on any calendar
	WD = 7; 	// "Week Days" (the number of days in a Week)
	
	//Calculations for Numbers of Days...
	OD = ND - ID; 	// Number of "Outer Days" displayed on this particular calendar (the number of total month days minus the "inner days"
	TD = WD - FD; 	// "Top Days" of this particular calendar (if the first day of the month is Sunday, the Top Days is 7; if it's Saturday, Top Days is 1; etc
	BD = OD - TD; 	// "Bottom Days" of this particular calendar (varies based on the number of "Outer days" and on what weekday the first day of the month falls)
	
	//Calculations for Number of Rows in Calendar
	TR = 1; 						// Number of "Top Rows" of a typical calendar (will always be 1 since the first day of the month will ALWAYS start on a day between Sunday and Saturday of the top row in a calendar)
	IR = 3; 						// Number of "Inner Rows" of a typical calendar (will always be 3 since the 2nd, 3rd, and 4th rows of a typical calendar will ALWAYS be a valid calendar day)
	BR = Math.round(BD/WD + 0.49); 	// Number of "Bottom Rows" of this particular calendar (will be between 0 and 2 based on the number of days in the calendar and what day the first day falls on)	
	NR = TR + IR + BR; 				// Total "Number of Rows" in this particular calendar (can be between 4 and 6 rows)	
	

	// Heading Info...
	nextMonthDate.setFullYear(selectedYear,selectedMonth+1,1);
	prevMonthDate.setFullYear(selectedYear,selectedMonth-1,1);

	nextMonth = nextMonthDate.getMonth();
	prevMonth = prevMonthDate.getMonth();
	
	var e = new Date();
	var todayDate = e.getDate();
	if (todayDate == 1){dayViewDate = "1st";} else if (todayDate == 2){dayViewDate = "2nd";} else {dayViewDate = todayDate + "th";}
}

function GetNumberOfDays(selectedMonth,selectedYear){
	// Create date for first day of next month
	var d = new Date();
	
	if (selectedMonth == 11){
		// Go to Next year
		d.setFullYear(selectedYear+1,1,1);
	}
	else {
		// Go to Next month
		d.setFullYear(selectedYear,selectedMonth+1,1);
	}
	
	// Get day number of last day in this month
	d.setDate(d.getDate()-1);
	return d.getDate();
}

function DisplayCalendar(){
	// Display Header...
	//DisplayCalendarHeader();
	DisplayCalendarHeaderWithDepts();

	// Display Days...
	DisplayCalendarDays();
	
	// Display Footer...
	DisplayCalendarFooter();
}

function DisplayCalendarHeader(){
	document.write ("<table border=\"0\" width=\"" + calendarWidth + "\" cellpadding=\"0\" cellspacing=\"0\" class=\"cal\">");
	document.write ("<tr>");
	document.write ("<td " + calHeadingLink + " align=\"left\" colspan=\"2\">&nbsp;<a href=\"/CampusCalendar/index.cfm?ID=CalView&selMonth=" + (prevMonthDate.getMonth()+1) + "&selYear=" + MozillaYearFix(prevMonthDate.getYear()) + "\">&lt;&lt; " + calMonths[prevMonth] + "</a></td>");
	document.write ("<td " + calHeading + " colspan=\"3\">" + calMonths[selectedMonth] + ", " + selectedYear + "</td>");
	document.write ("<td " + calHeadingLink + " align=\"right\" colspan=\"2\"><a href=\"/CampusCalendar/index.cfm?ID=CalView&selMonth=" + (nextMonthDate.getMonth()+1) + "&selYear=" + MozillaYearFix(nextMonthDate.getYear()) + "\">" + calMonths[nextMonth] + " &gt;&gt;</a>&nbsp;</td>");
	document.write ("</tr>");
	document.write ("<tr>");
	document.write ("<td class=\"MirrorStandardDisplay\" align=\"right\" " + calHeadingViews + " colspan=\"7\">");
	document.write ("<a href=\"index.cfm?ID=ListView&selMonth=" + (selectedMonth+1) + "&selYear=" + selectedYear + "\" >List View</a> | <a href=\"index.cfm?ID=DayView&selDay=" + todayDate.getDate() + "&selMonth=" + (selectedMonth+1) + "&selYear=" + selectedYear + "\">Day View (" + dayViewDate + ")</a>");
	document.write ("&nbsp;&nbsp;</td>");
	document.write ("</tr>");
	document.write ("<tr>");
	document.write ("<td " + calHeadingDay + " width=\"14%\">Sunday</td>");
	document.write ("<td " + calHeadingDay + " width=\"14%\">Monday</td>");
	document.write ("<td " + calHeadingDay + " width=\"14%\">Tuesday</td>");
	document.write ("<td " + calHeadingDay + " width=\"14%\">Wednesday</td>");
	document.write ("<td " + calHeadingDay + " width=\"14%\">Thursday</td>");
	document.write ("<td " + calHeadingDay + " width=\"14%\">Friday</td>");
	document.write ("<td " + calHeadingDay + " width=\"14%\">Saturday</td>");
	document.write ("</tr>");
}

function DisplayCalendarHeaderWithDepts(){
	document.write ("<table border=\"0\" width=\"" + calendarWidth + "\" cellpadding=\"0\" cellspacing=\"0\" class=\"cal\">");
	document.write ("<tr>");
	document.write ("<td " + calHeadingLink + " align=\"left\" colspan=\"2\">&nbsp;<a href=\"/CampusCalendar/index.cfm?ID=CalView&selMonth=" + (prevMonthDate.getMonth()+1) + "&selYear=" + MozillaYearFix(prevMonthDate.getYear()) + "\">&lt;&lt; " + calMonths[prevMonth] + "</a></td>");
	document.write ("<td " + calHeading + " colspan=\"3\">" + calMonths[selectedMonth] + ", " + selectedYear + "</td>");
	document.write ("<td " + calHeadingLink + " align=\"right\" colspan=\"2\"><a href=\"/CampusCalendar/index.cfm?ID=CalView&selMonth=" + (nextMonthDate.getMonth()+1) + "&selYear=" + MozillaYearFix(nextMonthDate.getYear()) + "\">" + calMonths[nextMonth] + " &gt;&gt;</a>&nbsp;</td>");
	document.write ("</tr>");
	document.write ("<tr>");
	document.write ("<td " + calHeadingViews + " colspan=\"7\">");
	document.write ("<table border=\"0\" width=\"" + calendarWidth + "\" cellpadding=\"0\" cellspacing=\"0\">");
	document.write ("<tr>");
	document.write ("<td " + calHeadingViews + " align=\"left\" width=\"50%\">");
	document.write ("&nbsp;Category: <select name=\"selDepts\" id=\"selDepts\" onchange=\"javascript:ChangeDept();\">");
	for (i=0; i<calDeptIDs.length; i++){
		if (calDeptIDs[i] == selectedDeptID){
			document.write ("<option value=\"" + calDeptIDs[i] + "\" selected>" + calDeptNames[i] + "</option>");
		}
		else{
			document.write ("<option value=\"" + calDeptIDs[i] + "\">" + calDeptNames[i] + "</option>");
		}
	}
	document.write ("</select>");
	document.write ("</td>");
	document.write ("<td class=\"MirrorStandardDisplay\" align=\"right\" width=\"50%\">");
	document.write ("<a href=\"PrintFriendly.cfm?ID=CalView&selMonth=" + (selectedMonth+1) + "&selYear=" + selectedYear + "\"><strong>Print</strong></a> | ");
	document.write ("<a href=\"index.cfm?ID=ListView&selMonth=" + (selectedMonth+1) + "&selYear=" + selectedYear + "\" >List View</a> | <a href=\"index.cfm?ID=DayView&selDay=" + todayDate.getDate() + "&selMonth=" + (selectedMonth+1) + "&selYear=" + selectedYear + "\">Day View (" + dayViewDate + ")</a>");
	document.write ("</td></tr></table>");
	document.write ("</td>");
	document.write ("</tr>");
	document.write ("<tr>");
	document.write ("<td " + calHeadingDay + " width=\"14%\">Sunday</td>");
	document.write ("<td " + calHeadingDay + " width=\"14%\">Monday</td>");
	document.write ("<td " + calHeadingDay + " width=\"14%\">Tuesday</td>");
	document.write ("<td " + calHeadingDay + " width=\"14%\">Wednesday</td>");
	document.write ("<td " + calHeadingDay + " width=\"14%\">Thursday</td>");
	document.write ("<td " + calHeadingDay + " width=\"14%\">Friday</td>");
	document.write ("<td " + calHeadingDay + " width=\"14%\">Saturday</td>");
	document.write ("</tr>");
}

function ChangeDept(){
	window.location = "/CampusCalendar/index.cfm?ID=CalView&selMonth=" + (selectedMonth+1) + "&selYear=" + selectedYear + "&CatID=" + document.DisplayCalendar.selDepts.value;
}

function DisplayCalendarFooter(){
	document.write ("<tr>");
	document.write ("<td colspan=\"7\" " + calFooter + ">" + footerText + "&nbsp;</td>");
	document.write ("</tr>");
	document.write ("</table>");
}

function DisplayCalendarDays(){
	// Counters...
	var cellCtr = 0;
	
	for (var i=0; i<NR; i++){
		document.write("<tr>");
		//For each row, write out each table cell
		for (var j=0; j<WD; j++){
			//Write Out Calendar Cell
			WriteThisCellWithTypes(cellCtr);
			cellCtr++;
		}
		document.write("</tr>");
	}
}

function WriteThisCell(thisCell){
	// Today's Date
	var today = new Date();
	
	// To Display "Today" indicator
	var topLeft = "";
	
	// Test Calendar Info
	var info = "";
	
	// Decide which classes to use...
	if (thisCell < FD){
		// This cell is not a part of this calendar (before first day in month)
		cssClass = blankCell;
		document.write ("<td " + cssClass + ">&nbsp;</td>");
	}
	else if ((thisCell+1) > (ND+FD)){
		// This cell is not part of this calendar (after last day in month)
		cssClass = blankCell;
		document.write ("<td " + cssClass + ">&nbsp;</td>");
	}
	else {
		// It's either a weekday or weekend
		if (today.getDate() == (thisCell - FD + 1)){
			// It's today
			topLeft = dayCellTopToday;
		}
		else{
			topLeft = dayCellTopLeft;
		}
		if ((thisCell+1)/WD == Math.floor((thisCell+1)/WD)){
			// It's evenly divisible by 7; it's a Saturday
			cssClass = weekendCell;
		}
		else if ((thisCell+7)/WD == Math.floor((thisCell+7)/WD)){
			// It plus 6 days is evenly divisible by 7; it's a Sunday
			cssClass = weekendCell;
		}
		else{
			// It's a day cell
			cssClass = dayCell;
		}
		document.write ("<td " + cssClass + ">");
		document.write ("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">");
		document.write ("<tr>");
		document.write ("<td width=\"75%\" " + topLeft + ">&nbsp;</td>");
		document.write ("<td width=\"25%\" " + dayCellTopRight + ">" + "<a href=\"index.cfm?ID=DayView&selDay=" + (thisCell - FD + 1) + "&selMonth=" + (selectedMonth+1) + "&selYear=" + selectedYear + "\">" + (thisCell - FD + 1) + "</a>" + "</td>");
		document.write ("</tr>");
		document.write ("</table>");
		document.write ("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">");
		document.write ("<tr>");
		document.write ("<td " + dayCellContainer + ">");
		for (var i=0; i<calData_Title.length; i++){
			if (Number(calData_Date[i].substr(6,4)) == selectedYear && Number(calData_Date[i].substr(0,2)) == (selectedMonth+1) && Number(calData_Date[i].substr(3,2)) == (thisCell - FD + 1)){
				document.write ("<span " + calText + "><a href=\"index.cfm?ID=ListView&selMonth=" + (selectedMonth+1) + "&selYear=" + selectedYear + "&selDay=" + (thisCell - FD + 1) + "\" >" + calData_Title[i] + "</a></span><br />");
			} 
		}
		document.write ("</td>");
		document.write ("</tr>");
		document.write ("</table>");
		document.write ("</td>");
	}
}

function WriteThisCellWithTypes(thisCell){
	// Today's Date
	var today = new Date();
	
	// To Display "Today" indicator
	var topLeft = "";
	
	// Test Calendar Info
	var info = "";
	
	// Decide which classes to use...
	if (thisCell < FD){
		// This cell is not a part of this calendar (before first day in month)
		cssClass = blankCell;
		document.write ("<td " + cssClass + ">&nbsp;</td>");
	}
	else if ((thisCell+1) > (ND+FD)){
		// This cell is not part of this calendar (after last day in month)
		cssClass = blankCell;
		document.write ("<td " + cssClass + ">&nbsp;</td>");
	}
	else {
		// It's either a weekday or weekend
		if (today.getDate() == (thisCell - FD + 1)){
			// It's today
			topLeft = dayCellTopToday;
		}
		else{
			topLeft = dayCellTopLeft;
		}
		if ((thisCell+1)/WD == Math.floor((thisCell+1)/WD)){
			// It's evenly divisible by 7; it's a Saturday
			cssClass = weekendCell;
		}
		else if ((thisCell+7)/WD == Math.floor((thisCell+7)/WD)){
			// It plus 6 days is evenly divisible by 7; it's a Sunday
			cssClass = weekendCell;
		}
		else{
			// It's a day cell
			cssClass = dayCell;
		}
		document.write ("<td " + cssClass + ">");
		document.write ("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">");
		document.write ("<tr>");
		document.write ("<td width=\"75%\" " + topLeft + ">&nbsp;</td>");
		document.write ("<td width=\"25%\" " + dayCellTopRight + ">" + "<a href=\"index.cfm?ID=DayView&selDay=" + (thisCell - FD + 1) + "&selMonth=" + (selectedMonth+1) + "&selYear=" + selectedYear + "\">" + (thisCell - FD + 1) + "</a>" + "</td>");
		document.write ("</tr>");
		document.write ("</table>");
		document.write ("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">");
		document.write ("<tr>");
		document.write ("<td " + dayCellContainer + ">");
		for (var j=0; j<calEventTypes.length; j++){
			typeLabelIndicator = 0;
			for (var i=0; i<calData_Title.length; i++){
				if (Number(calData_Date[i].substr(6,4)) == selectedYear && Number(calData_Date[i].substr(0,2)) == (selectedMonth+1) && Number(calData_Date[i].substr(3,2)) == (thisCell - FD + 1) && calData_Type[i] == calEventTypes[j]){
					typeLabelIndicator = typeLabelIndicator + 1;
					if (typeLabelIndicator == 1){
						document.write ("<span " + calTextHeading + ">" + calEventTypes[j] + "</span><br />");
					}
					document.write ("<span " + calText + "><a href=\"index.cfm?ID=ListView&selMonth=" + (selectedMonth+1) + "&selYear=" + selectedYear + "&selDay=" + (thisCell - FD + 1) + "&CatID=" + selectedDeptID + "\" >" + calData_Title[i] + "</a></span><br />");
				} 
			}
			if (typeLabelIndicator > 0){
				document.write ("<br />");
			}
		}
		document.write ("</td>");
		document.write ("</tr>");
		document.write ("</table>");
		document.write ("</td>");
	}
}
