/************************************************************
 *
 * Copyright (c) 2005 Brian T. Kelley. All Rights Reserved.
 *
 ***********************************************************/
var calXml = new Array();
var calData = new Array();
var calendarUpdating = new Array();

function firstRunCalendar(id) {
	var calendarDate = document.getElementById(id + "_CalendarDate").value.split("-");
	
	if(getBrowser() == "MSIE")
		document.getElementById(id + "_Table").style.width = "auto";
	
	loadMonth(id, calendarDate[1], calendarDate[0]);
} // function firstRunCalendar(id)

function loadMonth(id, year, month) {
	var args = ["id", id, "year", year, "month", month];
	document.getElementById(id + "_CalendarDate").value = month + "-" + year;
	loadXmlDocument("", "Calendar", "GetCalendarForDate", args, updateCalendar);
} // function loadMonth(id, year, month)

function loadSearch(id, year, month, query) {
	var args = ["id", id, "year", year, "month", month, "query", query];
	document.getElementById(id + "_CalendarDate").value = month + "-" + year;
	loadXmlDocument("", "Calendar", "SearchCalendar", args, updateSearchCalendar);
} // function loadSearch(id, year, month, query)

function calendarMove(id, by) {
	var calendarDate = document.getElementById(id + "_CalendarDate");
	var date = calendarDate.value.split("-");
	
	date[0] = parseInt(date[0]);
	date[0] += by;
	if(by < 0)
		while(date[0] < 1) {
			date[0] += 12;
			date[1]--;
		} // while
	else
		while(date[0] > 12) {
			date[0] -= 12;
			date[1]++;
		} // while
	
	document.getElementById(id + "_Heading").innerHTML += " - Loading";
	loadMonth(id, date[1], date[0]);
} // function calendarMove(id, by)

function calendarMoveWeek(id, by) {
	var weekView = document.getElementById(id + "_WeekView");
	var weekInfo = weekView.value.split("-");
	var current = document.getElementById(id + "_CalendarDate").value.split("-");
	var date = new Date(current[1], weekInfo[1], weekInfo[0]);
	
	var newDateMS = date.getTime() + (1000 * 60 * 60 * 24 * 7 * by);
	var newDate = new Date(newDateMS);
	
	weekView.value = newDate.getDate() + "-" + newDate.getMonth();
	if(--current[0] == newDate.getMonth())
		drawCalendarTable(id, newDate.getFullYear(), newDate.getMonth());
	else
		loadMonth(id, newDate.getFullYear(), newDate.getMonth() + 1);
} // function calendarMoveWeek(id, by)

function calendarJumpToDay(id, date) {
	var weekInfo = document.getElementById(id + "_WeekView").value.split("-");
	var current = document.getElementById(id + "_CalendarDate").value.split("-");
	document.getElementById(id + "_DayView").value = date;
	drawCalendarTable(id, parseInt(current[1]), parseInt(weekInfo[1]));
} // function calendarJumpToDay(id, date)

function calendarMonthJump(id) {
	if(!calendarUpdating[id]) {
		var monthJump = document.getElementById(id + "_monthJump");
		var date = monthJump.options[monthJump.selectedIndex].value.split("-");
		document.getElementById(id + "_Heading").innerHTML += " - Loading";
		loadMonth(id, date[1], date[0]);
	} // if
} // function calendarMonthJump(id)

function calendarCategoryChange(id) {
	if(!calendarUpdating[id]) {
		var categorySelect = document.getElementById(id + "_categorySelect");
		var date = document.getElementById(id + "_CalendarDate").value.split("-");
		
		drawCalendarTable(id, date[1], date[0] - 1);
	} // if
} // function calendarCategoryChange(id)

function calendarChangeView(id) {
	if(!calendarUpdating[id]) {
		var date = document.getElementById(id + "_CalendarDate").value.split("-");
		drawCalendarTable(id, date[1], date[0] - 1);
	} // if
} // function calendarChangeView(id)

