/*--
Description: 


Steps to use:
1. 
2. 
3. 
4. 

--*/

// adapted from web-fx XmlExtras by TRT, 2005-06-08
var $w = new function(){
   var p = null;
   var ps = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
   this.getTransport = function(){
      var http = null;
      if (window.XMLHttpRequest) {
         http = new XMLHttpRequest();
         return http;
      }
      if (p) {
         http = new ActiveXObject(p);
         return http;
      } else {
         for (var i = 0; i < ps.length; i++){
            try {
               http = new ActiveXObject(ps[i]);
               p = ps[i];
               return http;
            } catch(e){ }
         }
      }
      throw new Error("Your browser does not support XmlHttp.");
   }
}();
$w.States={UnInitialized:0,Loading:1,Loaded:2,Interactive:3,Completed:4};

//Wrapper for w.js
function ClassHttpRequest(){
   try{
      this.http = $w.getTransport();
   }catch(e){
     //alert(e);
      this.http = false;
   }
}

function ClassRPC() {
   ClassHttpRequest.call(this);
   if (!this.http) {
      //troubleshooting for now
      //alert("no httprequest obj available");
   }

  this.rpc = function(url, cb, async){
     var _cbdone = false;
     //this keyword used in function below refers to the new function itself
     var _http = this.http;
     
     _http.open("GET", url, async);
     
     //reuse in IE - http://keelypavan.blogspot.com/2006/03/reusing-xmlhttprequest-object-in-ie.html
     //onreadystatechange processed affter open
     this.http.onreadystatechange = function(){
       if (_http.readyState < 4) return;
       if (_cbdone) return;
       _cbdone = true;
       if (cb) cb(_http);
     }
     _http.send(null);
     if (!async && cb && !_cbdone) cb(_http);
     return true;
  }

  this.rpcPost = function(url, cb, async, paramName, paramValue){
     var _cbdone = false;
     //this keyword used in function below refers to the new function itself
     var _http = this.http;
     
     _http.open("POST", url, async);
     
     //reuse in IE - http://keelypavan.blogspot.com/2006/03/reusing-xmlhttprequest-object-in-ie.html
     //onreadystatechange processed affter open
     this.http.onreadystatechange = function(){
       if (_http.readyState < 4) return;
       if (_cbdone) return;
       _cbdone = true;
       if (cb) cb(_http);
     }
     
     _http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     _http.send(paramName+'='+paramValue);
     if (!async && cb && !_cbdone) cb(_http);
     return true;
  }
}

