/*
 * Library for XmlHttpRequest
 *
 * Joris Dormans (2006)
 *
 */

/*
 * Safe function to create XMLHttpRequest objects
 *
 */

function createXMLHttpRequest() {
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
  try { return new XMLHttpRequest(); } catch (e) {}
  alert("XMLHttpRequest not supported");
  return null;
}

var activeCall=null;

function XHReqCaller(objectName) {
// create a general chReqPost and a function to handle its response
  this.xhReq = new createXMLHttpRequest();
  this.responseFunction = "";
  this.timer;
  this.timeout=30000;
  this.objectName=objectName;
}

XHReqCaller.prototype.xhReqTimeout = function () {
  alert("Server did not respond within "+Math.floor(this.timeout/1000)+" seconds and maybe busy, try again later. (Url = "+this.url+" Ready state = "+this.xhReq.readyState+")");
  this.xhReq.onreadystatechange = this.xhReqNoResponse;
}

XHReqCaller.prototype.get = function (url, responseFunction) {
  if (activeCall) {
    alert("Request already sent, please try again in a few seconds");
    return;
  }
  activeCall=this;
  alert(activeCall);
  this.url=url;
  this.responseFunction = responseFunction;
  this.xhReq.open("GET", url, true);
  if (this.xhReq.readyState !=1) alert ("Server busy: ready state = "+this.xhReq.readyState);
  this.timer=setTimeout(this.objectName+".xhReqTimeout()", this.timeout);
  this.xhReq.onreadystatechange = this.xhReqResponse;
  this.xhReq.send(null);
}

XHReqCaller.prototype.sGet = function (url) {
  if (activeCall) {
    alert("Request already sent, please try again in a few seconds");
    return;
  }
  activeCall=this;
  this.url=url;
  this.xhReq.open("GET", url, false);
  if (this.xhReq.readyState !=1) alert ("Server busy: ready state = "+this.xhReq.readyState);
  this.timer=setTimeout(this.objectName+".xhReqTimeout()", this.timeout);
  this.xhReq.onreadystatechange = this.xhReqNoResponse;
  this.xhReq.send(null);
  clearTimeout(this.timer);
  activeCall=null;
  return(this.xhReq.responseText);
}

XHReqCaller.prototype.post = function (url, responseFunction, post) {
  if (activeCall) {
    alert("Request already sent, please try again in a few seconds");
    return;
  }
  activeCall=this;
  this.url=url;
  this.responseFunction = responseFunction;
  this.xhReq.open("POST", url, true);
  if (this.xhReq.readyState !=1) alert ("Server busy: ready state = "+this.xhReq.readyState);
  this.timer=setTimeout(this.objectName+".xhReqTimeout()", this.timeout);
  this.xhReq.onreadystatechange = this.xhReqResponse;
  this.xhReq.send(post);
}

XHReqCaller.prototype.xhReqNoResponse = function () {
  if (activeCall.xhReq.readyState !=4) return;
  clearTimeout(activeCall.timer);
  activeCall=null;
}

XHReqCaller.prototype.xhReqResponse = function () {
  if (activeCall.xhReq.readyState !=4) return;
  clearTimeout(activeCall.timer);
  if (activeCall.xhReq.status == 200) {
//    alert(activeCall.xhReq.responseText);
    if (activeCall.xhReq.responseText.indexOf("Error")>=0 || activeCall.xhReq.responseText.indexOf("error")>=0) alert(activeCall.xhReq.responseText);
    var r=activeCall.responseFunction;
    activeCall=null;
    eval(r);
  } else {
    alert(activeCall.xhReq.responseText);
  }
}



