(function ($) {
    "use strict";
    let $u = $('#username'), $p = $('#password'), $r = $('#remember-me'),
    getItemD = (key, d) => {
        var r = null;
        return r = localStorage.getItem(key), r ? r : d !== undefined ? d : null;
    }

    let r = !!localStorage.getItem('remember-me');
    $r.attr('checked', r);
    if (r) {
        $u.val(getItemD('username', ''));
        $p.val(getItemD('password', ''));
    }
    /*==================================================================
    [ Focus Contact2 ]*/
    $('.input100').each(function () {
        
        $(this).on('blur', function () {
            let that = $(this);
            that.val(that.val().trim())
            if (that.val().length) {
                that.addClass('has-val');
            } else {
                that.removeClass('has-val');
            }
        })
    })

    /*==================================================================
    [ Validate ]*/
    var input = $('.validate-input .input100');

    $('.validate-form').on('submit', function () {
        var check = true;

        for (var i = 0; i < input.length; i++) {
            if (validate(input[i]) == false) {
                showValidate(input[i]);
                check = false;
            }
        }
        if (check) {
            let r = $r.is(':checked');
            localStorage.setItem('remember-me', r)
            if (r) {
                localStorage.setItem('username', $u.val())
                localStorage.setItem('password', $p.val())
            }
            else {
                localStorage.removeItem('username')
                localStorage.removeItem('password')
            }
            $p.val(hex_md5($p.val()));
        }
        return check;
    });


    $('.validate-form .input100').each(function () {
        $(this).focus(function () {
            hideValidate(this);
        });
    });

    function validate(input) {
        input = $(input);
        if (input.attr('type') == 'email' || input.attr('name') == 'email') {
            if (input.val().trim().match(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{1,5}|[0-9]{1,3})(\]?)$/) == null) {
                return false;
            }
        } else {
            if (input.val().trim() == '') {
                return false;
            }
        }
    }

    function showValidate(input) {
        var thisAlert = $(input).parent();
        $(thisAlert).addClass('alert-validate');
    }

    function hideValidate(input) {
        var thisAlert = $(input).parent();
        $(thisAlert).removeClass('alert-validate');
    }


})(jQuery);