function S4()
/*---------*/
{
   return ( ( ( 1+Math.random() ) *0x10000 ) | 0 ).toString(16).substring(1);
}
function guid()
/*-----------*/
{
   return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}

function STR_ltrim( szStr )
/*-----------------------*/
{
    var szResult = '';
    var i        = 0;

    while ( szStr[i] == ' ' && i < szStr.length )
    {
        i++;
    }

    while ( i < szStr.length )
    {
        //szResult += szStr[i++];
        szResult += szStr.charAt(i++);
    }

    return ( szResult );
}

function STR_rtrim( szStr )
/*-----------------------*/
{
    var szResult = '';
    var k        = szStr.length - 1;
    var i        = 0;

    while ( szStr[k] == ' ' && k >= 0 )
    {
        k--;
    }

    while ( i <= k )
    {
        //szResult += szStr[i++];
        szResult += szStr.charAt(i++);
    }

    return ( szResult );
}

function adaptForm( oCombo )
/*------------------------*/
{
    switch( oCombo.value )
    {
        case    '1':
        case    '4':
        case    '5':
        case    '6':    document.getElementById('owner').style.display = 'none';
                        document.getElementById('email').style.display = 'block';
                        break;
        case    '-27':  o = document.getElementById('txtTTL');
                        if ( o )
                        {
                            x = STR_ltrim( STR_rtrim( o.value ) );

                            if ( ( x.length == 0 ) || 
                                 ( x == "0" ) 
                               )
                            {
                                o.value = '5184000';
                            }
                        }
                        break;
        default:        document.getElementById('owner').style.display = 'block';
                        document.getElementById('email').style.display = 'none';
                        break;
    }
}

/* Used when validating a search (search-form) */
function checkForm2( oForm,szLang )
/*-------------------------------*/
{
    var bRetVal = true;

    var szKey = STR_rtrim( STR_ltrim( oForm.txtKey.value ) );

    if ( ( oForm.cboCat ) && ( oForm.cboCat.value == 0 ) )
    {
        if ( szKey.length <= 0 )
        {
            switch ( szLang )
            {
                case 'fr'   :   szMsg = 'Veuillez fournir une clef <UIKey>';
                                break;
                default     :   szMsg = 'Please provide a <UIKey> key';
                                break;
            }
            alert( szMsg );
            bRetVal = false;
            oForm.txtKey.focus();
        }
    }

    return ( bRetVal );
}

function checkForm( oForm )
/*-----------------------*/
{
    var bRetVal = true;

    //iCategory = document.frmRegistration.cboCat.value;
    var iCategory = oForm.cboCat.value;
    var szKey     = STR_rtrim( STR_ltrim( oForm.txtKey.value ) );

    if ( szKey.length <= 0 )
    {
        alert( "Please provide a <UIKey> key" );
        bRetVal = false;
        oForm.txtKey.focus();
    }

    if ( bRetVal )
    {
        /* If individual, company, organization or body */
        if ( iCategory == 1 ||  /* Individual */
             iCategory == 4 ||  /* Company */
             iCategory == 5 ||  /* Organization */
             iCategory == 6 )   /* Body */
        {
            /* In that case, the email address must be filled
              (email address is NOT checked ... backend will do this */
            var szEmail   = STR_rtrim( STR_ltrim( oForm.txtEmail.value ) );
            var szFilter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            
            if ( ! szFilter.test( szEmail ) )
            {
                alert( "Please provide a valid email address" );
                bRetVal = false;
                oForm.txtEmail.focus();
            }
            
            if ( bRetVal )
            {
                /* When we register an individual, a company, an organization
                   or a body ... the key MUST always be active */
                oForm.chkActive.checked = true;
            }
        }
        else
        {
            var szOwner = STR_rtrim( STR_ltrim( oForm.txtOwner.value ) );
            if ( szOwner.length <= 0 )
            {
                alert( "Please provide your owner key" );
                bRetVal = false;
                oForm.txtOwner.focus();
            }
        }
    }

    return ( bRetVal );
}

function showLogin( szID )
/*----------------------*/
{
    if ( oDiv = document.getElementById(szID) )
    {
        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( o )
/*--------------*/
{
    if ( oChk = document.getElementById( 'chkHelp' ) )
    {
        if ( oChk.checked && ( x = o.getElementsByTagName( 'span' ) ) )
        {
            x[0].style.display = "block";
        }
    }
}

function Hide( o )
/*--------------*/
{
    if ( oChk = document.getElementById( 'chkHelp' ) )
    {
        if ( oChk.checked && ( x = o.getElementsByTagName( 'span' ) ) )
        {
            x[0].style.display = "none";
        }
    }
}

