﻿(function($){
    function validateLogin() {
        var email = $('#txtEmail');
        var password = $('#txtPassword');
        
        if (email.val() == null || email.val() == '') {
            alert('Vui lòng nhập email');
            email.focus();
            return false;
        }
        
        if (password.val() == null || password.val() == '') {
            alert('Vui lòng nhập mật khẩu');
            password.focus();
            return false;
        }
        
        return true;
    }
    
    function processEnter(e) {
        var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
        var target = e.target.tagName.toLowerCase();
            if (key === 13 && target === 'input') {
                e.preventDefault();            
            $('#btnLogin').trigger('click');
        }
    }
    
    $(document).ready(function() {
        $('#txtEmail').focus();
        
        $('#btnLogin').click(function() {
            if (validateLogin()) {
                $('#formAction').val('login');
                document.forms[0].submit();
                return false;
            }            
            return false;
        });
        
        $('#txtEmail').keypress(function(e){
            processEnter(e)
        });
        
        $('#txtPassword').keypress(function(e){
            processEnter(e)
        });
        
        if (typeof errMsg != 'undefined') {
            if (errMsg != '')
                alert(errMsg);
        }
    });
})(jQuery);