function updateCalendar() {
	if(req.readyState == 4) {
		var xml = req.responseXML.getElementsByTagName("Calendar")[0];
		
		if(req.status == 200 && xml != null) {
			var id = xml.getAttribute("id");
			calXml[id] = xml;
			calendarUpdating[id] = true;
			var calendarDate = document.getElementById(id + "_CalendarDate").value.split("-");
			var date = new Date();
			date.setFullYear(calendarDate[1], calendarDate[0] - 1, 1);
			
			// Setup Comboboxes
			var categorySelect = document.getElementById(id + "_categorySelect");
			var monthJump = document.getElementById(id + "_monthJump");
			
			var newCategory = document.createElement("option");
			newCategory.text = calendarAllCategories;
			
			if(getBrowser() == "MSIE") {
				while(monthJump.options.length)
					monthJump.options.remove(0);
				while(categorySelect.options.length)
					categorySelect.options.remove(0);
				categorySelect.add(newCategory);
			} // if
			else {
				while(monthJump.length)
					monthJump.remove(0);
				while(categorySelect.length)
					categorySelect.remove(0);
				categorySelect.add(newCategory, categorySelect.options[0]);
			} // else
			categorySelect.selectedIndex = 0;
			
			// Update Month Jumper
			var month = date.getMonth();
			var year = date.getFullYear() - 1;
			
			for(var i = 0; i < 25; i++) {
				var newMonth = document.createElement("option");
				newMonth.text = monthNames[month] + " " + year;
				
				var newMonthValue = document.createAttribute("value");
				newMonthValue.nodeValue = (month + 1) + "-" + year;
				newMonth.setAttributeNode(newMonthValue);
				
				if(getBrowser() == "MSIE")
					monthJump.add(newMonth);
				else
					monthJump.add(newMonth, monthJump.options[i]);
				if(++month == 12) {
					month = 0;
					year++;
				} // if
			} // for
			monthJump.selectedIndex = 12;
			
			// Update Week and Day Views
			var weekView = document.getElementById(id + "_WeekView");
			if(weekView.value.split("-")[1] != month - 1) {
				var today = new Date();
				today.setMonth(month - 1);
				var thisSunday = today.getDate() - today.getDay();
				var newMonth = today.getMonth();
				if(thisSunday <= 0) {
					if(newMonth == 0)
						newMonth = 11;
					thisSunday += daysInMonth(today.getFullYear(), newMonth);
				} // if
				document.getElementById(id + "_WeekView").value = thisSunday + "-" + newMonth;
			} // if
			
			today = new Date();
			var dayView = document.getElementById(id + "_DayView");
			if(dayView.value == ".1")
				dayView.value = today.getDate();
			
			// Draw Calendar
			if (getBrowser() == "Gecko")
				setTimeout("drawCalendarTable(" + id + "," + date.getFullYear() + "," +
					date.getMonth() + ")", 0);
			else
				drawCalendarTable(id, date.getFullYear(), date.getMonth());
			
			// Set the Month & Year
			document.getElementById(id + "_MonthTitle").innerHTML = monthNames[date.getMonth()] +
				" " + date.getFullYear();

			document.getElementById(id + "_Loading").style.display = "none";
			document.getElementById(id + "_CalContainer").style.display = "inline";
			
			var heading = document.getElementById(id + "_Heading");
			if(heading.innerHTML.indexOf(" - Loading") > 0)
				heading.innerHTML = heading.innerHTML.substring(0,
					heading.innerHTML.indexOf(" - Loading"));
			
			calendarUpdating[id] = false;
		} // if
		else {
			jsFailure("The calendar failed to load and will be unavailable.");
		} // else
	} // if
} // function updateCalendar()

function updateSearchCalendar() {
	if(req.readyState == 4) {
		var xml = req.responseXML.getElementsByTagName("Calendar")[0];
		
		if(req.status == 200 && xml != null) {
			var id = xml.getAttribute("id");
			calXml[id] = xml;
			calendarUpdating[id] = true;
			var calendarDate = document.getElementById(id + "_CalendarDate").value.split("-");
			var date = new Date();
			date.setFullYear(calendarDate[1], calendarDate[0] - 1, 1);
			
			// Hide Some Elements
			document.getElementById(id + "_categorySelect").style.display = "none";
			document.getElementById(id + "_monthJump").style.display = "none";
			document.getElementById(id + "_WeekView").style.display = "none";
			document.getElementById(id + "_changeView").style.display = "none";
			document.getElementById(id + "_nextMonth12").style.display = "none";
			document.getElementById(id + "_previousMonth12").style.display = "none";
			document.getElementById(id + "_download12").style.display = "none";
			document.getElementById(id + "_exitSearch12").style.display = "inline";
			if(document.getElementById(id + "_addevent12") != null)
				document.getElementById(id + "_addevent12").style.display = "none";
			
			// Draw Calendar
			var month = parseInt(calendarDate[0]) - 1;
			var year = parseInt(calendarDate[1]);
			month -= 5;
			if(month < 0) {
				month += 12;
				year--;
			} // if
			
			var frag = document.createElement("td");
			var wroteEvents = false;
			for(var i = 0; i < 12; i++) {
				var Tevents = xml.getElementsByTagName("Event" + (month + 1));
				date.setYear(year);
				date.setMonth(month);
				if(++month == 12) {
					month = 0;
					year++;
				} // if
				
				if(Tevents.length > 0) {
					wroteEvents = true;
					var events = new Array();
					for(var j = 0; j < Tevents.length; j++)
						events[j] = Tevents[j];
					events.sort(calendarSortByDate);
					
					for(var j = 0; j < events.length; j++) {
						date.setDate(events[j].getAttribute("Date"));
						
						var dateID = document.createElement("h3");
						var dateIDText = document.createTextNode(dayNames[date.getDay()] + ", " +
							monthNames[date.getMonth()] + " " + date.getDate() + ", " + year);
						dateID.appendChild(dateIDText);
						frag.appendChild(dateID);
						
						j--;
						var odd = true;
						while(j + 1 < events.length && events[j + 1].getAttribute("Date") == date.getDate()) {
							calendarRenderEvent(id, events[++j], odd, frag);
							frag.appendChild(document.createElement("br"));
							odd = !odd;
						} // while
						frag.appendChild(document.createElement("br"));
					} // for(j)
				} // if
			} // for(i)
			if(!wroteEvents)
				frag.appendChild(document.createTextNode(calendarSearchFailed));
			removeChildren(id + "_THead");
			removeChildren(id + "_TBody");
			var tr = document.createElement("tr");
			tr.appendChild(frag);
			var docFrag = document.createDocumentFragment();
			docFrag.appendChild(tr);
			document.getElementById(id + "_TBody").appendChild(docFrag);
			
			// Set the Month & Year
			document.getElementById(id + "_MonthTitle").innerHTML = "Search Results";

			document.getElementById(id + "_Loading").style.display = "none";
			document.getElementById(id + "_CalContainer").style.display = "inline";
			
			var heading = document.getElementById(id + "_Heading");
			if(heading.innerHTML.indexOf(" - Loading") > 0)
				heading.innerHTML = heading.innerHTML.substring(0,
					heading.innerHTML.indexOf(" - Loading"));
			
			calendarUpdating[id] = false;
		} // if
		else {
			jsFailure("The calendar search failed to load and will be unavailable.");
		} // else
	} // if
} // function updateSearchCalendar()

