<?
global $gClient, $lang, $accessToken, $helper, $redirectURL, $fbPermissions;
if ( !$gClient -> getAccessToken() )
{
  $authUrl    = $gClient -> createAuthUrl();
  $btn_google = '<a href="' . str_replace( 'g_register%3D0', 'g_register%3D1', filter_var( $authUrl, FILTER_SANITIZE_URL ) ) . '" class="google-login">' . ucfirst( $lang['zarejestruj-sie'] ) . '</a>';
}

if ( !$accessToken )
{
  $loginURL     = $helper -> getLoginUrl( $redirectURL, $fbPermissions );
	$btn_facebook = '<a href="' . str_replace( 'f_register%3D0', 'f_register%3D1', htmlspecialchars( $loginURL ) ) . '" class="facebook-login">' . ucfirst( $lang['zarejestruj-sie'] ) . '</a>';
}
?>
<div id="user-register">
  <div class="mini-buttons" id="buttons">
    <div class="btn btn-user">
      <?= $lang['rejestracja-jako-kandydat'];?>
    </div>
    <div class="btn btn-firm">
      <?= $lang['rejestracja-jako-pracodawca'];?>
    </div>
  </div>
  <div class="mini-box">
    <div class="title">rejestracja jako kandydat</div>
    <div class="container-fluid">
      <div class="row">
        <div class="col-12">
          <div class="content user">
            <form method="POST" action="/" id="f-user-register">
              <input type="hidden" id="type" name="type" value="0">
              <div class="form-group row">
                <label form="email" class="col-12 col-sm-4"><?= ucfirst( $lang['email'] );?>:</label>
                <div class="col-12 col-sm-8">
                  <input type="email" required id="email" class="form-control">
                </div>
              </div>
              <div class="form-group row">
                <label form="password" class="col-12 col-sm-4"><?= ucfirst( $lang['haslo'] );?> (<?= $lang['min'];?> 6 <?= $lang['znakow'];?>):</label>
                <div class="col-12 col-sm-8">
                  <input type="password" required id="password" pattern="^\S{6,}$" class="form-control">
                </div>
              </div>
              <div class="form-group row">
                <label form="password" class="col-12 col-sm-4"><?= ucfirst( $lang['powtorz-haslo'] );?>:</label>
                <div class="col-12 col-sm-8">
                  <input type="password" required id="password-repeat" pattern="^\S{6,}$" class="form-control" oninput="check_match(this)">
                </div>
              </div>
              <div class="form-group row agremment">
                <div class="col-12 col-sm-8 offset-sm-4">
                  <input type="checkbox" required id="agremment"> *
                  <?
                    $text = str_replace( '[LINK-REGULAMIN]', '<a href="/regulamin">' . $lang['regulamin'] . '</a>', $lang['rejestracja-akceptacja-regulaminu'] );
                    echo $text = str_replace( '[LINK-POLITYKA-PRYWATNOSCI]', '<a href="/polityka-prywatnosci">' . $lang['polityke-prywatnosci'] . '</a>', $text );
                  ?>
                </div>
              </div>
              <div class="form-group row agremment-profile">
                <div class="col-12 col-sm-8 offset-sm-4">
                  <input type="checkbox" id="agremment-profile"> <?= $lang['rejestracja-zgoda-na-wyswietlenie-profilu'];?>
                </div>
              </div>
              <div class="form-group row agremment-marketing">
                <div class="col-12 col-sm-8 offset-sm-4">
                  <input type="checkbox" id="agremment-marketing"> Wyrażam zgodę na kontakt mailowy i/lub telefoniczny  ze strony globelus.pl
                </div>
              </div>
              <div class="form-group row">
                <div class="col-12 col-sm-8 offset-sm-4 mt20">
                  <input type="submit" value="<?= ucfirst( $lang['zarejestruj-sie'] );?>" class="btn btn-success">
                  <? $btn_google . $btn_facebook;?>
                  <div class="clearfix"></div>
                  <span><?= $lang['masz-juz-konto'];?> <a href="/logowanie"><?= ucfirst( $lang['zaloguj-sie'] );?></a></span>
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<script class="footer" type='text/javascript'>
  $( function()
  {
    var hash = document.URL.substr( document.URL.indexOf( '#' ) + 1 );
    if ( hash === 'pracodawca' )
    {
      $( '#user-register .title' ).html( 'rejestracja jako pracodawca' );
      $( '#f-user-register .agremment-profile' ).hide();
      $( '#f-user-register #type' ).val( 1 );
      $( '#f-user-register #agremment-profile' ).removeAttr( 'required' );
    }

    $( 'body' ).on( click_event, '#user-register #buttons .btn', function()
    {
      if ( $( this ).hasClass( 'btn-firm' ) )
      {
        $( '#user-register .title' ).html( 'rejestracja jako pracodawca' );
        $( '#f-user-register .agremment-profile' ).hide();
        $( '#f-user-register #type' ).val( 1 );
        $( '#f-user-register #agremment-profile' ).removeAttr( 'required' );

        var url = $( '.google-login' ).attr( 'href' ).toString();
        url = url.replace( 'g_type%3D0', 'g_type%3D1' );
        $( '.google-login' ).attr( 'href', url );

        var url_fb = $( '.facebook-login' ).attr( 'href' ).toString();
        url_fb = url_fb.replace( 'f_type%3D0', 'f_type%3D1' );
        $( '.facebook-login' ).attr( 'href', url_fb );
      }

      if ( $( this ).hasClass( 'btn-user' ) )
      {
        $( '#user-register .title' ).html( 'rejestracja jako kandydat' );
        $( '#f-user-register .agremment-profile' ).show();
        $( '#f-user-register #type' ).val( 0 );

        $( '#f-user-register #agremment-profile' ).attr( 'required', 'required' );
        var url = $( '.google-login' ).attr( 'href' );
        url = url.replace( 'g_type%3D1', 'g_type%3D0' );
        $( '.google-login' ).attr( 'href', url );

        var url_fb = $( '.facebook-login' ).attr( 'href' ).toString();
        url_fb = url_fb.replace( 'f_type%3D1', 'f_type%3D0' );
        $( '.facebook-login' ).attr( 'href', url_fb );
      }
    });

    $( 'body' ).on( 'submit', '#f-user-register', function(e)
    {
      e.preventDefault();

      $.ajax(
      {
        type: 'POST',
        cache: false,
        url: '/globelusUser/signup/',
        data:
        {
          email: $( '#f-user-register #email' ).val(),
          password: $( '#f-user-register #password' ).val(),
          type: $( '#f-user-register #type' ).val(),
          agremment_profile: $( '#f-user-register #agremment-profile' ).val(),
          agremment_marketing: $( '#f-user-register #agremment-marketing' ).val()
        },
        beforeSend: function() {

        },
        success: function( data )
        {
          response = jQuery.parseJSON( data );

          if ( response.status == 'bad2' )
          {
            $.alert(
            {
              title: '<?= ucfirst( $lang['informacja'] );?>',
              icon: 'fa fa-exclamation',
              content: response.msg,
              type: 'blue',
              closeIcon: true,
              closeIconClass: 'fa fa-close',
              typeAnimated: true,
              animation: 'opacity',
              autoClose: 'confirm|10000',
              columnClass: 'large',
              theme: 'modern',
              buttons:
              {
                confirm:
                {
                  text: 'Zaloguj się',
                  btnClass: 'btn-blue',
                  keys: ['enter'],
                  action: function() {
                    document.location.href = '/logowanie';
                  }
                }
              },
              onClose: function ()
              {
                if ( response.status === 'ok' )
                  document.location.href = '/logowanie';
              }
            });
          }
          else
          {
            $.alert(
            {
              title: '<?= ucfirst( $lang['informacja'] );?>',
              icon: 'fa fa-exclamation',
              content: response.msg,
              type: 'blue',
              closeIcon: true,
              closeIconClass: 'fa fa-close',
              typeAnimated: true,
              animation: 'opacity',
              autoClose: 'confirm|10000',
              columnClass: 'large',
              theme: 'modern',
              buttons:
              {
                confirm:
                {
                  text: '<?= $lang['zamknij'];?>',
                  btnClass: 'btn-blue',
                  keys: ['enter'],
                  action: function() {}
                }
              },
              onClose: function ()
              {
                if ( response.status === 'ok' )
                  document.location.href = '/logowanie';
              }
            });
          }
        }
      });

      return false;
    });
  });

  function check_match(input)
  {
    if ( input.value !== document.getElementById( 'password' ).value )
      input.setCustomValidity( '<?= $lang['podane-hasla-sa-rozne' ];?>');
    else
      input.setCustomValidity('');
  }
</script>