﻿function getAjaxInstance()
{
	var reponse = null;
	
	if(window.XMLHttpRequest) // Firefox et autres
	{
		reponse = new XMLHttpRequest();
	} 
	else if(window.ActiveXObject)  // Internet Explorer
	{
		var ieversions = ['Msxml2.XMLHTTP',
						'Microsoft.XMLHTTP',
						'Msxml2.XMLHTTP.5.0',
						'Msxml2.XMLHTTP.4.0',
						'Msxml2.XMLHTTP.3.0'
						];             
		for(var i=0; !reponse && i<ieversions.length; i++)     
		{         
			try        
			{            
				reponse = new ActiveXObject(ieversions[i]);        
                break;						       
			}         
			catch(e)         
			{             
				reponse = null;               
			}     
		}
	}
	 
	if(!reponse)// XMLHttpRequest non supporté par le navigateur
		alert("Votre navigateur ne supporte pas les objets XMLHttpRequest...");

	return reponse;
}

function AjaX_HTML(url, isAsynch)
{
//alert("sync:"+url);		
//window.status = "AjaX_HTML("+url+")";
    if (null == isAsynch || true != isAsynch)
        isAsynch = false;
	var AjaX_xmlHttp = getAjaxInstance();
//window.status = "1 AjaX_HTML("+url+")";
	AjaX_xmlHttp.open("GET",url,isAsynch);
//window.status = "2 AjaX_HTML("+url+")";

	if (AjaX_xmlHttp.readyState == 4) /* 4 : état "complete" */
	{
//window.status = "4 AjaX_HTML("+url+")";
		if (AjaX_xmlHttp.status == 200) /* 200 : code HTTP pour OK */
		{
//window.status = "5 AjaX_HTML"+AjaX_xmlHttp.responseText+"";
//alert( AjaX_xmlHttp.responseText );
    		return AjaX_xmlHttp.responseText;
		}else
		{
//window.status = "6 AjaX_HTML("+url+")";
         return "Error: wrong status "+AjaX_xmlHttp.status+", please retry...";
		}
	}else
	{
//window.status = "7 AjaX_HTML("+url+")";
	 return "Error: wrong readyState "+AjaX_xmlHttp.readyState+", please retry...";
	}

    AjaX_xmlHttp.send(null);
//window.status = "3 AjaX_HTML("+url+")";

//window.status = "end AjaX_HTML("+url+")";
}


function AjaX_getHTML(url, destDivObj, isAsynch)
{
    if (null == destDivObj || null == url)
        return;
    if (null == isAsynch || true != isAsynch)
        isAsynch = false;
    var loadingHTML = "<center><br style='line-height:15px;'/><table><tr><td><img src='/images/loading.gif' style='display:block;'></td><td><span style='padding-left:4px'>chargement...</span></td></tr></table></center>";
    destDivObj.innerHTML = loadingHTML;
    
    var AjaX_xmlHttp = getAjaxInstance();
    if (true == isAsynch){
        // asynchronous request
	    AjaX_xmlHttp.open("GET",url,true);
        AjaX_xmlHttp.onreadystatechange = function () {
	        if (AjaX_xmlHttp.readyState == 4) { /* 4 : état "complete" */
		        if (AjaX_xmlHttp.status == 200) { /* 200 : code HTTP pour OK */
    		        destDivObj.innerHTML = AjaX_xmlHttp.responseText;
		        }
		        else
                    destDivObj.innerHTML = "<center><b>Error: wrong status "+AjaX_xmlHttp.status+", please retry...</b></center>";
	        }
        }
        AjaX_xmlHttp.send(null);
    }
    else {
        // synchronous request
        AjaX_xmlHttp.open("GET",url,false);
        AjaX_xmlHttp.send(null);
        if (AjaX_xmlHttp.readyState == 4) { /* 4 : état "complete" */
	        if (AjaX_xmlHttp.status == 200) { /* 200 : code HTTP pour OK */
		        destDivObj.innerHTML = AjaX_xmlHttp.responseText;
	        }
	        else
                destDivObj.innerHTML = "<center><b>Error: wrong status "+AjaX_xmlHttp.status+", please retry...</b></center>";
        }
    }
}