function calendarExitSearch(id) {
	document.getElementById(id + "_categorySelect").style.display = "inline";
	document.getElementById(id + "_monthJump").style.display = "inline";
	document.getElementById(id + "_WeekView").style.display = "inline";
	document.getElementById(id + "_changeView").style.display = "inline";
	document.getElementById(id + "_nextMonth12").style.display = "inline";
	document.getElementById(id + "_previousMonth12").style.display = "inline";
	document.getElementById(id + "_download12").style.display = "inline";
	document.getElementById(id + "_exitSearch12").style.display = "none";
	if(document.getElementById(id + "_addevent12") != null)
		document.getElementById(id + "_addevent12").style.display = "inline";
	
	var date = document.getElementById(id + "_CalendarDate").value.split("-");
	loadMonth(id, date[1], date[0]);
} // function calendarExitSearch(id)

function drawCalendarTable(id, year, month) {
	var categorySelect = document.getElementById(id + "_categorySelect");
	var viewType = document.getElementById(id + "_changeView").value;
	var xml = calXml[id];
	var categories = new Array();
	var today = new Date();
	var date = new Date();
	date.setFullYear(year, month, 1);
	calendarUpdating[id] = true;
	hideCalendarDetails(id)
	
	var numberOfDays = daysInMonth(year, month);
	var numberOfRows = ((numberOfDays == 31 && date.getDay() > 4) ||
		(numberOfDays == 30 && date.getDay() > 5)) ? (6) : (5);
	if(numberOfDays == 28 && date.getDay() == 0)
		numberOfRows = 4;
	removeChildren(id + "_THead");
	removeChildren(id + "_TBody");
	
	// Parse the XML Document
	var events, tableData = new Array(numberOfRows);
	for(var i = 0; i < numberOfRows; i++)
		tableData[i] = new Array(7);
	
	var dateOneCol = date.getDay();
	var newMonth = month;
	if(--newMonth == -1)
		newMonth = 11;
	for(var i = 0; i < 3; i++) {
		date.setMonth(newMonth, 1);
		var row = (i == 0) ? (0) : (numberOfRows - 1);
		events = xml.getElementsByTagName("Event" + (newMonth + 1));
		
		for(var j = 0; j < events.length; j++) {
			date.setDate(events[j].getAttribute("Date"));
			if(events[j].getAttribute("Category") != null)
				categories[categories.length] = events[j].getAttribute("Category");
			
			if(i == 1) {
				row = parseInt(date.getDate() / 7);
				if(date.getDay() < dateOneCol && date.getDate() % 7 != 0)
					row++;
				else if(date.getDate() % 7 == 0 && dateOneCol == 0)
					row--;
			} // if
			var dayArr = tableData[row][date.getDay()];
			
			if(dayArr == null) {
				tableData[row][parseInt(date.getDay())] = new Array();
				dayArr = tableData[row][parseInt(date.getDay())];
			} // if
			
			dayArr[dayArr.length] = events[j];
		} // for(j)
		
		if(++newMonth == 12)
			newMonth = 0;
	} // for(i)
	
	// Add the Categories
	if(categorySelect.length == 1) {
		var previousCategory = "";
		categories.sort();
		for(var i = 0; i < categories.length; i++) {
			if(previousCategory != categories[i]) {
				previousCategory = categories[i];
				var newCategory = document.createElement("option");
				newCategory.text = categories[i];
				
				if(getBrowser() == "MSIE")
					categorySelect.add(newCategory);
				else
					categorySelect.add(newCategory, categorySelect.options[i + 1]);
			} // if
		} // for
	} // if
	
	var frag = document.createDocumentFragment();
	var td, tr = document.createElement("tr");
	switch(viewType) {
		case "month":
		// Create Table Header Row
		for(var i = 0; i < dayNames.length; i++) {
			td = document.createElement("th");
			td.appendChild(document.createTextNode(dayNames[i]));
			tr.appendChild(td);
		} // for(i);
		frag.appendChild(tr);
		document.getElementById(id + "_THead").appendChild(frag);
		
		// Create Table Data Rows
		newMonth = month;
		if(--newMonth == -1)
			newMonth = 11;
		date.setMonth(month, 1);
		var cellDate = daysInMonth(date.getFullYear(), newMonth) - date.getDay() + 1;
		var thisMonth = false;
		var madeWidth = false;
		
		frag = document.createDocumentFragment();
		for(var i = 0; i < numberOfRows; i++) {
			tr = document.createElement("tr");
			
			for(var j = 0; j < 7; j++) {
				td = document.createElement("td");
				
				if(cellDate - 1 >= daysInMonth(date.getFullYear(), newMonth)) {
					if(++newMonth == 12)
						newMonth = 0;
					cellDate = 1;
					thisMonth = !thisMonth;
				} // if
				
				if(!thisMonth) {
					var tdClass = document.createAttribute("class");
					tdClass.nodeValue = "OffMonth";
					td.setAttributeNode(tdClass);
				} // if
				else {
					var onMonth = document.getElementById(id + "_CalendarDate").value.split("-")[0];
					if(today.getDate() == cellDate && (today.getMonth() + 1) == parseInt(onMonth)) {
						var tdClass = document.createAttribute("class");
						tdClass.nodeValue = "Today";
						td.setAttributeNode(tdClass);
					} // if
				} // else
				
				td.appendChild(document.createTextNode(cellDate++));
				td.appendChild(document.createElement("br"));
				
				if(tableData[i][j] != null) {
					tableData[i][j].sort(calendarSortByTime);
					var odd = true;
					for(var k = 0; k < tableData[i][j].length; k++) {
						if(categorySelect.selectedIndex == 0 ||
							categorySelect.options[categorySelect.selectedIndex].text ==
							tableData[i][j][k].getAttribute("Category")) {
							
							calendarRenderEvent(id, tableData[i][j][k], odd, td);
							
							odd = !odd;
							td.appendChild(document.createElement("br"));
						} // if
					} // for(k)
				} // if
				if(!madeWidth && getBrowser() == "MSIE" && td.childNodes.length == 2) {
					madeWidth = true;
					var makeWidth = document.createElement("span");
					makeWidth.appendChild(document.createTextNode("1 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2" +
						" 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 7 0 2 4 6 8 0 2 4 6"));
					makeWidth.style.visibility = "hidden";
					td.appendChild(makeWidth);
				} // if
				tr.appendChild(td);
			} // for(j)
			frag.appendChild(tr);
		} // for(i)
		break;
		case "week":
			var weekInfo = document.getElementById(id + "_WeekView").value.split("-");
			date.setMonth(weekInfo[1], weekInfo[0]);
			var week = parseInt(date.getDate() / 7);
			if(date.getDay() < dateOneCol && date.getDate() % 7 != 0)
				week++;
			if(weekInfo[1] < month)
				week = 0;

			var previousWeekContainer = document.createElement("div");
			var previousWeekContainerClass = document.createAttribute("class");
			previousWeekContainerClass.nodeValue = "FloatLeft";
			previousWeekContainer.setAttributeNode(previousWeekContainerClass);
			
			var nextWeekContainer = document.createElement("div");
			var nextWeekContainerClass = document.createAttribute("class");
			nextWeekContainerClass.nodeValue = "FloatRight";
			nextWeekContainer.setAttributeNode(nextWeekContainerClass);
			
			var nextWeek = document.createElement("a");
			var previousWeek = document.createElement("a");
			var nextWeekHRef = document.createAttribute("href");
			var previousWeekHRef = document.createAttribute("href");
			
			var nextWeekAction = "calendarMoveWeek('" + id + "', 1);";
			var previousWeekAction = "calendarMoveWeek('" + id + "', -1);";
			
			if(getBrowser() == "MSIE") {
				nextWeekHRef.nodeValue = "javascript:" + nextWeekAction;
				previousWeekHRef.nodeValue = "javascript:" + previousWeekAction;
			} // if
			else {
				nextWeekHRef.nodeValue = "#";
				previousWeekHRef.nodeValue = "#";
				
				var nextWeekClick = document.createAttribute("onclick");
				nextWeekClick.nodeValue = nextWeekAction + "return false;";
				nextWeek.setAttributeNode(nextWeekClick);
				
				var previousWeekClick = document.createAttribute("onclick");
				previousWeekClick.nodeValue = previousWeekAction + "return false;";
				previousWeek.setAttributeNode(previousWeekClick);
			} // else
			nextWeek.setAttributeNode(nextWeekHRef);
			nextWeek.appendChild(document.createTextNode(calendarNextWeek));
			nextWeekContainer.appendChild(nextWeek);
			
			previousWeek.setAttributeNode(previousWeekHRef);
			previousWeek.appendChild(document.createTextNode(calendarPreviousWeek));
			previousWeekContainer.appendChild(previousWeek);
			
			newMonth = weekInfo[1];
			for(var i = 0; i < 7; i++) {
				td = document.createElement("th");
				if(i == 0) {
					td.appendChild(previousWeekContainer);
					td.appendChild(nextWeekContainer);
				} // if
				td.appendChild(document.createTextNode(dayNames[i] + ", " + monthNames[newMonth] +
					" " + weekInfo[0]++));
				tr = document.createElement("tr");
				tr.appendChild(td);
				frag.appendChild(tr);
				
				if(weekInfo[0] > daysInMonth(year, newMonth)) {
					weekInfo[0] = 1;
					if(++newMonth == 12)
						newMonth = 0;
				} // if
				
				tr = document.createElement("tr");
				td = document.createElement("td");
				if((weekInfo[0] - 1) == today.getDate() && newMonth == today.getMonth()) {
					var tdClass = document.createAttribute("class");
					tdClass.nodeValue = "Today";
					td.setAttributeNode(tdClass);
				} // if
				if(tableData[week][i] != null) {
					tableData[week][i].sort(calendarSortByTime);
					var odd = true;
					for(var j = 0; j < tableData[week][i].length; j++) {
						if(categorySelect.selectedIndex == 0 ||
							categorySelect.options[categorySelect.selectedIndex].text ==
							tableData[week][i][j].getAttribute("Category")) {
							
							calendarRenderEvent(id, tableData[week][i][j], odd, td);
							odd = !odd;
							td.appendChild(document.createElement("br"));
						} // if
					} // for(j)
				} // if
				tr.appendChild(td);
				frag.appendChild(tr);
			} // for(i)
			break;
		case "day":
			var dayInfo = document.getElementById(id + "_DayView").value;
			date.setMonth(month, dayInfo);
			var week = parseInt(date.getDate() / 7);
			if(date.getDay() < dateOneCol && date.getDate() % 7 != 0)
				week++;
			var day = date.getDay();
			
			tr = document.createElement("tr");
			td = document.createElement("th");
			td.appendChild(document.createTextNode(dayNames[day] + ", " + monthNames[month] +
					" " + dayInfo));
			tr.appendChild(td);
			frag.appendChild(tr);
			
			tr = document.createElement("tr");
			td = document.createElement("td");
			
			// Create Day Jumper
			date.setDate(1);
			var col = date.getDay() - 1;
			var row = 0;
			
			var jmTBody = document.createElement("tbody");
			var jmTR = document.createElement("tr");
			var jmTD;
			for(var i = 0; i <= col; i++) {
				jmTD = document.createElement("td");
				jmTR.appendChild(jmTD);
			} // for(i)
			
			for(var i = 1; i <= numberOfDays; i++) {
				if(++col == 7) {
					col = 0;
					row++;
					jmTBody.appendChild(jmTR);
					jmTR = document.createElement("tr");
				} // if
				
				jmTD = document.createElement("td");
				if(tableData[row][col] != null) {
					var jmA = document.createElement("a");
					var jmHRef = document.createAttribute("href");
					var jmAction = "calendarJumpToDay(" + id + ", " + i + ");";
					
					if(getBrowser() == "MSIE")
						jmHRef.nodeValue = "javascript:" + jmAction;
					else {
						jmHRef.nodeValue = "#";
						
						var jmClick = document.createAttribute("onclick");
						jmClick.nodeValue = jmAction + "return false;";
						jmA.setAttributeNode(jmClick);
					} // else
					jmA.setAttributeNode(jmHRef);
					jmA.appendChild(document.createTextNode(i));
					jmTD.appendChild(jmA);
				} // if
				else
					jmTD.appendChild(document.createTextNode(i));
				jmTR.appendChild(jmTD);
			} // for(i)
			
			for(var i = col; i < 6; i++) {
				jmTD = document.createElement("td");
				jmTR.appendChild(jmTD);
			} // for(i)
			jmTBody.appendChild(jmTR);
			
			var jmTable = document.createElement("table");
			jmTable.className = "DateTable";
			jmTable.appendChild(jmTBody);
			td.appendChild(jmTable);
			
			// Add Day's Events
			if(tableData[week][day] != null) {
				tableData[week][day].sort(calendarSortByTime);
				var odd = true;
				for(var i = 0; i < tableData[week][day].length; i++) {
					if(categorySelect.selectedIndex == 0 ||
						categorySelect.options[categorySelect.selectedIndex].text ==
						tableData[week][day][i].getAttribute("Category")) {
							
						calendarRenderEvent(id, tableData[week][day][i], odd, td);
						odd = !odd;
						td.appendChild(document.createElement("br"));
					} // if
				} // for(i)
			} // if
			tr.appendChild(td);
			frag.appendChild(tr);
			break;
	} // switch
	document.getElementById(id + "_TBody").appendChild(frag);
	calendarUpdating[id] = false;
} // function drawCalendarTable(id, date, numberOfRows, tableData)

