///////////////////////
// Script Constants:
//

var http=       false;  // This is the connection to the server.
var gateClosed= false;  // This is the processing gate.  Can't do more than one.
var keepLast=   false;  // Keep the last received code.
var argBuf=     "";     // Buffer to store buffer.
var sectionId=  "";     // To replace with output
var timeoutCmd= "";     // Code to execute if we timeout.


///////////////////////////////////////////////////////////////////////////
// Do we want to report errors?  True means supress, False means report:
//

window.onerror = function(){
  return false;
}



/////////////////////////////////////////////////////////////////////
// Extract javascript from the received ajax text, and execute it:
//

function runAjaxScript(txt){
  var re= /<script.*?>([\s\S]*?)<\//igm;
  var match;
  var cmd="";

  while(match=re.exec(txt)) cmd+=match[1];
  if(cmd!="") eval(cmd);
}


/////////////////////////////////////////////
// Get the response back from the request:
//

function ajaxResponse(){
  if(http.readyState==4) {
    if(http.status==200){
      setCellContents(sectionId, http.responseText);
      runAjaxScript(http.responseText);
      document.body.style.cursor="";
      if(keepLast) lastCode=http.responseText;
      gateClosed=false;
    } else {
      if(http.status==0 || http.status==408){
        document.body.style.cursor="";
        gateClosed=false;
        eval(timeoutCmd);
      } else alert('Error Code: "'+http.status+'"');
    }
  }
}


//////////////////////////////////////////////////////////////////////
// Post to a server page, and replace the doc "id" contents with it:
//

function ajaxPost( divId, url, keep, postArgs ) {
  gateClosed=true;
  keepLast=keep;
  sectionId=divId;

  // Get Ajax http Handle:
  http=(navigator.appName=="Microsoft Internet Explorer") ?
    new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();

  http.onreadystatechange=ajaxResponse;
  http.open("POST", url, true);
  http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  http.send(postArgs);
}


function ajaxBusy(){
  return gateClosed;
}


function setCellContents(id, text){
  document.getElementById(id).innerHTML=text;
}


//////////////////////////////////////////////////////////////////////
// Get a server page, and replace the doc "id" contents with it:
//

function ajaxGet( divId, url, keep, getArgs) {
  gateClosed=true;
  keepLast=keep;
  sectionId=divId;

  // Get Ajax http Handle:
  http=(navigator.appName=="Microsoft Internet Explorer") ?
    new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();

  http.onreadystatechange=ajaxResponse;
  http.open("GET", url+"?"+getArgs, true);
  http.send("");
}


/////////////////////////////////////////////////////////
// Build a buffer to hold the post field values:
//

function buildArgs(field, val){
  if(field==""){
    argBuf="";
    return val;
  }
  var pair=field+"="+val;
  if(argBuf!="") argBuf+="&";
  argBuf+=pair;
  return val;
}
