//*****************************************************************************
// Do not remove this notice.
//
// Copyright 2001 by Mike Hall.
// See http://www.brainjar.com for terms of use.
//*****************************************************************************

//============================================================================
// This code handles the calendar display and interaction.
//============================================================================

	// Default target date to today.
	var targetDate = new Date();
	//var targetDate2 = new Date(Date.parse(targetDate));

//----------------------------------------------------------------------------
// This function updates the calendar display to reflect the current target
// date.
//----------------------------------------------------------------------------

function setCalendars(event) {

	var el, tableEl, rowEl, cellEl, linkEl;
	var el2, tableE2, rowE2, cellE2, linkE2;
	var tmpDate, tmpDate2;
	var i, j, z;
	z = 0;

	// Force a redraw by hiding the entire page.
	document.body.style.display = "none";

	// Update month name.
	el = document.getElementById("calendarHeader").firstChild;
	el.nodeValue = targetDate.getMonthName() + "\u00a0" + targetDate.getFullYear();
		
	// Start with the first day of the month and go back if necessary to the previous Sunday.
	tmpDate = new Date(Date.parse(targetDate));
	tmpDate.setDate(1);
	while (tmpDate.getDay() != 0) {
		tmpDate.addDays(-1);
	}
	
	// Go through each calendar day cell in the table and update it.
	tableEl = document.getElementById("calendar");
		
	for (i = 2; i <= 7; i++) {
		rowEl = tableEl.rows[i];

		// Hide row if it contains no dates in the current month.
		tmpDate2 = new Date(Date.parse(tmpDate));
		
		tmpDate2.addDays(6);
		
		if (tmpDate.getMonth() != targetDate.getMonth() && 
		tmpDate2.getMonth() != targetDate.getMonth())
			rowEl.className = "empty";
		else
			rowEl.className = "";
		
		// Loop through a week.
		for (j = 0; j < rowEl.cells.length; j++) {
		
			cellEl = rowEl.cells[j];			
			linkEl = cellEl.firstChild;
		  
			//reset appearance of cells
			cellEl.className = "";
			cellEl.innerHTML = "<span>&nbsp;</span>";
					  
			if (tmpDate.getMonth() == targetDate.getMonth()) {
				cellEl.innerHTML = "<span>" + tmpDate.getDate() + "</span>";
				linkEl.date = new Date(Date.parse(tmpDate));
				s = tmpDate.toString().split(" ");
				linkEl.title = s[0] + " " + s[1] + " " + s[2] + ", " + s[3];
				linkEl.style.visibility = "";

				var thisDay = tmpDate.getDate();
				var thisMonth = tmpDate.getMonth() + 1; 
				var thisYear = tmpDate.getFullYear();
				for (k = z; k < eventsAr.length; k++){
					eventT = eventsAr[k].date.toString().split('/');
					eventDay = parseInt(eventT[2],10);
					eventMonth = parseInt(eventT[1],10);
					eventYear = eventT[0];
					if (eventDay == thisDay && eventMonth == thisMonth && eventYear == thisYear){
						cellEl.className = "event_hlt";
						z = k;
						if (eventsAr[k].href !="eventDay")
						{cellEl.innerHTML = eventsAr[k].href.replace("eventDay",thisDay);}
					}
				}
				if (eventSpanAr.length > 0){ 
			    	    for( x = 0; x < eventSpanAr.length;x++){
			    		eventT = eventSpanAr[x].toString().split('/');
					eventDay = eventT[1];
					eventMonth = eventT[0];
					eventYear = eventT[2];
					if (eventDay == thisDay && eventMonth == thisMonth && eventYear == thisYear){
						cellEl.className = "event_hltb";
						// remove the hyperlink for current event days if desired
						// cellEl.innerHTML = "<span>" + thisDay + "</span>";	
					}
				    }
				}

			}else
				linkEl.style.visibility = "hidden";				
				tmpDate.addDays(1);
		}
	}
	

	if (dualDisplay){
		z = 0;
		tableEl2 = document.getElementById("calendar2");
		targetDate2 = new Date(Date.parse(targetDate));
		targetDate2.addMonths(1);
		el2 = document.getElementById("calendarHeader2").firstChild;
		el2.nodeValue = targetDate2.getMonthName() + "\u00a0" + targetDate2.getFullYear();
		tmpDate = new Date(Date.parse(targetDate2));
		tmpDate.setDate(1);
		while (tmpDate.getDay() != 0) {
			tmpDate.addDays(-1);
		}
		for (i = 2; i <= 7; i++) {
			rowE2 = tableEl2.rows[i];
			tmpDate2 = new Date(Date.parse(tmpDate));
			tmpDate2.addDays(6);
			if (tmpDate.getMonth()  != targetDate2.getMonth() && 
			tmpDate2.getMonth() != targetDate2.getMonth())
				rowE2.className = "empty";
			else{
				rowE2.className = "";}
				
			// Loop through a week.
			for (j = 0; j < rowE2.cells.length; j++) {
				cellE2 = rowE2.cells[j];
				linkE2 = cellE2.firstChild;

				//reset appearance of cells
				cellE2.className = "";
				cellE2.innerHTML = "<span>&nbsp;</span>";

				if (tmpDate.getMonth() == targetDate2.getMonth()) {
				    	cellE2.innerHTML = "<span>" + tmpDate.getDate() + "</span>";
				    	linkE2.date = new Date(Date.parse(tmpDate));
				    	s = tmpDate.toString().split(" ");
					linkE2.title = s[0] + " " + s[1] + " " + s[2] + ", " + s[3];
				
					linkE2.style.visibility = "";

					var thisDay = tmpDate.getDate();
					var thisMonth = tmpDate.getMonth() + 1; 
					var thisYear = tmpDate.getFullYear();
					for (k = z; k < eventsAr.length; k++){
						eventT = eventsAr[k].date.toString().split('/');
						eventDay = parseInt(eventT[2],10);
						eventMonth = parseInt(eventT[1],10);
						eventYear = eventT[0];
						if (eventDay == thisDay && eventMonth == thisMonth && eventYear == thisYear){
							cellE2.className = "event_hlt";
							z = k;
							if (eventsAr[k].href !="eventDay")
							{cellE2.innerHTML = eventsAr[k].href.replace("eventDay",thisDay);}
						}
					}
				}else
					linkE2.style.visibility = "hidden";
					tmpDate.addDays(1);
			}
		}
	}
	// Restore the page display.
	document.body.style.display = "";
}

