var cancel=0;

function createXhrObject()
{
    if (window.XMLHttpRequest)
        return new XMLHttpRequest();
		
    if (window.ActiveXObject)
    {
        var names = [
            "Msxml2.XMLHTTP.6.0",
            "Msxml2.XMLHTTP.3.0",
            "Msxml2.XMLHTTP",
            "Microsoft.XMLHTTP"
        ];
		
        for(var i in names)
        {
            try{ return new ActiveXObject(names[i]); }
            catch(e){}
        }
    }
    window.alert("Votre navigateur ne prend pas en charge l'objet XMLHTTPRequest.");
    return null; // non supporté
	
}


function ajax(url, mydiv, lang, wait_type){
    var ajaxRequest;  // The variable that makes Ajax possible!  
	
	//var cancel=0;
	
	var ajaxDisplay = document.getElementById(mydiv);
	//ajaxDisplay.innerHTML="Veuillez patienter un moment pendant le chargement du fichier...!";
	
	if (wait_type!=2){
		
		var txt="<div align='center' style='display:block; top:35%; position:relative; text-align:center;'><table style='display:block;'><tr>";
		txt+= "<td align='center' valign='middle' style='padding-left:10px;'><img src='images/WAIT.gif' height='40px' width='40px' style='border: none;' /></td>";
		txt+="<td align='center' valign='middle' style='font-size:15px; font-weight:bold; color:#AA2200; vertical-align:middle;'>Veuillez patienter un moment pendant le chargement du fichier...!</td></table></div>";
	}else{
		var txt="<div style='display:block; position:relative; top:-10px;'><span style='font-size:12px;'>Please Wait...</span><br/> <a href='http://download.microsoft.com/download/A/6/8/A6831711-EF4D-4BBC-A217-E65EA0F6C55B/installation_ie8-xp.exe' style='font-size:8px; color:#CC5500'>[MAJ IE8 - XP]</a>    <a href='http://download.microsoft.com/download/A/6/8/A6831711-EF4D-4BBC-A217-E65EA0F6C55B/installation_ie8-Vista.exe' style='font-size:8px; color:#CC5500'>[MAJ IE8 - Vista]</a></div>";
	}
	
	ajaxDisplay.innerHTML=txt;
	//debut try
    /*try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
		
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				
            } catch (e){
                alert("Your browser broke!");
                return false;
            }
        }
    }//fin try
	*/
	ajaxRequest=createXhrObject();

    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        //if(cancel==3){return;}
		if(ajaxRequest.readyState == 4){
            var ajaxDisplay = document.getElementById(mydiv);
			if(lang==2) {
				ajaxDisplay.style.textAlign="right";
			}else if(lang==3){
				ajaxDisplay.style.textAlign="center";
			}else{ajaxDisplay.style.textAlign="left";}
			
            ajaxDisplay.innerHTML = ajaxRequest.responseText;
			cancel=0;			
			clearTimeout(tm);
        }
    }
	
	ajaxRequest.timeout1 = function(){
		
		clearTimeout(tm);
		cancel++;
		if(cancel<5){ //on repete la procédure 5 fois si non on annulle
			ajaxRequest=null;
			ajax(url, mydiv, lang);
			//alert(c);
		}
		//ajaxRequest.onreadystatechange.stopPropagation();
		return;
	}

    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null);
	
	//tm=setTimeout(ajaxRequest.timeout, 10000); //10 secondes
	tm=setTimeout(ajaxRequest.timeout1, 4000); //4 secondes
	
	
}