/* ALIGNEMENT VERTICAL */
// Recupere la taille de la fenetre pour un alignement vertical
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;
}

// Effectue l'alignement vertical sur l'identifiant 'content'
function setContents() {
    if (document.getElementById) {
        var windowHeight = getWindowHeight();
        if (windowHeight > 0) {
            var contentElement = document.getElementById('content_vertical');
            if(contentElement != null){
                var contentHeight = contentElement.offsetHeight;
                if (contentHeight == 0) contentHeight = windowHeight;
                //alert(windowHeight + " - " + contentHeight);
                if (windowHeight - contentHeight >= 0) {
                    contentElement.style.position = 'relative';
                    contentElement.style.top = ((windowHeight / 2) - (contentHeight / 2)) + 'px';
                }
                else {
                    contentElement.style.position = 'static';
                }
            }
        }
    }
    montre();
}
/**********************************************************/

/* Navigation au travers des menus verticaux-horizontaux */
function montre(id) {
    var d = document.getElementById(id);
        for (var i = 1; i<=20; i++) {
            if (document.getElementById('niveau'+i)) {
                document.getElementById('niveau'+i).style.display='none';
            }
        }
    if (d) {d.style.display='block';}
}
/**********************************************************/

/* Montre ou cache un element */
function montre_statut(id,statut) {
    var d = document.getElementById(id);
    if(statut != ""){
        if (d) {d.style.display=statut;}
    }
    else{
        if (d) {
            if(d.style.display == "block"){statut = "none";}
            else statut = "block";
            d.style.display=statut;
        }
    }
}
/**********************************************************/

/* Confirmation suppression */
function confirm_delete(){
    var valret;
    valret = confirm("Confirmez-vous la suppression de cet élément ?");
    return valret;
}
/******************************************************/

// Initialisation à l'ouverture de la page
window.onload = function() {
    setContents();
}

// Initialisation au moment du redimensionnement de la page
window.onresize = function() {
    setContents();
}
/******************************************************/
/* Fonction d'ouverture d'une popup */
function OuvrirPop(theURL,features,h,w){
    splits = theURL.split(".");
    window.open(theURL,splits[0],features+',width='+w+',height='+h+',top='+((screen.height-h)/2)+',left='+((screen.width-w)/2));
}

/******************************************************/
/* Ajout d'une information dans une listebox complexe */
function add_info(element, form_name, texte, type){
    var comment;
    comment=prompt(texte+" ?","");
    if(comment != null && comment != "") {
         if(type == "1") {document.forms[form_name].elements[element].value = comment;}
         else document.forms[form_name].elements[element].value = " WHERE date_envoi = '"+comment+"'";
         return true;
    }
    else return false;
}

/******************************************************/
/* Fonction de confirmation d'envoi */
function confirm_envoi(){
    var valret;
    valret = confirm("Confirmez-vous l'envoi de cet Emailing ? ");
    return valret;
}

/******************************************************/
/* Fonction de suppression de l'emailing */
function confirm_delete(){
    var valret;
    valret = confirm("Confirmez-vous la suppression définitive de cet Emailing et de toutes les informations s'y rapportant ? ");
    return valret;
}

/******************************************************/
// the timeout for the menu
var timeout = 10;

// not very clean but simple
// the function can be run in the HTML for faster display
// window.onload=initMenu;

// creat timeout variables for list item
// it's for avoid some warning with IE
for( var i = 0; i < 100; i++ )
{
    eval("var timeoutli" + i + " = false;");
}

