/*  File name               :   ls.js
    Version                 :   1.0
    Author                  :   Patrick Boens
    Company                 :   Lato Sensu Management
                                Rue Bois des Mazuis, 47
                                5070 Vitrival (Fosses-La-Ville)
                                Belgique
    Creation date           :   2008-12-08 - 16:54
    Last modification date  :   2008-12-08 - 16:54
    Last modified by        :   Patrick Boens
    Copyrights              :   (c) 2008 - Lato Sensu Management
    Purpose                 :   Provides some scripting
*/

//document.onmousemove = MousePos;
MouseX = 0;
MouseY = 0;

function showLogin( szID )
/*----------------------*/
{
    if ( ( navigator.userAgent.match( /iPhone/i ) ) || 
         ( navigator.userAgent.match( /iPod/i   ) ) ||
         ( navigator.userAgent.match( /iPad/i   ) )
       )
    {
        location.replace( "/login.php" );
    }

    txt = "Browser CodeName: "  + navigator.appCodeName     + "\n";
    txt+= "Browser Name: "      + navigator.appName         + "\n";
    txt+= "Browser Version: "   + navigator.appVersion      + "\n";
    txt+= "Cookies Enabled: "   + navigator.cookieEnabled   + "\n";
    txt+= "Platform: "          + navigator.platform        + "\n";
    txt+= "User-agent header: " + navigator.userAgent       + "\n";

    // alert( txt );
    // <link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="iphone.css" type="text/css" />

    // Browser CodeName: Mozilla
    // Browser Name: Opera
    // Browser Version: 9.80 (Windows NT 6.1; U; fr)
    // Cookies Enabled: true
    // Platform: Win32
    // User-agent header: Opera/9.80 (Windows NT 6.1; U; fr) Presto/2.8.131 Version/11.11



    if ( oDiv = document.getElementById( szID ) )
    {
        //alert( "J'ai trouvé '" + szID + "'" );
        //return;

        if ( oDiv.style.display == 'none' )
        {
            if ( typeof window.innerWidth != 'undefined' )
            {
                iViewportWidth   = window.innerWidth;
                iViewportHeight  = window.innerHeight;
            }
            else if ( ( typeof document.documentElement             != 'undefined' ) &&
                      ( typeof document.documentElement.clientWidth != 'undefined' ) && 
                      ( document.documentElement.clientWidth != 0 )
                    )
            {
                iViewportWidth  = document.documentElement.clientWidth;
                iViewportHeight = document.documentElement.clientHeight;
            }
            else
            {
                iViewportWidth  = document.getElementsByTagName('body')[0].clientWidth;
                iViewportHeight = document.getElementsByTagName('body')[0].clientHeight;
            }

            bLogoff = false;

            if ( oSubmit = document.getElementById( 'LS_VAESOLI_LOGON_FORM_SUBMIT' ) )
            {
                szValue = oSubmit.value;
                bLogoff = ( szValue.toLowerCase() == 'logoff' );

                if ( bLogoff )
                {
                    oSubmit.style.width  = "180px";
                    oSubmit.style.margin = "0 5px";
                }
            }

            if ( bLogoff )
            {
                iHeight = 90;
                iWidth  = 200;
            }
            else
            {
                iHeight = 135;
                iWidth  = 350;
            }
            oDiv.style.position = "absolute";
            oDiv.style.height   = iHeight + "px";
            oDiv.style.width    = iWidth  + "px";
            oDiv.style.display  = "block";
            oDiv.style.border   = "1px solid #000";
            oDiv.style.left     = ( ( iViewportWidth  - iWidth  ) / 2 ) + "px";
            oDiv.style.top      = ( ( iViewportHeight - iHeight ) / 2 ) + "px";
        }
        else
        {
            oDiv.style.display    = 'none';
        }
    }
}
function Show( szID )
/*-----------------*/
{
    if ( document.getElementById && ( o = document.getElementById( szID ) ) )
    {
        o.style.display = 'block';
    }
}
function NotYet( szTerm )
/*---------------------*/
{
    alert( "Sorry. La fonctionnalité de " + szTerm +" sera programmée bientôt." );
}

function Hide( szID )
/*-----------------*/
{
    if ( document.getElementById && ( o = document.getElementById( szID ) ) )
    {
        o.style.display = 'none';
    }
}

