
var XML_HTTP_REQUEST_POOL = [];

function Send_Request(method, returnType, strURL, strQUERY, callBackFunction, timeoutTime, timeoutFunction) {
   var request = false;
   var isJSON = (returnType.toUpperCase() == "JSON" ? true : false);
   
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      request = new XMLHttpRequest();
      if (request.overrideMimeType) {
         if (isJSON) {
            request.overrideMimeType("text/javascript");
         }
         else {
            request.overrideMimeType("text/xml");
         }
      }
   }
   else if (window.ActiveXObject) { // IE
      try {
         request = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e1) {
         try {
            request = new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch (e2) { request = false; }
      }
   }
   if (!request) {
      window.alert("Cannot create XMLHTTP instance");
      return -1;
   }
   request.open(method.toUpperCase(), strURL, true);
   request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   request.setRequestHeader("Content-length", strQUERY.length);
   request.setRequestHeader("Connection", "close");
   request.onreadystatechange = function() {
      if (callBackFunction && request.readyState == 4 && callBackFunction !== "") {
         if (isJSON) {
            var response = request.responseText;
            eval(callBackFunction + "("+response+")");
         }
         else {
            var response = request.responseXML;
            eval(callBackFunction + "(response)");
         }
      }
   };
   var index = XML_HTTP_REQUEST_POOL.push(request);
   index = index -1;
   if (timeoutTime && timeoutFunction) {
      window.setTimeout("Request_Timeout(" + index + ", \"" + timeoutFunction + "\")", timeoutTime);
   }
   request.send(strQUERY);
   return index;
}

function Request_Timeout(index, callback) {
   if (XML_HTTP_REQUEST_POOL[index]) {
      var req = XML_HTTP_REQUEST_POOL[index];
      switch (req.readyState) {
         case 1:
         case 2:
         case 3:
            req.abort();
            eval(callback + "("+ index +")");
         break;

         // State 0 + 4
         default:
            return false;
      }
   }
}

function XML_Post(strURL, strQUERY, callBackFunction, timeoutTime, timeoutFunction) {
   return Send_Request("POST", "XML", strURL, strQUERY, callBackFunction, timeoutTime, timeoutFunction);
}
function XML_Get(strURL, strQUERY, callBackFunction, timeoutTime, timeoutFunction) {
   return Send_Request("GET", "XML", strURL, strQUERY, callBackFunction, timeoutTime, timeoutFunction);
}
function JSON_Post(strURL, strQUERY, callBackFunction, timeoutTime, timeoutFunction) {
   return Send_Request("POST", "JSON", strURL, strQUERY, callBackFunction, timeoutTime, timeoutFunction);
}
function JSON_Get(strURL, strQUERY, callBackFunction, timeoutTime, timeoutFunction) {
   return Send_Request("GET", "JSON", strURL, strQUERY, callBackFunction, timeoutTime, timeoutFunction);
}
