Login = {
    Initialize : function() {
        
        $("#userName").focus();
        
        $('#loginButton').bind('click', function(event) {
            event.preventDefault();
            
            $.ajax({
                type: "POST",
                url: "Components/Authentication/AuthenticationActionHandler.php",
                data: "action=Login&userName=" + $('#userName').val() + "&password=" + $('#password').val(),
                dataType: "json",
                success: function(response) {
                    if(response.HasError) { $("#loginErrorMessage").html(response.Message); $("#userName").focus(); }
                    else { $("#loginErrorMessage").empty();
                        window.location = window.location;
                    }
                }
            });
        });
    }
};

Logout = {
    Initialize : function() {
        // Logout button click
        $('#logoutButton').bind('click', function(event) {
            event.preventDefault();
            
            $.ajax({
                type: "POST",
                url: "Components/Authentication/AuthenticationActionHandler.php",
                data: "action=Logout",
                dataType: "json",
                success: function(response) { window.location = response.Redirect; }
            });
        });
        
        // Change password button click
        $('#changePasswordButton').bind('click', function(event) {
            event.preventDefault();
            $('#changePasswordForm').slideToggle(200, function() { $('#password1').focus(); } );
        });
        
        // Change password button click
        $('#doPasswordChangeButton').bind('click', function(event) {
            event.preventDefault();
            
            if($("#password1").val() != $("#password2").val())
            {
                $("#passwordChangeErrorMessage").html("A két jelszó nem egyezik");
                $('#password2').focus();
            }
            else if( $("#password1").val() == "" )
            {
                $("#passwordChangeErrorMessage").html("Nem adtál meg jelszót");
                $('#password1').focus();
            }
            else
            {
                $.ajax({
                    type: "POST",
                    url: "Components/Authentication/AuthenticationActionHandler.php",
                    data: "action=ChangePassword&pwd=" + $("#password1").val(),
                    dataType: "json",
                    success: function(response) {
                        $("#password1").val("");
                        $("#password2").val("");
                        $('#changePasswordForm').slideToggle(200);
                    }
                });
            }
        });
        
    }
};
