<!--
// TURN ON DEBUG WINDOW FEEDBACK
// I WILL WANT TO DISABLE THIS IN THE LIVE VERSION
var httpTesting = false;
// SET MY REQUEST OBJECT
var http = createRequestObject();
// VARIABLE TO TRACK IF WE ARE CURRENTLY IN A CALL
var inCall = false;
// QUEUE FOR CALLS
var callToArray = new Array();
// QUEUE FOR FUNCTION TO EXECUTE WHEN CALL COMPLETE
var returnToArray = new Array();

var visibleArray = new Array();

var timers = new Array();

function createRequestObject() {
   var request_;
   var browser = navigator.appName;
   if (browser == "Microsoft Internet Explorer") {
      request_ = new ActiveXObject("Microsoft.XMLHTTP");
   } else {
      request_ = new XMLHttpRequest();
   }
   return request_;
}

function ReadFile(whereTo, returnTo){
  // GET THE NEXT ARRAY ITEM AND REMOVE FROM THE ARRAY
  callToArray.push(whereTo);
  returnToArray.push(returnTo);
}

function callQueue(){
  // IF WE HAVE A WAY OF MONITORING THE QUEUE, UPDATE IT
  if(httpTesting){
    calls = "";
    for(i=0;i<callToArray.length;i++){
      calls += callToArray[i] + "\n";
    }
    document.getElementsByName("queueTest")[0].value = calls;
    document.getElementsByName("queueInCall")[0].value = inCall;
    document.getElementsByName("queueReadyState")[0].value = http.readyState;
  }
  // CHECK THE QUEUE AND SEND THE NEXT CALL IN LINE
  if(!inCall && callToArray.length > 0){
    // DO WE HAVE ANYTHING IN THE QUEUE?
    if(callToArray.length > 0){
      // WE DO, SO GET THE FIRST ITEM IN THE CALL ARRAY AND REMOVE IT
      whereTo = callToArray.shift();
      returnTo = returnToArray.shift();
      // SEND THAT CALL
      doCall(whereTo, returnTo);
    }else{
      // UPDATE DEBUG QUEUE
      if(httpTesting){
        document.getElementsByName("queueMsg")[0].value = "no items in queue.";
      }
    }
  }else{
    // UPDATE DEBUG QUEUE
    if(httpTesting){
      if(inCall){
        document.getElementsByName("queueMsg")[0].value = "currently in a call.";
      }else{
        document.getElementsByName("queueMsg")[0].value = "no items in queue.";
      }
    }
  }
}

function doCall(whereTo, returnTo) {
  inCall = true;
  http.open('get', whereTo, true);
  // DO WE HAVE A FUNCTION TO CALL ONCE CALL IS COMPLETED?
  if(returnTo.length > 0){
    http.onreadystatechange = function() {
      if (http.readyState == 4) {
         inCall = false;
         response = http.responseText;
         if (response != undefined) {
            document.getElementById(returnTo).innerHTML = response;
         } else {
            document.getElementById(returnTo).innerHTML = "An error occured while loading the page. Please try again in a few minutes. We apologise for the inconvenience.";
         }
      }
    }
  }
  // SEND CALL
  http.send(null);
}

function hr_inCall(){
  if(http.readyState == 4){
    inCall = false;
  }
}

var queueWatcher = setInterval(callQueue, 100);

function PopUp(URL, width, height, scroll) {
   var WindowWidth = width;
   var WindowHeight = height;
   var ScrollBars = scroll;

   var WindowLeft = (screen.width - WindowWidth) / 2;
   var WindowTop = (screen.height - WindowHeight) / 2;

   day = new Date();
   id = day.getTime();

   eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars="+ScrollBars+",location=0,statusbar=0,menubar=0,resizable=0,width="+WindowWidth+",height="+WindowHeight+",left="+WindowLeft+",top="+WindowTop+"');");
}

function submitEnter(formfield,e) {
  var keycode;
  if (window.event)
    keycode = window.event.keyCode;
  else if (e)
    keycode = e.which;
  else
    return true;

  if (keycode == 13) {
    document.getElementById(formfield).submit();
    return false;
  }
  else
    return true;
}

function toggleVisible(frameset) {
   if (document.getElementById(frameset).style.visibility == 'hidden')
      document.getElementById(frameset).style.visibility = 'visible';
   else
      document.getElementById(frameset).style.visibility = 'hidden';
   if (document.getElementById(frameset).style.display == 'none')
      document.getElementById(frameset).style.display = 'block';
   else
      document.getElementById(frameset).style.display = 'none';
}

// -->
