/*
HTTPd.js

WASD VMS Web Services, Copyright (C) 1996-2022 Mark G.Daniel.
Licensed under the Apache License, Version 2.0 (the "License").
Software under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See http://www.apache.org/licenses/LICENSE-2.0

Version Log
-----------
12-SEP-2013  MGD  move all JS out of inline
*/

function confact (btn,msg1,msg2) {
   this.btn = btn;
   this.msg1 = msg1;
   this.msg2 = msg2;
}

var confary = new Array();
confary[0] = new confact ("restart","Restart "," after allowing current requests to complete?");
confary[1] = new confact ("restart/now","Restart "," DISCONNECTING current requests?");
confary[2] = new confact ("restart/quiet","Restart "," when current requests reach zero?");
confary[3] = new confact ("exit","Exit "," allowing current requests to complete?");
confary[4] = new confact ("exit/now","Exit "," DISCONNECTING current requests?");
confary[6] = new confact ("zero","Zero "," server accounting?");
confary[7] = new confact ("zero/proxy","Zero "," proxy accounting?");
confary[8] = new confact ("active","Allow "," network connections to all instances (active mode)?");
confary[9] = new confact ("passive","Limit "," network connections to just one instance (passive mode)?");
confary[10] = new confact ("net/purge","Purge "," persistent (idle) network connections?");
confary[11] = new confact ("net/purge/all","Purge "," all (idle and in-progress) network connections, DISCONNECTING current requests?");

function doIt(link,btn) {
scope = "node instance";
   for (idx in confary)
      if (confary[idx].btn == btn) return confirm (confary[idx].msg1 + scope + confary[idx].msg2);
   return true;
}

var pingGO;
function doPing() {
   var url = '/rtt?ping';
   xhr = new XMLHttpRequest();
   xhr.open('GET', url, true);
   xhr.onload = function() {
      var mSecs = Date.now() - pingGO;
      if (mSecs < 100) 
         var ping = mSecs + '.0ms';
      else
         var ping = mSecs + 'ms';
      document.getElementById('pingRTT').innerHTML = '&nbsp;(' + ping + ')';
   };
   pingGO = Date.now();
   xhr.send();
}
setTimeout(doPing,200);

// Safari (at least) does not honour HTTP cache control headers!
// Its back-forward cache mechanism breaks stuff.  These are kludges!
var dueToSafariFBCache = new Date().getTime();
window.addEventListener("pageshow",function(event) {
   if (event.persisted) window.location.reload()
});
window.addEventListener("focus",function(event) {
  if (new Date().getTime() - dueToSafariFBCache > 60000) window.location.reload()
});