function calendarSearch(id) {
	var calendarDate = document.getElementById(id + "_CalendarDate").value.split("-");
	var query = document.getElementById(id + "_Search").value;
	if(query == "")
		alert("Please enter text to search for.");
	else
		loadSearch(id, calendarDate[1], calendarDate[0], query);
} // function calendarSearch(id)

function drawCalendarPrint(id) {
	var printWin = window.open("", "calendarPrint", "");
	var doc = printWin.document;
	var monthText = document.getElementById(id + "_MonthTitle").innerHTML;
	
	doc.write("<html><head><title>" + monthText + "</title>");
	doc.write("<link href=\"Default.css\" rel=\"stylesheet\"></head>");
	doc.write("<body><div class=\"PortalControl\"><div class=\"Calendar\"><div class=\"MonthTitle\">");
	doc.write(monthText + "</div><table><thead>");
	doc.write(document.getElementById(id + "_THead").innerHTML);
	doc.write("</thead><tbody>");
	doc.write(document.getElementById(id + "_TBody").innerHTML);
	doc.write("</tbody></table></div></div></body></html>");
	doc.close();
} // function drawCalendarPrint(id, landscape)

function calendarRenderEvent(id, eventData, odd, td) {
	var eventID = id + "-" + eventData.getAttribute("id");
	calData[eventID] = eventData;

	var link = document.createElement("a");
	var linkHRef = document.createAttribute("href");
	link.id = eventID;

	var action = "updateCalendarDetails('" + eventID + "');";
	var text = document.createTextNode(getText(eventData));
	
	/* if(eventData.getAttribute("Time") == null && eventData.getAttribute("Location") == null &&
		eventData.getAttribute("IID") == null && eventData.getElementsByTagName("Details").length == 0) {
			
			var span = document.createElement("span");
			var spanClass = document.createAttribute("class");
			spanClass.nodeValue = (odd) ? ("odd") : ("even");
			span.setAttributeNode(spanClass);
			
			span.appendChild(text);
			td.appendChild(span);
	} // if
	else { */
		if(getBrowser() == "MSIE") {
			var span = document.createElement("span");
			
			var spanClass = document.createAttribute("class");
			spanClass.nodeValue = (odd) ? ("odd") : ("even");
			span.setAttributeNode(spanClass);

			linkHRef.nodeValue = "javascript:" + action;
			link.setAttributeNode(linkHRef);
			
			span.appendChild(text);
			link.appendChild(span);
			td.appendChild(link);
		} // if
		else {
			linkHRef.nodeValue = "#";
			link.setAttributeNode(linkHRef);
			
			var linkClick = document.createAttribute("onclick");
			linkClick.nodeValue = action + " return false;";
			link.setAttributeNode(linkClick);
			
			var linkClass = document.createAttribute("class");
			linkClass.nodeValue = (odd) ? ("odd") : ("even");
			link.setAttributeNode(linkClass);
			
			link.appendChild(text);
			td.appendChild(link);
		} // else
	// } // else
} // function calendarRenderEvent(id, eventData, odd, td)

