// Universal xmlHTTPrequest object 1.1 Simple
// Copyright (C) 2010 ConsidoNet Solutions
function XHR() {
  var xmlHttp=null;
  
  function getXHO() {
    var xh;
    try { xh=new XMLHttpRequest(); }
    catch (e) {
      try { xh=new ActiveXObject("Msxml2.XMLHTTP"); }
      catch (e) { xh=new ActiveXObject("Microsoft.XMLHTTP"); }
    }
    if (!xh) alert('Brak obsługi AJAX. Zaktualizuj swoją przeglądarkę.');
    return xh;
  }
  
  this.reponseText = '';
  this.responseXML = null;
  this.statusText = '';
  this.status = 0;
  this.readyState = 0;
  
  xmlHttp = getXHO();
  
  // Original xmlHTTPrequest methods
  this.open = function(method, url, async, username, password) { 
    if(xmlHttp && typeof(xmlHttp.abort)!='unknown') xmlHttp.abort();
    delete xmlHttp;
    xmlHttp = getXHO();
    this.responseText = null;
    this.responseXML = null;
    this.readyState = 1;
    // Initializing properties
    var that = this;
    xmlHttp.onreadystatechange = function() {
      that.readyState = xmlHttp.readyState;
      if(xmlHttp.readyState==4) {
        if(xmlHttp.status) that.status = xmlHttp.status;
        if(xmlHttp.statusText) that.statusText = xmlHttp.statusText;
        if(xmlHttp.responseText) that.responseText = xmlHttp.responseText;
        if(xmlHttp.responseXML) that.responseXML = xmlHttp.responseXML;
      }
      // Executing onreadystatechange
      if(that.onreadystatechange) that.onreadystatechange();
    }
    if(this.onerror) xmlHttp.onerror = this.onerror;
    if(this.onload) xmlHttp.onload = this.onload;
    if(this.onprogress) xmlHttp.onprogress = this.onprogress;
    if(this.ontimeout) xmlHttp.ontimeout = this.ontimeout;
    
    return xmlHttp.open(method, url, async, username, password); 
  }
  this.send = function(data) { this.readyState = 2; return xmlHttp.send(data); }
  this.setRequestHeader = function(header, value) { return xmlHttp.setRequestHeader(header, value); }
  this.getAllResponseHeaders = function() { return xmlHttp.getAllResponseHeaders(); }
  this.getResponseHeader = function(header) { return xmlHttp.getResponseHeader(header); }
  this.abort = function() { return xmlHttp.abort(); }
  this.overrideMimeType = function(mimetype) { return xmlHttp.overrideMimeType(mimetype); }
  
  // New methods
  this.aLoad = function(method, url, data, onready, onerror) {
    this.onreadystatechange = function() {
      if(this.readyState==4 && this.status==200 && onready) { onready(); delete xmlHttp; }
      if(this.readyState==4 && this.status!=200 && onerror) { onerror(); delete xmlHttp; }
    }
    this.open(method, url, true);
    if(method=='post') {
      this.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
      this.setRequestHeader('Content-length', data.length);
      this.setRequestHeader('Connection', 'close');
    }
    this.send(data);
  }
  
  this.sLoad = function(method, url, data) {
    this.open(method, url, false);
    if(method=='post') {
      this.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
      this.setRequestHeader('Content-length', data.length);
      this.setRequestHeader('Connection', 'close');
    }
    this.send(data);
    this.readyState = xmlHttp.readyState;
    if(xmlHttp.readyState==4) {
      if(xmlHttp.status) this.status = xmlHttp.status;
      if(xmlHttp.statusText) this.statusText = xmlHttp.statusText;
      if(xmlHttp.responseText) this.responseText = xmlHttp.responseText;
      if(xmlHttp.responseXML) this.responseXML = xmlHttp.responseXML;
    }
  }
}
