(function($){ 

    var handleSubmit = function(event) {
        // alert('In handleSubmit');
        // console.log('this: '+this);
        $(this)
            .find('input#form-submit').hide().end()
            .find('img#spinner').show().end()
            .find('input[name=email]').attr("disabled", true);

        var inputs = [];
        $(':input', this).each(function() {
            if (this.type == 'radio' && ! this.checked) {
                // console.log('Radio, unselected: '+this.value);
                return true;
            }
            inputs.push(this.name + '=' + escape(this.value));
        });
        inputs.push('uri='+escape('mt-add-notify.cgi'));

        var action = $('input[name=__mode]:checked', this).val();
        var email = $("input[name=email]", this).val();
        console.log('Action is '+action);
        var resultdiv;
        console.dir(inputs);
        var req = {
            data:   inputs.join('&'),
            // url:    '/ajax-mt-proxy.php',
            url:    'http://betawave.com/cgi-bin/mt/mt-add-notify.cgi',
            type:   this.method,
            timeout: 2000,
            complete: function(msg){
                        console.log("Ajax completed, now showing resultdiv: "+resultdiv);
                        $("form#list-subscribe")
                            .find(resultdiv).fadeIn("slow");
                        console.dir({'msg' : msg, 'this' : this});
                  }, 
            error: function(msg, eT) {
                        console.log("Ajax failed");
                        console.dir({'msg' : msg, 'this' : this, 'eT' : eT});
                        resultdiv = '#form-response-error';
                        $("#spinner", this).hide();
                        $('input[name=email]', this).removeAttr("disabled");
                        $('input#form-submit', this).show();
                  },
            success: function(r) { 
                        console.log("Ajax succeeded with action: "+action);
                        console.dir({'this' : this, 'response' : r});
                        if (action == 'unsubscribe') {
                            resultdiv = '#form-response-unsub';
                        } else if (action == 'subscribe') {
                            resultdiv = '#form-response-sub';
                        }
                        $("form#list-subscribe")
                            .find("> #form-controls").hide();
                    }
        };

        console.dir(req);
        event.preventDefault();
        $.ajax(req);
    };

    jQuery(function($) { 
        // Your code using failsafe $ alias here... 
        
        
        $("#form-mask").show();
        var f = $("form#list-subscribe");
        console.log('Form: '+f);
        $(f).find("#spinner").hide().end()
            .each( function () { this.reset() } )
            .find('input[title!=""]').hint().end()
            .find('input[name=email]').removeAttr("disabled")
            .find('input[value=subscribe]').click();
        
        $(f).submit(handleSubmit);
    });



})(jQuery);