//function crypt_data(){var e=$("#username").val(),r=$("#password").val(),a=crypt.encrypt(e),n=crypt.encrypt(r);$("#enc_username").val(a),$("#enc_password").val(n)}function decrypt_data(){var e=$("#enc_username").val(),r=$("#enc_password").val(),a=crypt.decrypt(e),n=crypt.decrypt(r);$("#username").val(a),$("#password").val(n)}$(function(){"use strict";function e(e){e.preventDefault();var r=GlobalVariables.baseUrl+"/index.php/user/ajax_check_login",a="";$("#remember").prop("checked")&&(a="on"),crypt_data();var n={csrfToken:GlobalVariables.csrfToken,username:$("#username").val(),password:$("#password").val(),enc_username:$("#enc_username").val(),enc_password:$("#enc_password").val(),remember:a},t=$(".alert");t.addClass("d-none"),$.post(r,n).done(function(e){e===GlobalVariables.AJAX_SUCCESS?window.location.href=GlobalVariables.destUrl:(t.text(EALang.login_failed),t.removeClass("d-none alert-danger alert-success").addClass("alert-danger"))})}var r=$("#login-form");r.on("submit",e)}),$(document).ready(function(){""!=$("#enc_username").val()&&""!=$("#enc_password").val()&&decrypt_data()});var crypt={secret:"cookie",encrypt:function(e){var r=CryptoJS.AES.encrypt(e,crypt.secret);return r=r.toString(),r},decrypt:function(e){var r=CryptoJS.AES.decrypt(e,crypt.secret);return r=r.toString(CryptoJS.enc.Utf8),r}};
$(function () {
    'use strict';

    var $loginForm = $('#login-form');

    /**
     * Login Button "Click"
     *
     * Make an ajax call to the server and check whether the user's credentials are right.
     * If yes then redirect him to his desired page, otherwise display a message.
     */
    function onLoginFormSubmit(event) {
        event.preventDefault();

        var url = GlobalVariables.baseUrl + '/index.php/user/ajax_check_login';
        
        var remember_me = "";
        if($('#remember').prop("checked")){
            remember_me = "on";
         }

          crypt_data();

        var data = {
            'csrfToken': GlobalVariables.csrfToken,
            'username': $('#username').val(),
            'password': $('#password').val(),
			'login_type': $('#login_type').val(),
            'enc_username': $('#enc_username').val(),
            'enc_password': $('#enc_password').val(),
            'remember': remember_me
        };

        var $alert = $('.alert');

        $alert.addClass('d-none');

        $.post(url, data)
            .done(function (response) {
                if (response === GlobalVariables.AJAX_SUCCESS) {
                    window.location.href = GlobalVariables.destUrl;
                } else {
                    $alert.text(EALang['login_failed']);
                    $alert
                        .removeClass('d-none alert-danger alert-success')
                        .addClass('alert-danger');
                }
            });
    }

    $loginForm.on('submit', onLoginFormSubmit);
});

$(document).ready(function(){
    if($("#enc_username").val() != "" && $("#enc_password").val() != ""){
      decrypt_data();
    }
 });

var crypt = {
    secret: "cookie",
  encrypt: function (clear){
    var cipher = CryptoJS.AES.encrypt(clear, crypt.secret);
    cipher = cipher.toString();
    return cipher;
  },
  decrypt: function (cipher){
    var decipher = CryptoJS.AES.decrypt(cipher, crypt.secret);
    decipher = decipher.toString(CryptoJS.enc.Utf8);
    return decipher;
  }
};

function crypt_data(){
 var username = $("#username").val();
 var password = $("#password").val();
 var enc_username = crypt.encrypt(username);
 var enc_password = crypt.encrypt(password);
 $("#enc_username").val(enc_username);
 $("#enc_password").val(enc_password);
}

function decrypt_data(){
 var enc_username = $("#enc_username").val();
 var enc_password = $("#enc_password").val();
 var username = crypt.decrypt(enc_username);
 var password = crypt.decrypt(enc_password);
 $("#username").val(username);
 $("#password").val(password);
}
