/* set up variables used to init clock in BODY's onLoad handler; should be done as early as possible */ var clockLocalStartTime = new Date(); var clockServerStartTime = new Date(2010,3,10,2,42,14); /* stub functions for older browsers; will be overridden by next JavaScript1.2 block */ function clockInit() { } function simpleFindObj(name, inLayer) { return document[name] || (document.all && document.all[name]) || (document.getElementById && document.getElementById(name)) || (document.layers && inLayer && document.layers[inLayer].document[name]); } var clockIncrementMillis = 60000; var localTime; var clockOffset; var clockExpirationLocal; var clockShowsSeconds = false; var clockTimerID = null; function clockInit(localDateObject, serverDateObject) { var origRemoteClock = parseInt(clockGetCookieData("remoteClock")); var origLocalClock = parseInt(clockGetCookieData("localClock")); var newRemoteClock = serverDateObject.getTime(); // May be stale (WinIE); will check against cookie later // Can't use the millisec. ctor here because of client inconsistencies. var newLocalClock = localDateObject.getTime(); var maxClockAge = 60 * 60 * 1000; // get new time from server every 1hr if (newRemoteClock != origRemoteClock) { // new clocks are up-to-date (newer than any cookies) document.cookie = "remoteClock=" + newRemoteClock; document.cookie = "localClock=" + newLocalClock; clockOffset = newRemoteClock - newLocalClock; clockExpirationLocal = newLocalClock + maxClockAge; localTime = newLocalClock; // to keep clockUpdate() happy } else if (origLocalClock != origLocalClock) { // error; localClock cookie is invalid (parsed as NaN) clockOffset = null; clockExpirationLocal = null; } else { // fall back to clocks in cookies clockOffset = origRemoteClock - origLocalClock; clockExpirationLocal = origLocalClock + maxClockAge; localTime = origLocalClock; // so clockUpdate() will reload if newLocalClock // is earlier (clock was reset) } /* Reload page at server midnight to display the new date, by expiring the clock then */ var nextDayLocal = (new Date(serverDateObject.getFullYear(), serverDateObject.getMonth(), serverDateObject.getDate() + 1)).getTime() - clockOffset; if (nextDayLocal < clockExpirationLocal) { clockExpirationLocal = nextDayLocal; } } function clockOnLoad() { clockUpdate(); } function clockOnUnload() { clockClearTimeout(); } function clockClearTimeout() { if (clockTimerID) { clearTimeout(clockTimerID); clockTimerID = null; } } function clockToggleSeconds() { clockClearTimeout(); if (clockShowsSeconds) { clockShowsSeconds = false; clockIncrementMillis = 60000; } else { clockShowsSeconds = true; clockIncrementMillis = 1000; } clockUpdate(); } function clockTimeString(inHours, inMinutes, inSeconds) { return inHours == null ? "-:--" : ((inHours == 0 ? "12" : (inHours <= 12 ? inHours : inHours - 12)) + (inMinutes < 10 ? ":0" : ":") + inMinutes + (clockShowsSeconds ? ((inSeconds < 10 ? ":0" : ":") + inSeconds) : "") + (inHours < 12 ? " AM" : " PM")); } function clockDisplayTime(inHours, inMinutes, inSeconds) { clockWriteToDiv("ClockTime", clockTimeString(inHours, inMinutes, inSeconds)); } function clockWriteToDiv(divName, newValue) // APS 6/29/00 { var divObject = simpleFindObj(divName); //newValue = '

' + newValue + '<' + '/p>'; if (divObject && divObject.innerHTML) { divObject.innerHTML = newValue; } else if (divObject && divObject.document) { divObject.document.writeln(newValue); divObject.document.close(); } // else divObject wasn't found; it's only a clock, so don't bother complaining } function clockGetCookieData(label) { /* find the value of the specified cookie in the document's semicolon-delimited collection. For IE Win98 compatibility, search from the end of the string (to find most specific host/path) and don't require "=" between cookie name & empty cookie values. Returns null if cookie not found. One remaining problem: Under IE 5 [Win98], setting a cookie with no equals sign creates a cookie with no name, just data, which is indistinguishable from a cookie with that name but no data but can't be overwritten by any cookie with an equals sign. */ var c = document.cookie; if (c) { var labelLen = label.length, cEnd = c.length; while (cEnd > 0) { var cStart = c.lastIndexOf(';',cEnd-1) + 1; /* bug fix to Danny Goodman's code: calculate cEnd, to prevent walking the string char-by-char & finding cookie labels that contained the desired label as suffixes */ // skip leading spaces while (cStart < cEnd && c.charAt(cStart)==" ") cStart++; if (cStart + labelLen <= cEnd && c.substr(cStart,labelLen) == label) { if (cStart + labelLen == cEnd) { return ""; // empty cookie value, no "=" } else if (c.charAt(cStart+labelLen) == "=") { // has "=" after label return unescape(c.substring(cStart + labelLen + 1,cEnd)); } } cEnd = cStart - 1; // skip semicolon } } return null; } /* Called regularly to update the clock display as well as onLoad (user may have clicked the Back button to arrive here, so the clock would need an immediate update) */ function clockUpdate() { var lastLocalTime = localTime; localTime = (new Date()).getTime(); /* Sanity-check the diff. in local time between successive calls; reload if user has reset system clock */ if (clockOffset == null) { clockDisplayTime(null, null, null); } else if (localTime < lastLocalTime || clockExpirationLocal < localTime) { /* Clock expired, or time appeared to go backward (user reset the clock). Reset cookies to prevent infinite reload loop if server doesn't give a new time. */ document.cookie = 'remoteClock=-'; document.cookie = 'localClock=-'; location.reload(); // will refresh time values in cookies } else { // Compute what time would be on server var serverTime = new Date(localTime + clockOffset); clockDisplayTime(serverTime.getHours(), serverTime.getMinutes(), serverTime.getSeconds()); // Reschedule this func to run on next even clockIncrementMillis boundary clockTimerID = setTimeout("clockUpdate()", clockIncrementMillis - (serverTime.getTime() % clockIncrementMillis)); } }