<script>
    $(document).ready(function(){
        $("body").on("change", "select.rapid-operator", function(){
            $(this).siblings("div").addClass("hidden");
            var op = $(this).val();
            $(this).siblings("div." + op).removeClass("hidden");
        });
        $("body").on("click", "a.rapid-add-filter", function(event){
            event.preventDefault(true);
            var op = $(this).siblings("select.rapid-operator").val();
            var field = $(this).siblings("span.rapid-field-name").text();
            var dv = $(this).siblings("div." + op);
            var frm = $(this).closest("div.rapid-filter-selection").parent().siblings("form.rapid-filterset")
            var target = frm.children("div.rapid-field-filters." + field);
			var dv_clone = dv.clone();
			//dv_clone.append($("<a>", {"class": "rapid-remove-filter"}).html('<span class="fa fa-times"></span>'));
            target.append(dv.clone());
            target.removeClass("hidden");
            $(this).closest("div.rapid-filter-selection").addClass("hidden");
            frm.submit();
        });
        $("body").on("click", "a.rapid-filter", function(event){
            event.preventDefault(true);
            var act = $(this).children(".rapid-filter-field-name").text();
            var f = $(this).closest("table").siblings("div.rapid-filter-selection").children("div.rapid-filter-selection." + act);
            var pos = $(this).parent().offset();
            pos.left -= 10;
            pos.top += $(this).parent().height() + 10;
            f.toggleClass("hidden");
            var w = f.children("div").outerWidth();
            if(pos.left + w > $(window).width()){
                pos.left = $(window).width() - w;
                if(pos.left < 0){
                    pos.left = 0;
                }
            }
            f.offset(pos);
        });
		$("body").on("click", "a.rapid-remove-filter", function(event){
			event.preventDefault(true);
			var e = $(this).closest("div");
			var f = $(this).closest("form");
			e.remove();
			f.submit();
		});
		$("body").on("click", ".rapid-submit-form", function(event){
			event.preventDefault(true);
			$(this).closest("form").submit();
		});
    })
</script>