// Display a clock, synchronized with server
// John Kellermann Friday, March 24, 2006
// Usage:
// <body onLoad="getSync()">
//
// <b id="clock"></b>

  var req; //Global variable for XMLHttpRequest usage.
  // var ustatus; //Global for status of update request. (optional)
  var timerID = null;
  var timerRunning = false;
  var showClock;
  var sync;

//Process the update request. User pressed Update button.
  function getSync() {
    // DO PREPARATION CODE HERE:
    //showClock=document.getElementById('clock');
    // ustatus=document.getElementById('upstatus'); // e.g. <b id="upstatus"> *** </b>
    // ustatus.innerHTML = "Updating";
    //showClock.innerHTML = "Updating";
    showClock=document.getElementById('clock');
    if (!showClock) {
    // no place to display clock on page, forget it.
    	return;
    }
    var pcDate = new Date();
    var pcTime = escape(pcDate.getTime());
	 var pcOffset = escape(pcDate.getTimezoneOffset());

    //Synchronize.
    var zeRequest = "/cgi-bin/synctime2.cgi";
    if (window.XMLHttpRequest) { //Non-Microsoft Browsers
      req = new XMLHttpRequest();
    } else if (window.ActiveXObject) { //Microsoft Browsers
      req = new ActiveXObject("MSXML2.XMLHTTP.3.0");
    }
    req.open("POST", zeRequest,  true);
    req.onreadystatechange = processNewTime; //This is setting the callback function.
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    //req.send('pcTime='+pcTime+'&pcOffset='+pcOffset);
    req.send('pcTime='+pcTime);
    //An asynchronous request has been sent.
    // The processNewTime() function will update status when done.
  }

//Process the XMLHttpRequest results (requires global variables "req" & "ustatus")
  //This function is the "doneFunc" parameter of HttpRequest(reqString, doneFunc).
  function processNewTime() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
      // only if "OK"
      if (req.status == 200) {
        // ...processing statements go here...
        // ustatus.innerHTML = "Update Finished";
        // alert(req.responseText); //this is a stub.
        stopclock();
        // ustatus.innerHTML = req.responseText;
        showClock=document.getElementById('clock');
        if (!showClock) {
            return;
        }
        sync = req.responseText;
        showTime(sync);
      } else {
         alert("Server Failure Message:\n" + req.statusText);
         ustatus.innerHTML = "Update Failed";
      }
    }
  }

function stopclock () {
        if(timerRunning)
        clearTimeout(timerID);
        timerRunning = false;
}

function showTime (sync) {
//return;
        var now = new Date();
        now.setTime (now.getTime() - sync);
        var hours = now.getHours();
        var minutes = now.getMinutes();
        var seconds = now.getSeconds();
        var ampm = (hours >= 12) ? "pm" : "am";  
        hours = ((hours >12) ? hours -12 :hours);    
        hours = ((hours == 0) ? 12 :hours); // after midnight
        hours = ((hours <10)? "&nbsp;" :"") + hours; // don't jump to left
        var timeValue = "Current time: " + hours;
        timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
        timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
        timeValue += ampm;
        showClock.innerHTML = timeValue;
        timerID = setTimeout("showTime(sync)",1000);
        timerRunning = true;
}

// End -->

