﻿(function($) {
    if (typeof $ == "undefined") { return; }
    $.extend(true, Rudder.Modal, {
        SignUp: {
            redirectUrl: "",
            currentUrl: window.location.pathname.toLowerCase() + (window.location.pathname.lastIndexOf("/") == window.location.pathname.length-1 ? "default.aspx": ""),
            hasGuestAccount: false,
            useModal: false,
            init: function() {
                if (!jQuery.modal) { alert("INCLUDE ERROR: add jquery modal reference"); }
                if (!jQuery.validator) { alert("INCLUDE ERROR: add jquery validator reference"); }
                if (!jQuery.tooltip) { alert("INCLUDE ERROR: add jquery tooltip reference"); }
                if (!jQuery.fn.serializeObject) { alert("INCLUDE ERROR: add serializeObject reference"); }
                $("#signUpViewCloseBtn, #signUpViewCancelBtn").click(function() { $.modal.close(); return false; });
                $("#signUpViewCreateBtn").click(this.create);
                $("#signUpForm input,#signUpForm select").focused();
                $("#signUpForm").validate({
                    rules: {
                        EmailConfirm: { equalTo: "#signUpViewEmail" },
                        Password: { minlength: 8 },
                        PasswordConfirm: { equalTo: "#signUpViewPassword" }
                    }
                });
                $(".attach-signup-modal").click(this.show);
            },
            show: function() {
                if ($RM("SignUp").useModal) { $("#signUpView").modal(); }
                else { window.location = "/SignUp/"; }
                return false;
            },
            create: function() {
                if ($("#signUpForm").valid()) {
                    var json = $("#signUpForm").serializeObject();
                    delete json.EmailConfirm;
                    delete json.PasswordConfirm;
                    $.post(
                        "/services/UserAction.ashx",
                        "action=" + ($RM("SignUp").hasGuestAccount ? "registerguest" : "add") + "&data=" + JSON.stringify(json) + "&location=" + $("SignUp").currentUrl,
                        function(responseContent, textStatus) {
                            if (textStatus == "success") {
                                if (responseContent.message && responseContent.message == "success") {
                                    location.href = $RM("SignUp").redirectUrl;
                                } else if (responseContent.error) {
                                    alert(responseContent.error);
                                }
                            }
                        },
                        "json"
                    );
                }
                return false;
            }
        }
    });
    if ($(document).ready) { $(document).ready(function() { $RM("SignUp").init.apply($RM("SignUp"), arguments); }); }
})(window.jQuery);