//Developed with jQuery 1.32. Will probably work with later versions.



var TestingAjax = function(){
    testing_object = this;
    this.$result_hook = $("#testing_result");
    this.$interface_hook = $("#testing_interface");
    this.$result = jQuery("#testing_result .result");
    this.$button_hook = $("#testing_interface div.buttons");
    this.$interface_form = $("#testing_form");
    this.form_action = $("#testing_form").attr("action");

    //Call this function
    this.setup_ajax = function(){
        this.disable_button();
        this.setup_click();
    }
    this.disable_button = function(){
        var $button = this.$button_hook.find("button");
        var button_text = $button.html();
        var deactivate = "<BUTTON class=negative type='button'>" + button_text + "</BUTTON>";
        this.$button_hook.html(deactivate);
    }
    this.setup_click = function(){
        this.$button_hook.find("button").data("self", this);
        this.$button_hook.find("button").click(function(self){
            var self = $(this).data("self");
            if (self.is_ajax_already_submitted(self)){
                return;
            } else {
                self.insert_loading_image(self);
                self.send_ajax_and_insert_reply(self);
            }
        });
    }
    
    this.is_ajax_already_submitted = function(self){
        var loading_flag = self.$result_hook.find("#loading_img");
        if (loading_flag.length){
            return true;
        }
        return false;
    }
    this.insert_loading_image = function(self){
        self.$result.html("<img id='loading_img' src='images/loading.gif'>");
        self.$result_hook.parent().show()
    }
    this.send_ajax_and_insert_reply = function(self){
        var post = {};
        var $select = self.$interface_hook.find("select");
        $select.each( 
            (function(post){
                return function(){
                    var name = $(this).attr("name");
                    var value = $(this).find(":selected").val();
                    post[name] = value
                }
            })(post)
        );
        var $checked = self.$interface_hook.find(":checked")
        $checked.each( 
            (function(post){
                return function(){
                    var name = $(this).attr("name");
                    var value = $(this).val();
                    post[name] = value;
                }
            })(post)
        );
        var $text = self.$interface_hook.find(":text")
        $text.each( 
            (function(post){
                return function(){
                    var name = $(this).attr("name");
                    var value = $(this).val();
                    post[name] = value;
                }
            })(post)
        );
        self.$result.load(self.form_action,
                          post,
                          self.on_success_remove_current_option
                          );
    }
    this.on_success_remove_current_option = function(self){
        var error = testing_object.$result_hook.find(".test_error")
        if(error.length){
            return;
        }
        else {
            testing_object.$interface_form.find(":selected").remove(); 
        }
    }
}
