// Renvoie le numéro de l'élément (identifiant dans la BDD)
function id_BDD( elt, separateur )
{
  if (!separateur) separateur = '_';
  return $(elt).id.substring($(elt).id.indexOf(separateur)+1, $(elt).id.length);
}

// Renvoie l'id formaté d'un élément.
function formate_id( element_id ){
    return element_id.substring( element_id.indexOf('_') + 1, element_id.length );
}

// Renvoie le type de l'élément : file, sdir ou dir.
function recup_type( element_id ){
  return element_id.substring( 0, element_id.indexOf('_') );
}


// InnerHTML 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) {}
}

// Remplace le contenu d'un div par la réponse AJAX
function alimente_HTML_Ajax(url_ajax, parametre_ajax, id_objet_html) {

    var url = url_ajax;

    if ( parametre_ajax )
      var parametres = parametre_ajax ;


    var myAjax = new Ajax.Updater(
      id_objet_html,
      url,
      {
      asynchronous:false,
      method: 'post',
      parameters: parametres,
      evalScripts: true
      }
    );
}


// Retourne la réponse AJAX d'un script PHP.
function recupere_HTML_Ajax(url_ajax, parametre_ajax )
{
    var hauteur =  Get_Hauteur();
    var largeur = Get_Largeur();
    var url = url_ajax;

    if ($("loading"))
    {
      $("loading").style.left = largeur  / 2;
      $("loading").style.top = hauteur / 2;
      $("loading").style.display = "block";
    }

    if ( parametre_ajax )
      var parametres = parametre_ajax;

    var retour_ajax;
    var myAjax = new Ajax.Request(
      url,
      {
        asynchronous:false,
        method: 'get',
        parameters: parametres ,
        onComplete: function(element){  retour_ajax = element.responseText; }
      }
    );

  if ($("loading")) $("loading").style.display = "none";

  return retour_ajax;
}


// Fonction qui retourne la hauteur de la fenetre du navigateur.
function Get_Hauteur()
{
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' )
    //Non-IE
    myHeight = window.innerHeight;
  else if( document.documentElement &&  document.documentElement.clientHeight )
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  else if( document.body && document.body.clientHeight )
    //IE 4 compatible
    myHeight = document.body.clientHeight;

  return myHeight;
}

// Fonction qui retourne la largeur de la fenetre du navigateur.
function Get_Largeur() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' )
    //Non-IE
    myWidth = window.innerWidth;
  else if( document.documentElement &&  document.documentElement.clientWidth )
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  else if( document.body && document.body.clientWidth )
    //IE 4 compatible
    myWidth = document.body.clientWidth;

  return myWidth;
}


/* Fonction qui récupère le décalage gauche (left) d'un objet dans la page */
function getPositionLeft (obj)
{
  var curleft = 0;
  if (obj.offsetParent)
  {
    curleft = obj.offsetLeft;
    while (obj = obj.offsetParent)
      curleft += obj.offsetLeft;
  }
  return curleft;
}

/* Fonction qui récupère le décalage haut (top) d'un objet dans la page */
function getPositionTop (obj)
{
  var curtop = 0;
  if (obj.offsetParent)
  {
    curtop = obj.offsetTop;
    while (obj = obj.offsetParent)
      curtop += obj.offsetTop;
  }
  return curtop;
}

// Permet de remonter dans un div en overflow hidden.
function move_up( div, pas )
{
  if ( ! pas ) pas = 1;
  pas = 5 * pas;
  $(div).scrollTop =  $(div).scrollTop  + pas;
}

// Permet de redescendre dans un div en overflow hidden.
function move_down(div)
{
  $(div).scrollTop  = $(div).scrollTop  - 5;
}

// Fonction qui encode une chaîne en utf8.
function utf8_encode ( string )
{
  string = (string+'').replace(/\r\n/g, "\n").replace(/\r/g, "\n");

  var utftext = "";
  var start, end;
  var stringl = 0;

  start = end = 0;
  stringl = string.length;
  for (var n = 0; n < stringl; n++)
  {
    var c1 = string.charCodeAt(n);
    var enc = null;

    if (c1 < 128)
    {
      end++;
    }
    else if((c1 > 127) && (c1 < 2048))
    {
      enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
    }
    else
    {
      enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
    }
    if (enc != null)
    {
      if (end > start)
      {
        utftext += string.substring(start, end);
      }
      utftext += enc;
      start = end = n+1;
    }
  }

  if (end > start)
  {
    utftext += string.substring(start, string.length);
  }

  return utftext;
}


