var dateMsg = ""

function changeDate(theDate) {
   /* Change the next two lines to correct for a special meeting date. *
    * oldDate should be the normal meeting - yr, mo, day, hr, and min. *
    * newDate should be the special date the meeting is held on. You   *
    * must include - yr, mo, day, hr, min. Min can be blank if zero.   *
    * Remember that months go from 0 - 11, so December is 11.          */                  
   oldDate = new Date(2010, 0, 2, 8);
   newDate = new Date(2010, 0, 9, 8);
   if ((theDate.getDate() == oldDate.getDate()) &&
       (theDate.getMonth() == oldDate.getMonth()) &&
       (theDate.getFullYear() == oldDate.getFullYear()) &&
       (theDate.getHours() == oldDate.getHours()) && 
       (theDate.getMinutes() == oldDate.getMinutes())) {
      dateMsg = "<br>(This a special date or time. See Announcements<br>" +
                "on the home page for details) ";
      return newDate;
      }
   else {
      return theDate;
   }
}
      
function myMinutes(theDate) {
   if (theDate.getMinutes() == 0)
      return '00'
   else
      return theDate.getMinutes();
}

function monthName(m) {
   switch (m) {
      case  0: return "January";
      case  1: return "February";
      case  2: return "March";
      case  3: return "April";
      case  4: return "May";
      case  5: return "June";
      case  6: return "July";
      case  7: return "August";
      case  8: return "September";
      case  9: return "October";
      case 10: return "November";
      case 11: return "December";
      default: return "";
   }
}


function weekdayName(m) {
   switch (m) {
      case  0: return "Sunday";
      case  1: return "Monday";
      case  2: return "Tuesday";
      case  3: return "Wednesday";
      case  4: return "Thursday";
      case  5: return "Friday";
      case  6: return "Saturday";
      default: return "";
   }
}


function getSpecDate2(theYear, theMonth, theWeek, theDayOfWeek) {
   // theYear: a full four digit year
   // theMonth: a number 0 - 11
   // theWeek: a number between 1 and 5
   // theDayOfWeek: a number between 0 and 6
   // Returns a date object cooresponding to the arguments

   oneWeek = 604800000;  // milliseconds in a week
   // Start with 1st of month (noon chosen)
   theDate = new Date(theYear, theMonth, 1, 12);
   // Find first occurance of wanted day-of-week
   offset = theDayOfWeek - theDate.getDay();
   if (offset < 0) {
      offset += 7;
      }
   theDate.setDate(theDate.getDate() + offset);
   // Add weeks to the reach the desired week
   theDate.setTime(theDate.getTime() + (theWeek - 1) * oneWeek);
   // Make sure we're still in the same month
   if (theDate.getMonth() == theMonth) {
      return theDate;
      }
   else {
      return null;
}
}
   
function getSpecDate(theYear, theMonth, type) {
   // theYear a full four digit year
   // theMonth a number 0 - 11
   // type = 0 for First Saturday of month
   // type = 1 for third Friday of month
   // type = 2 for fourth Saturday of month
   //          (Start of June Field Day)
   // Returns a date object cooresponding to the arguments;

    switch (type) {
      case (0): // Club Breakfast date (OLD - First Saturday of the month.)
         // Set theDate to last day of the first week of the month.
         // This is the last day the breakfast can be held on.
         theDate = new Date (theYear, theMonth, 7, 8);
         daysOffset = 1; // no. days between Saturday and Sunday
         break;
      case (1):  // Club Meeting date (Third Friday of the month.)
         // Set theDate to last day of the third week of the month.
         // This is the last day the meeting can be held on.
         theDate = new Date (theYear, theMonth, 21, 19);
         daysOffset = 2; // no. days between Friday and Sunday
         break;
      case (2): // June Field Day date (Fourth Saturday of the month.)
         // (month is forced to June)
         // Set theDate to last day of the fourth week of the month.
         // This is the last day Field Day can start on.
         theDate = new Date (theYear, 5, 28, 11);
         daysOffset = 1; // no. days between Saturday and Sunday
         break;
      case (3): // Club Breakfast date (NEW - Second Saturday of the month.)
         // Set theDate to last day of the second week of the month.
         // This is the last day the breakfast can be held on.
         theDate = new Date (theYear, theMonth, 14, 8);
         daysOffset = 1; // no. days between Saturday and Sunday
         break;
      }
   // Using theDate's day-of-the-week, back to desired day
   daysSinceMtg = theDate.getDay() + daysOffset;
   if (daysSinceMtg > 6) {
      daysSinceMtg = daysSinceMtg - 7;
      }
   theDate.setDate(theDate.getDate() - daysSinceMtg);
   return theDate;
}

function meetingDate(mtgType) {
   // mtgType = 0 for next Breakfast meeting (Old - 1st Sat. of Mo.)
   // mtgType = 1 for next Club Meeting
   // mtgType = 3 for next Breakfast meeting (New - 2nd Sat. of Mo.)
   today = new Date(); // Get current date
   theYear = today.getFullYear();
   theMonth = today.getMonth();
   dateMsg = "";
   mtgDate = getSpecDate(theYear, theMonth, mtgType);
   mtgDate = changeDate(mtgDate);
   if (today > mtgDate) {
      // The meeting date has already passed so increment month
      theMonth = theMonth + 1;
      if (theMonth > 11) {
         theMonth = 0; // It's next year
         theYear = theYear + 1;
         }
      dateMsg = "";
      mtgDate = getSpecDate(theYear, theMonth, mtgType);
      mtgDate = changeDate(mtgDate);
      }
   // This code takes care of the fact that there is no General
   // December meeting!
   if (mtgType == 1) {
      if (mtgDate.getMonth() == 11) {
         theMonth = 0; // Increment to January of next year
         theYear = theYear + 1;
         mtgDate = getSpecDate(theYear, theMonth, 1);
         dateMsg = '<br>There is no December meeting due ' +
               'the annual Holiday Dinner';
         }
   }
   return weekdayName(mtgDate.getDay()) + ', '
          + monthName(mtgDate.getMonth()) + ' '
          + mtgDate.getDate() + ', '
          + mtgDate.getFullYear() + ' at '
          + mtgDate.getHours() + ':'
          + myMinutes(mtgDate) +' Hrs.'
          + dateMsg;

}

function fdDate() {
   aDay = 86400000; // milliseconds in a Day 
   theYear = new Date().getFullYear();
   fd = getSpecDate(theYear, 5, 2);
   today = new Date();
   today.setTime(today.getTime() + aDay); // add a day for FD period
   if (today > fd) {
      fd = getSpecDate(theYear + 1, 5, 2);
      }
   document.write(' the weekend of June ' + fd.getDate() +
   ' and ' + (fd.getDate() + 1) + ', ' + fd.getFullYear() + '.');
}
