﻿var Login =
{
    _minUserNameLength: 4,
    _minPasswordLength: 4,

    init: function() {
        $.validator.addMethod('userName',
                            function(value, element) {
                                return /^([a-zA-Z])[a-zA-Z_-]*[\w_-]*[\S]$|^([a-zA-Z])[0-9_-]*[\S]$|^[a-zA-Z]*[\S]$/.test(value);
                            },
                            'Der Benutzername enthält ungültige Zeichen.'
                     );
        $('#frmOpenIdLogin').validate(
                {
                    rules: {
                        identifier: 'required'
                    },
                    messages:
                            {
                                identifier: 'Open ID darf nicht leer sein.'
                            },
                    errorPlacement: onErrorPlacement,
                    highlight: onHighlight,
                    unhighlight: onUnhighlight
                }
            );

        $('#frmLogin').validate(
                                    {
                                        rules: {
                                            userName: 'required',
                                            password: 'required'
                                        },
                                        messages:
                                                {
                                                    userName: 'Benutzername darf nicht leer sein.',
                                                    password: 'Passwort darf nicht leer sein.'
                                                },
                                        submitHandler: function(form) {
                                            var options = {
                                                dataType: 'json',
                                                beforeSubmit: function() {
                                                    $('#loginMessage').hide().text('').css('color', '');

                                                    $U.disableInputs('#loginBox', true);
                                                    $U.showProgress('Authentifizierung...', '#chkLoginRememberMe');

                                                },
                                                success: function(result) {
                                                    $U.hideProgress();
                                                    if (result.isSuccessful) {
                                                        window.location = result.redirectUrl;
                                                    }
                                                    else {
                                                        $U.disableInputs('#loginBox', false);
                                                        $('#loginMessage').text(result.errorMessage).css({ color: '#ff0000', display: 'block' });
                                                        $U.focus('txtLoginUserName');
                                                    }
                                                }
                                            };
                                            $(form).ajaxSubmit(options);
                                            return false;
                                        },
                                        errorPlacement: onErrorPlacement,
                                        highlight: onHighlight,
                                        unhighlight: onUnhighlight
                                    }
                            );
        function onErrorPlacement(error, element) {
            element.next('span.error').text(error.text());
        }

        function onHighlight(element, errorClass) {
            $(element).next('span.error').show();
        }

        function onUnhighlight(element, errorClass) {
            $(element).next('span.error').hide();
        }
    },

    dispose: function() {
        $('#loginBox').unbind();
    },

    _show: function() {
        $U.blockUI();

        var modal = $('#loginBox');
        $U.center(modal);
        modal.fadeIn();

        modal.find('input').each(
                                function() {
                                    var input = $(this)[0];
                                    var type = input.type.toLowerCase();

                                    if ((type == 'text') || (type == 'file') || (type == 'password')) {
                                        input.value = '';
                                    }
                                    else if ((type == 'checkbox') || (type == 'radio')) {
                                        input.checked = false;
                                    }
                                }
                            );

        $('span.error').hide();
        $('span.message').hide();
    }
}