// Generated by CoffeeScript 1.12.8
(function() {
  var sendUrl, tryssl;

  tryssl = function() {
    var e, path;
    path = window.location.pathname;
    console.log('path -> ', path);
    console.log('Call URL -> ', window.datas.sslHost);
    e = jQuery.Event("sslAttempt");
    $(document).trigger(e);
    if (!e.isDefaultPrevented()) {
      $.ajax(window.datas.sslHost, {
        dataType: 'json',
        xhrFields: {
          withCredentials: true
        },
        success: function(data) {
          console.log('Success -> ', data);
          e = jQuery.Event("sslSuccess");
          $(document).trigger(e, [data]);
          if (!e.isDefaultPrevented()) {
            if (data.ajax_auth_token) {
              $('#lform').find('input[name="ajax_auth_token"]').attr("value", data.ajax_auth_token);
            }
            return sendUrl(path);
          }
        },
        error: function(result) {
          console.log('Error during AJAX SSL authentication', result);
          e = jQuery.Event("sslFailure");
          $(document).trigger(e, [result]);
          if (!e.isDefaultPrevented()) {
            if (result.status === 0) {
              sendUrl(path);
            }
            if (result.responseJSON && 'error' in result.responseJSON && result.responseJSON.error === "9") {
              sendUrl(path);
            }
            if (result.responseJSON && 'html' in result.responseJSON) {
              $('#errormsg').html(result.responseJSON.html);
              return $(window).trigger('load');
            }
          }
        }
      });
    }
    return false;
  };

  sendUrl = function(path) {
    var form_url;
    form_url = $('#lform').attr('action');
    if (form_url.match(/^#$/)) {
      form_url = path;
    } else {
      form_url = form_url + path;
    }
    console.log('form action URL -> ', form_url);
    $('#lform').attr('action', form_url);
    return $('#lform').submit();
  };

  $(document).ready(function() {
    return $('.sslclick').on('click', tryssl);
  });

}).call(this);
