  /*
   *
   * DYNAMIC+
   *
  */


  var loginCheck =
  {
    bAlreadyInjected: false,

    bIsLogged: false,
    hWaitBox: null,

    hCurrentLink: null,
    hLoginBox: null,
    hLoginClose: null,
    hLoginForm: null,
    hLoginUserName: null,
    hLoginPassword: null,

    asyncUrl: '/session/ajax',

    init: function( )
    {
      loginCheck.hLoginBox = document.getElementById( 'contentLogin' );
      loginCheck.hLoginClose = document.getElementById( 'loginClose' );
      loginCheck.hLoginForm = document.forms['loginForm'];
      loginCheck.hLoginUserName = document.forms['loginForm'] ? document.forms['loginForm'].elements['loginUserName'] : null;
      loginCheck.hLoginPassword = document.forms['loginForm'] ? document.forms['loginForm'].elements['loginPassword'] : null;

      if ( loginCheck.hLoginBox && loginCheck.hLoginClose && loginCheck.hLoginForm && loginCheck.hLoginUserName && loginCheck.hLoginPassword )
      {
        loginCheck.hLoginForm.onsubmit = function( ) { loginCheck.login( ); return false; }
        loginCheck.hLoginClose.onclick = function( ) { loginCheck.close( ); return false; }

        if ( window.addEventListener )
          window.addEventListener( 'DOMContentLoaded', loginCheck.inject, false );

        else if( window.attachEvent )
          window.attachEvent( 'onload', loginCheck.inject );

        else
        {
          window.__onload = window.onload;
          window.onload = function( )
          {
            loginCheck.inject( );

            if ( window.__onload )
              window.__onload( );
          }
        }

        return true;
      }

      else
        alert( 'Alcuni componenti DHTML della pagina sono mancanti.' );

      return false;
    },


    inject: function( )
    {
      if ( !loginCheck.bAlreadyInjected )
      {
        loginCheck.bAlreadyInjected = true;

        var aLinks = document.links;

        for ( var i = 0; i < aLinks.length; i++ )
        {
          var oLink = aLinks[i];

          if ( /\blogin_check\b/.test(oLink.className) && !oLink.__bAlreadyInjected )
          {
            oLink.__bAlreadyInjected = true;

            oLink.onclick = function( )
            {
              if ( !loginCheck.bIsLogged )
              {
                loginCheck.check( this );
                return false;
              }
            }
          }
        }

      }

      return false;
    },


    call: function( strUrl, strPostData, fCallBack )
    {
      var hHttpRequest = null;

      if( window.XMLHttpRequest )
      {
        try
        {
          hHttpRequest = new XMLHttpRequest( );
        }

        catch( oErr )
        {
          hHttpRequest = null;
        }
      }

      else if( window.ActiveXObject )
      {
        var aStrMsxhtml = new Array( 'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP' );

        for ( var i = 0; i < aStrMsxhtml.length && !hHttpRequest; i++ )
        {
          try
          {
            hHttpRequest = new ActiveXObject( aStrMsxhtml[i] );
          }

          catch( oErr )
          {
            hHttpRequest = null;
          }
        }
      }

      if ( hHttpRequest != null )
      {
        hHttpRequest.onreadystatechange = function( )
        {
          if ( hHttpRequest.readyState == 4 )
            fCallBack(hHttpRequest.status == 200 && hHttpRequest.responseText ? hHttpRequest.responseText : null );
        }

        hHttpRequest.open( strPostData == null ? 'GET' : 'POST', strUrl, true );

        if ( strPostData != null )
        {
          hHttpRequest.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
          hHttpRequest.setRequestHeader( 'Content-Length', strPostData.length );
        }

        hHttpRequest.setRequestHeader( 'If-Modified-Since', 'Thu, 1 Jan 1970 00:00:00 GMT' );
        hHttpRequest.setRequestHeader( 'X-Requested-With', 'XMLHttpRequest' );

        hHttpRequest.send( strPostData );

        return true;
      }

      fCallBack( null );
      return false;
    },


    wait: function( bWait )
    {
      var aBody = document.getElementsByTagName( 'body' );
      var hBody = aBody.length > 0 ? aBody[0] : null;

      if ( hBody )
      {
        if ( bWait )
        {
          loginCheck.hWaitBox = document.createElement( 'div' );

          with ( loginCheck.hWaitBox.style )
          {
            position = 'fixed';
            display = '';

            left = '0px';
            top = '0px';

            width = '100%';
            height = '100%';

            backgroundColor = 'black';
            backgroundImage = 'url("/images/be/black-loader.gif")';
            backgroundRepeat = 'no-repeat';
            backgroundPosition = 'center center';

            zIndex = 100000;

            if ( typeof filters != typeof void 0 )
              filters.alpha.opacity = 75;

            MozOpacity = 0.75;
            opacity = 0.75;
          }

          loginCheck.hWaitBox.title = 'Caricamento in corso: attendere...';
          hBody.appendChild( loginCheck.hWaitBox );
        }

        else
        {
          if ( loginCheck.hWaitBox )
            hBody.removeChild( loginCheck.hWaitBox );

          loginCheck.hWaitBox = null;
        }
        return true;
      }

      return false;
    },



    check: function( hCurrentLink )
    {
      if ( !loginCheck.hWaitBox && !loginCheck.hCurrentLink )
      {
        loginCheck.hCurrentLink = hCurrentLink;
        loginCheck.wait( true );

        var aParams = new Array( );
        aParams[ aParams.length ] = 'rand_' + Math.round(Math.random()*999999999) + '=' + Math.round(Math.random()*999999999);
        aParams[ aParams.length ] = 'mode=0';

        loginCheck.call( loginCheck.asyncUrl, aParams.join('&'), function( strResponse )
        {
          loginCheck.wait( false );

          if ( strResponse )
          {
            try
            {
              var oResponse = eval( '(' + strResponse + ')' );
              loginCheck.bIsLogged = typeof oResponse == 'object' ? oResponse.data.is_logged : false;
            }

            catch ( e )
            {
            }
          }

          if ( loginCheck.bIsLogged )
            location.href = loginCheck.hCurrentLink.href;

          else
            loginCheck.hLoginBox.style.display = 'block';

        });


        return true;
      }

      return false;
    },

    close: function( )
    {
      if ( !loginCheck.hWaitBox )
      {
        if ( loginCheck.hCurrentLink )
        {
          loginCheck.hCurrentLink = null;
          loginCheck.hLoginBox.style.display = 'none';

          return true;
        }
      }

      return false;
    },


    logout: function( )
    {
      if ( !loginCheck.hWaitBox )
      {
        if ( confirm('Sei sicuro di voler effettuare il logout?') )
        {
          loginCheck.wait( true );

          var aParams = new Array( );
          aParams[ aParams.length ] = 'rand_' + Math.round(Math.random()*999999999) + '=' + Math.round(Math.random()*999999999);
          aParams[ aParams.length ] = 'mode=2';

          loginCheck.call( loginCheck.asyncUrl, aParams.join('&'), function( strResponse )
          {
            location.reload( true );
            return true;
          } );

          return true;
        }
      }

      return false;
    },


    login: function( )
    {
      if ( !loginCheck.hWaitBox && loginCheck.hCurrentLink )
      {
        loginCheck.wait( true );
        loginCheck.hLoginBox.style.display = 'none';

        var aParams = new Array( );
        aParams[ aParams.length ] = 'rand_' + Math.round(Math.random()*999999999) + '=' + Math.round(Math.random()*999999999);
        aParams[ aParams.length ] = 'mode=1';

        aParams[ aParams.length ] = 'user_name=' + encodeURIComponent( loginCheck.hLoginUserName.value );
        aParams[ aParams.length ] = 'password=' + encodeURIComponent( loginCheck.hLoginPassword.value );

        loginCheck.call( loginCheck.asyncUrl, aParams.join('&'), function( strResponse )
        {
          loginCheck.wait( false );
          loginCheck.hLoginBox.style.display = 'block';

          if ( strResponse )
          {
            try
            {
              var oResponse = eval( '(' + strResponse + ')' );
            }

            catch ( e )
            {
              var oResponse = null;
            }


            if ( oResponse != null && typeof oResponse == 'object' )
            {
              if ( oResponse.success )
              {
                if ( oResponse.data.is_logged )
                {
                  loginCheck.bIsLogged = true;
                  loginCheck.hLoginBox.style.display = 'none';
                  loginCheck.wait( true );

                  if ( /\blogin_check_reload\b/.test(loginCheck.hCurrentLink.className) )
                    location.reload( true );

                  else
                  {
                    location.href = loginCheck.hCurrentLink.href;
                  }
                }

                else
                {
                  loginCheck.bIsLogged = false;
                  alert( oResponse.description );
                }

                return true;
              }

              else
                alert( oResponse.description );

            }

            else
              alert( 'Il server ha risposto con dei dati corrotti: riprovare più tardi.' );
          }

          else
            alert( 'Il server ha risposto in modo inatteso oppure la connessione è stata annullata: riprovare più tardi.' );


          return false;
        });
      }

      return false;
    }

  };