var Subscription = Class.create({

    initialize: function(formId){
        this.ids = new Hash();
        this.form = $(formId);
        this.subscriptionURL = 'https://magazines.autotraderclassics.com/ausubcombo.aspx';
        this.subscribeButton = $(formId).select(".subscribe").reduce();
        this.subscribeButton.observe('click', this.submitSubscriptions.bindAsEventListener(this));
    },

    setSubscriptions: function(){
        var subscriptionCheckboxes = $(this.form).select("input[type=checkbox]");
        subscriptionCheckboxes.each(function(checkbox) {
            var id = checkbox.value;
            if (checkbox.checked) {
                this.ids.set(id, id);
            } else {
                this.ids.unset(id);
            }
        }.bind(this));

    },

    submitSubscriptions: function() {
        this.setSubscriptions();
            var params = "";

            if (this.ids.size() >= 1) {
                params = "?PX=";

                this.ids.each(function(pair) {
                    params += pair.value + "," ;
                });
            }
            document.location = this.subscriptionURL + params;
    }

});
