// Async.js
// Asynchronous communication library
//

///////////////////////////////////////////////////////////////////////////////
//
//  AsyncMgr
//  Manages asynchronous communication between the browser and the server
//  Returns     The async manager.
//  
//  TODO: add more responce handlers (xml, json etc..)
AsyncMgr = {
 
// getRequest
// main function is to obtain an ajax object. On its first invocation will 
// select the best way of creating the ajax object and will use that method in all
// future invocations 
// Arguments: N/A
// Returns:   an ajax object   
    getRequest: function()
    {
      var ajax = null;
      try{
        ajax = this.getHTTPRequest();
      }catch(e){
        try{
          ajax = this.getMSXML2();
        }catch(e){
          try{
            ajax = this.getMicrosoft();
          }catch(e){
            alert("no Ajax:-(");
          }
        }
      }
      return ajax;
    },
    getHTTPRequest : function(){return new XMLHttpRequest();},
    getMSXML2 : function(){return new ActiveXObject('MSXML2.XMLHTTP');},
    getMicrosoft : function(){return new ActiveXObject('Microsoft.XMLHTTP');},
// stringifyData
// Copies the mData member into a string of the form 'prop1=value1&prop2=value2...'
// Arguments: N/A
// Returns:   The transformed string    
  stringifyData: function(_url, _obj)
  { 
    var str = _url;
    var deliminator = "?";
    if (_obj)
    {
      for (var prop in _obj)
      {
        str += deliminator;
        str+=encodeURIComponent(prop)+"="+encodeURIComponent(_obj[prop]);
        deliminator = "&";
      }
    }
    return str;
  },
  
// send
// sends an request to a server
// Arguments: _requestId     the id of an asyncBase derived object
// Returns:   a request id       
    send: function(_url, _obj)
    {
      var ajax = this.getRequest();

      // try to open the ajax object, if fail then notify the error
      try {
        ajax.open("AsyncGET", this.stringifyData(_url, _obj), true);
      }catch(e){
        return;
      }

      try {
        ajax.send(null); 
      }catch(e){
        return;
      }   
    }             
  };
  