function updateCalendarDetails(calID) {
	var date = new Date();
	var id = calID.split("-")[0];
	var xml = calData[calID];
	
	// Find and set the correct date
	var calendarDate = document.getElementById(id + "_CalendarDate").value.split("-");
	var year = parseInt(calendarDate[1]);
	var month = parseInt(calendarDate[0]) - 1;
	var eventMonth = parseInt(xml.nodeName.substring(5)) - 1;
	var today = new Date();
	
	if(eventMonth != month)
		if(eventMonth < month) {
			month -= (month - eventMonth);
			if(month < 0) {
				month += 12;
				year--;
			} // if
		} // if
		else {
			month += (eventMonth - month);
			if(month > 12) {
				month -= 12;
				year++;
			} // if
		} //else
	date.setFullYear(year, month, xml.getAttribute("Date"));
	
	// Create the Details pane
	removeChildren(id + "_Details");
	var frag = document.createDocumentFragment();
	
	// Create the close and remind me, and invite links
	var closeDiv = document.createElement("div");
	var closeClass = document.createAttribute("class");
	closeClass.nodeValue = "Close";
	closeDiv.setAttributeNode(closeClass);
	
	var closeLink = document.createElement("a");
	var closeHRef = document.createAttribute("href");
	var closeAction = "hideCalendarDetails('" + id + "');";
	
	var remindLink = document.createElement("a");
	var remindHRef = document.createAttribute("href");
	var remindAction = "calendarRemindMe('" + calID + "');";
	
	var inviteLink = document.createElement("a");
	var inviteHRef = document.createAttribute("href");
	var inviteAction = "calendarInvite('" + calID + "');";
	
	if(getBrowser() != "MSIE") {
		closeHRef.nodeValue = "#";
		remindHRef.nodeValue = "#";
		inviteHRef.nodeValue = "#";
		
		var closeClick = document.createAttribute("onclick");
		closeClick.nodeValue = closeAction + " return false;";
		closeLink.setAttributeNode(closeClick);
		
		var remindClick = document.createAttribute("onclick");
		remindClick.nodeValue = remindAction + " return false;";
		remindLink.setAttributeNode(remindClick);
		
		var inviteClick = document.createAttribute("onclick");
		inviteClick.nodeValue = inviteAction + " return false;";
		inviteLink.setAttributeNode(inviteClick);
	} // if
	else {
		closeHRef.nodeValue = "javascript:" + closeAction;
		remindHRef.nodeValue = "javascript:" + remindAction;
		inviteHRef.nodeValue = "javascript:" + inviteAction;
	} // else
	
	closeLink.setAttributeNode(closeHRef);
	closeLink.appendChild(document.createTextNode(calendarCloseText));
	closeDiv.appendChild(closeLink);
	frag.appendChild(closeDiv);
	
	var showLinks = (date.getFullYear() > today.getFullYear()) ? (true) :
		((date.getMonth() > today.getMonth()) ? (true) :
		(date.getMonth() == today.getMonth() && date.getDate() >= today.getDate()));
	if(showLinks) {
		remindLink.setAttributeNode(remindHRef);
		if(date.getDate() != today.getDate())
			remindLink.appendChild(document.createTextNode(calendarRemindMeText));
		
		inviteLink.setAttributeNode(inviteHRef);
		inviteLink.appendChild(document.createTextNode(calendarInviteText));
	} // if
	
	closeDiv.appendChild(document.createElement("br"));
	closeDiv.appendChild(remindLink);
	closeDiv.appendChild(document.createTextNode(" "));
	closeDiv.appendChild(inviteLink);
	
	if(xml.getAttribute("IID") != null) {
		var modifyLink = document.createElement("a");
		var modifyHRef = document.createAttribute("href");
		modifyHRef.nodeValue = "addevent.aspx?id=" + id + "&iid=" + calendarDate[1] +
			"-" + calendarDate[0] + "-" + xml.getAttribute("IID");
		var modifyTarget = document.createAttribute("target");
		modifyTarget.nodeValue = "_blank";
		
		modifyLink.setAttributeNode(modifyHRef);
		modifyLink.setAttributeNode(modifyTarget);
		modifyLink.appendChild(document.createTextNode(calendarModifyText));
		
		var deleteLink = document.createElement("a");
		var deleteHRef = document.createAttribute("href");
		deleteHRef.nodeValue = "delete.aspx?id=" + id + "&iid=" + calendarDate[1] +
			"-" + calendarDate[0] + "-" + xml.getAttribute("IID");
		var deleteTarget = document.createAttribute("target");
		deleteTarget.nodeValue = "_blank";
		
		deleteLink.setAttributeNode(deleteHRef);
		deleteLink.setAttributeNode(deleteTarget);
		deleteLink.appendChild(document.createTextNode(calendarDeleteText));
		
		closeDiv.appendChild(document.createElement("br"));
		closeDiv.appendChild(modifyLink);
		closeDiv.appendChild(document.createTextNode(" "));
		closeDiv.appendChild(deleteLink);
	} // if
	
	// Create the event heading
	var heading = document.createElement("h3");
	heading.appendChild(document.createTextNode(getText(xml)));
	frag.appendChild(heading);
	
	// Create the Date, Time, and Location
	var when = document.createElement("span");
	var whenClass = document.createAttribute("class");
	whenClass.nodeValue = "When";
	when.setAttributeNode(whenClass);
	
	var whenText = document.createTextNode("");
	whenText.appendData(dayNames[date.getDay()] + ", ");
	whenText.appendData(monthNames[date.getMonth()] + " ");
	whenText.appendData(date.getDate() + ", ");
	whenText.appendData(date.getFullYear());
	when.appendChild(whenText);
	
	if(xml.getAttribute("Time") != null) {
		when.appendChild(document.createElement("br"));
		when.appendChild(document.createTextNode(xml.getAttribute("Time")));
		if(xml.getAttribute("End") != null) {
			when.appendChild(document.createTextNode(" - "+ xml.getAttribute("End")));
		} // if
	} // if
	if(xml.getAttribute("Location") != null) {
		when.appendChild(document.createElement("br"));
		when.appendChild(document.createTextNode(xml.getAttribute("Location")));
	} // if
	when.appendChild(document.createElement("br"));
	
	frag.appendChild(when);
	frag.appendChild(document.createElement("br"));
	
	// Configure the Details pane
	var details = document.getElementById(id + "_Details");
	details.appendChild(frag);
	if(xml.getElementsByTagName("Details").length > 0)
		details.innerHTML += getText(xml.getElementsByTagName("Details")[0]);
	details.style.display = "inline";
	
	var coords = getCoords(document.getElementById(calID));
	if(getBrowser() == "Safari") {
		coords[0] += 7;
		coords[1] += 7;
	} // if
	if(coords[0] + calendarDetailsWidth >= getDocumentSize()[0])
		coords[0] = getDocumentSize()[0] - calendarDetailsWidth - 20;
	if(coords[1] + details.offsetHeight + 2 >= getDocumentSize()[1] &&
		coords[1] - details.offsetHeight > 0)
		coords[1] = coords[1] - details.offsetHeight;
	else
		coords[1] += document.getElementById(calID).offsetHeight + 2;
	
	details.style.left = coords[0] + "px";
	details.style.top = coords[1] + "px";
} // function updateCalendarDetails(calID)