//-------------------------------------------------------------------------------------------------------
// Event handlers for the calendar elements.
//-------------------------------------------------------------------------------------------------------

function addMonths(event, n) {
	targetDate.addMonths(n);
	setCalendars(event);
}


function addYears(event, n) {

		  // Advance the calendar year and update the display.

		  targetDate.addYears(n);
		  setCalendars(event);
}

function setTargetDate(event, link) {

		  // Change the target date and update the calendar.

		  if (link.date != null) {
			targetDate = new Date(Date.parse(link.date));
			setCalendars(event);
		  }
}

function formatDate() {

		  var mm, dd, yyyy;

		  // Return the target date in "mm/dd/yyyy" format.

		  mm = String(targetDate.getMonth() + 1);
		  while (mm.length < 2)
			mm = "0" + mm;
		  dd = String(targetDate.getDate());
		  while (dd.length < 2)
			dd = "0" + dd;
		  yyyy = String(targetDate.getFullYear());
		  while (yyyy.length < 4)
			yyyy = "0" + yyyy;

		  return mm + "/" + dd + "/" + yyyy;
}

//============================================================================
// Add new properties and methods to the Date object.
//============================================================================

// Properties

Date.prototype.monthNames = new Array("January", "February", "March", "April",
		  "May", "June", "July", "August", "September", "October", "November",
		  "December");
Date.prototype.savedDate  = null;

// Methods

Date.prototype.getMonthName = dateGetMonthName;
Date.prototype.getDays      = dateGetDays;
Date.prototype.addDays      = dateAddDays;
Date.prototype.addMonths    = dateAddMonths;
Date.prototype.addYears     = dateAddYears;


//----------------------------------------------------------------------------
// getMonthName(): Returns the name of the date's month.
//----------------------------------------------------------------------------

function dateGetMonthName() {
		  return this.monthNames[this.getMonth()];
}

//----------------------------------------------------------------------------
// getDays(): Returns the number of days in the date's month.
//----------------------------------------------------------------------------

function dateGetDays() {

		  var tmpDate, d, m;

		  tmpDate = new Date(Date.parse(this));
		  m = tmpDate.getMonth();
		  d = 28;
		  do {
			d++;
			tmpDate.setDate(d);
		  } while (tmpDate.getMonth() == m);

		  return d - 1;
}

//----------------------------------------------------------------------------
// addDays(n): Adds the specified number of days to the date.
//----------------------------------------------------------------------------

function dateAddDays(n) {

		  // Add the specified number of days.

		  this.setDate(this.getDate() + n);

		  // Reset the new day of month.

		  this.savedDate = this.getDate();
}

//----------------------------------------------------------------------------
// addMonths(n): Adds the specified number of months to the date, adjusting
// the day of the month if necessary.
//----------------------------------------------------------------------------

function dateAddMonths(n) {

		  // Save the day of month if not already set.

		  if (this.savedDate == null)
			this.savedDate = this.getDate();

		  // Set the day of month to the first to avoid rolling.

		  this.setDate(1);

		  // Add the specified number of months.

		  this.setMonth(this.getMonth() + n);

		  // Restore the saved day of month, if possible.

		  this.setDate(Math.min(this.savedDate, this.getDays()));
}

//----------------------------------------------------------------------------
// addYears(n): Adds the specified number of years to the date, adjusting the
// day of the month for leap years.
//----------------------------------------------------------------------------

function dateAddYears(n) {

		  // Save the day of month if not already set.

		  if (this.savedDate == null)
			this.savedDate = this.getDate();

		  // Set the day of month to the first to avoid rolling.

		  this.setDate(1);

		  // Add the specified number of years.

		  this.setFullYear(this.getFullYear() + n);

		  // Restore the saved day of month, if possible.

		  this.setDate(Math.min(this.savedDate, this.getDays()));
}
