<!--
// These global variables are available not just to
//  all these routines, but to callers as well.
var http_req, http_req, lock, params

lock = false  // keeps us from trying to validate twice
params = new Object  // allows us to pass parameters to finish_req and to cleanup routine

//This function registers a page-specific handler
// to be invoked after the XML transaction is finished
//  It accepts any number of arguments, and saves them as params.arg[1] .. params.args[n]
  function user_cleanup(fn) {
    params.cleanup = fn
    if (arguments.length>1) {
      params.args = Array();
      for (i=1; i<arguments.length; i++) {
	params.args[i] = arguments[i];
      }
    }
  }

//This function always returns false.
  function user_get(cmdstr) {
  if (!lock) {
    lock = true
    if (!http_req) {http_req = newRequest();}
    http_req.open('GET', cmdstr, true);
    http_req.onreadystatechange = function(){finish_req(http_req);}
    http_req.send(null);
    }
  return false
}
//This function always returns false.
  function user_post(url, datastr) {
  if (!lock) {
    lock = true
    if (!http_req) {http_req = newRequest();}
    http_req.open('POST', url , true);
    http_req.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
    http_req.onreadystatechange = function(){finish_req(http_req);}
    http_req.send(datastr);
    }
  return false
}

//This function is called each time the XML-HTTP ready state changes.
//  When that changes to 4 [complete], this function completes the  transaction.
//  After generic cleanup, a user-registered callback routine is invoked, passing
//  the reply XML object as an explicit parameter.  Other parameters can be
//  retrieved from the global params object.
  function finish_req(reqObj) {
  if (!reqObj) return
  var reply
  if (reqObj.readyState == 4) {
      if (reqObj.status == 200) {
	reply = reqObj.responseXML
      } else {
	alert('There was a problem with an AJAX request:' + reqObj.status)
      }
      lock = false
      if (params.cleanup) params.cleanup(reply)
      params.cleanup = null
      params.args = null
      if (params.chain) params.chain()
      params.chain = null
    }
  }

function newRequest() {
    var http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
	http_request = new XMLHttpRequest();
	if (http_request.overrideMimeType) {
	    http_request.overrideMimeType('text/xml');
	    // See note below about this line
	}
    } else if (window.ActiveXObject) { // IE
	try {
	    http_request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	    try {
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (e) {}
	}
    }

    if (!http_request) {
	alert('Giving up. This browser is too old to create an XMLHTTP instance');
	return false;
    } else { return http_request }
}

// This function returns a string value corresponding
//  to a tag from an XML object.
// If the tag does not exist, the return is NULL.
// If the tag exists but is empty, the return is the empty string.
function xml_tag_value(xmlobj, s_label) {
  var p = xmlobj.getElementsByTagName(s_label)
  if (p.length == 0) {
    return null
  } else {
    if (p[0].nodeValue != null) return p[0].nodeValue
    if (p[0].childNodes.length == 0) return ""
    return p[0].childNodes[0].nodeValue
  }
}

// Instead of XML, we might want the text between two tags:
function strip_tags(text,tag) {
  var i,j
  i= text.indexOf('<'+tag+'>')
  j= text.indexOf('</'+tag+'>')
  text = ((i>=0 && j>=0)?text.substring(i+tag.length+2,j):'')
  return text
}

//AJAX versions of GET and POST with callback
function html_prep(nmvect,valvect) {
  var req
  var pairs = [];
  var regexp = /%20/g
  req = ''
  if (nmvect && nmvect.length>0) {
    for (i=0; i<nmvect.length; i++) {
      var nval = encodeURIComponent(nmvect[i]).replace(regexp,'+')+'='+encodeURIComponent(valvect[i]).replace(regexp,'+')
      pairs.push(nval)
    }
    req += pairs.join('&')
  }
  return req
}

function html_get(reqStr,cb_func,nmvect,valvect) {
  user_cleanup(cb_func)
  var req = html_prep(nmvect,valvect)
  user_get(reqStr+((req.length==0)?'':'?'+req))
}

function html_post(url,cb_func,nmvect,valvect) {
  user_cleanup(cb_func)
  var req = html_prep(nmvect,valvect)
  user_post(url,req)
}

//-->

