/*
* Logging in to the system
*/


'use strict';


var endpoint = '/sys/login';


/*
* Log in to System
* @param <data> - {Object} email-password combination
* @param <callback> - {Function} callback(err)
*/
function login(data, callback) {
  reqwest({
    url: endpoint,
    method: 'POST',
    data: data,
    success: function(data) {
      return callback(null, data);
    },
    error: function(err) {
      return callback(err);
    }
  });
}


// Attaching the Submit event
$('#loginForm').submit(function(e) {
  e.preventDefault();
  // Login button
  var btnLogin = $('#btnLogin');
  var loginErrorBanner = $('#loginError');
  // Disabling the button
  $(btnLogin).attr('disabled', 'disabled');
  // Hiding the Error banner
  $(loginErrorBanner).addClass('hidden');
  // Making a login request
  login($(this).serialize(), function(err, data) {
    if (err) {
      // show error message
      $(loginErrorBanner).removeClass('hidden');
      // error occurred. Clear form data
      $('#txtEmail').val('');
      $('#txtPassword').val('');
      // re-enable login button
      $(btnLogin).removeAttr('disabled');
      return;
    }
    // successful login. Redirecting user to user page
    window.location = "/user/";
    return;
  });
});


@@ -993,17 +993,25 @@
  $('
-#txtE
+input%5Bname=e
 mail
+%5D
 ').v
@@ -1031,20 +1031,28 @@
  $('
-#txtP
+input%5Bname=p
 assword
+%5D
 ').v

fix clearing form data if login fails