'use strict';

/*
 * AJAX request for handling error
 * messages displayed inside modals.
 */

$(function() {
  // Bind to the submit event on the login form
  $('body').on('submit', '#loginForm', function(e) {
    // Store the form
    var $form = $(this);

    // Disable inputs for the duration of the AJAX request
    var $inputs = $form.find('input, button');
    var serializedData = $form.serialize();
    $inputs.prop('disabled', true);

    // Send request
    var request = $.post('/login', serializedData, function(response) {
      console.log('Response: ' + response);
    });

    // Callback handler called on success
    // 
    // This may actually be the worst thing I have ever done
    request.done(function(response, textStatus, jqXHR) {
      if (response === 'Incorrect username/password combination.') {
        $('#password-error').html(response);
      } else {
        window.location.replace('/');
      }
    });

    // Callback handler called on failure
    request.fail(function(jqXHR, textStatus, errorThrown) {
      console.log('An error occurred: ' + errorThrown);
    });

    // Callback handler called no matter what
    request.always(function() {
      // Reenable the form inputs
      $inputs.prop('disabled', false);
    });

    // Prevent default handling of form
    e.preventDefault();
  });
});

/*
 * Open all external links in new windows.
 */

$(function() {
  $('a').each(function() {
    var a = new RegExp('/' + window.location.host + '/');
    if (!a.test(this.href)) {
      $(this).click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        window.open(this.href, '_blank');
      });
    }
  });
});


@@ -8,21 +8,36 @@
 rict';%0A%0A
-/*%0A *
+$(function() %7B%0A%0A  //
  AJAX re
@@ -62,17 +62,19 @@
  error%0A 
-*
+ //
  message
@@ -100,35 +100,16 @@
 modals.%0A
- */%0A%0A$(function() %7B
 %0A  // Bi
@@ -1335,18 +1335,13 @@
 %7D);%0A
-%7D);%0A%0A/*%0A *
+%0A  //
  Ope
@@ -1381,27 +1381,8 @@
 ws.%0A
- */%0A%0A$(function() %7B
 %0A  $

refactor public js