// this fonction apply the CSS style and the event
function initMenu()
{
    // a test to avoid some browser like IE4, Opera 6, and IE Mac
    if ( browser.isDOM1 
    && !( browser.isMac && browser.isIE ) 
    && !( browser.isOpera && browser.versionMajor < 7 )
    && !( browser.isIE && browser.versionMajor < 5 ) )
    {
        // get some element
        var menu = document.getElementById('pmenu'); // the root element
        var lis = menu.getElementsByTagName('li'); // all the li
        
        // change the class name of the menu, 
        // it's usefull for compatibility with old browser
        menu.className='pmenu';
        
        // i am searching for ul element in li element
        for ( var i=0; i<lis.length; i++ )
        {
            // is there a ul element ?
            if ( lis.item(i).getElementsByTagName('ul').length > 0 )
            {        
                // improve IE key navigation
                if ( browser.isIE )
                {
                    addAnEvent(lis.item(i),'keyup',show);
                }
                // link events to list item
                addAnEvent(lis.item(i),'mouseover',show);
                addAnEvent(lis.item(i),'mouseout',timeoutHide);
                addAnEvent(lis.item(i),'blur',timeoutHide);
                addAnEvent(lis.item(i),'focus',show);
                
                // add an id to list item
                lis.item(i).setAttribute( 'id', "li"+i );
            }
        }
    }
}

function addAnEvent( target, eventName, functionName )
{
    // apply the method to IE
    if ( browser.isIE )
    {
        //attachEvent dont work properly with this
        eval('target.on'+eventName+'=functionName');
    }
    // apply the method to DOM compliant browsers
    else
    {
        target.addEventListener( eventName , functionName , true ); // true is important for Opera7
    }
}
    
// hide the first ul element of the current element
function timeoutHide()
{
    // start the timeout
    eval( "timeout" + this.id + " = window.setTimeout('hideUlUnder( \"" + this.id + "\" )', " + timeout + " );");
}

// hide the ul elements under the element identified by id
function hideUlUnder( id )
{   
    document.getElementById(id).getElementsByTagName('ul')[0].style['visibility'] = 'hidden';
    document.getElementById(id).getElementsByTagName('a')[0].className = 'linkOut';
}

// show the first ul element found under this element
function show()
{
    // show the sub menu
    this.getElementsByTagName('ul')[0].style['visibility'] = 'visible';
    var currentNode=this;
    while(currentNode)
    {
            if( currentNode.nodeName=='LI')
            {
                currentNode.getElementsByTagName('a')[0].className = 'linkOver';
            }
            currentNode=currentNode.parentNode;
    }
    // clear the timeout
    eval ( "clearTimeout( timeout"+ this.id +");" );
    hideAllOthersUls( this );
}

// hide all ul on the same level of  this list item
function hideAllOthersUls( currentLi )
{
    var lis = currentLi.parentNode;
    for ( var i=0; i<lis.childNodes.length; i++ )
    {
        if ( lis.childNodes[i].nodeName=='LI' && lis.childNodes[i].id != currentLi.id )
        {
            hideUlUnderLi( lis.childNodes[i] );
        }
    }
}

// hide all the ul wich are in the li element
function hideUlUnderLi( li )
{
    var as = li.getElementsByTagName('a');
    for ( var i=0; i<as.length; i++ )
    {
        as.item(i).className="";
    }
    var uls = li.getElementsByTagName('ul');
    for ( var i=0; i<uls.length; i++ )
    {
        uls.item(i).style['visibility'] = 'hidden';
    }
} 

// Permet d'afficher une image en grand
function Affichegrande(cheminImage,texte)
{
    newImage = new Image;
    newImage.src = cheminImage;
    html = '<HTML><HEAD><TITLE>'+texte+'</TITLE><meta http-equiv="Pragma" content="no-cache"></HEAD><BODY leftmargin=0 marginwidth=0 topmargin=0 marginheigth=0 oncontextmenu="return false"><CENTER>'+
    '<a href="#" onClick="window.close()"><IMG SRC="'+cheminImage+'" BORDER=0 NAME=monImage alt="'+texte+'"border="0" onLoad="window.resizeTo(document.monImage.width+20,document.monImage.height+80); window.moveTo((screen.width-document.monImage.width)/2,5)"> </a></CENTER></BODY></HTML>';
    ouvrirImage = window.open('','_blank','toolbar=0,location=0,menuBar=0,scrollbars=0,resizable=0');
    ouvrirImage.document.write(html);
}

