//
// Crude AJAX framework.
//
//   By Dave Voorhis <dave@armchair.mb.ca>
//
// NOTE: Be sure to create an element with an ID of 'status',
//       or the "Loading... Please wait." message will
//       not appear, and errors will be generated.

// Show a specified status message in the 'status' element.
function showStatus(msg) {
    element = document.getElementById('status')
    if (element != null)
	element.innerHTML = msg
}

var loadMsgCounter = 0

function showLoading() {
  if (loadMsgCounter++ > 0)
	return
  showStatus('Loading...  Please wait.')
}

function hideLoading() {
  if (--loadMsgCounter == 0) {
	showStatus('')
  }
  if (loadMsgCounter < 0)
	loadMsgCounter = 0
}

// Obtain an XMLHttpRequest object.
function getXMLHttpReq() {
    if (window.XMLHttpRequest) {         // Mozilla/Safari
        return new XMLHttpRequest()
    }
    else if (window.ActiveXObject) {     // IE
        return new ActiveXObject("Microsoft.XMLHTTP")
    }
    return null
}

// Perform an asynchronous GET transaction, given:
//
//  strURL - the server URL, e.g. http://blah.com/something.php
//  query - query string consisting of urlencoded name=value pairs,
//          separated by ampersands, e.g. x=3&y=4&z=a%20b
//  updateFn - a function of the form function(msg) {}; msg contains
//             the result of the GET transaction.
//
// showLoading() and hideLoading() will be invoked to show progress.
//
function xmlhttpPost(strURL, query, updateFn) {
    var xmlHttpReq = getXMLHttpReq()
    xmlHttpReq.open('GET', strURL, true)
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
    xmlHttpReq.onreadystatechange = function() {
        if (xmlHttpReq.readyState == 4 || xmlHttpReq.readyState=="complete") {
	   hideLoading()
	   if (xmlHttpReq.status == 200)
	    	updateFn(xmlHttpReq.responseText)
	   else
		showStatus("Disconnected?  Error: " + xmlHttpReq.status)
	}
    }
    showLoading()
    xmlHttpReq.send(query)
}

// Perform an asynchronous GET transaction, given:
//
//  strURL - the server URL, e.g. http://blah.com/something.php
//  query - query string consisting of urlencoded name=value pairs,
//          separated by ampersands, e.g. x=3&y=4&z=a%20b
//  updateFn - a function of the form function(msg) {}; msg contains
//             the result of the GET transaction.
//
// No progress display is given.
//
function xmlhttpPostSilent(strURL, query, updateFn) {
    var xmlHttpReq = getXMLHttpReq()
    xmlHttpReq.open('GET', strURL, true)
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
    xmlHttpReq.onreadystatechange = function() {
        if (xmlHttpReq.readyState == 4 || xmlHttpReq.readyState=="complete") {
	   if (xmlHttpReq.status == 200)
	    	updateFn(xmlHttpReq.responseText)
	   else
		showStatus("Disconnected?  Error: " + xmlHttpReq.status)
	}
    }
    xmlHttpReq.send(query)
}

// Perform a synchronous GET transaction, given:
//
//  strURL - the server URL, e.g. http://blah.com/something.php
//  query - query string consisting of urlencoded name=value pairs,
//          separated by ampersands, e.g. x=3&y=4&z=a%20b
//  updateFn - a function of the form function(msg) {}; msg contains
//             the result of the GET transaction.
//
// showLoading() and hideLoading() will be invoked to show progress.
//
function xmlhttpPostSynchronous(strURL, query, updateFn) {
    var xmlHttpReq = getXMLHttpReq()
    xmlHttpReq.open('GET', strURL, false)
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
    showLoading()
    xmlHttpReq.send(query)
    if (xmlHttpReq.status == 200)
	updateFn(xmlHttpReq.responseText)
    else
	showStatus("Disconnected?  Error: " + xmlHttpReq.status)
    hideLoading()
}

// Perform an asynchronous GET transaction, given:
//
//  strURL - the server URL, e.g. http://blah.com/something.php
//  query - query string consisting of urlencoded name=value pairs,
//          separated by ampersands, e.g. x=3&y=4&z=a%20b
//  updateFn - a function of the form function(msg, param) {}; msg contains
//             the result of the GET transaction.
//  param - may be used to pass an argument to updateFn()
//
// showLoading() and hideLoading() will be invoked to show progress.
//
function xmlhttpPostParam(strURL, query, updateFn, param) {
    var xmlHttpReq = getXMLHttpReq()
    xmlHttpReq.open('GET', strURL, true)
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
    xmlHttpReq.onreadystatechange = function() {
        if (xmlHttpReq.readyState == 4 || xmlHttpReq.readyState=="complete") {
           hideLoading()
	   if (xmlHttpReq.status == 200)
	    	updateFn(xmlHttpReq.responseText, param)
	   else
		showStatus("Disconnected?  Error: " + xmlHttpReq.status)
	}
    }
    showLoading()
    xmlHttpReq.send(query)
}
