$(document).ready(function() {
    $('#btn_loginsubmit').click(function() {
      validateLoginForm();
    });
  
    $("#username, #password").keyup(function(e) {
      if (e.keyCode == 13) {
        validateLoginForm();
      }
    });
});

function validateLoginForm() {
    $.ajax({
      type: 'POST',
      url: 'ajax/validatelogin.php',
      data: {
        username: $('#username').val(),
        password: $('#password').val(),
        remember: ($('#remember').prop('checked') ? "1" : "0")
      },
      dataType: 'json',
      success: function(data, stat, jqo) {
        if (data.status == "valid") {
          window.location.replace('index.php');
        } else {
          toastr.error("Invalid username or password", "Account Error");
          $('#username').focus();
        }
      },
      error: function(jqo, status, error) {
        toastr.error("Server error validating credentials!\n" + error, "Server Error");
        //console.log(error);
      }
    });
}