function hideCalendarDetails(id) {
	document.getElementById(id + "_Details").style.display = "none";
} // function hideCalendarDetails(id)

function calendarRemindMe(calID) {
	openCalendarCommunication(calID, "remindme.html");
} // function calendarRemindMe(calID)

function calendarInvite(calID) {
	openCalendarCommunication(calID, "invite.html");
} // function calendarInvite(calID)

function openCalendarCommunication(calID, pageFile) {
	var xml = calData[calID];
	var height = 275;
	var width = 350;
	
	var coords = getCoords(document.getElementById(calID));
	if(getBrowser() == "Safari") {
		coords[0] += 7;
		coords[1] += 7;
	} // if
	if(coords[0] + width >= getDocumentSize()[0])
		coords[0] = getDocumentSize()[0] - width - 20;
	if(coords[1] + height >= getDocumentSize()[1] &&
		coords[1] - height > 0)
		coords[1] = coords[1] - height;
	else
		coords[1] += document.getElementById(calID).offsetHeight + 2;
	coords[0] += window.screenX;
	coords[1] += window.screenY;
	
	var myDate = escape(xml.nodeName.substr(5) + "-" + xml.getAttribute("Date"));
	var myTime = ""; var myLocation = "";
	if(xml.getAttribute("Time") != null)
		myTime = escape(xml.getAttribute("Time"));
	if(xml.getAttribute("Location") != null)
		myLocation = escape(xml.getAttribute("Location"));
	
	var remindWindow = window.open(pageFile + "?title=" + escape(getText(xml)) + "&date=" + myDate +
		"&time=" + myTime + "&location=" + myLocation,
		"", "height=" + height + ",width=" + width + ",screenX=" + coords[0] + ",screenY=" + coords[1]);
	var doc = remindWindow.document;
} // function openCalendarCommunication(calID, pageFile)

