﻿
$.extend({
    login: new function() {
        var _self = this;

        _self.initialize = function() {
            var form = $("#login-form");

            $("#submit", form).click(function() {
                $.post(
                    form.attr("action"),
                    form.serialize(),
                    function(data, textStatus) {
                        if (textStatus == "success") {
                            if (data.Success) {
                                window.location.href = data.Href;
                            } else {
                                ValidationSummary.show(form, data.ErrorMessages);
                            }
                        }
                    },
                    "json");
            });

            $(form).submit(function() { return false; });

            $("#forgot-name-password").click(function() {
                MessageBox.alert("Forgot user name or password?",
                    'Enter your email address and we will send you an email with your credentials on it.'
                    + '<input type="text" value="" name="email" id="email" class="textFieldAlert"/>'
                    + '<div id="messages-indicator"></div>',
                    { "Submit": function() {
                        var messageBox = $(this);

                        $.ajax({
                            type: 'POST',
                            url: Environment_APP_PATH + 'account/submitusercredential',
                            cache: false,
                            data: { email: $("#email").val() },
                            dataType: 'json',
                            success: function(result) {
                                if (result.Success) {
                                    messageBox.dialog("close");
                                    MessageBox.info("Information", "Your credentials have been sent...");
                                } else {
                                    $("#messages-indicator").html(result.ErrorMessages[0]);
                                }
                            }
                        });
                    }
                    });
            });
        }
    }
});

$(function() {
    $.login.initialize();
});
