/* 
-------------------
Go to Calendar Date
------------------- */
function goToUrl(month,day,year) {
   var date = new Date(year,month,day);
   var mm = date.getMonth() + 1;
   var dd = date.getDate();
   var yy = date.getFullYear();

   <!-- If month is single digit, add zero before it -->
   if (mm.toString().length == 1) {
      mm = "0" + mm;
   }

   <!-- If day is single digit, add zero before it -->
   if (dd.toString().length == 1) {
      dd = "0" + dd;
   }

   window.location="/calendar/calendar.xml?month="+mm+"&day="+dd+"&year="+yy;
}

/* 
--------------------
Day of Week Function
-------------------- */
function compute(month,day,year) {
   var Calendar = new Date(year,month,day);
   var dayofweek = Calendar.getDay();
   return dayofweek;	
}

/* 
------------------------
Function to change month
------------------------ */
function changeMonth(change,selectMenu,day,year) {
   month = selectMenu.value;
   setCalendar(change,month,day,year);
}

/* 
-----------------------
Function to change year
----------------------- */
function changeYear(change,month,day,selectMenu) {
   year = selectMenu.value;
   setCalendar(change,month,day,year);
}


/* 
---------------------
Set Calendar Function
--------------------- */
function setCalendar(change,month,day,year) {

   /* Create new Date object. */
   var defaultdate = new Date();
   var defaultmonth = defaultdate.getMonth();		// Returns month (0-11)
   var defaultday = defaultdate.getDate();		// Returns day (1-31)
   var defaultyear = defaultdate.getFullYear();		// Returns year

   /* Create new Date object which calculates default values. */
   var date = new Date();

      /* If month not selected... */
      if (month == "" && month != 0) {
         var selectedMonth = date.getMonth();     // Returns month (0-11)
      } else {
         var selectedMonth = month;
      }

      /* If day not selected... */
      if (day == "") {
         var selectedDay = date.getDate();	    // Returns day (1-31)
      } else {
         var selectedDay = day;
      }

      /* If year not selected... */
      if (year == "") {
         var year = date.getFullYear();   // Returns year
      } else {
         var selectedYear = year;
      }

   /* Arrays. */
   var day_of_week = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
   var month_of_year = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
   var monthsabbr = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

   /* If change is next... */
   if (change == "next") {
	
      if (selectedMonth == 11) {
         selectedMonth = 0;
	 selectedYear++;
      } else {
         selectedMonth++;
      }
	
   }
	
   /* If change is prev... */
   else if (change == "prev") {
	
      if (selectedMonth == 0) {
         selectedMonth = 11;
	 selectedYear--;
      } else {
         selectedMonth--;
      }

   } 

   if (selectedMonth == 0) {
      prevMonth = 11;
      nextMonth = parseInt(selectedMonth) + 1;
   } else if (selectedMonth == 11) {
      prevMonth = parseInt(selectedMonth) - 1;
      nextMonth = 0;
   } else {
      prevMonth = parseInt(selectedMonth) - 1;
      nextMonth = parseInt(selectedMonth) + 1;
   }

   var startday = compute(selectedMonth,1,selectedYear);   // first day of the current month
   var days_of_week = 7;		   // "constant" for number of days in a week
   var days_of_month = 31;		   // "constant" for number of days in a month
   var weekday = date.getDay();		   // Returns day of week (0-6)	
   var cal;

   date.setDate(1);        // Start the calendar day at '1'
   date.setMonth(selectedMonth);   // Start the calendar month at now

   var startday = compute(selectedMonth,date.getDate(),selectedYear);

   /* 
   ------------------------------------
   Start adding values to cal variable.
   ------------------------------------ */
   cal = '<div style="float:left; width:50px;"><a href="javascript:setCalendar(\'prev\','+selectedMonth+','+selectedDay+','+selectedYear+')">&laquo; '+monthsabbr[prevMonth]+'</a></div>';
  
   // Begin calendar select form
   cal += '<form ID="calselect" name="calselect" style="float:left; width:118px; text-align:center; margin-bottom:10px;">';
						
      // Begin month select field
      cal += '<select name="mnth" onchange="javascript:changeMonth(null,this,'+defaultday+','+selectedYear+')">';
							
         // Run loop through months
         for (z=0; z < 12; z++) {          
		
            // if this value is the current month
	    if (z == selectedMonth) {
	       cal += '<option value="'+z+'" selected>'+monthsabbr[z]+'</option>';
	    } else { // not the current month
	       cal += '<option value="'+z+'">'+monthsabbr[z]+'</option>';
	    }
				
	 }
							
      // End month select field	
      cal += '</select>';
							
      // Begin month select field
      cal += '<select name="yr" onchange="javascript:changeYear(null,'+selectedMonth+','+defaultday+',this)">';
					
	 // Run loop through months
         for (z=1970; z < (defaultyear + (defaultyear-1970)); z++) {
						
            // if this value is the current year
	    if (z == selectedYear) {
	       cal += '<option value="'+z+'" selected>'+z+'</option>';
	    } else { // not the current year
	       cal += '<option value="'+z+'">'+z+'</option>';
	    }
									
         }
							
      // End month select field	
      cal += '</select>';
						
   // End calendar select form
   cal += '</form>';

   cal += '<div style="float:right; width:50px; text-align:right;"><a href="javascript:setCalendar(\'next\','+selectedMonth+','+selectedDay+','+selectedYear+')">'+monthsabbr[nextMonth]+' &raquo;</a></div>';

   cal += '<div class="clear"></div>';
   
   // Show today's date
   cal += '<div style="background:#d6f6cf; border:1px solid #a6c69f; margin-top:5px; margin-bottom:5px; padding:3px; text-align:center;"><b>Today is: </b>' + month_of_year[defaultmonth] + ' ' + defaultday + ', ' + defaultyear + '<br><a href="javascript:setCalendar(\'null\','+defaultmonth+','+defaultday+','+defaultyear+')">Current Month</a></div>';

      // Begin the calendar table
      cal += '<table border="0" cellpadding="0" cellspacing="1">';

         // Begin the days of the week row
	 cal += '<tr>';
		
	    /* Loop through days of week. */
	    for(index=0; index < days_of_week; index++) {
			
	       // bold today's day of week
	       if (startday == index && defaultmonth == selectedMonth && defaultyear == selectedYear) {
	          cal += '<td class="dowbox"><b>' + day_of_week[index] + '</b></td>';
	       }
				
	       // not today's day of week
	       else {
	          cal += '<td class="dowbox">' + day_of_week[index] + '</td>';
	       }
			
	    } // end loop through days of week.
		
	 // End the days of the week row
         cal += '</tr>';
		
	 // Begin the calendar days row
	 cal += '<tr valign="middle">';
		
	    /* Display empty days before first day of the month. */
	    for(index=0; index < startday; index++) {
	       cal += '<td class="dateboxoff"></td>';
	    }
			
	    // Loop through days in month
	    for(index=0; index < days_of_month; index++) {
			
	       if (date.getDate() > index) {
				
	          // Return the next day of the month.
		  week_day = compute(selectedMonth,date.getDate(),selectedYear);
					
		  // Start new row for first day of month
		  if (week_day == 0) {
		     cal += '<tr valign="middle">';
		  }
					
		  if (week_day != days_of_week) {
					
		     // Set variable for loop/day increment
		     var day  = date.getDate();
						
		     // Highlight today's date
		     if( selectedMonth == defaultmonth && selectedDay == day && selectedYear == defaultyear ) {
	                cal += '<td class="dateboxnow" onClick="goToUrl(\''+selectedMonth+'\',\''+day+'\',\''+selectedYear+'\')">' + day + '</td>';
		     }
						
		     // Print normal day (not today's date)
		     else {
                        cal += '<td class="dateboxon" onClick="goToUrl(\''+selectedMonth+'\',\''+day+'\',\''+selectedYear+'\')" onMouseOver="this.className=\'dateboxover\'" onMouseOut="this.className=\'dateboxon\'">' + day + '</td>';
		     }
						
		  }
					
		  // END ROW FOR LAST DAY OF WEEK
		  if (week_day == days_of_week)
		  cal += '</tr>';

	       }
				
	       // Increment until end of the month.
	       date.setDate(date.getDate()+1);

            }// end for loop through days in month
			
	    /* While there are empty days left 
            in the week, add empty boxes. */
	    var daysleft = days_of_week - week_day - 1;
	    while (daysleft > 0) {
	       cal += '<td class="dateboxoff"></td>';
	       daysleft--;
	    }
		
            // End the calendar days row
	    cal += '</tr>';
		
	// End the calendar table	
	cal += '</table>';
	
	/* Print the value of the "cal" variable
	inside the calendar-module div tag. */
	document.getElementById("calendar-module").innerHTML = cal;

} // end setCalendar() function