/////////////////////////////
//     Affiche chargement
////////////////////////////

var temps=7;
var boucleChargement;
function afficheChargement(){
  var a="<div style='text-align:center'>";
    a+="<table style='background-color:transparent;border:0px solid red;line-height:1.2em;margin:50px auto'";
    a+="<tr style='border:0px'><td style='text-align:center;font-size:36pt;font-weight:900;border:0px;line-height:1.2em' align=center colspan=7>Chargement";
  a+="<tr style='border:0px'><td style='border:0px'>&nbsp";
  for(i=1;i<6;i++){
    a+="<td style='width:1.2em;border:0px' id=case"+i+">&nbsp";
  }
  a+="<td style='border:0px'>&nbsp</table></div>";

  document.getElementById("Aff").innerHTML=a; 
  boucleChargement=setInterval(couleurChargement,500);
}

var couleur=new Array("transparent","#999999","#AAAAAA","#BBBBBB","#CCCCCC","#DDDDDD","transparent","transparent","transparent","transparent","transparent");

function couleurChargement(){
  for(i=1;i<6;i++){
    x=temps-i;
    y=Math.round((x/10-Math.floor(x/10))*10);
    document.getElementById("case"+i).style.backgroundColor=couleur[y];
  }
  temps+=1;
}





////////////////////////////
//     fonction AJAX
////////////////////////////

function clique(fichier,action,cible,plus){
    if(cible==null)
	cible="Aff";

    //Affichage du chargement
    if(cible=="Aff")
	afficheChargement();	

    //xmlhttprequest
    var xhr_object = null; 
    if(window.XMLHttpRequest) // Firefox 
	xhr_object = new XMLHttpRequest(); 
    else if(window.ActiveXObject) // Internet Explorer 
	xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
    else { // XMLHttpRequest non supporté par le navigateur 
	alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	return; 
    }

    url="charge.php?choix="+action+"&fichierCible="+fichier;
    if(plus!=null)
	url+="&"+plus
     
    if(document.location.href.indexOf("127.0.0.1")!=-1)
	document.title=url

    xhr_object.open("GET", url, true);
    //xhr_object.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr_object.setRequestHeader("Content-Type", "text/html; charset=ISO-8859-1");

    xhr_object.onreadystatechange = function() { 
	if(xhr_object.readyState == 4) {
	    if(cible=="Aff"){
		clearInterval(boucleChargement);
		window.scrollTo(0,0);
	    }
	    //Desactive vielle feuille style
	    a=document.styleSheets
	    for(i=1;i<a.length;i++){
		a[i].disabled=true;
	    }
	    
            setInnerHTML(document.getElementById(cible),xhr_object.responseText)
	    //document.getElementById(cible).innerHTML=xhr_object.responseText
	}
    }
    xhr_object.send(null);
}







 // setInnerHTML Sécurisé
function setInnerHTML(divContent, HTML) {
    divContent.innerHTML=HTML;
    try {
      var All=divContent.getElementsByTagName("*");
      for (var i=0; i<All.length; i++) {
        All[i].id=All[i].getAttribute("id")
        All[i].name=All[i].getAttribute("name")
        //All[i].className=All[i].getAttribute("class")
      }
    } catch (ex) {}
    try {
      var AllScripts=HTML.extractTags("script");
      AllScripts.forEach(function (v) {
        eval(v);
      })
    } catch (ex) {}
    try {
      var AllStyles=HTML.extractTags("style");
      AllStyles.forEach(function (v) {
        var s=document.createStyleSheet()
        s.cssText=v;
        s.enabled=true;
      }, true)
    } catch (ex) {}
}

String.prototype.extractTags=function(tag) {
    var matchAll = new RegExp('(?:<'+tag+'.*?>)((\n|\r|.)*?)(?:<\/'+tag+'>)', 'img');
    var matchOne = new RegExp('(?:<'+tag+'.*?>)((\n|\r|.)*?)(?:<\/'+tag+'>)', 'im');
    return (this.match(matchAll) || []).map(function(scriptTag) {
      return (scriptTag.match(matchOne) || ['', ''])[1];
    });
  }

//Detect IE5.5+
version=0;
if(navigator.appVersion.indexOf("MSIE")!=-1)
{
temp=navigator.appVersion.split("MSIE");
version=parseFloat(temp[1]);
}
// NON IE browser will return 0
if(version>=5.5)
{
Object.prototype.forEach=function(delegate, ownpropertiesonly) {
    if (typeof(delegate)=="function") {
        if (this instanceof Array && typeof(ownpropertiesonly)=="undefined") {
            ownpropertiesonly=true;
        }
        for (key in this) {
            var ok = (!ownpropertiesonly);
            if (!ok) {
                try {
                    ok=this.hasOwnProperty(key)
                } catch (ex) {}
            }
            if (ok) {
                try { delegate(this[key], key, this) } catch(e) {
                    // ...
                }
            }
        }
    }
    return false;
}

Object.prototype.map=function(iterator) {
    var results = [];
    this.forEach(function(value, index) {
      results.push(iterator(value, index));
    });
    return results;
}
}
