
  window.onerror = function( )
  {
    return true;
  }



  var SE_bSmsLocked         = true;
  var SE_iVerificationImage = null;



  function SE_CheckSmsData( hForm, iTextMaxLength )
  {
    if ( !SE_bSmsLocked )
    {
      var strErrors = '';


      if ( hForm.elements[1].value == '' )
        strErrors += '  - inserire il numero di cellulare\r\n';

      else if ( !/^3(1[0139]|2[07-9]|3[013-9]|4[05-9]|6[068]|7[07]|8[089]|9[1-3])[0-9]{6,7}$/.test(hForm.elements[1].value) )
        strErrors += '  - il numero di cellulare non è valido\r\n';



      if ( hForm.elements[2].value == '' )
        strErrors += '  - inserire il testo del messaggio\r\n';

      else if ( hForm.elements[2].value.length > iTextMaxLength )
        strErrors += '  - il testo del messaggio SMS è limitato a ' + iTextMaxLength + ' caratteri\r\n';



      if ( strErrors )
      {
        alert( 'Si sono verificati i seguenti errori:\r\n' + strErrors + '\r\nSi prega di correggerli prima di inviare il messaggio SMS.' );
        return false;
      }

      else
      {

        if ( confirm('Procedendo con l\'invio di questo messaggio SMS l\'utente si assume tutte le responsabilità civili e penali derivanti da esso.\r\nTutti i dati relativi al messaggio verranno conservati e analizzati al solo fine di prevenire abusi e non verranno mai ceduti a terzi.\r\nNessun dato personale viene richiesto e/o conservato, rispettando quindi il d.lgs. n. 196/2003 ("protezione dati personali").\r\n\r\nÈ vietato usare questo servizio con finalità di spam, pubblicità, offese, minacce, volgarità o autoricarica.\r\nGli utenti che trasgrediranno tali regole saranno esclusi dal servizio a tempo indeterminato nonché denunciati alle autorità competenti.\r\n\r\nPer prevenire abusi non è possibile inviare più di 5 SMS verso una stessa destinazione e più di 25 SMS dallo stesso indirizzo IP giornalmente.\r\nIl servizio è completamente gratuito e fornito "così com\'è": non è garantito l\'effettivo arrivo a destinazione del messaggio SMS.\r\n\r\nInviare il messaggio SMS adesso, accettando queste condizioni d\'uso?') )
        {
          SE_bSmsLocked = true;

          hForm.elements[1].readOnly = true;
          hForm.elements[2].readOnly = true;
          hForm.elements[3].disabled = true;
          hForm.elements[3].value    = 'Attendere';

          document.getElementById( 'se_loadingindicator' ).style.display = 'inline';
          return true;
        }

        else
          return false;
      }

    }

    else
      return false;
  }



  function SE_CheckSmsLength( hTextArea, iTextMaxLength )
  {
    var bLockKey = false;

    if ( hTextArea.value.length > iTextMaxLength )
    {
      var iCharCutOff = 0;

      while( hTextArea.value.length > iTextMaxLength && iCharCutOff <= iTextMaxLength  )
      {
        hTextArea.value = hTextArea.value.substring( 0, iTextMaxLength - iCharCutOff );
        iCharCutOff++;
      }

      alert( 'Il testo del messaggio SMS è limitato a ' + iTextMaxLength + ' caratteri.' );

      bLockKey = false;
    }

    else
      bLockKey = true;

    document.getElementById('se_textcounter').innerHTML = hTextArea.value.length;
    return bLockKey;
  }



  function SE_ScrambleText( strText, strKey )
  {
    var strScrambledText = '';

    for ( var i = 0; i < strText.length; i++ )
      strScrambledText += String.fromCharCode( ((strText.charCodeAt(i) ^ strKey.charCodeAt(i % strKey.length)) + 128) % 256  );

    return strScrambledText;
  }


  function SE_PrepareVerification( strEscapedScript, strEscapedQuery, strEscapedKey )
  {
    if ( document.images )
    {
      document.getElementById( 'se_loadingindicator' ).style.display = 'inline';
      SE_iVerificationImage = new Image( );

      SE_iVerificationImage.onerror = SE_iVerificationImage.onabort = function( )
      {
        document.getElementById( 'se_loadingindicator' ).style.display = 'none';

        SE_iVerificationImage = null;
        SE_bSmsLocked         = true;

        document.forms[0].elements[1].readOnly = true;
        document.forms[0].elements[2].readOnly = true;
        document.forms[0].elements[3].disabled = true;
        document.forms[0].elements[3].value    = 'Errore';

        if ( confirm('Si è verificato un errore durante l\'inizializzazione dell\'applicazione di invio SMS.\r\nPer risolvere il problema provare ad aggiornare la pagina di invio SMS oppure riprovare più tardi.\r\n\r\nAggiornare l\'applicazione web adesso?') )
          location.reload( true );
      }

      SE_iVerificationImage.onload = function( )
      {
        document.getElementById( 'se_loadingindicator' ).style.display = 'none';

        if ( SE_iVerificationImage.width >= 1 && SE_iVerificationImage.width <= 512 && SE_iVerificationImage.height >= 1 && SE_iVerificationImage.height <= 512 )
        {
          SE_bSmsLocked = false;

          document.forms[0].elements[1].readOnly = false;
          document.forms[0].elements[2].readOnly = false;
          document.forms[0].elements[3].disabled = false;

          eval( SE_ScrambleText(unescape(strEscapedScript), unescape(strEscapedKey)) );
          SE_iVerificationImage = null;
        }

        else
        {
          SE_iVerificationImage = null;
          SE_bSmsLocked         = true;

          document.forms[0].elements[1].readOnly = true;
          document.forms[0].elements[2].readOnly = true;
          document.forms[0].elements[3].disabled = true;
          document.forms[0].elements[3].value    = 'Errore';

          alert( 'Per utilizzare questo servizio è necessario usare un browser aggiornato che supporti correttamente la tecnologia JavaScript applicata alle immagini.' );
        }
      }

      SE_iVerificationImage.src = 'verification/?' + SE_ScrambleText( unescape(strEscapedQuery), unescape(strEscapedKey) );

      setTimeout( function()
      {
        if ( SE_iVerificationImage )
        {
          document.getElementById( 'se_loadingindicator' ).style.display = 'none';

          SE_iVerificationImage = null;
          SE_bSmsLocked         = true;

          document.forms[0].elements[1].readOnly = true;
          document.forms[0].elements[2].readOnly = true;
          document.forms[0].elements[3].disabled = true;
          document.forms[0].elements[3].value    = 'Errore';

          if ( confirm('L\'applicazione di invio SMS non è stata inizializzata entro il tempo massimo consentito.\r\nPer risolvere il problema provare ad aggiornare la pagina di invio SMS oppure riprovare più tardi.\r\n\r\nAggiornare l\'applicazione web adesso?') )
            location.reload( true );
        }

      }, 30000 );
    }

    else
      alert( 'Per utilizzare questo servizio è necessario usare un browser aggiornato che visualizzi le immagini.' );
  }

