function Ajax() {}

Ajax.makeRequestSynchroneText = function(url, get, post) {
   var http_request = null;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
         http_request.overrideMimeType('text/xml');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return null;
   }

   get += "&json=1";

   if (post==null) {
       http_request.open('GET', url + '?' + encodeURI(get), false);
       http_request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
       http_request.send(null);
   } else {
       http_request.open('POST', url + '?' + encodeURI(get), false);
       //http_request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
       http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       http_request.send(post);
   }

   return http_request.responseText;
}

Ajax.makeRequestSynchroneJson = function(url, parameters, post) {
    var res = Ajax.makeRequestSynchroneText(url, parameters, post);
    return Ajax.jsonDecode(res);
}

Ajax.makeRequestAsynchroneJson = function(url, get, post, callback) {
    var res = Ajax.makeRequestAsynchroneText(url, parameters);
    return Ajax.jsonDecode(res);
}

Ajax.makeRequestServer = function(url, parameters) {
    var res = Ajax.makeRequestSynchroneText(url, parameters);
    return Ajax.jsonDecodeFromServer(res);
}

Ajax.makeRequestAppli = function(url, get, post) {
    var res = Ajax.makeRequestSynchroneText(url, get, post);
    return Ajax.jsonDecodeForAppli(res);
}

Ajax.jsonDecode = function(json) {
    //alert(json);
    try {
	eval('var a = ' + json);
	
	if (a['reponse']==null) {
	    alert("Le serveur n'a pas envoyé son code de réponse");
	    return null; 
	}
	
	if (a['reponse']!=1) {
	    if (a['error']==null) {
		alert("Le serveur a rencontré une erreur, mais n'a pas fourni le message correspondant");
		return null;
	    }
	    alert(a['error']);
	    return null;
	}
	
	if (a['infos']!=null) alert(a['infos']);

	return a;
	
    } catch(ex) {
	alert("La réponse du serveur au format JSON n'a pas pu être évaluée");
	return null;
    }
    
    return null;
}

Ajax.jsonDecodeFromServer = function(json) {
    try {
	eval('var a = ' + json);
	
	if (a['reponse']==null || a['reponse']['code']==null) {
	    alert("Le serveur n'a pas envoyé son code de réponse");
	    return null; 
	}
	
	if (a['reponse']['code']!=1) {
	    if (a['reponse']['messg']==null) {
		alert("La commande a échouée, mais le serveur n'en a pas précisé la raison");
		return null;
	    }
	    alert(a['reponse']['messg']);
	    return null;
	}
	
	return a;
	
    } catch(ex) {
	alert("La réponse du serveur au format JSON n'a pas pu être évaluée");
	return null;
    }
    
    return null;
}

Ajax.jsonDecodeForAppli = function(json) {
    try {
	eval('var a = ' + json);
	return a;
	
    } catch(ex) {
	alert("La réponse du serveur au format JSON n'a pas pu être évaluée");
	return null;
    }
    
    return null;
}

Ajax.makeRequestAsynchroneText = function(url, get, post, callback) {
   var http_request = null;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
         http_request.overrideMimeType('text/xml');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return null;
   }

   http_request.onreadystatechange = function() {
       Ajax.onreadystatechange(http_request, callback);
   }

   get += "&json=1";

   if (post==null) {
       http_request.open('GET', url + '?' + encodeURI(get), true);
       http_request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
       http_request.send(null);

   } else {
       http_request.open('POST', url + '?' + encodeURI(get), true);
       //http_request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
       http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       http_request.send(post);
   }

   return http_request.responseText;
}

Ajax.onreadystatechange = function(http_request, callback) { 
    if (http_request.readyState==4) {
	var res;
	if (http_request.status!=200) res = {'reponse':0};
	else eval('res = ' + http_request.responseText);

	callback(res);
    }
}
 
