
var beAjax =
{
   _msxml_progid :
       [
       'MSXML2.XMLHTTP.3.0',
       'MSXML2.XMLHTTP',
       'Microsoft.XMLHTTP'
       ],

   getConnection : function ()
   { 
      try
       {
        // Instantiates XMLHttpRequest in non-IE browsers.
        http = new XMLHttpRequest();
       }
      catch(e)
       {
        for(var i=0; i < this._msxml_progid.length; ++i)
          try
           {
             // Instantiates XMLHttpRequest for IE.
             http = new ActiveXObject(this._msxml_progid[i]);
             break;
           }
          catch(e){}
       }
      finally { return http; }
   },

   
   request : function(method, url, callback, postData)
   {
      if(url && url.indexOf("http://") == 0)
       {
        if(method && method == "POST" || postData)
         {
          this.sendForm(null, callback, postData, url, method);
         }
        else if(method == "GET")
         {
          if(container = this.createRequestContainer(callback))
            container.contentWindow.location.replace(url);
         }
       }
      else
        this.standardRequest(method, url, callback, postData);

   },
   
   standardRequest : function(method, url, callback, postData)
   {
      var conn = this.getConnection();
      if(!conn) return null;
      
      conn.open(method, url, true);
      conn.onreadystatechange = function()
      {
         if(conn.readyState != 4) return;
         
         if(conn.status >= 200 && conn.status < 300 && callback)
          {
           if(callback.onSuccess)
            {
             try { var vars = eval('(' + conn.responseText + ')'); }
             catch(e) { vars = null; }
             callback.onSuccess(conn, vars);
            }
          }
         else
           if(callback.onFailure) callback.onFailure(conn);
      }
      if(postData)
        conn.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      conn.send(postData || null);
   },

   
   
   sendForm : function(form, callback, postData, url, method)
   {
      if(typeof form == 'string')
        oForm = (document.getElementById(form) || document.forms[form]);
      else if(typeof form == 'object')
        oForm = form;
      else
        oForm = null;

      container = this.createRequestContainer(callback);
      if(!container) return null;

      if(oForm)  oForm.target = container.id;

      if(!oForm) 
        try 
         {
          doc = container.contentWindow.document;
          oForm = doc.createElement('form');
          doc.body.appendChild(oForm);
         }
        catch(e) { return null; }

      if(method) oForm.method = method;
      if(url)    oForm.action = url;
      
      if(postData) this.appendPostData(oForm, postData);
      setTimeout(function(){ oForm.submit(); }, 1)
      return container;
   },

   createRequestContainer : function(callback)
   {
      var io = this.createFrame();
      if(!io) return null;
      
      var requestCallback = function()
      {
        resp = {};
        try
        {
         resp.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
         resp.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
        }
        catch(e){}

        if(callback && callback.onSuccess)
         {
          try { var vars = eval('(' + resp.responseText + ')'); }
          catch(e) { vars = null; }
          callback.onSuccess(resp, vars);
         }
        setTimeout(function(){ beAjax.stopFormSending(io); }, 100);
      };

      // Bind the onload handler to the iframe to detect the file upload response.
      setTimeout( function()
      { 
       if(window.attachEvent)
         io.attachEvent('onload', requestCallback);
       else
         io.addEventListener('load', requestCallback, false);
      }, 1);
      return io;
   },
   
   stopFormSending : function(container)
   {
      if(container)
        try
         {
          try { container.contentWindow.document.execCommand('Stop'); }
          catch(e) { try { container.stop(); } catch(e) {} }
          document.body.removeChild(container);
          delete container;
         }
        catch(e) {}
   },
   
   createFrame : function()
   {
      // IE does not allow the setting of id and name attributes as object
      // properties via createElement().  A different iframe creation
      // pattern is required for IE.
      var frameID = 'frmIO' + (new Date()).getMilliseconds();
      if(window.ActiveXObject)
       {
        var io = document.createElement('<iframe id="' + frameID + '" name="' + frameID + '" />');

        // IE will throw a security exception in an SSL environment if the
        // iframe source is undefined.
        io.src = 'javascript:false';
       }
      else
       {
        var io = document.createElement('iframe');
        io.id = frameID;
        io.name = frameID;
       }

      io.style.position = 'absolute';
      io.style.top = '-1000px';
      io.style.left = '-1000px';

      document.body.appendChild(io);
      return io;
   },

   appendPostData : function(form, postData)
   {
      var postMessage;
      if(typeof postData == "string")
       {
        postFields = postData.split('&');
        for(var i=0; i < postFields.length; i++)
         {
          var delimitPos = postFields[i].indexOf('=');
          if(delimitPos != -1)
           {
            formElement = document.createElement('input');
            formElement.type = 'hidden';
            formElement.name = postFields[i].substring(0,delimitPos);
            formElement.value = postFields[i].substring(delimitPos+1);
            form.appendChild(formElement);
           }
         }
       }
      else if(typeof postData == "object")
       {
        postFields = postData;
        for(var i in postFields)
         {
          formElement = document.createElement('input');
          formElement.type = 'hidden';
          formElement.name = i;
          formElement.value = postFields[i];
          form.appendChild(formElement);
         }
       }
   },

    formToString : function(oForm)
    {
        var oElement, oName, oValue, oDisabled;
        var hasSubmit = false;
        var _sFormData = '';
        // Iterate over the form elements collection to construct the
        // label-value pairs.
        for (var i=0; i<oForm.elements.length; i++)
         {
            oElement = oForm.elements[i];
            oDisabled = oForm.elements[i].disabled;
            oName = oForm.elements[i].name;
            oValue = oForm.elements[i].value;

            // Do not submit fields that are disabled or
            // do not have a name attribute value.
            if(!oDisabled && oName)
            {
                switch (oElement.type)
                {
                    case 'select-one':
                    case 'select-multiple':
                        for(var j=0; j<oElement.options.length; j++){
                            if(oElement.options[j].selected){
                                if(window.ActiveXObject){
                                    _sFormData += encodeURIComponentNew(oName) + '=' + encodeURIComponentNew(oElement.options[j].attributes['value'].specified?oElement.options[j].value:oElement.options[j].text) + '&';
                                }
                                else{
                                    _sFormData += encodeURIComponentNew(oName) + '=' + encodeURIComponentNew(oElement.options[j].hasAttribute('value')?oElement.options[j].value:oElement.options[j].text) + '&';
                                }

                            }
                        }
                        break;
                    case 'radio':
                    case 'checkbox':
                        if(oElement.checked){
                            _sFormData += encodeURIComponentNew(oName) + '=' + encodeURIComponentNew(oValue) + '&';
                        }
                        break;
                    case 'file':
                        // stub case as XMLHttpRequest will only send the file path as a string.
                    case undefined:
                        // stub case for fieldset element which returns undefined.
                    case 'reset':
                        // stub case for input type reset button.
                    case 'button':
                        // stub case for input type button elements.
                        break;
                    case 'submit':
                        if(hasSubmit == false){
                            _sFormData += encodeURIComponentNew(oName) + '=' + encodeURIComponentNew(oValue) + '&';
                            hasSubmit = true;
                        }
                        break;
                    default:
                        _sFormData += encodeURIComponentNew(oName) + '=' + encodeURIComponentNew(oValue) + '&';
                        break;
                }
            }
        }

        _sFormData = _sFormData.substr(0, _sFormData.length - 1);
        return _sFormData;
    }
};

   var hexchars = "0123456789ABCDEF";
   var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
   var ascii = "€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—?™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ";

   function encodeURIComponentNew(s)
   {
    var enc = new Array();
     for(i = 0; i < s.length; i++)
      enc[i] = (okURIchars.indexOf(s.charAt(i)) == -1) ? "%" + toHex((n = s.charCodeAt(i)) < 0x80 ? n : 0x80 + ascii.indexOf(s.charAt(i)) ) : s.charAt(i);
    return enc.join("");
   }

   function toHex(n)
   {
     return hexchars.charAt(n>>4) + hexchars.charAt(n & 0xF);
   }