function MousePos()
/*---------------*/
{
    MouseX = event.x + document.body.scrollLeft;
    MouseY = event.y + document.body.scrollTop;
}

function tooltip( szID,szVerb,szStyle )
/*-----------------------------------*/
{
    if ( szVerb == 'hide' )
    {
        if ( oDiv = document.getElementById( szID /* 'LSTooltip' */ ) )
        {
            oDiv.style.display = "none";
        }
    }
    else if ( szVerb == 'show' )
    {
        if ( oDiv = document.getElementById( szID /* 'LSTooltip' */ ) )
        {
            MousePos();            

            oDiv.style.display  = "block";
            oDiv.style.left     = MouseX;
            oDiv.style.top      = MouseY;

            if ( szStyle != null )
            {
                var aTokens;
                aTokens = szStyle.split(';');
                if ( aTokens.length > 0 )
                {
                    for ( i=0;i<aTokens.length;i++ )
                    {
                        szCSSInstr  = aTokens[i];
                        aCSSMembers = szCSSInstr.split(':');
                        if ( aCSSMembers.length == 2 )
                        {
                            szCmd = "oDiv.style." + aCSSMembers[0] + '= "' + aCSSMembers[1] + '"';
                            eval( szCmd );
                        }
                    }
                }
            }
        }
    }
    return ( true );
}

function slide( szID )
/*------------------*/
{
    if ( oDiv = document.getElementById( szID ) )
    {
        if ( oDiv.style.height == "118px" )
        {
            oDiv.style.height = "15px";
        }
        else
        {
            oDiv.style.height = "118px";
        }
    }
}

function createCookie( szName,szValue,iDays )
/*-----------------------------------------*/
{
    if ( iDays )
    {
        var date = new Date();
        date.setTime( date.getTime() + ( iDays * 86400000 ) );
        var szExpires = "; expires=" + date.toGMTString();
    }
    else
    {
        szExpires = "";
    }

    document.cookie = szName + "=" + szValue + szExpires + "; path=/";
    return false;
}

function createACookie( szMsg )
/*---------------------------*/
{
    szCookie = 'LSCookie';
    if ( szMsg == null )
    {
        szMsg = 'Entrez une valeur pour le cookie à créer';
    }

    szValue  = prompt( szMsg,'Bonjour ... valeur par défaut' );
    createCookie( szCookie,szValue,1 );
    return false;
}

function readCookie( szName )
/*-------------------------*/
{
    var szEQ     = szName + "=";
    var aCookies = document.cookie.split(';');

    for( var i=0;i < aCookies.length;i++)
    {
        var szCookie = aCookies[i];

        while ( szCookie.charAt(0) == ' ' )
        {
            szCookie = szCookie.substring( 1,szCookie.length );
        }

        if ( szCookie.indexOf( szEQ ) == 0 )
        {
            return ( szCookie.substring( szEQ.length,szCookie.length ) );
        }
    }

    return null;
}

function readACookie()
/*-------------------*/
{
    szCookie = 'LSCookie';
    szValue  = readCookie( szCookie );

    if ( szValue )
    {
        alert( "Le cookie vaut: " + szValue );
    }
}

function setStylesheet( szTitle )
/*-----------------------------*/
{
    var i, oLink, main;

    /* Pour chaque <link> trouvé dans le document */
    for( i=0;(oLink = document.getElementsByTagName("link")[i] );i++ )
    {
        /* S'il y a un attribut "rel" et qu'il contient "style"
           de même qu'un attribut "title" */
        if ( ( oLink.getAttribute( "rel"   ).indexOf("style") != -1 ) &&
             ( oLink.getAttribute( "title" ) ) )
        {
            oLink.disabled = true;      /* Désactiver la feuille de style */

            /* Si c'est la feuille de style que l'on cherche */
            if ( oLink.getAttribute( "title" ) == szTitle )
            {
                oLink.disabled = false; /* Réactiver la feuille de style */
            }
        }
    }
    return true;
}

function ResetStylesheet( szCookie )
/*--------------------------------*/
{
    if ( szCookie == null )
    {
        szCookie = 'LSStylesheet';
    }

    szStylesheet = readCookie( szCookie );

    if ( szStylesheet )
    {
        setStylesheet( szStylesheet );
    }
    else
    {
        //alert( "Pas de stylesheet trouvée" );
    }       
}
