// Constructor for cvhXHR objects, which serve to hold the XHR and the function it is supposed to run when the server responds (passed in as callback). In the callback, use this.XHR to access the XHR object.
function cvhXHR(callback, url, post) {
  this.XHR; // sets this as a public variable.
  var post = (post === undefined) ? null : post; // sets a default for post
  var objectThis = this; // allows access to the cvhXHR instance from the XHR's onreadystatechange handler.
  if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    this.XHR = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // IE
    this.XHR = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    this.XHR = false;
  }
  if(this.XHR) {
    this.oncomplete = callback;
    this.XHR.onreadystatechange = function() {
      if(objectThis.XHR.readyState == 4) {
	if(objectThis.XHR.status == 200) {
	  // try {
	    objectThis.oncomplete();
	  // }
	  /* catch(err) {
	    alert("XHR error:\nError calling callback method");
	  } */
	} else {
	  alert("XHR error:\n"+objectThis.XHR.statusText);
	}
      }
    }
    this.XHR.open((post ? "POST" : "GET"), url, true);
    if(post) {
      this.XHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); // Need to check this, shouldn't this be set for both code blocks?
      this.XHR.send(post);
    } else {
      this.XHR.send('');
    }
  }
}

