
// This script was modified from one provided by:  Doug Lawson (dlawson@clark.net) 
// The original was downloaded from 
// The JavaScript Source!! http://javascript.internet.com

var calStartDOW = 0;

function geoEvent(lat,lon)
{
  this . lat = lat;
  this . lon = lon;
}

var geoEvents = new Array();
// the geoEvents are defined in index.html so that I only have to update one file. The geoEvents are indexed by their date string. that is
// geoEvents["30 Nov 2003"] = new geoEvent(46.191032,-123.832902);

// this returns the geoEvent for a given date string
function getEvent(dStr)
{ 
  return geoEvents[dStr];
} 
// this is the callback that is invoked when a date is clicked on
// the name of each blog entry must be exactly right

function calendarCB(dStr)
{
   var gE = getEvent(dStr);
   if (gE != null) { 
      pm_setLatLong(gE.lat,gE.lon);
      var newLoc = "jog";
      var count = 0;
      var si = "";
//alert(dStr);
      while (count < dStr.length) {
         si = dStr.charAt(count);
//alert(si);
         if (si != " ") { 
             newLoc = newLoc + si;
             }
         count = count + 1;
   }
   newLoc = newLoc + ".html";
   top.blogentries.document.location = newLoc;
  }
}
	

// ABBREVIATED MONTH NAMES
// months[] is used internally for (among other things) compatibility with 
// javascript Date.parse() methods.  You should only have to change it if 
// your language variable changes. 
var months = new Array( "Jan","Feb","Mar", "Apr","May","Jun", "Jul","Aug","Sep","Oct", "Nov", "Dec");

// Long month names 
// longmonths is used for calendar titles. 
// 
var longmonths = new Array( "January", "February", "March","April", "May", "June","July", "August", "September","October", "November", "December" ); 

var leapdays = new Array(31,29,31, 30,31,30, 31,31,30, 31,30,31); 
var yeardays = new Array(31,28,31, 30,31,30, 31,31,30, 31,30,31); 

// the symbols to use at the top of the calendars
var dow = new Array("S","M","T","W","T","F","S"); 

// a global date object to speed things up a little, since 
// there are lots of places where we need today's date

var myDate = new Date();
 
// thisMonth and thisYear are used as static vars for 
// newCalendars() (for persistence between calls

var thisMonth = myDate.getMonth(); 
var thisYear  = myDate.getFullYear(); 
// Now, figure out the arguments the page was loaded with 
// and what they mean

function isLeapYear( year ){
  // is it leap year ? returns a boolean
  return ( (0 == (year%4)) && ( (0 != (year%100)) || (0 == (year%400)))); 
  // ie, if the year divides by 4, but not by 100 except when it divides by
  // 400, it is leap year
} 

function isValidDayOfMonth( day, month, year) 
{ 
  // determines whether a day is valid 
  // (ie, prevents Feb 30 from being processed) 
  // ( not used but left here for other pages that use 
  // this calendar, like the time-off request form ) 
  // 
  if (day <= 0) { return false; }
  if (isLeapYear(year)) { return (day <= leapdays[month])}
  return ( day <= yeardays[month]); 
} 

function getDateStr(year,mon,mday)
{ 
  // it the date on the list? 
  // the format is a little sensitive, you
  // have to make sure that 'mon' is
  // something that Date.parse() can 
  // understand 
  var dStr = mday + " " + mon + " " + year; 
  return dStr;
} 

