//@set @debug=true

//=================================================================================================
// url: the page you are requesting.
// context: a context object passed to your callback.
// callback: your method that will be called and passed context and an ajaxReturn object.
//
// Example:
//    var context = {somvar:true, index:3};
//    var a = new ajaxRequest("sompage.aspx", context, someCallback);
//    a.addParameter("someParm", "1");
//    a.addParameter("anotherParm", "1");
//    a.execute();
//
//    // Callback, ar is an ajaxReturn object.
//    function someCallback(context, ar)
//    {
//       if(ar.returnVal) { do something with it }
//       ar.evaluateJs();
//    }
//
var ajaxAsyncCallbacks = new Array();
function ajaxRequest(url, context, callback)
{
   this.url = url;
   this.context = context;
   this.callback = callback;
   this.isPost = true;
   this.parameterNames = new Array();
   this.parameterVals = new Array();
   
   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
   this.addParameter = function (name, value)
   {
      this.parameterNames.push(name);
      this.parameterVals.push(value);
   };

   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   // Changes from POST to GET. 
   this.setGet = function ()
   {
      this.isPost = false;
   };
   
   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
   this.registerCallback = function () 
   {
      var i;
      for(i = 0; i < ajaxAsyncCallbacks.length; i++) {
         if(! ajaxAsyncCallbacks[i])
            break;
      }
      ajaxAsyncCallbacks[i] = this;
      
      return (i);
   };

   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
   this.getArgs = function ()
   {
      var i;
      var a = '';
      
      for(i = 0; i < this.parameterNames.length; i++) {
         if(i > 0)
            a += '&';
         a += this.parameterNames[i] + '=' + this.parameterVals[i];
      }
      
      return (a);
   };
   
   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
   this.execute = function () 
   {
      var a = this.getArgs();
      
      this.registerCallback();

      if(this.callback) {
         this.xmlHttpRequest.onreadystatechange = this.responseComplete;
      }
      if(this.isPost) {
         this.xmlHttpRequest.open("POST", url, true);
         this.xmlHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
         this.xmlHttpRequest.setRequestHeader("Content-length", a.length);
         this.xmlHttpRequest.setRequestHeader("Connection", "close");
         this.xmlHttpRequest.send(a);
      }
      else {
         if(url.indexOf('?') == -1) {
            this.xmlHttpRequest.open("GET", url + '?' + a, true);
         }
         else {
            this.xmlHttpRequest.open("GET", url + '&' + a, true);
         }
         this.xmlHttpRequest.send("");
      }
   };
   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
   this.responseComplete = function ()
   {
      var i;
      var cb;
      
      for(i = 0; i < ajaxAsyncCallbacks.length; i++) {
         cb = ajaxAsyncCallbacks[i];
         if(cb && cb.xmlHttpRequest && cb.xmlHttpRequest.readyState == 4) {
            cb.processCallback();
            ajaxAsyncCallbacks[i] = null;
         }
      }
   };
   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
   this.processCallback = function ()
   {
      var ar = new ajaxReturn();
      ar.setReturn(this.getChunk(this.xmlHttpRequest.responseText, "_pmrStart", "_pmrEnd"));
      ar.setHtml(this.getChunk(this.xmlHttpRequest.responseText, "_pmhStart", "_pmhEnd"));
      ar.setJs(this.getChunk(this.xmlHttpRequest.responseText, "_pmjStart", "_pmjEnd"));
      ar.setResponse(this.xmlHttpRequest.responseText);
      
      this.callback(this.context, ar);
   };
   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
   this.getChunk = function (responseText, strStart, strEnd)
   {
      var si = responseText.indexOf(strStart);
      var se = responseText.indexOf(strEnd);
      var s;
      
      if(si > -1 && se > -1) {
         s = responseText.substring(si + strStart.length, se);
         if(s.length == 0)
            return (null);
         else
            return (s);
      }
      
      return (null);
   };


   // Force random refresh.
   var d = new Date();
   this.addParameter('__r', d.getTime());

   try {
      this.xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
   } 
   catch(ex) {
      try {
         this.xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } 
      catch(ex) {
         this.xmlHttpRequest = false;
      }
   }
   if(! this.xmlHttpRequest && typeof XMLHttpRequest != 'undefined') {
      this.xmlHttpRequest = new XMLHttpRequest();
   }

}

//=================================================================================================
// Object returned by your callback to ajaxRequest.
//   this.returnValue is an optional string returned by the requested page.
//   this.html is optional html returned by the requested page.
//   this.js is optional javascript returned by the requested page.
//
function ajaxReturn()
{
   this.returnValue = null;
   this.html = null;
   this.js = null;
   this.response = null;
   
   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
   this.setReturn = function (v)
   {
      this.returnValue = v;
   };
   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
   this.setHtml = function (v)
   {
      this.html = v;
   };
   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
   this.setJs = function (v)
   {
      this.js = v;
   };
   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
   this.setResponse = function (v)
   {
      this.response = v;
   };
   
   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
   this.evaluateJs = function ()
   {
      try {
         if(this.js && this.js.length > 0) {
            eval(this.js);
         }
      }
      catch(ex) {
      }
   }
}
