// Ricardo Garcia Sanchez 2007
//...........................................................
// OBJET
//...........................................................
/*
* OBJET NWoAjax
* Constructeur de la requette XMLHTTPRequest
*/
function NWoAjax() {
	//...
	this.aXHRObject = null;
	this.aAjaxIsCreate= false;
	this.aMethod = "GET";
	this.aAsync = false;
	//...
	 this.aXHRObject = this.getHTTPObject();
	 this.aAjaxIsCreate= (this.aXHRObject != null);
	//...
}
/*
* METHODE getHTTPObject
* Creation de l'objet
*/
NWoAjax.prototype.getHTTPObject = function(){
var xhr = null;

   if(window.XMLHttpRequest)xhr = new XMLHttpRequest();

   else if(window.ActiveXObject){

      try{

         xhr = new ActiveXObject("Msxml2.XMLHTTP");

      }catch (e){

         xhr = new ActiveXObject("Microsoft.XMLHTTP");

      }

   }else{

      alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");

      xhr = false;

   }

   return xhr;
}
//...............................................................
NWoAjax.prototype.getHTTPObject2 = function()
{
	
  var xmlhttp = false;

  /* Compilation conditionnelle d'IE */
  /*@cc_on
  @if (@_jscript_version >= 5)
     try
     {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     }
     catch (e)
     {
        try
        {
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
           xmlhttp = false;
        }
     }
  @else
     xmlhttp = false;
  @end @*/

  /* on essaie de créer l'objet si ce n'est pas déjà fait */
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
  {
     try
     {
        xmlhttp = new XMLHttpRequest();
     }
     catch (e)
     {
        xmlhttp = false;
     }
  }
	if (xmlhttp){
		 // Évite un bug du navigateur Safari :
	   if (xmlhttp.overrideMimeType) {
		 xmlhttp.overrideMimeType("text/xml");
	   }
	}
  return xmlhttp;
}
/*
* METHODE CreateRequest
* Creation de l'objet requette
*/
NWoAjax.prototype.CreateRequest = function(p_strURL, p_functResponse, p_strDatas) {
	//...
	if(this.aXHRObject != null){
		//...
		switch(this.aMethod){
			case "GET":
				this.aXHRObject.open("GET", p_strURL, this.aAsync); 
				this.aXHRObject.send(null); 
			break;
			case "POST":
				this.aXHRObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
				this.aXHRObject.send(p_strDatas);
			break;
			default:
			break;
		}
		//...
		this.aXHRObject.onreadystatechange = function() {
			/*
			0 (uninitialized)	non initialisé
			1 (loading)	début du transfert des données
			2 (loaded)	données transférées
			3 (interactive)	les données reçues sont accssibles en partie
			4 (complete)	les données sont complètement accessibles
			*/
			if(this.aXHRObject.readyState == 4) {
				p_functResponse(this.aXHRObject.responseText);
			}
		}
	} 
	
}
/*
* METHODE FileToContainer
* Chargement d'un fichier dans un container
*/
NWoAjax.prototype.FileToContainer = function(p_strURL, p_strContainer) {
	//...
	if(this.aXHRObject != null){
		//...
		var v_oDisplay = document.getElementById(p_strContainer);
		if(v_oDisplay){
			//...
			v_oDisplay.innerHTML = "Chargement";
			//...
			this.aXHRObject.open("GET", p_strURL, this.aAsync); 
			this.aXHRObject.send(null); 
			//...
			if(this.aXHRObject.readyState == 4) {
			
				v_oDisplay.innerHTML = this.aXHRObject.responseText;
			
			} 
			//... 
			
		}
	} 
	
}
/*
* METHODE PostToContainer
* Chargement d'un fichier dans un container
*/
NWoAjax.prototype.PostToContainer = function(p_strURL, p_strDatas, p_strContainer) {
	//...
	if(this.aXHRObject != null){
		//...
		var v_oDisplay = document.getElementById(p_strContainer);
		if(v_oDisplay){
			//...
			v_oDisplay.innerHTML = "Chargement";
			//...
			this.aXHRObject.open("POST", p_strURL, this.aAsync); 
			this.aXHRObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			this.aXHRObject.send(p_strDatas); 
			//...
			if(this.aXHRObject.readyState == 4) {
				if (this.aXHRObject.status == 200) {
				  //...
					v_oDisplay.innerHTML = this.aXHRObject.responseText;
				}
			} 
			//... 
			
		}
	} 
	
}

/*
* METHODE SendDatas
* Envoi de donnees dans une BD
*/
NWoAjax.prototype.SendDatas = function(p_strURL, p_strDatas, p_oFunction) {
	
	if (!this.aXHRObject){
        return false;
    }
	//...
	if(this.aXHRObject == null){
		 return false;
	}else{
		v_oXHRObject = this.aXHRObject;
		//... on définit ce qui doit se passer quand la page répondra
		 v_oXHRObject.onreadystatechange=function() {
			 
			if (v_oXHRObject.readyState == 4) {
			  /* 4 : état "complete" */
			  if (v_oXHRObject.status == 200) {
				  //...
				p_oFunction(v_oXHRObject);
				//...
			  }
			}
		 }
		
		switch(this.aMethod){
			case "POST":
				v_oXHRObject.open("POST", p_strURL, this.aAsync);
				v_oXHRObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				v_oXHRObject.send(p_strDatas); 
				break;
			default:
				v_oXHRObject.open("GET", p_strURL+"?"+p_strDatas, this.aAsync);
				v_oXHRObject.send(null); 
				break;
		}
		
		 return true;
	 
	} 
	
}
/*
* METHODE GetMethodRequest
* Lecture de la methode par defaut de la requette
*/
NWoAjax.prototype.GetMethodRequest = function() {
	//...
	return this.aMethod;
	
}
/*
* METHODE SetMethodRequest
* Fixe la methode par defaut de la requette
*/
NWoAjax.prototype.SetMethodRequest = function(p_strMethod) {
	//...
	this.aMethod = p_strMethod;
	
}
/*
* METHODE GetAsyncRequest
* Lecture du mode Asynchrone par defaut de la requette
*/
NWoAjax.prototype.GetAsyncRequest = function() {
	//...
	return this.aAsync;
	
}
/*
* METHODE SetAsyncRequest
* Fixe le mode Asynchrone par defaut de la requette
*/
NWoAjax.prototype.SetAsyncRequest = function(p_bAsync) {
	//...
	this.aAsync = p_bAsync;
	
}