﻿var check = ':checked';
function checkInterests(interestError, optionSelected, msgId) {
    if (!optionSelected) {
        if (interestError == undefined) {
            interestError = $('<span class="error">You must select an interest</span>');
            $(msgId).html(interestError);
        }
    } else if (interestError != undefined) {
        $(interestError).remove();
        interestError = null;
    }
    return interestError;
}
function throwError(MSG, msg) {
    $(MSG).html('<div class="error">Sorry, we could not process this request. ' + msg + '</div>');
}
SubScribe = {
    init: function() {
        var SUBSCRIBE = '#subscribeButton';
        var SUBSCRIBE_FORM = '#subscribeForm';

        var BEL = '#Bellagio';
        var CAW = '#CAW';
        var GEN = '#General';
        var GRA = '#Grants';
        var MED = '#Media';
        var MSG = '#subscribeMsg';
        var LISTS = '#SubscribeLists';
        var interestError = null;

        var validator = $(SUBSCRIBE_FORM).validate();
        $(LISTS).val(0);
        $(SUBSCRIBE_FORM + ' :checkbox').click(function() {
            $(LISTS).val($(SUBSCRIBE_FORM + " input:checked").length);
            if (interestError != null && $(LISTS).val() > 0) $(interestError).remove();
        });

        $(SUBSCRIBE).click(function() {
            var options = $(LISTS).val() > 0;
            interestError = checkInterests(interestError, options, '#listMsg');

            if ($(SUBSCRIBE_FORM).valid() && options) {
                $(SUBSCRIBE).text('Saving...');
                $.post("/sign-up/subscribe", {
                    FullName: $('#FullName').val(),
                    Email: $('#Email').val(),
                    Country: $('#Country').val(),
                    UserType: $('#UserType').val(),
                    General: $(GEN).is(check),
                    CAW: $(CAW).is(check),
                    Bellagio: $(BEL).is(check),
                    Grants: $(GRA).is(check),
                    Media: $(MED).is(check)
                },
                    function(data) {
                        $(SUBSCRIBE).text('Subscribe');
                        if (data.success) {
                            $('#subscribeFields').slideUp();
                            validator.resetForm();
                            $(MSG).html('<span class="success">Thank you for signing up.</span>');
                        } else {
                            throwError(MSG, data.msg);
                        }
                    }, 'json');
            }
        });
    }
};
UnSubScribe = {
    init: function() {
        var UNSUBSCRIBE = '#unsubscribeButton';
        var UNSUBSCRIBE_FORM = '#unsubscribeForm';

        var BEL = '#RemoveBellagio';
        var CAW = '#RemoveCAW';
        var GEN = '#RemoveGeneral';
        var GRA = '#RemoveGrants';
        var MED = '#RemoveMedia';
        var MSG = '#unsubscribeMsg';
        var check = ':checked';
        var LISTS = '#UnsubscribeLists';
        var interestError = null;

        var validator = $(UNSUBSCRIBE).validate();
        $(LISTS).val(0);
        $(UNSUBSCRIBE_FORM + ' :checkbox').click(function() {
            $(LISTS).val($(UNSUBSCRIBE_FORM + " input:checked").length);
            if (interestError != null && $(LISTS).val() > 0) $(interestError).remove();
        });

        $(UNSUBSCRIBE).click(function() {
            var options = $(LISTS).val() > 0;
            interestError = checkInterests(interestError, options, '#unlistMsg');

            if ($(UNSUBSCRIBE_FORM).valid() && options) {
                $(UNSUBSCRIBE).text('Saving...');
                $.post("/sign-up/unsubscribe", {
                    Email: $('#UnsubscribeEmail').val(),
                    General: $(GEN).is(check),
                    CAW: $(CAW).is(check),
                    Bellagio: $(BEL).is(check),
                    Grants: $(GRA).is(check),
                    Media: $(MED).is(check)
                },
                    function(data) {
                        $(UNSUBSCRIBE).text('Unsubscribe');
                        if (data.success) {
                            $('#unsubscribeFields').slideUp();
                            validator.resetForm();
                            $(MSG).html('<span class="success">Your email was removed successfully.</span>');
                        } else {
                            throwError(MSG, data.msg);
                        }
                    }, 'json');
            }
        });
    }
};

$().ready(function() {
    SubScribe.init();
    UnSubScribe.init();
});