/********************************************************************
*  CALENDAR DATE PICKER
*
*  Purpose:  A pop up calendar that allows you to pick a date and
*            have it populate a form field with said date picked.
*
*  Author:   Brent Gustafson
*  Created:  December 28, 2001
*********************************************************************/

// Add stylesheet for calendar
document.write("<link rel='stylesheet' type='text/css' href='/html/date_calendar.css' />");

// Globals
var date_obj;
var date_field;
var date_in = 0;
var date_onColor = "#DADADA";   // Color of date when rolled on
var date_offColor = "#FFFFFF";  // Color of date when rolled off
var date_selectedMonth;
var date_selectedYear;
var date_currentDate = new Date();
var date_currentMonth = date_currentDate.getMonth();
var date_monthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var date_monthName = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var date_currentYear = date_currentDate.getYear();
if (date_currentYear < 1000) date_currentYear += 1900;
var date_today = date_currentDate.getDate();


// Calculates and returns month and year for header of the calendar
function date_headerFunc(date_year, date_month) {
   if (date_month == 1) {
   date_monthDays[1] = ((date_year % 400 == 0) || ((date_year % 4 == 0) && (date_year % 100 !=0))) ? 29 : 28;
   }
   var date_headerStr = date_monthName[date_month] + " " + date_year;
   return date_headerStr;
}


// Creates the entire calendar in HTML and writes it to the screen.
function date_calendarFunc(date_year, date_month) {
   var date_firstDate = new Date(date_year, date_month, 1);
   var date_heading = date_headerFunc(date_year, date_month);
   var date_firstDay = date_firstDate.getDay() + 1;
   
   // Calculate number of rows of weeks there need to be.
   if (((date_monthDays[date_month] == 31) && (date_firstDay >= 6)) || ((date_monthDays[date_month] == 30) && (date_firstDay == 7))) {
      var date_rows = 6;
   }
   else if ((date_monthDays[date_month] == 28) && (date_firstDay == 1)) {
      var date_rows = 4;
   }
   else {
      var date_rows = 5;
   }

   // Display Header and days of the week below
	document.getElementById("calHead").innerHTML = "<h2 style='cursor:pointer; cursor:hand;' onclick='goToPage(\"" + dateLink(date_month, date_dayCount, date_year, 1) + "\");'>" + date_heading + "</h2>";
	
   var date_html = "<table border='0' cellspacing='3' cellpadding='0' width='100%'><tr><tr><td class='date_dotw'>S</td><td class='date_dotw'>M</td><td class='date_dotw'>T</td><td class='date_dotw'>W</td><td class='date_dotw'>T</td><td class='date_dotw'>F</td><td class='date_dotw'>S</td></tr>";
   
   // Display calendar dates below
   var date_dayCount = 1;
   var date_loopCount = 1;
	var date_hilite = null;
   //date_html +="<table border='0' cellspacing='3' cellpadding='0' width='100%'>";
   for (var j = 1; j <= date_rows; j++) {
      date_html += "<tr align='left'>";
      for (var i = 1; i < 8; i++) {
         
         // Write indivdual day
         if ((date_loopCount >= date_firstDay) && (date_dayCount <= date_monthDays[date_month])) {
            if ((date_dayCount == date_today) && (date_year == date_currentYear) && (date_month == date_currentMonth)) date_hilite = "titleBox";
            else date_hilite = "resultEven";
	
            date_html += "<td id=\"" + date_dayCount + "\" class='" + date_hilite + " date_dayLink' onclick='goToPage(\"" + dateLink(date_month, date_dayCount, date_year) + "\");'><a href='javascript:goToPage(\"" + dateLink(date_month, date_dayCount, date_year) + "\");'>" + date_dayCount + "</a></td>";
            date_dayCount++;    
         }
         else {
            date_html += "<td> </td>";
         }
         date_loopCount++;
      }
      date_html += "</tr>";
   }
   date_html += "</table>";
   date_obj.innerHTML = date_html;
}


// Move calendar forward and back a month
function date_skipFunc(date_forward) {
   if (date_forward) {
      if (date_selectedMonth == 11) {
         date_selectedMonth = 0;
         date_selectedYear++;
      }
      else date_selectedMonth++;
   }
   else {
      if (date_selectedMonth == 0) {
         date_selectedMonth = 11;
         date_selectedYear--;
      }
      else date_selectedMonth--;
   }
   date_calendarFunc(date_selectedYear, date_selectedMonth);
}

// Create link to another page (all = show entire month)
function dateLink (month, dayCount, year, all) {
	var eventLink;
	if (all) eventLink = "/eventList.do?action=list&sd=" + (month+1) + "/1/" + year + "&ed=" + (month+1) + "/" + date_monthDays[month] + "/" + year;
	else eventLink = "/eventList.do?action=list&sd=" + (month+1) + "/" + dayCount + "/" + year + "&ed=" + (month+1) + "/" + dayCount + "/" + year;
	return eventLink;
}

// Link to another page
function goToPage (url) {
	document.location = url;
}


// Parse start date out of URL if there is one, to display by default
function parseURL() {
	var theURL = window.location.href;
	if (theURL.indexOf("sd=") != -1) {
		var startDate = theURL.slice(theURL.indexOf("sd=")+3,theURL.indexOf("&ed="));
		var dateParts = unescape(startDate).split("/");
		date_selectedMonth = dateParts[0]-1;
	  	date_selectedYear = dateParts[2];
	}
	else {
		date_selectedMonth = date_currentMonth;
	  	date_selectedYear = date_currentYear;
	}
}


// Initialize variables and draw current calendar
function date_initFunc() {
  	date_obj = document.getElementById("calendar");
	parseURL();
	date_calendarFunc(date_selectedYear, date_selectedMonth);
}

//Init
date_initFunc();