function calendarString(myYear, myMonth, bday)
{ 
  var calStr = "";
  var gE;
  // creates a string which is the code for a calendar  for myMonth myYear. 
  // get a new date for the first day of the month the user is looking at
  var calDate = new Date( myYear,  myMonth, 1, 0,0,0,0 ); 
  // how many days are in the month ? 
  var mDays = isLeapYear(myYear) ? leapdays[myMonth]: yeardays[myMonth]; 
  var i = 0; 
  // what day of the week does the month start on? 
  var wkDay = calDate.getDay(); 
  var dateBgAtt = ""; 
  var dateHref = "";
  var hrefCloseTag = ""; 
  var boldTag = "";
  var unBoldTag = ""; 
  // if the function was called with the bday argument, then the 
  // caller wants a date written in bold 
  var useBold = arguments.length >= 3 ? true: false; 
  // the header for the calendar ( month and year ) 
  calStr = "<TABLE class='calendar'>\n" + "<TR class='calendarHeading'>" + "<TD COLSPAN='5'>";
  calStr = calStr + longmonths[myMonth] + "</TD><TD COLSPAN='2'>" + myYear + "</TD></TR>\n"; 
  // the abbreviations for days of the week into the top line of the calendar
  calStr = calStr + "<TR>"; 
  for(i=0; i< 7; i++) 
	 { calStr = calStr + "<TD class='calendarDays'>" + dow[((i+calStartDOW)%7)] + "</TD>"; }
  calStr = calStr + "</TR>\n" + "<TR>";
  // start the first line with blank spaces until we get to the first day of the month
  var rowCount = 0;
  for(i=0 ; i < ((7 - calStartDOW + wkDay)%7); i++) 
	 { calStr = calStr + "<TD> &nbsp;</TD>";	 }
  // since javascript doesn't do modulus on negative numbers, 
  // add 7 to anything that might be negative
  var cmdate = i - ((7 - calStartDOW + wkDay)%7); 
  // write the weekdays
  for( i=i; cmdate < mDays ; i++) 
	 {  
		// what is the date ? 
		cmdate++; 
	   // if we have reached the end of a week, start another one
		if(0 == (i%7)){ calStr = calStr + "</TR>\n<TR>"; rowCount=rowCount+1; } 
		// if the date is an event, set it in color
                                   var dateStr = getDateStr(calDate.getFullYear(),months[calDate.getMonth()],cmdate);
                                  gE = getEvent(dateStr);
                                   if (gE != null)
		  { 	 
			 dateBgAtt = "class='calendarElement'"; 
			 dateHref = "<A class='calendarEvent' href='javascript:calendarCB("  + '"' + dateStr + '"' + ");'>";
			 hrefCloseTag = "</A>"; 
                                      }
		else  
		  {	 dateBgAtt = "class='calendarElement'"; 
			 dateHref = "";
			 hrefCloseTag = "";   		 
                                       } 

		// set the days off in bold
		if( ( useBold )  && (cmdate == bday))
		  { 	 boldTag = "<STRONG><EM>"; 
			 unBoldTag = "</EM></STRONG>"; } 
		else  { 
			 boldTag = ""; 
			 unBoldTag = "";   } 
		calStr = calStr + "<TD " + dateBgAtt +  " >" + dateHref + boldTag + cmdate + unBoldTag + hrefCloseTag + "</TD>";
	 }
  while(0 != (i%7)) { 	calStr = calStr + "<TD>   </TD>"; i++; }
  calStr = calStr + "</TR>";
   calStr = calStr + "<TR class='calendarHeading'><TD align='center' colspan=7><a class= 'calendarEvent' href='javascript:previousCalendar();'>&lt;&lt;</A> | <a class= 'calendarEvent' href='javascript:nextCalendar();'> &gt;&gt;</A></TD></TR>" ;
  calStr = calStr + "</TABLE>\n"; 
  return calStr;
} 

function drawCalendar(ourYear, ourMonth, boldDay)
{
  // serves as a wrapper for writeCalendar() 
  // if you want to do anything special, such as changing the colors, 
  // or opening a special window, you can do it here before calling writeCalendar(). 
  // arguments: 
  // ourYear, ourMonth - integers, typically returned by Date.getMonth() and Date.getDate()
  // boldDay 
  var myMonth = myDate.getMonth(); 
  var myYear  = myDate.getFullYear(); 
  // strictly speaking, all the arguments are optional. 
  // if you only want this month's calendar, called drawCalendar() with no args. 
  if(arguments.length >= 1) { myYear = ourYear; } 
  if(arguments.length >= 2) { myMonth = ourMonth; } 
  var calStr = calendarString(myYear, myMonth, boldDay);
  return calStr;
} 

// more static vars for cruising through the calendars  
var lastMonth = (thisMonth + 11) % 12; 
var lmYear    = (thisMonth == 0)? thisYear -1: thisYear; 
var nextMonth = (thisMonth +1)%12; 
var nmYear    = (thisMonth == 11)? thisYear + 1: thisYear;
 
function newCalendar()
{ 
  var myMonth = thisMonth; 
  var myYear  = thisYear; 
  // it's surprising how many coders don't use modular arithmetic to 
  // do date and time. Javascript doesn't understand negative numbers in
  // positive modulus, so we add 11 instead of subtracting 1. 
  lastMonth = (myMonth + 11) % 12; 
  lmYear    = (myMonth == 0)? myYear -1: myYear; 
  nextMonth = (myMonth +1)%12; 
  nmYear    = (myMonth == 11)? myYear + 1: myYear;
  var isThisMonth = ((myMonth == myDate.getMonth())&& (lmYear==myDate.getFullYear())); 
  var calEl = document.getElementById("eventCalendar");
  var calStr = drawCalendar(myYear, myMonth, (isThisMonth ? myDate.getDate():0)); 
 // alert(calStr);
  calEl.innerHTML = calStr;

} 

function previousCalendar()
{
  thisMonth = lastMonth;
  thisYear = lmYear;
  newCalendar();
}

function nextCalendar()
{
  thisMonth = nextMonth;
  thisYear = nmYear;
  newCalendar();
}

