function creeObjHttpRequest(){		
	var xhr_local=null;	
	if(window.XMLHttpRequest) // Firefox et autres
   		xhr_local = new XMLHttpRequest(); 
	else if(window.ActiveXObject){ // Internet Explorer 
	   try {
			xhr_local = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			xhr_local = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xhr_local;			
}		

function go(c){
	if(!c.data.replace(/\s/g,""))
		c.parentNode.removeChild(c);
}
	
function clean(d){
	var bal=d.getElementsByTagName("*");
	for(i=0;i<bal.length;i++){
		a=bal[i].previousSibling;
		if(a && a.nodeType==3)
			go(a);
		b=bal[i].nextSibling;
		if(b && b.nodeType==3)
			go(b);
	}
	return d;
}



function urlCentreByAjaxFromCGI(url,cgis,htmlAttente){
	var xhr_local=creeObjHttpRequest();

	if (htmlAttente)var objMsg=getDivSurPageWithHtml(htmlAttente);
	
	
	xhr_local.onreadystatechange = function(){
		if(xhr_local.readyState == 4 && xhr_local.status == 200){
			var contenuVisuStruct=xhr_local.responseText;	
			//document.getElementById("page").innerHTML=contenuVisuStruct;
			
			var xhr_head=creeObjHttpRequest();
			xhr_head.onreadystatechange = function(){
				if(xhr_head.readyState == 4 && xhr_head.status == 200){
					var contenuHead=xhr_head.responseText;	
					document.getElementsByTagName("head").item(0).innerHTML=contenuHead;
					document.getElementById("page").innerHTML=contenuVisuStruct;
					effaceDivSurPage();
				}
			}
			
			xhr_head.open("POST","/lib/ajax/contenuFenetre.php",true);
			xhr_head.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
			xhr_head.send("typeInfo=HEAD");		
		}
	}
			
	xhr_local.open("POST",url,true);
	xhr_local.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
	xhr_local.send(cgis+"&JUSTE_LE_CENTRE=JLC");
}



function listeByAjaxFromCGI(cgis,htmlAttente){
	urlCentreByAjaxFromCGI("/liste",cgis,htmlAttente);
}

function listeByAjaxFromForm(idForm,htmlAttente){
	var objForm=document.getElementById(idForm);
	if (!objForm)return;

	var tabForm=getTabChmpForm(objForm);
	var query="";
	
	for (var i=0;i<tabForm.length;i++){
		query+="&"+tabForm[i].id+"="+tabForm[i].value;	
	}
	urlCentreByAjaxFromCGI("/liste",query,htmlAttente);
}


function urlDsPageCurrByAjaxAvecMsgAttente(url,cgi,htmlAttente){

}




function affUrlInDiv(nomDiv,url,vars,msgAttente,foncQdFini){
	var idx=url.indexOf("?");
	if (idx>0){
		vars += url.substr(idx+1);
		url = url.substr(0,idx);
	}
	vars+=getInfoSession("PHPSESSID");
	
	var xhr_local=creeObjHttpRequest();		
	xhr_local.onreadystatechange = function(){
		if(xhr_local.readyState == 4 && xhr_local.status == 200){
		
			var reponse = xhr_local.responseText;
			var elem=$(nomDiv);		
			if (elem) elem.innerHTML=reponse;
			if (foncQdFini)foncQdFini(elem);			
		}
		if(xhr_local.readyState == 4 && xhr_local.status!=200){
			if (foncQdFini)foncQdFini(elem);			
		}		
	}
	
	if (msgAttente!="aucun"){
		var elem=document.getElementById(nomDiv);
		if (elem && msgAttente)elem.innerHTML=msgAttente;		
		else affMsgAttenteInDiv(nomDiv);		
	}
	
	xhr_local.open("POST",url,true);
	xhr_local.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
	xhr_local.send(vars);
}

function affUrlInDivFromHtml(html,foncQdFini){
	var data=html;
	var idx_debhtml=data.indexOf("-->");
	if (idx_debhtml>0){
		var js = data.substr(4,idx_debhtml-4);
		if (js.substr(0,14)=="JS_affUrlInDiv"){
			var tabInfos=js.split("&#164;");
			affUrlInDiv(tabInfos[1],tabInfos[2],tabInfos[3],"",foncQdFini);
		}else{
			if (window.foncQdFini)window.foncQdFini(false);
		}
	}

}


function affUrlSsTourInDivAvecMsgAttente(nomDiv,url,query_string,html_attente){
	var xhr_local=creeObjHttpRequest();
	xhr_local.onreadystatechange = function(){
		if(xhr_local.readyState == 4 && xhr_local.status == 200){
			var contenu=xhr_local.responseText;	
			document.getElementById(nomdiv).innerHTML=contenu;
		}
	}
			
	xhr_local.open("POST",url,true);
	xhr_local.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
	xhr_local.send(query_string);
}

function chargeAdresseHTMLDansDiv(nomdiv,url,query_string){
	var xhr_local=creeObjHttpRequest();
	xhr_local.onreadystatechange = function(){
		if(xhr_local.readyState == 4 && xhr_local.status == 200){
			var contenu=xhr_local.responseText;	
			document.getElementById(nomdiv).innerHTML=contenu;
		}
	}
			
	xhr_local.open("POST",url,true);
	xhr_local.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
	xhr_local.send(query_string);
}
 
 
function appelAjaxToFunc(url,query_string,foncQdFini){
	var xhr_local=creeObjHttpRequest();
	xhr_local.onreadystatechange = function(){
		if(xhr_local.readyState == 4 && xhr_local.status == 200){
			var contenu=xhr_local.responseText;	
			if (foncQdFini)foncQdFini(contenu);
		}
	}			
	xhr_local.open("POST",url,true);
	xhr_local.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
	xhr_local.send(query_string);
} 
 
function appelAjaxXMLToFunc(url,query_string,foncQdFini){
	var xhr_local=creeObjHttpRequest();
	xhr_local.onreadystatechange = function(){
		if(xhr_local.readyState == 4 && xhr_local.status == 200){
			var contenu=clean(xhr_local.responseXML.documentElement);
			if (foncQdFini)foncQdFini(contenu);
		}
	}			
	xhr_local.open("POST",url,true);
	xhr_local.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
	xhr_local.send(query_string);
}
 
function appelAdrAjaxInDivAvecTransparence(adr,sending,msg,w,h){
	var html_init="<div id='divChargementAjx'><img src=/config_v3/imgs_defaut/loading/roue16x16.gif /><br />"+msg+"</div>";
	
	if(!w) w=200;
	if(!h) h=150;
	afficheDivAvecTransparence(html_init,w,h);

	var objStatus=document.getElementById("divChargementAjx");
	var xhr_local=creeObjHttpRequest();
  	xhr_local.onreadystatechange = function(){
		if(xhr_local.readyState == 4 && xhr_local.status == 200){
			reponse = xhr_local.responseText;
			objStatus.innerHTML=reponse;
			}
		}
	xhr_local.open("POST",adr,true);
 	xhr_local.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
	xhr_local.send(sending);
	
	
}
function affParamCGICurrInObjById(idDiv,param,acc,chaineAvant,chaineApres){
	var obj=document.getElementById(idDiv);
	if (obj){
		var url="/lib/ajax/infosCGI.php";
		var vars="typeInfo="+param+"&val="+acc;
		vars+=getInfoSession("PHPSESSID");
		
		var xhr_local=creeObjHttpRequest();		
		xhr_local.onreadystatechange = function(){
			if(xhr_local.readyState == 4 && xhr_local.status == 200){
				var reponse = new String(xhr_local.responseText);
				var ch="";
				if (reponse.length>0){
					if (chaineAvant)ch=chaineAvant;
					ch=ch+reponse;
					if (chaineApres)ch=ch+chaineApres;
					obj.innerHTML=ch;	
				}								
						
			}
		}
		obj.innerHTML="<img src=/config_v3/imgs_defaut/chargement_3Points.gif id=imgChargementAffParamCGI width=10/>";
		xhr_local.open("POST",url,true);
		xhr_local.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
		xhr_local.send(vars);
	}
}

function setCGIByAjax(querystring,rechargeListe,foncQdFini,paramFcQdFini){
	var url="/lib/ajax/setInfosCgi.php";
	var vars="query_string="+escape(querystring);
	if (rechargeListe)vars+="&reloadList=O";
	vars+=getInfoSession("PHPSESSID");
	
	var xhr_local=creeObjHttpRequest();		
	xhr_local.onreadystatechange = function(){
		if(xhr_local.readyState == 4 && xhr_local.status == 200){
			var reponse = new String(xhr_local.responseText);
			if (foncQdFini && paramFcQdFini)foncQdFini(reponse=="OK",paramFcQdFini);
			else if (foncQdFini)foncQdFini(reponse=="OK");
		}
	}
	xhr_local.open("POST",url,true);
	xhr_local.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
	xhr_local.send(vars);
}

function chargeMotCleDsDiv(idDiv,motCle){
	if (!window.jsonMotcle)window.jsonMotcle=[];
	for (var i=0;i<window.jsonMotcle.length;i++){
		if (window.jsonMotcle[i].motcle==motCle){
			if ($(idDiv))$(idDiv).innerHTML=window.jsonMotcle[i].content;
			return;
		}
	}
	var adr="/lib/ajax/contenuFenetre.php";
	var sending=getInfoSession()+"&element=critere&typeInfo=motcleByCritere&val="+motCle;
	appelAjaxToFunc(adr,sending,function(reponse){
		window.jsonMotcle.push({'motcle':motCle,'content':reponse});
		window.jsonMotcle.uniq();
		if ($(idDiv))$(idDiv).innerHTML=reponse;
	});
	
	//affUrlInDiv(idDiv,adr,sending,"aucun");
}
