function createREQ() {
try {
     req = new XMLHttpRequest(); /* e.g. Firefox */
     } catch(err1) {
       try {
       req = new ActiveXObject("Msxml2.XMLHTTP");
       /* some versions IE */
       } catch (err2) {
         try {
         req = new ActiveXObject("Microsoft.XMLHTTP");
         /* some versions IE */
         } catch (err3) {
       req = false;
      }
    }
  }
  return req;
}


function requestGET(url, query, req) {
myRand=parseInt(Math.random()*99999999);
if(query == 'noquery') {
req.open("GET",url+'?rand='+myRand,true);
}
else if(query != 'noquery') {
req.open("GET",url+'?'+query+'&rand='+myRand,true);/* open() prepares communication between xmlhttprequest object and the server, specifies HTTP method, the target, and whether the request is asynchronous or not */
}
req.send(null);
}

function requestPOST(url, query, req) {
req.open("POST", url,true);/* open() prepares communication between xmlhttprequest object and the server, specifies HTTP method, the target, and whether the request is asynchronous or not */
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.send(query);
}

function doCallback(callback,item) {
eval(callback + '(item)'); /* eval executes the 'callback' function which is passed to it as an argument, while also passing to that function an argument of its own, via 'item'. */
}


function doAjax(url,query,cbackobj,loadout,callback,reqtype,responsetype) { /* doAjax encapsulates the execution of the xmlhttprequest variable creation function, server request function, and the callback function */
var myreq = createREQ();/* create the XMLHTTPRequest object instance, storing it in the myreq variable */

myreq.onreadystatechange = function() { /* onreadystatechange property stores the function that will process the response from a server */
if(myreq.readyState == 4) { /* if the HTTP request integer status reads as 'completed' */
   if(myreq.status == 200) { /* if the HTTP request status code reads as 'successful' */ 
       /* stores the text data returned from the server into the item variable */
      if(responsetype=="textresp") {
	 var item = myreq.responseText;
	 doCallback(callback, item);
      }
      else if(responsetype=="textxml") {
	 var item = myreq.responseXML; /* stores the xml data returned from the server into the 'item' variable */
         doCallback(callback, item);
      }
      else if(responsetype=="noresponsetext") {
	 eval(callback + '()');
      }
      
    }
  }
  else { 
  while (document.getElementById(cbackobj).hasChildNodes()) {
	document.getElementById(cbackobj).removeChild(document.getElementById(cbackobj).firstChild);
	}
	document.getElementById(cbackobj).innerHTML= '<img src="'+loadout+'" />';
  }
}
if(reqtype=='post') {
requestPOST(url,query,myreq);
} else {
requestGET(url,query,myreq);
}
}

