
  window.onerror = function( )
  {
    return true;
  }


  var GSE_IsFormLocked = false;


  function GSE_CheckStep1( hForm, hMsisdnField, hTextField, iMaxMessageLength )
  {
    if ( !GSE_IsFormLocked )
    {
      var strDetectedErrors = '';

      if ( hMsisdnField.value == '' )
        strDetectedErrors += '  - inserire il numero di cellulare\r\n';

      else if ( !/^3(1[0139]|2[07-9]|3[013-9]|4[06-9]|6[068]|7[07]|8[089]|9[1-3])\d{6,7}$/.test(hMsisdnField.value) )
        strDetectedErrors += '  - il numero di cellulare non \xe8 valido\r\n';


      if ( hTextField.value == '' )
        strDetectedErrors += '  - inserire il testo del messaggio\r\n';

      else if ( GSE_GetSmsLength(hTextField.value) > iMaxMessageLength  )
        strDetectedErrors += '  - il testo del messaggio SMS \xe8 limitato a ' + iMaxMessageLength  + ' caratteri\r\n';



      if ( strDetectedErrors )
        alert( 'Si sono verificati i seguenti errori:\r\n' + strDetectedErrors + '\r\nSi prega di correggerli prima di inviare il messaggio SMS.' );

      else
      {
        GSE_IsFormLocked = true;
        return true;
      }
    }

    else
      alert( 'Si prega di attendere: il messaggio SMS \xe8 in fase di verifica...' );

    return false;
  }



  function GSE_CheckStep2( hForm )
  {
    if ( !GSE_IsFormLocked )
    {
      GSE_IsFormLocked = true;
      return true;
    }

    else
      alert( 'Si prega di attendere: il messaggio SMS \xe8 in fase di conferma...' );

    return false;
  }



  function GSE_CheckStep3( hForm )
  {
    if ( !GSE_IsFormLocked )
    {
      document.getElementById('GSE_bannerContainer').style.display = 'none';
      document.getElementById('GSE_animationContainer').style.display = 'block';

      GSE_IsFormLocked = true;
      return true;
    }

    else
      alert( 'Si prega di attendere: il messaggio SMS \xe8 in fase di invio...' );

    return false;
  }


  function GSE_SubmitStep3( )
  {
    if ( GSE_CheckStep3(document.forms['GSE_smsFormNext']) )
      document.forms['GSE_smsFormNext'].submit( );

    return true;
  }
  
  
  function GSE_GetSmsLength( strMessage )
  {
    var iLength = 0;
  
    for ( var i = 0; i < strMessage.length; i++ )
    {
      iLength++;
            
      if ( /[\r^{}\\\[~\]|€]/.test(strMessage.charAt(i)) )
        iLength++;
    }
  
    return iLength;
  }


  function GSE_CheckSmsLength( hTextField, hUsedCharsElement, iMaxMessageLength )
  {
    var bCanUserType = false;

    if ( GSE_GetSmsLength(hTextField.value) > iMaxMessageLength )
    {
      var iCuttedChars = 0;

      while ( GSE_GetSmsLength(hTextField.value) > iMaxMessageLength && iCuttedChars <= iMaxMessageLength )
      {
        hTextField.value = hTextField.value.substring( 0, iMaxMessageLength-iCuttedChars );
        iCuttedChars++;
      }

      alert( 'Il testo del messaggio SMS \xe8 limitato a ' + iMaxMessageLength + ' caratteri.' );
    }

    else
      bCanUserType = true;

    if ( hUsedCharsElement != null )
      hUsedCharsElement.innerHTML = GSE_GetSmsLength( hTextField.value );

    return bCanUserType;
  }


  function GSE_CheckTimeCountdown( hTimeoutElement )
  {
    if ( hTimeoutElement.innerHTML <= 0 )
    {
      location.reload( true );
      return true;
    }

    else
    {
      hTimeoutElement.innerHTML--;
      setTimeout( function() { GSE_CheckTimeCountdown(hTimeoutElement) }, 1000 );
    }

    return false;
  }



