//////////////////////////////////////////////////////////
// common.js
// contains common javascript functions for millihelens, including:
//   - methods to create collapsible sections
//   - asynchronous content fetching (Not yet implemented)
/////////////////////////////////////////////////////////

// Just hide the complexity of creating an XMLHttpRequest object
// on different browsers
function createXmlHttpRequest() {
  if (typeof XMLHttpRequest != "undefined") {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  return req;
}

function sendAsyncRequest(url, callback) {
  req = createXmlHttpRequest();
  req.open("GET", url, true);
  req.onreadystatechange = createCallback(req, callback);
  req.send("");
}

function createCallback(req, callback) {
  return function() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
	  if (callback != null) {
	    callback(req);
	  }
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
  }
}

