// John Ironmonger      Created 20090924

// Functions to implement Ajax requests.

// ajaxRequest(returnFunction, url, passthrough)

// returnFunction The javascript function that will be
//                called once the http request is done
//                returnFunction(xmcDoc, passthrough)
// url            The url the AJAX request should use to get data
// passThrough    Any ohect. This is not used in the AJAX
//                reqeust but will be passed back to the 
//                returnFunction and be available when processing 
//                the result.

// Removed again 10200831
// Return Type
//   'raw'        Return the actual HttpResponse Object
//   'string'     Return the content of the HTTP response as a string
//   'XML'        Return the content of the HTTP response as an XML DOM Object 
//   '' or missing Defaults to XML

// Did add some attributes to try and do ajax processing of forms.
// Removed again 10200831
// function ajaxRequest(returnFunction, url, passThrough, returnType, requestMethod) {
function ajaxRequest(returnFunction, url, passThrough) {
  var xmlHttp = createAjaxObject();
  if (!xmlHttp) return;
  
// Removed again 10200831
//  if (requestMethod != "POST") requestMethod = "GET";
  
  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4) {
      if (xmlHttp.status == 200) {
        // Used for debug
        // alert(xmlHttp.responseText);
// Removed again 10200831
//        var returnDoc;
//        if (returnType == 'Raw' || returnType == 'raw')
//          returnDoc = xmlHttp;
//        else if (returnType == 'String' || returnType == 'string')
//          returnDoc = xmlHttp.responseText;
//        else
//          returnDoc = xmlHttp.responseXML;
//        returnFunction(returnDoc, passThrough);
        returnFunction(xmlHttp.responseXML, passThrough);
      }
    }
  }

  // Add random number to AJAX url to make sure there
  // will not be any browser caching.
  // Removed randon number and changed cache headers
// Removed again 10200831
//  xmlHttp.open(requestMethod, url + "&r=" + Math.random(), true);
  xmlHttp.open("GET", url, true);
  xmlHttp.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
  xmlHttp.setRequestHeader('Cache-Control', 'no-store');
  xmlHttp.send(null);
}



// Works through the different browser calls
// and returns a HttpRequest ohject that can
// be used for the AJAX calls
function createAjaxObject() {
  var ajaxObject;

  if (window.XMLHttpRequest) {
    // Mozilla/Safari/IE7+
    ajaxObject = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    // IE6-
    try {ajaxObject = new ActiveXObject("Msxml2.XMLHTTP");}
    catch (e) {
      try {ajaxObject = new ActiveXObject("Microsoft.XMLHTTP");}
      catch (e) {}
    }
  }

  return ajaxObject; 
}