// Fonction qui décode une chaîne en utf8.
function utf8_decode ( str_data ) {
    var tmp_arr = [], i = 0, ac = 0, c1 = 0, c2 = 0, c3 = 0;

    str_data += '';

    while ( i < str_data.length ) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if ((c1 > 191) && (c1 < 224)) {
            c2 = str_data.charCodeAt(i+1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i+1);
            c3 = str_data.charCodeAt(i+2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }

    return tmp_arr.join('');
}

function affiche_loading()
{
	$("loading").style.left = Get_Largeur()  / 2;
	$("loading").style.top = Get_Hauteur() / 2;
	$("loading").style.display = "block";
}

function cache_loading()
{
	new Effect.BlindUp('loading');
	//$("loading").style.display = "none";
}

function getWindowHeight() {
    var windowHeight=0;
    if (typeof(window.innerHeight)=='number') {
        windowHeight=window.innerHeight;
    }
    else {
     if (document.documentElement&&
       document.documentElement.clientHeight) {
         windowHeight = document.documentElement.clientHeight;
    }
    else {
     if (document.body&&document.body.clientHeight) {
         windowHeight=document.body.clientHeight;
      }
     }
    }
    return windowHeight;
}

function setFooter() {
    if (document.getElementById) {
        var windowHeight=Get_Hauteur();
      alert(windowHeight);
        if (windowHeight>0) {
            var contentHeight=
            document.getElementById('container').offsetHeight;
            var footerElement=document.getElementById('footer');
            var footerHeight=footerElement.offsetHeight;
        if (windowHeight-(contentHeight+footerHeight)>=0) {
            footerElement.style.position='relative';
            footerElement.style.top=
            (windowHeight-(contentHeight+footerHeight))+'px';
        }
        else {
            footerElement.style.position='static';
        }
       }
      }
}

function test_actif_js()
{
  if (navigator.appName.indexOf("Microsoft") != -1){
    var version = navigator.appVersion;
    version = version.substring(version.indexOf("MSIE") + 5);
    version = version.substring(0, version.indexOf(";"));
    if (parseInt(version) >= parseInt("7") && parseInt(version) < parseInt("8")){
      var sous_menu, sous_liste, max_width=0;
      sous_liste = document.getElementsByTagName("ul")[0].getElementsByTagName("ul");
      for (var i=0;i<sous_liste.length;i++){
        sous_liste[i].style.marginTop = "26px";
        sous_liste[i].style.marginLeft = "-125px";
        sous_menu = sous_liste[i].getElementsByTagName("li");
        for (var j=0;j<sous_menu.length;j++){
          max_width = Math.max(parseInt(sous_menu[j].clientWidth), max_width);
          sous_menu[j].style.width = "310px";
        }
      }
    }
    if (parseInt(version) < parseInt("8")){
      var elt = document.getElementsByClassName("haut_bloc_sidebar");
      for(var i=0;i<elt.length;i++){elt[i].style.marginTop="0px";}
    }
  }
  if (navigator.appName.indexOf("Microsoft") != -1)
  {
    var version = navigator.appVersion;
    version = version.substring(version.indexOf("MSIE") + 5);
    version = version.substring(0, version.indexOf(";"));
    if (parseInt(version) < parseInt("7"))
    {
      var annonce = "<p style=\"text-align:left;\"><strong>Vous devriez mettre à jour votre navigateur : </strong><br /><br />Vous utilisez une version trop ancienne d'internet explorer. Nous vous conseillons vivement de la mettre à jour ou d'utiliser un autre navigateur :<br /><br />&nbsp; <a href=\"http://www.mozilla-europe.org/fr/firefox/\" target=\"_blank\">Télécharger firefox</a>&nbsp;&nbsp;<a href=\"http://www.mozilla-europe.org/fr/firefox/\" target=\"_blank\"><img title=\"Télécharger firefox\" style=\"border:0;width:88px;\" src=\"img/firefox.png\" alt=\"Firefox\" /></a><br /> &nbsp; <a href=\"http://www.microsoft.com/france/windows/products/winfamily/ie/ie8/default.aspx\" target=\"_blank\">Télécharger internet explorer 8</a>&nbsp;&nbsp;<a href=\"http://www.microsoft.com/france/windows/products/winfamily/ie/ie8/default.aspx\" target=\"_blank\"><img title=\"Télécharger internet explorer\" style=\"border:0;width:88px;\" src=\"img/ie.png\" alt=\"IE\" /></a></p>";
      montre(annonce, "fixe", 600, "", "cache");
    }
  }

}