var xmlDocumentLoaded;
var isIE = false;
var xmlDocVersions = new Array("Msxml2.DOMDocument.5.0", "Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.3.0", "Msxml2.DOMDocument", "Microsoft.DOMDocument");
var XMLcache;

Activsoft.Manager.register("activsoft.ajax");

activsoft.ajax.RequestToSend = function() {
  //Initialisation de la partie XMLHttpRequest
  this.xmlDocVersions = new Array("Msxml2.DOMDocument.5.0", "Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.3.0", "Msxml2.DOMDocument", "Microsoft.DOMDocument");
  
  //Requête
  this.xmlHTTPRequest = null;
  
  //Requête 
  this.request = null;
  
  //nombre d'essai avant de pêter une erreur
  this.nbTries = 1;
  
  //nombre d'essai en cours
  this.nbTriesCount = 0;
  
  //method
  this.method = "POST";  

  //page appellée par défaut
  this.page = "xslt.aspx";
  
  this.afterLoadCallBack = this.defaultAfterLoadCallBack;
  
  this.httpErrorCallBack = this.defaultHttpErrorCallBack;
  
  this.jsErrorCallBack = this.defaultJsErrorCallBack;
  
  this.xmlDocumentLoaded = null;
  
  this.onTry = false;
}

activsoft.ajax.RequestToSend.prototype.send = function() {
  var currentObject = this;
  if(window.XMLHttpRequest) {
    this.xmlHTTPRequest = new XMLHttpRequest();
  }
  else {
    if(window.ActiveXObject) {
      this.xmlHTTPRequest = new ActiveXObject("Microsoft.XMLHTTP");
      isIE = true; 
    }
    else {
      // XMLHttpRequest non supporté par le navigateur   
      alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");   
      return;   
    }
  }
  var connectionURL = this.page;
  if (this.method=="GET") {
    if (this.request) {
	  connectionURL += "?" + this.request;
      if (this.request.length>5000) {
        alert("Erreur : URL trop longue en post");
      }
 	}
  }
  if (this.onTry) {
    this.nbTriesCount++;
  }
  else {
    this.nbTriesCount = 0;
    this.onTry = true;
  }
  this.xmlHTTPRequest.onreadystatechange = function() {
    currentObject.xmlLoaded();
  };
  this.xmlHTTPRequest.open(this.method, connectionURL, true);
  if (this.method=="POST") {
    this.xmlHTTPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    this.xmlHTTPRequest.send(this.request);
  }
  else {
    this.xmlHTTPRequest.send();
  }
}

activsoft.ajax.RequestToSend.prototype.xmlLoaded = function() {
  if (this.xmlHTTPRequest.readyState == 4) {
    try {
      if (this.xmlHTTPRequest.status == 200) {
        this.xmlDocumentLoaded = this.xmlHTTPRequest.responseXML;
        this.afterLoadCallBack();
      }
      else {
      	if (this.nbTriesCount<this.nbTries) {
      	  this.send();
      	}
      	else {
      	  this.httpErrorCallBack();	
      	}
      }
    }
    catch(err) {
      this.jsErrorCallBack(err);
    }
  }
}

activsoft.ajax.RequestToSend.prototype.defaultJsErrorCallBack = function(err) {
  throw(err);
}

activsoft.ajax.RequestToSend.prototype.defaultHttpErrorCallBack = function() {
  alert("Après " + nbTries + " essais, le serveur renvoie une erreur, veuillez contacter votre administrateur technique.");
}

activsoft.ajax.RequestToSend.prototype.defaultAfterLoadCallBack = function() {
  alert(this.xmlHTTPRequest.responseText);
}


//Fin partie AJAX

activsoft.ajax.RequestToSend.prototype.loadFromXml = function(xmlText, index) {
  if (window.ActiveXObject && index < this.xmlDocVersions.length){ 
    try{ 
      this.xmlDocumentLoaded = new ActiveXObject("Msxml2.DOMDocument"); 
      this.xmlDocumentLoaded.async="false"; 
      this.xmlDocumentLoaded.loadXML(xmlText); 
    }
    catch(e){ 
      this.loadFromXml(xmlText, index+1); 
      return;
    } 
  } 
  if (document.implementation.createDocument){ 
    var parser = new DOMParser(); 
    xmlDocumentLoaded = parser.parseFromString(xmlText, "text/xml");
  } 
}