function createXHR()
{
	var http=null;
	if(window.XMLHttpRequest)
		http=new XMLHttpRequest();
	else
	if(window.ActiveXObject) {
		var versions=["Microsoft.XMLHTTP","Msxml2.XMLHTTP.7.0","Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0","Msxml2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
		for(var i=0;i<versions.length;i++) {
			try {
				http=new ActiveXObject(versions[i]);
				if(http) break;
			}
			catch(objException){};
		}
	}
	return(http);
}

function trAjax(p_cible,p_xml,p_xsl,p_method,p_param,asynchrone,chargement,callbackFunc)
{
	var newParam = "_tmpUID="+(new Date).valueOf();
	p_param = p_param+"&"+newParam;
	p_xsl = p_xsl+"?"+newParam;
	
	
	if(document.getElementById("divDEBUG")) {
		var newDIV=document.createElement('div');
		newDIV.style.border="1px solid red";
		newDIV.style.margin="10px";
		newDIV.style.padding="5px";
		newDIV.style.whiteSpace="normal";
		var chInner="";
		var newDIVEntete=document.createElement('div');
		newDIVEntete.innerHTML='NEW TRANSFORMATION AJAX IN DIV "<font color="red">'+p_cible+'</font>" : <br/>';
		var newDIVXML=document.createElement('div');
		newDIVXML.innerHTML='<strong>XML FILE : </strong>';
		var newAXML=document.createElement('a');
		newAXML.href="javascript:void(0)";
		newAXML.innerHTML=p_xml;
		newAXML.onclick=function(){window.open(p_xml+'?'+p_param);}
		newDIVXML.appendChild(newAXML);
		chInner="<strong>[PARAM]</strong><br/>";
		var tParam=p_param.split("&");
		for(var i=0;i<tParam.length;i++) {
			if (tParam[i] !='')
				chInner+=tParam[i].replace('=',' ==> ')+"<br/>"
		}
		chInner+="<strong>[/PARAM]</strong><br/>";
		chInner+='<strong>XSL FILE : </strong><a href="#" onclick=\'openSource("'+p_xsl+'")\'>'+p_xsl+'<br/>';
		var newDIVXSL=document.createElement('div');
		newDIVXSL.innerHTML=chInner
		newDIV.appendChild(newDIVEntete);
		newDIV.appendChild(newDIVXML);
		newDIV.appendChild(newDIVXSL);
		document.getElementById("divDEBUG").appendChild(newDIV);
	}
	
	
	
	
	var sauvPosition = null;
	if ((chargement)&&(chargement!="")) {
		var divCible = document.getElementById(p_cible);
		if (divCible) {
			var divLoading = document.createElement('div');
			divLoading.style.backgroundColor = 'white';
			divLoading.style.width = divCible.offsetWidth + 'px';
			divLoading.style.height = divCible.offsetHeight + 'px';
			divLoading.style.position = 'absolute';
			divLoading.style.left = '0px';
			divLoading.style.top = '0px';
			divLoading.style.filter='alpha(opacity=80)';
			divLoading.style.opacity='.80';
			divLoading.innerHTML = chargement;
			sauvPosition = divCible.style.position;
			divCible.style.position='relative';
			divCible.appendChild(divLoading);
		}
	}
	
	if (asynchrone == undefined) asynchrone=false;
	p_method = p_method.toUpperCase();
	if (p_method != 'POST') p_method='GET';
	
	/***************** MODE ASYNCHRONE *****************/
	if (asynchrone==true) {
		var httpXML = createXHR();
		httpXML.open(p_method, (p_method=='POST'?p_xml:p_xml+'?'+p_param), true);
		if (p_method=='POST')
			httpXML.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpXML.onreadystatechange = function() {
			if(httpXML.readyState == 4) {
				if(httpXML.status == 200) {
					var httpXSL = createXHR();
					httpXSL.open('GET', p_xsl, asynchrone);
					httpXSL.onreadystatechange = function() {
						if(httpXSL.readyState == 4) {
							if(httpXSL.status == 200) {
								if (sauvPosition != null)
									document.getElementById(p_cible).style.position=sauvPosition;
								transformation(p_cible,httpXML.responseXML,httpXSL.responseXML);
								if (typeof(callbackFunc)=='function')
									callbackFunc();
							}
						}
					}
					httpXSL.send('');
				}
			}
		};
		httpXML.send((p_method=='POST'?p_param:''));
	}
	/****************************************************/
	
	
	/****************** MODE SYNCHRONE *****************/
	if (asynchrone==false) {
		var httpXML = createXHR();
		httpXML.open(p_method, (p_method=='POST'?p_xml:p_xml+'?'+p_param), false);
		if (p_method=='POST')
			httpXML.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpXML.send((p_method=='POST'?p_param:''));
		if(httpXML.readyState == 4) {
			if(httpXML.status == 200) {
				var httpXSL = createXHR();
				httpXSL.open('GET', p_xsl, false);
				httpXSL.send('');
				if(httpXSL.readyState == 4) {
					if(httpXSL.status == 200) {
						if (sauvPosition != null)
							document.getElementById(p_cible).style.position=sauvPosition;
						transformation(p_cible,httpXML.responseXML,httpXSL.responseXML)
						if (typeof(callbackFunc)=='function')
								callbackFunc();
					}
				}
			}
		}
	}
	/****************************************************/
}

function transformation(s_cible,fichierXML,fichierXSL)
{
	try {
		if (window.XSLTProcessor) {
			var xsltProcessor = new XSLTProcessor();
			xsltProcessor.importStylesheet(fichierXSL);
			var fragment = xsltProcessor.transformToFragment(fichierXML, document);
			if ((cible!="")&&(document.getElementById(s_cible))) {
				var cible = document.getElementById(s_cible);
				cible.innerHTML="";
				cible.appendChild(fragment);
			}
			else {
				var tmpElement = document.createElement('div');
				tmpElement.appendChild(fragment);
				document.write(tmpElement.innerHTML);
			}
		}
		else if (window.ActiveXObject) {
			if (s_cible!="") {
				var cible = document.getElementById(s_cible);
				cible.innerHTML="";
				cible.innerHTML = fichierXML.transformNode(fichierXSL.documentElement);
			}
			else
				document.write(fichierXML.transformNode(fichierXSL.documentElement));
		}
	}
	catch (e) {
		alert(e.description);		
	}
}
