/**
 * File version : 1.0
 * Date : 16/05/07
 * Modifications :
 */

Ajax = function (json){
  this.xhr = {xhrObject : null , params : json}; 
  this.error = false;
 
  this.cree = function(){

    if(window.XMLHttpRequest) {
      try {
        this.xhr.xhrObject = new XMLHttpRequest();
      } 
      catch(e) {
        this.xhr.xhrObject = false;
        this.error = true;
      }
    
    } else if(window.ActiveXObject) {
      try {
        this.xhr.xhrObject = new ActiveXObject("Msxml2.XMLHTTP");
      } 
      catch(e) {
        try {
          this.xhr.xhrObject = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e) {
          this.xhr.xhrObject = false;
          this.error = true;
        }
      }
    }
    
  }
  this.sendData = function(url, param, method, callback){
    this.send(this.xhr,url, param, method, callback);
  }
  
  this.send = function(xhr, url, param, method, callback){
 
    method = method.toUpperCase();
    //httpRequestObject = this.xmlHttpObject.xhr;
    if(xhr.xhrObject){
    //si on envoie par la m?ode GET:
    if(method == "GET"){
      if(param){
      if(param.indexOf("?")>=0){
	     url = url+"&"+param
	  }
	  else{
         url = url+"?"+param
    }
    }
      //Ouverture du fichier s?ctionn?
      try{  
	     xhr.xhrObject.open("GET", url); }
        catch(e){
        this.error = true;
      	return false;
      }
    }
    else if(method == "POST")
      {
	try{ 
	  //Ouverture du fichier en methode POST
	  xhr.xhrObject.open("POST", url, true);
	}
	catch(e){
	this.error = true;
          return false;
        }
      } 
    
    //Ok pour la page cible
    xhr.xhrObject.onreadystatechange = function()
      { 
     
    
	if (xhr.xhrObject.readyState == 4  && !(xhr.xhrObject.status >= 200  && xhr.xhrObject.status < 400)){
    callback.onfailure(xhr);
	}  
	if (xhr.xhrObject.readyState == 4  && (xhr.xhrObject.status >= 200  && xhr.xhrObject.status < 400)){
	  callback.onsuccess(xhr);
	}
      }    
    
    if(method == "GET")
      {
      xhr.xhrObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=utf-8;');
	try{
	  xhr.xhrObject.send(null);
	}
	catch(e){
	this.error = true;
          return false;
	}
      }
    else if(method == "POST")
      {
	xhr.xhrObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=utf-8;');
	try{
	  xhr.xhrObject.send(param);
	}
	catch(e){
	this.error = true;
          return false;
        }
      }
  }
  else{
   this.error = true;
    return false;
  }
  this.error = false;
  return true;
  
  }
   this.cree();
  
}