function downloadCalendar(id) {
	var calendarDate = document.getElementById(id + "_CalendarDate").value.split("-");
	var download = window.open("download.aspx?id=" + id + "&month=" + calendarDate[0] +
		"&year=" + calendarDate[1], "", "height=10,width=10");
} // function downloadCalendar(id)

function daysInMonth(year, month) {
	switch(month)
	{
		case 0:
		case 2:
		case 4:
		case 6:
		case 7:
		case 9:
		case 11:
			return 31;
		case 3:
		case 5:
		case 8:
		case 10:
			return 30;
	} // switch
	
	if(year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0))
		return 29;
	return 28;
} // function daysInMonth(year, month)

function calendarSortByDate(a, b) {
	var date1 = parseInt(a.getAttribute("Date"));
	var date2 = parseInt(b.getAttribute("Date"));
	
	return (date1 == date2) ? (0) : ((date1 < date2) ? (-1) : (1));
} // function calendarSortByDate

function calendarSortByTime(a, b) {
	try {
		var time1 = a.getAttribute("Time");
		var time2 = b.getAttribute("Time");
		
		var ampm1 = time1.substr(time1.indexOf("M") - 2, 1);
		var ampm2 = time2.substr(time2.indexOf("M") - 2, 1);
		
		if(ampm1 == ampm2) {
			var hour1 = parseInt(time1.substring(0, time1.indexOf(":")));
			var hour2 = parseInt(time2.substring(0, time2.indexOf(":")));
			
			hour1 = (hour1 == 12) ? (0) : (hour1);
			hour2 = (hour2 == 12) ? (0) : (hour2);
			
			if(hour1 == hour2) {
				var minute1 = parseInt(time1.substr(time1.indexOf(":") + 1, 2));
				var minute2 = parseInt(time2.substr(time2.indexOf(":") + 1, 2));
				
				return (minute1 == minute2) ? (0) : ((minute1 < minute2) ? (-1) : (1));		
			} // if
			else
				return(hour1 < hour2) ? (-1) : (1);
		} // if
		else
			return (ampm1 == "A") ? (-1) : (1);
	} // try
	catch(e) {
		return 0;
	} // catch
} // function calendarSortByTime(a, b)