﻿BenefulMap.ModalRegistrationForm = BenefulMap.ModalForm.extend({
    init: function(modalTitle, modalElements, modalOptions, snippetPath, params, formElements) {
        this._super(modalTitle, modalElements, modalOptions, snippetPath, params, formElements);
        this.openListener("registration.open");
        this.closeListener("registration.close");
        this.modalToOpenOnRegister = null;
    },
    getModalToOpenOnRegister: function() {
        return this.modalToOpenOnRegister;
    },
    setModalToOpenOnRegister: function(modalToOpen) {
        this.modalToOpenOnRegister = modalToOpen;
    },
    addModalListeners: function() {
        this._super();
        this.handleAjaxProTimeout();
        this.thankYouButtonListeners();
        this.loginButtonListener();
    },
    handleAjaxProTimeout: function() {
        var self = this;
        AjaxPro.onTimeout = function() {
            console.warn("Ajax pro method timed out.");
            if (self) {
                self.redisplayFormWithMessage(9);
            }
        };
    },
    thankYouButtonListeners: function() {
        var self = this;
        $("#register-add-spot").click(function(event) {
            event.preventDefault();
            var href = window.location.href + "#add-spot";
            window.location.href = href;
            location.reload();
        });
    },
    loginButtonListener: function() {
        var self = this;
        $(this.getModalDiv() + " a.registration-login").click(function(event) {
            event.preventDefault();

            if (VotingOpen == true) {
                $(document).trigger("login.open", ['vote-dreamdogpark']);
            } else {
                $(document).trigger("login.open");
            }
            
            $(self.getModalDiv()).dialog('close');
        });
    },
    injectAdditionalDataIntoSerializedArray: function() {
        var self = this;
        var radioButtonIsChecked = false;
        $("#modal-registration input:radio").each(function() {
            if (this.checked) {
                self.setIndividualFormData("EmailCommunication", this.value);
                radioButtonIsChecked = true;
            }
        });
        if (!radioButtonIsChecked) {
            self.setIndividualFormData("EmailCommunication", "N");
        }
    },
    testAdditionalRequiredFields: function() {
        var radioButtonsStatus = false;
        $("#modal-registration input:radio").each(function() {
            console.log(this);
            if (this.checked) {
                radioButtonsStatus = true;
            }
        });

        var agreeCheckStatus = false;
        if ($("#modal-registration input:checkbox").length) {
            if ($("#modal-registration input:checkbox").get(0).checked) {
                console.log($("#modal-registration input:checkbox"));
                agreeCheckStatus = true;
            }
        }

        var additionalFieldStatus = false;
        if (radioButtonsStatus && agreeCheckStatus) {
            additionalFieldStatus = true;
        }
        console.log(additionalFieldStatus);
        return additionalFieldStatus;
    },
    showThankYou: function() {
        this.setTitle("Thanks for Joining!");
        var params = {};
        params.ph = 2;
        this.setParams(params);
        this.populate();
    },
    redisplayFormWithMessage: function(message) {
        var params = {};
        params.ph = 1;
        params.message = message;
        this.setParams(params);
        this.populate();
    },
    checkResult: function(pass, msg, goToLogin) {
        console.log(pass, msg);
        if (pass == true) {
            if (msg == "vote-dreamdogpark") {
                this.close();
                VotingPH = 1;
                VotingLogin = true;
                $(document).trigger("votedreamdogpark.open", [VotingPH]);
            } else {
                this.showThankYou();
            }
        } else if (goToLogin == true) {
            $(this.getModalDiv()).dialog('close');
            $(document).trigger("login.open", [null, msg]);
        } else {
            this.redisplayFormWithMessage(msg);
        }
    },
    submitFormData: function() {
        this._super();
        var self = this;
        // pull data out of serialized array
        var firstName = this.getIndividualFormData('FirstName');
        var lastName = this.getIndividualFormData('LastName');
        var nickName = this.getIndividualFormData('ScreenName');
        var dogName = this.getIndividualFormData('DogsName');
        var emailAddress = this.getIndividualFormData('EmailAddress');
        var password = this.getIndividualFormData("Password");
        var password2 = this.getIndividualFormData("PasswordConfirm");
        var emailOptIn = this.getIndividualFormData('EmailCommunication');
        var eloquaUniqueIdentifier = $("#elqCustomerGUID").val(); // this is a hidden input on the outer shell page used in registration
        // send data to server
        WagWorld.Web.AjaxUtility.RegisterUser(firstName, lastName, nickName, dogName, emailAddress, password, password2, emailOptIn, eloquaUniqueIdentifier, self.getModalToOpenOnRegister(), function(result) {
            console.log("Result: ", result.value);
            var pass = result.value.Pass;
            var reason = result.value.Reason;
            var goToLogin = false;
            //var goToVote = false;
            if (result.value.AuxiliaryNumber == 1) {
                goToLogin = true;
            }
            self.checkResult(pass, reason, goToLogin);
        });
    },
    openListener: function(eventName) {
        var self = this;
        $(document).bind(eventName, function(event, message) {
            self.setModalToOpenOnRegister(message);
            self.open();
        });
    },
    dialogCloseListener: function() {
        var self = this;
        $(this.getModalDiv()).bind('dialogclose', function(event, ui) {
            console.info(self.getModalTitle(), " now closing");
            if (self.getTitle() == "Thanks for Joining!") {
                location.reload();
            } else {
                self.revertToInitialState();
            }
        });
    },
    close: function() {
        console.log("closing");
        $(this.getModalDiv()).dialog('close');
    }
});




