//****************************************************************************
// refresh.js
//
// Description
//   The script checks to see whether a page should be refreshed.
//
//   If refresh=TRUE is in the URL path, the page will be refreshed.
//   If refresh=FALSE is in the URL path, the page will not be refreshed.
//   If refresh=TRUE and refresh=FALSE are not in the URL path, and the page 
//      was last refreshed in the prior month, then the page will be refreshed.
//
//   It is acknowledged that getCalendarMonth returns the month of the visitor's
//   time zone, while getModMonth returns the host server's time zone.  For that
//   reason, the search client (located at theURL) does its own check, comparing
//   the host server's current time to the file's last mod.
//
// Known limitations:
//   1) The refresh is done by redirecting to the value of "theURL". 
//     I suppose that theURL could be anything, even g00gle.com, which is hardly
//     a refresh.  However, it's assumed that theURL will be a call to the
//     searchandshop search engine.  This is not currently enforced.
//
//   Date            Name     Description of change
//   -------     ----------   ----------------------------------------------
//   06/13/07    Mike Trutt   New
//****************************************************************************

//****************************************************************************
// getCalendarMonth()
//
// Description
//   The function returns the current month, on the user's PC
//
//   Date            Name     Description of change
//   -------     ----------   ----------------------------------------------
//   06/13/07    Mike Trutt   New
//   06/20/07    Mike Trutt   Changed to specify refresh in URL
//****************************************************************************
function getCalendarMonth()
{
   var now         = new Date();
   var monthnumber = now.getMonth() + 1;

   return monthnumber;
} // function getCalendarMonth()


//****************************************************************************
// getModMonth()
//
// Description
//   The function returns the month in which the current static webpage
//   was last modified.
//
//   Date            Name     Description of change
//   -------     ----------   ----------------------------------------------
//   06/13/07    Mike Trutt   New
//****************************************************************************
function getModMonth()
{
   var modMonth;

   lastmod = document.lastModified     // get string of last modified date
   lastmoddate = Date.parse(lastmod)   // convert modified string to date
   if(lastmoddate == 0){               // unknown date (or January 1, 1970 GMT)
      modMonth = getCalendarMonth() - 1;
   } else {
      d = new Date(lastmod);
      modMonth = d.getMonth() + 1;     // getMonth is zero-based
   }

   return modMonth;
} // function getModMonth()

//****************************************************************************
// getValue(Name)
//
// Description
//
//   Date            Name     Description of change
//   -------     ----------   ----------------------------------------------
//   06/20/07    Mike Trutt   New
//****************************************************************************
function getValue(Name) 
{
   var search = Name + "=";
   var returnvalue = "";
      
   if (self.location.href.length > 0) 
   {
      offset = self.location.href.indexOf(search);
      if (offset != -1) 
      {
         offset += search.length;
         end = self.location.href.indexOf("&", offset);
         if (end == -1) end = self.location.href.length;
            
         returnvalue=unescape(self.location.href.substring(offset, end))
      }
   }
      
   return returnvalue;
}

//****************************************************************************
// refresh(theURL, force)
//
// Description
//   The function checks to see whether a page should be refreshed.
//
//   If refresh=TRUE is in the URL path, the page will be refreshed.
//   If refresh=FALSE is in the URL path, the page will not be refreshed.
//   If refresh=TRUE and refresh=FALSE are not in the URL path, and the page 
//      was last refreshed in the prior month, then the page will be refreshed.
//
//   Date            Name     Description of change
//   -------     ----------   ----------------------------------------------
//   06/13/07    Mike Trutt   New
//   06/20/07    Mike Trutt   Changed to specify refresh in URL
//****************************************************************************
function refresh(theURL)
{
   var calendarMonth = getCalendarMonth();
   var modMonth = getModMonth();

   var chooseRefresh = getValue("refresh");

   if (chooseRefresh == "TRUE") 
   {
      window.location.href = theURL + "?force_refresh=TRUE";
      // Note that this decision will tell the search engine located at theURL
      // to refresh, even if the page does not appear to be out of date.
   }
   else if (chooseRefresh == "FALSE") 
   {
      return;
      // Note that this decision prevents refreshing the page, even
      // if it is out of date.
   }
   else if (modMonth < calendarMonth)
   {
      window.location.href = theURL;
      // Note that this decision is being made based on the user's local clock,
      // which may not be in the same time zone as the host server.  Therefore,
      // theURL, which points to the appropriate search client domain, will
      // will re-evaluate whether the file is out of date by comparing it to
      // the host server's current time.
   }
} // refresh
