<? global $lang;?>
<div id="newsletter">
  <form method="POST" data-toggle="validator" role="form" id="newsletter-form">
    <div class="form-group">
      <label for="newsletter-email"><?= ucfirst( $lang['email'] );?> <span class='red'>*</span>:</label>
      <input type='email' id="email" name="email" class="form-control" required />
      <div class="help-block with-errors"></div>
    </div>
    <div class="form-group">
      <input type="checkbox" id="agreement" /> <?= $lang['newsletter-zgoda'];?>
    </div>
    <div class="form-group clearfix">
      <a href="#" class="btn btn-primary" id="newsletter-signin">Zapisz się</a>
    </div>
    <div class="form-group">
      <span id="first" class="bold"><?= $lang['newsletter-administrator-danych-1'];?></span> <span id="second" class="hidden"><?= $lang['newsletter-administrator-danych-2'];?></span><a href="#" id="toggle-data"><?= $lang['rozwin'];?></a>
    </div>
  </form>
</div>
<script type="text/javascript">
  $( function()
  {
    $( 'body' ).on( 'click', '#newsletter-form #toggle-data', function()
    {
      if ( $( '#newsletter-form #second' ).hasClass( 'hidden' ) )
      {
        $( '#newsletter-form #second' ).removeClass( 'hidden' );
        $( '#newsletter-form #toggle-data' ).html( '<?= $lang['zwin'];?>' );
      }
      else
      {
        $( '#newsletter-form #second' ).addClass( 'hidden' );
        $( '#newsletter-form #toggle-data' ).html( '<?= $lang['rozwin'];?>' );
      }
      return false;
    });
    
    $( 'body' ).on( 'click', '#newsletter-form #newsletter-signin', function()
    {
      if ( !$( '#newsletter-form #agreement' ).is( ':checked' ) )
      {
        $.alert(
        {
          title: '<?= ucfirst( $lang['blad'] );?>',
          content: '<?= $lang['newsletter-zgoda-komunikat'];?>',
          type: 'orange',
          closeIcon: true,
          closeIconClass: 'fa fa-close',
          typeAnimated: true,
          animation: 'opacity',
          autoClose: 'confirm|10000',
          columnClass: 'col-sm-6 col-sm-offset-3',
          buttons: 
          {
            confirm: 
            {
              text: '<?= $lang['zamknij'];?>',
              btnClass: 'btn-blue',
              keys: ['enter'],
              action: function() {}
            }
          }
        });
        return false;
      }
    
      var email   = $.trim( $( '#newsletter-form #email' ).val() );
      var token   = $.trim( $( '#newsletter-form #token' ).val() );

      if ( email === '' )
      {
        $.alert(
        {
          title: '<?= ucfirst( $lang['blad'] );?>',
          content: '<?= $lang['newsletter-prosze-uzupelnic-email'];?>',
          type: 'orange',
          closeIcon: true,
          closeIconClass: 'fa fa-close',
          typeAnimated: true,
          animation: 'opacity',
          autoClose: 'confirm|10000',
          columnClass: 'col-sm-6 col-sm-offset-3',
          buttons: 
          {
            confirm: 
            {
              text: '<?= $lang['zamknij'];?>',
              btnClass: 'btn-blue',
              keys: ['enter'],
              action: function() {}
            }
          }
        });
        return false;
      }
      
      $.ajax(
      {
        type: 'POST',
        cache: false,
        url: '/newsletter/signin/',
        data: 
        {
          email: email
        },
        beforeSend: function() 
        {
          alert1 = $.alert(
          {
            title: '<?= $lang['informacja'];?>',
            content: '<?= $lang['prosze-czekac'];?>',
            type: 'blue',
            closeIcon: true,
            closeIconClass: 'fa fa-close',
            typeAnimated: true,
            animation: 'opacity',
            autoClose: 'confirm|60000',
            columnClass: 'col-sm-6 col-sm-offset-3',
            buttons: 
            {
              confirm: 
              {
                text: '<?= $lang['zamknij'];?>',
                btnClass: 'btn-blue',
                keys: ['enter'],
                action: function() {}
              }
            }
          });
        },
        success: function( data ) 
        {
          response = jQuery.parseJSON( data );

          $( '.jqibox' ).remove();
  
          if ( response.status === 'ok' )
            var msg = '<?= $lang['newsletter-potwierdzenie-dodania'];?>';
          else
            var msg = '<?= $lang['newsletter-potwierdzenie-dodania-blad'];?>';

          alert1.close();

          $.alert(
          {
            title: '<?= $lang['informacja'];?>',
            content: msg,
            type: 'blue',
            closeIcon: true,
            closeIconClass: 'fa fa-close',
            typeAnimated: true,
            animation: 'opacity',
            autoClose: 'confirm|10000',
            columnClass: 'col-sm-6 col-sm-offset-3',
            buttons: 
            {
              confirm: 
              {
                text: '<?= $lang['zamknij'];?>',
                btnClass: 'btn-blue',
                keys: ['enter'],
                action: function() {}
              }
            }
          });
        }
      });
    });
  });
</script>
