/*
  ********************************************************************************
  *                                                                              *
  *    Use this javascript code to insert a calendar table into an html page.    *
  *                                                                              *
  *    Insert:                                                                   *
  *      <script type="text/javascript">                                         *
  *         calendar();                                                          *
  *       </script>                                                              *
  *    into the body of the html page where you want the calendar to appear.     *
  *                                                                              *
  *    If you want a certain date (for example: May 28, 2006)                    *
  *    insert instead:                                                           *
  *       <script type="text/javascript">                                        *
  *          calendar("May 28, 2006");                                           *
  *       </script>                                                              *
  *                                                                              *
  *    Be sure to also include the calendar.css file                             *
  *    and add the following code in the head section of the html file:          *
  *    <link href="calendar.css" rel="stylesheet" type="text/css" />             *
  *                                                                              *
  ********************************************************************************
      
   This code was take from the book "New Perspectives on JavaScript"
                                     by Carey and Canovatchel
                                     published by Thomson
                                     ISBN 0-619-26797-6

   Editied by: Gil Hanson
   Date:       Sept 12, 2006
*/



function calendar(calendarDay) //------------- MAIN FUNCTION -------------------
{
   var calDate;                        // create calDate variable
   
   if (calendarDay == null)            // If calendarDay paramater is null 
                                       // set calDate to the current date.
   {
      calDate = new Date()
   }
   else                                // Otherwise set calDate to the value of the parameter.
   {
      calDate = new Date(calendarDay);
   }
      
   document.write("<table id='calendar_table' align='center'>");  // start the table
      writeCalTitle(calDate);                      // write the title row
      writeDayNames()                              // write the row of day names
      writeCalDays(calDate);                       // fill in the calendar day numbers
   document.write("</table>");                     // end the table
}
//------------------------------------------------------------------------------


function writeCalTitle(calendarDay)//***********************************
// write the first row which displays the month and year
{
   // create the month names
   var monthName = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
   
   var thisMonth = calendarDay.getMonth();
   var thisYear = calendarDay.getFullYear();
   
   document.write("<tr>");
   document.write("   <th id='calendar_head' colspan='7'>");
   document.write(       monthName[thisMonth] + " " + thisYear);
   document.write("   </th>");
   document.write("</tr>");
}

function writeDayNames() //*************************************************
// write the second row which displays the day names
{
   // create the day names
   var dayName = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
   
   // start the row
   document.write("<tr>");
   
   // write each dayName in its own cell
   for (var i=0; i<dayName.length; i++)
   {
      document.write("<th class='calendar_weekdays'>" + dayName[i] + "</th>");
   }
   
   // end the row
   document.write("</tr>");
}

function daysInMonth(calendarDay)//*****************************************
// calculate the number of days in February (leap year or no leap year)
{
   var thisYear = calendarDay.getFullYear();
   var thisMonth = calendarDay.getMonth();
   
   // create an array that contains the usual number of days in each month
   var dayCount = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
   
   // Calculate if this is a leap year
   if (thisYear % 4 == 0)  // a leap year must be divisible by 4
   {
      // Then
      // a leap year must NOT be divisble by 100
      // OR
      // a leap year must be divisble by 400
      if (  (thisYear % 100 != 0) || (thisYear % 400 == 0)  )
      {
         dayCount[1] = 29;  // this is a leap year so change the number of days to 29
      }
   }
   
   return dayCount[thisMonth]; // return the number of days in the month
}

function writeCalDays(calendarDay) //*****************************************
// fill in the calendar
{
   var currentDay = calendarDay.getDate();          // get the number of the current day
   
   // determine the starting day of the week for this months calendar
   var dayCount = 1;                                // set dayCount to the first day of the month
   var totalDays = daysInMonth(calendarDay);        // get the number of days in the current month
   calendarDay.setDate(1);                          // set the date to the first day of the month
   var weekDay = calendarDay.getDay();              // get the day of the week the first day falls on
   
   // write blank cells preceding the starting day
   if (weekDay != 0) document.write("<tr>");        // start the table row if weekDay is not zero (Sunday)
                                                    // Note that if weekDay is Sunday
                                                    // the row will be started in the following while loop
                                                    
   for (var i=0; i<weekDay; i++)                    // run the loop if weekDay is greater than zero
   {
      document.write("<td>&nbsp;</td>");            // write a non-breaking space into the cell
   }
   
   // write cells for each day of the month
   while (dayCount <= totalDays)
   {
      if (weekDay == 0) document.write("<tr>");     // start a new row if the day is Sunday
      
      if (dayCount == currentDay)                   // check to see if dayCount is the current day
                                                    // if so, highlight it
      {
         // highlight the current day
         document.write("<td id='calendar_today'>" + dayCount + "</td>");
      }
      else
      {
         // display the day as usual
         document.write("<td class='calendar_dates'>" + dayCount + "</td>");
      }
      
      if (weekDay == 6) document.write("</tr>");     // if the day is Saturday, end the row
      
      dayCount++;                                    // move to the next day
      calendarDay.setDate(dayCount);                 // change the calendarDay variable to the next day
      weekDay = calendarDay.getDay();                // update the weekDay variable
   }
   
   document.write("</tr>");                          // close the final table row
}
