/**
* Fonctions liées au XMLHTTPRequest...
**/

var httpReqPleaseWait=false;
function HttpReq(url, data, method, fctAsynch, isXML, pleaseWait) {
	if(! data || data == "") data = null;
	if(! method || method.toUpperCase() != "GET") method = "POST";
	else if(data!=null) {
		url=url+"?"+data;
		data=null;
	}

	var xhr_object = null;
	if(window.XMLHttpRequest) // Firefox >= 1.0
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer >= 5.5
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else { // XMLHttpRequest non supporté par le navigateur
		alert("<MSX>Votre navigateur ne supporte pas les objets XMLHTTPRequest...</MSX>");
		return "";
	}

	xhr_object.open(method, url, ((typeof fctAsynch)=="function"));
	if((typeof fctAsynch)=="function") {
		if(pleaseWait) httpReqPleaseWait=true;
		//Si c'est une fonction, on est en mode asynchrone --> on déclare la fonction suivante...
		xhr_object.onreadystatechange = function() {
			if(httpReqPleaseWait && xhr_object.readyState==4) {
                                $.unblockUI();
                        }
			fctAsynch(xhr_object);
		}
	}

	if(method=="POST") xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr_object.send(data);

	if(!((typeof fctAsynch)=="function")) {
		if(xhr_object.readyState == 4) { //Sans mode asynchrone, on récupère la réponse quand on a tout recu (readyState=4)
			if(isXML)
				return xhr_object.responseXML;
			else return xhr_object.responseText;
		} else {
			alert("<MSX>Attention ! Un problème a eu lieu lors de la requête XMLHTTPRequest...</MSX>");
			return "";
		}
	} else {
		return "";
	}
}

function httpReqEvalAsynch(url, data, method, pleaseWait, msgWait) {
	if(!method) method="POST";
	if(pleaseWait) {
		if(!msgWait) msgWait="Merci de patienter...";
		$.blockUI( {message: "<img align=\"absmiddle\" src=\"images/wait.gif\" />&nbsp;"+msgWait} );
	}
	HttpReq(url, data, method, httpReqFctEvalAsynch,false,true);
}
function httpReqFctEvalAsynch(o) {
	if(o.readyState == 4) { //Quand on a tout reçu...
		eval(o.responseText);
	}
}
