http_sockets = new Array();

// handle HTTP response
function http_response(request_obj)
{
    var ctype = request_obj.get_header('Content-Type');
    if (ctype){
      ctype = String(ctype).toLowerCase();
      var ctype_array=ctype.split(";");
      ctype = ctype_array[0];
    }

    // if we get javascript code from server -> execute it
    if (request_obj.get_text() && (ctype=='text/javascript' || ctype=='application/x-javascript'))
      eval(request_obj.get_text());

    // process the response data according to the sent action
    switch (request_obj.__action)
    {
        case 'checkmail':
            var rqText = request_obj.get_text();
            if (rqText != "ERR") {
                parts = rqText.split("/");
                if (!isNaN(parts[0]) && !isNaN(parts[1])) {
                    calcPrice(parts[0], parts[1]);
                } 
            }
            break;
            
        case 'askinfo':
            thankYou();
            break;
            
        case 'kxaverify':
            break;
            
        case 'setcurr':
            break;
    }

    request_obj.reset();
};

// handle HTTP request errors
function http_error(request_obj)
{
    request_obj.reset();
    request_obj.__lock = false;
};

// find a non-busy socket or create a new one
function get_request_obj()
{
    for (var n=0; n<this.http_sockets.length; n++)
    {
        if (!http_sockets[n].busy)
            return http_sockets[n];
    }
    
    // create a new XMLHTTP object
    var i = http_sockets.length;
    http_sockets[i] = new rcube_http_request();
    
    return http_sockets[i];
};
    
// send a http request to the server
function httpRequest(action, url, lock)
{
    var request_obj = get_request_obj();

    // send request
    if (request_obj)
    {
        request_obj.__lock = lock ? true : false;
        request_obj.__action = action;
        request_obj.onerror = function(o){ http_error(o); };
        request_obj.oncomplete = function(o){ http_response(o); };
        request_obj.GET((url.substr(0, 4) == 'http') ? url : (g_defaultUrl + url));
    }
};

// send a http POST request to the server
function httpPost(action, url, postdata, lock)
{
    var request_obj = get_request_obj();
    
    // send request
    if (request_obj)
    {
        request_obj.__lock = lock ? true : false;
        request_obj.__action = action;
        request_obj.onerror = function(o){ http_error(o); };
        request_obj.oncomplete = function(o){ http_response(o); };
        request_obj.POST((url.substr(0, 4) == 'http') ? url : (g_defaultUrl + url), postdata);
    }
};

/**
 * Class for sending HTTP requests
 * @constructor
 */
function rcube_http_request()
  {
  this.url = '';
  this.busy = false;
  this.xmlhttp = null;


  // reset object properties
  this.reset = function()
    {
    // set unassigned event handlers
    this.onloading = function(){ };
    this.onloaded = function(){ };
    this.oninteractive = function(){ };
    this.oncomplete = function(){ };
    this.onabort = function(){ };
    this.onerror = function(){ };
    
    this.url = '';
    this.busy = false;
    this.xmlhttp = null;
    }


  // create HTMLHTTP object
  this.build = function()
    {
    if (window.XMLHttpRequest)
      this.xmlhttp = new XMLHttpRequest();
    else if (window.ActiveXObject)
      {
      try { this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
      catch(e) { this.xmlhttp = null; }
      }
    else
      {
      
      }
    }

  // send GET request
  this.GET = function(url)
    {
    this.build();

    if (!this.xmlhttp)
      {
      this.onerror(this);
      return false;
      }

    var _ref = this;
    this.url = url;
    this.busy = true;

    this.xmlhttp.onreadystatechange = function(){ _ref.xmlhttp_onreadystatechange(); };
    this.xmlhttp.open('GET', url);
    this.xmlhttp.setRequestHeader('X-Datrix-Flag', 1);
    this.xmlhttp.send(null);
    };


  this.POST = function(url, body, contentType)
    {
    // default value for contentType if not provided
    if (typeof(contentType) == 'undefined')
      contentType = 'application/x-www-form-urlencoded';

    this.build();
    
    if (!this.xmlhttp)
    {
       this.onerror(this);
       return false;
    }
    
    var req_body = body;
    if (typeof(body) == 'object')
    {
      req_body = '';
      for (var p in body)
        req_body += (req_body ? '&' : '') + p+'='+urlencode(body[p]);
    }

    var ref = this;
    this.url = url;
    this.busy = true;
    
    this.xmlhttp.onreadystatechange = function() { ref.xmlhttp_onreadystatechange(); };
    this.xmlhttp.open('POST', url, true);
    this.xmlhttp.setRequestHeader('Content-Type', contentType);
    this.xmlhttp.setRequestHeader('X-Datrix-Flag', 1);
    this.xmlhttp.send(req_body);
    };


  // handle onreadystatechange event
  this.xmlhttp_onreadystatechange = function()
    {
    if(this.xmlhttp.readyState == 1)
      this.onloading(this);

    else if(this.xmlhttp.readyState == 2)
      this.onloaded(this);

    else if(this.xmlhttp.readyState == 3)
      this.oninteractive(this);

    else if(this.xmlhttp.readyState == 4)
      {
      try {
        if (this.xmlhttp.status == 0)
          this.onabort(this);
        else if(this.xmlhttp.status == 200)
          this.oncomplete(this);
        else
          this.onerror(this);

        this.busy = false;
        }
      catch(err)
        {
        this.onerror(this);
        this.busy = false;
        }
      }
    }

  // getter method for HTTP headers
  this.get_header = function(name)
    {
    return this.xmlhttp.getResponseHeader(name);
    };

  this.get_text = function()
    {
    return this.xmlhttp.responseText;
    };

  this.get_xml = function()
    {
    return this.xmlhttp.responseXML;
    };

  this.reset();
  
}  // end class rcube_http_request

