function disp_info() {
  var a = document.createElement('DIV');
  a.innerHTML = '<span>xhrlist.length : '+xhrlist.length+'<br></span>';
  $('result').appendChild(a);            
  for (i=0; i < xhrlist.length; i++) {
    var d = document.createElement('DIV');  
    d.innerHTML = ' <span>xhrlist['+i+'].inuse : '+xhrlist[i].inuse+'<br></span>';
    $('result').appendChild(d);
  }
}

function eg_xhr() { 
  this.inuse = false; 
  this.xhr = false; 
  if (window.XMLHttpRequest) { 
    this.xhr = new XMLHttpRequest(); 
  } 
  else if (window.ActiveXObject) { 
    this.xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
  } 
} 

var xhrlist = new Array;
function eg_ajax() {}
XHR = new eg_ajax();

//id starts from 1
eg_ajax.prototype.complete = function(x) {
  return (xhrlist[x-1].xhr.readyState == 4);
}

//id starts from 1
eg_ajax.prototype.abort = function(x) {
  if (xhrlist[x-1] && xhrlist[x-1].inuse) { 
    xhrlist[x-1].xhr.abort();
    xhrlist[x-1].inuse = false;
  }
}

eg_ajax.prototype.bind = function(arg) { 
  var x = -1; 
  for (var i=0; i < xhrlist.length; i++) { 
    if (!xhrlist[i].inuse) { x = i; break; }
  } 
  if (x == -1) { x = xhrlist.length; xhrlist[x] = new eg_xhr(); } 
  if (xhrlist[x].xhr) { 
    xhrlist[x].inuse = true; 
    if (arg.handler) xhrlist[x].userhandler = arg.handler; else xhrlist[x].userhandler = false;
    //parse content (javascript object)
    var data='';
    for (c in arg.content) data = data + '&' + c + '=' + encodeURIComponent(arg.content[c]);

    if (!arg.no_random && (arg.url.indexOf('rnd=') == -1)) {
      arg.url = arg.url + (arg.url.indexOf('?') == -1 ? '?' : '&') + 'rnd='+Math.floor(Math.random()*10000000000); 
    }

    document.body.style.cursor = 'progress';
    //GET
    if (!data && (!arg.method || arg.method == 'GET')) {
      xhrlist[x].xhr.open('GET', arg.url, true);
      xhrlist[x].xhr.onreadystatechange = function() { if (typeof(xhr_state_change) != 'undefined') { xhr_state_change(x); } }
      if (window.XMLHttpRequest) xhrlist[x].xhr.send(null); 
      else if (window.ActiveXObject) xhrlist[x].xhr.send(); 
    } 
    //POST
    else {
      xhrlist[x].xhr.open('POST', arg.url, true);
      xhrlist[x].xhr.onreadystatechange = function() { if (typeof(xhr_state_change) != 'undefined') { xhr_state_change(x); } }    
      xhrlist[x].xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; Charset=utf-8");
      xhrlist[x].xhr.setRequestHeader("Cache-Control", "no-cache");
/*      xhrlist[x].xhr.onprogress = function(pe) {
          if(pe.lengthComputable) {
            alert(pe.loaded + ' av totalt ' + pe.total);
          }
      }
      xhrlist[x].xhr.onloadend = function(pe) {
        alert('färdigt');
      }*/
      xhrlist[x].xhr.send(data.substr(1));
    }    
    return x+1;
  } 
  return false;
}
 
xhr_state_change = function(x) { 
  if (typeof(xhrlist[x]) != 'undefined' && xhrlist[x].inuse && xhrlist[x].xhr.readyState == 4) { 
      if (xhrlist[x].userhandler) {
        if (xhrlist[x].xhr.status == 200 || xhrlist[x].xhr.status == 304) { 
          xhrlist[x].userhandler('ok', xhrlist[x].xhr.responseText, x+1, xhrlist[x].xhr.getAllResponseHeaders());
        } else switch(xhrlist[x].xhr.status) {
              case 404:
                  xhrlist[x].userhandler('error', {message:'Kunde inte hitta filen'}, x+1, null);
                  break;
              default:
                  xhrlist[x].userhandler('error', {message:xhrlist[x].xhr.statusText}, x+1, null);
                  break;
        }
      }
      xhrlist[x].inuse = false;
      
      var all_free = true;
      for (var i=0; i < xhrlist.length; i++) 
          if (xhrlist[i].inuse) { all_free = false; break; }       
      if (all_free) document.body.style.cursor = 'default';
  } 
}
