// Function for constructing a request object and running the request.
//   reqType      The HTTP request type such as GET or POST.
//   url          The URL of the server program.
//   respHandler  The function that will handle the response.
//   postData     POST data.

function httpRequest (reqType, url, respHandler, postData) {
  var request = null;

  // Mozilla based browsers

  if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();

  // Internet Explorer based browsers

  } else if (window.ActiveXObject) {
    request = new ActiveXObject("Msxml2.XMLHTTP");
    if (! request) {
      request = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }

  // Very unlikely, but we test for a null request if neither ActiveXObject was initialized

  if (request) {
    try {
      request.onreadystatechange = function () {
        try {
          if (request.readyState == 4) {
            if (request.status == 200) {
              respHandler(request);
            } else {
              alert(
                "A problem occurred with communicating between the XMLHttpRequest object and the server program."
              );
            }
          }
        } catch(err) {
          alert(
            "It does not appear that the server is available for this application. Please try again very soon.\n" +
            "Error detail: " + err.message
          );
        }
      };
      request.open(reqType, url, true);
      if (reqType.toLowerCase() == "post") {
        request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        request.send(postData);
      } else {
        request.send(null);
      }
    } catch (err) {
      alert(
        "The application cannot contact the server at the moment. Please try again in a few seconds.\n" +
        "Error detail: " + err.message
      );
    }
  } else {
    alert("Your browser does not permit the use of all of this application's features!");
  }
}

