SignupPage = Tmatex.UIContainer.subClass({
    init: function(container, pageParameters) {
        this._super.apply(this, [container]);
        var THIS = this;

        var rate = 50;
        var rateGuideRegular = "${0} / month".format(rate);
        var rateVHRRegular = "${0} / month <em>(free with 2 pricing guides)</em>".format(rate);
        var rateVHRFree = "<em>FREE</em>";

        this.selectedFeatures = [];
        this.features = {/*BBK: [checkbox, rate container], etc*/};
        var clickHandler = function(event) {
            Tmatex.CheckBox.clickHandler(event);
            var guidesCount = 0;
            jQuery.each(THIS.features, function(feature, elements) {
                if (Tmatex.CheckBox.isChecked(elements[0]) && !feature.startsWith("VH_"))
                    guidesCount++;
            });
            if (!event.target.getAttribute("data").startsWith("VH_")) {
                var carfax = THIS.features["VH_CARFAX"];
                var autoCheck = THIS.features["VH_AUTOCHK"];
                if (guidesCount >= 2) {
                    Tmatex.CheckBox.setChecked(carfax[0], true);
                    carfax[1].html(rateVHRFree);
                    Tmatex.CheckBox.setChecked(autoCheck[0], true);
                    autoCheck[1].html(rateVHRFree);
                } else if (Tmatex.CheckBox.isChecked(carfax[0]) || Tmatex.CheckBox.isChecked(autoCheck[0])) {
                    Tmatex.CheckBox.setChecked(carfax[0], false);
                    carfax[1].html(rateVHRRegular);
                    Tmatex.CheckBox.setChecked(autoCheck[0], false);
                    autoCheck[1].html(rateVHRRegular);
                }
            }
            var total = 0;
            THIS.selectedFeatures = [];
            THIS.markup.find("table.features .checkbox.on").each(function(ignored, checkBox) {
                var feature = checkBox.getAttribute("data");
                if (!feature.startsWith("VH_") || guidesCount < 2)
                    total += rate;
                THIS.selectedFeatures.push(feature);
            });
            THIS.markup.find("table.features .total").html(total > 0 ? "$" + total : "&nbsp;");
        };

        this.markup.find("table.features .checkbox").each(function(ignored, htmlElement) {
            var feature = htmlElement.getAttribute("data");
            var checkBox = jQuery(htmlElement);
            var rate = checkBox.parent().siblings("td");
            THIS.features[feature] = [checkBox, rate];
            checkBox.click(clickHandler);
            rate.html(feature.startsWith("VH_") ? rateVHRRegular : rateGuideRegular);
        });

        this.acceptTerms = this.markup.find(".reviewTerms button").click(function(event) {
            Tmatex.CheckBox.clickHandler(event);
            THIS.createAccountButton.setEnabled(Tmatex.CheckBox.isChecked(THIS.acceptTerms));
        });

        this.form = new Tmatex.Form(this.markup.find("table.form:first"));
        this.form.populate({ccProviderDomain: Autoniq.CCProviderDomain, ccExpMonthDomain: Autoniq.CCExpiresMonthDomain, ccExpYearDomain: Autoniq.CCExpiresYearDomain});

        // State selector
        new Autoniq.StateDropDown(this.markup.find('select[data-type^="state"]')).populate();

        this.createAccountButton = new Tmatex.Button(this.markup.find(".createAccount button"), {
            onClick: function(button) {
                if (!Tmatex.CheckBox.isChecked(THIS.acceptTerms))
                    alert("Please indicate that you agree to Terms of Service, Privacy and Refund policies by clicking " +
                    "on the checkbox above.");

                else if (THIS.selectedFeatures.length === 0)
                    alert("At least one Subscription option must be selected.");

                else {
                    var accountFields = THIS.form.validate();
                    if (accountFields) {
                        if (accountFields.passwd.trim().length < 4) {
                            THIS.form.setError(THIS.markup.find('input[data-type^="passwd"]'), "Minimum password length is 4 characters.");
                            return;
                        } else if (accountFields.passwd != accountFields.passwdConfirm) {
                            THIS.form.setError(THIS.markup.find('input[data-type^="passwd"]'), "Passwords do not match.");
                            return;
                        }
                        THIS.ajax("signup", {
                            type: "POST",
                            data: jQuery.extend({features: THIS.selectedFeatures}, accountFields),
                            success: function() {
                                var message = "Your account was successfully created.";
                                if (jQuery.inArray("VH_CARFAX", THIS.selectedFeatures) !== -1 || jQuery.inArray("VH_AUTOCHK", THIS.selectedFeatures) !== -1)
                                    message += '\nPlease take a moment to enter your CARFAX and/or AutoCheck userID and password(s) on "My Account" page.';
                                alert(message);
                                window.location = "myAccount.html";
                            },
                            error: THIS.form.serverErrorsHandler
                        });
                    }
                }
                if (window.rounded_elements_resize) //hello IE rounded corners
                    window.rounded_elements_resize();
            }
        }).setEnabled(false);
    }
});

