﻿App.module("registration", function (exports, require) {

    var App = require("app");
    var Utils = require("utils");

    App.addActions({
        register: function ($el) {
            var form = $el.closest(".form");
            if ($('#indicator').text() == 'Too short') {
                $('.resultsHolder').hide();
                form.data('validator').invalidate({ Password: 'Pasword is too short!' });
                App.displayInfo('Password is too short!');

            } else {
                form.find(".error-message").html('');
                Utils.formPost("register", $el, function (resp, form) {
                    App.displayInfo("registration successful, redirecting...")
                    setTimeout(function () {
                        window.location = resp.url;
                    }, 500);
                });
            }
        },

        clearError: function ($el) {
            var theform = $el.closest(".form");
            theform.find(".error-message").html('');
        },

        suggestUsername: function ($el) {
            App.log("suggestUsername");



            var form = $el.closest(".form"),
                loadingIcon = form.find(".check-availability-loading-icon").show(),
                username = form.find("[name=Username]").val();

            if (!username) {
                form.data('validator').invalidate({ Username: 'Required field cannot be left blank.' });
                App.displayInfo('Required field cannot be left blank.');
                return loadingIcon.hide();
            }

            App.log(username);
            form.find(".error-message").html('');


            App.post("suggestUsername", { Username: username }, function (err, data) {
                loadingIcon.hide();
                if (err) {
                    form.find(".error-message").html(err).show();
                    form.data("validator").invalidate(data);
                    return;
                } else {
                    if (!data.availible) {
                        form.data("validator").invalidate({ Username: data.msg });
                        App.displayInfo(data.msg);
                        $('#suggestionsHolder').show();
                        $("#suggestionsHolder").html("");
                        $("#suggestionsHolder").append("<div class='suggestionsHeading'>Username Suggestions:</div></br>");
                        for (var val in data.usernames) {
                            $("#suggestionsHolder").append("<input type='radio' name='suggestions' class='action radio-suggestions' data-action='setUsername' value='" + data.usernames[val] + "' onclick='javascript:HideRadios()'>" + data.usernames[val] + "</input><br />");
                        }
                        $('.error').hide();
                    } else {
                        loadingIcon.show();
                        App.displayInfo(data.msg);
                        loadingIcon.hide();
                        Utils.hideValidatorMessages(form, ['Username']);
                        $('#suggestionsHolder').hide();
                    }
                }
            });
        },

        setUsername: function ($el) {
            var value = $el.closest('input[name=suggestions]').val();
            $('input[name=Username]').val(value);
            var form = $el.closest(".form");
            Utils.hideValidatorMessages(form, ['Username']);
            return true;
        },

        isUsernameAvailible: function ($el) {
            App.log("n");

            var form = $el.closest(".form"),
                loadingIcon = form.find(".loading-icon").show(),
                username = form.find("[name=Username]").val();

            if (!username)
                return loadingIcon.hide();

            App.log(username);
            form.find(".error-message").html('');

            App.post("isUsernameAvailible", { Username: username }, function (err, data) {
                loadingIcon.hide();
                if (err) {
                    form.find(".error-message").html(err).show();
                    form.data("validator").invalidate(data);
                    return;
                } else {
                    if (!data.availible) {
                        form.data("validator").invalidate({ Username: data.msg });
                        App.displayError(data.msg);
                    } else {
                        App.displayInfo(data.msg);
                        Utils.hideValidatorMessages(form, ['Username']);
                    }
                }
            });
        },

        isEmailInUse: function ($el) {
            App.log("isEmailInUse");

            var form = $el.closest(".form"),
                loadingIcon = form.find(".loading-icon").show(),
                email = form.find("[name=Email]").val();

            if (!email)
                return loadingIcon.hide();

            App.log(email);
            form.find(".error-message").html('');

            App.post("isEmailInUse", { Email: email }, function (err, data) {
                loadingIcon.hide();
                if (err) {
                    form.find(".error-message").html(err).show();
                    form.data("validator").invalidate(data);
                    App.displayError(err);
                    return;
                } else {
                    if (data.availible) {
                        form.data("validator").invalidate({ Email: data.msg });
                        App.displayError(data.msg);
                    } else {
                        Utils.hideValidatorMessages(form, ['Email']);
                    }
                }
            });
        },

        resendConfirmation: function ($el) {
            Utils.formPost('ResendConfirmation', $el, function (data) {
                App.closeOverlay();
                App.displayInfo(data.msg);
            });
        },

        forgotPassword: function ($el) {
            Utils.formPost('forgotPassword', $el, function (data) {
                App.closeOverlay();
                App.displayInfo(data.msg);
            });
        }
    })

});
