1 function clear_inputs(node, new_node) {
2 var selects = $(node).find("select");
3 $(selects).each(function(i) {
5 $(new_node).find("select").eq(i).val($(select).val());
7 var inputs = $(node).find("input");
8 $(inputs).each(function(i) {
10 $(new_node).find("input").eq(i).val($(input).val());
14 function remove_block_action( link ) {
15 var blocks = $(link).parent().parent();
16 if( $(blocks).find(".block").length > 2 ) {
17 $(blocks).find("a.remove_block").show();
19 $(blocks).find("a.remove_block").hide();
21 $(link).parent().remove();
24 function remove_rule_action( link ) {
25 if( $("#rules").find(".rule").length < 2 ) {
29 $(link).parent().parent().remove();
33 function clone_block(block) {
34 var new_block = $(block).clone(1);
35 clear_inputs(block, new_block);
36 $(new_block).find('a.remove_block').show();
37 var blocks = $(block).parent();
38 $(blocks).append(new_block);
39 $(blocks).find('a.remove_block').click(function(e){
41 remove_block_action($(this));
45 function update_rule_count(){
46 rules = $(".rulecount");
47 rules.each( function( i ){
48 $(this).text( i + 1 );
52 $(document).ready(function() {
53 $("#new_rule .remove_rule").hide();
54 $("#new_rule a.remove_block").hide();
55 $("#rules a.remove_block").click(function(e){
57 remove_block_action($(this));
59 $("#rules .remove_rule").click(function(e){
61 remove_rule_action($(this));
64 var unique_id = $(".rule").length + 1;
65 $(".add_rule").click(function(e){
67 var rule = $("#new_rule");
68 var rules = $("#rules");
69 var new_rule = rule.clone(1);
70 new_rule.removeAttr('id');
71 new_rule.attr('class', 'rule');
72 clear_inputs(rule, new_rule);
73 new_rule.find("select[name='condition_field']").attr('name', 'condition_field_' + unique_id);
74 new_rule.find("select[name='substitution_field']").attr('name', 'substitution_field_' + unique_id);
75 new_rule.find("input[name='condition_value']").attr('name', 'condition_value_' + unique_id);
76 new_rule.find("input[name='substitution_value']").attr('name', 'substitution_value_' + unique_id);
77 new_rule.find("input[name='age']").attr('name', 'age_' + unique_id);
78 new_rule.find("select[name='agefield']").attr('name', 'agefield_' + unique_id);
79 new_rule.find("input[name='unique_id']").val(unique_id);
81 $("#rules").append(new_rule);
83 var scrollToPoint = new_rule.position();
84 window.scroll(0, scrollToPoint.top - $("#toolbar").height() );
86 if( $("#rules").find(".rule").length > 0 ) {
90 if( $("#rules").find(".conditions > .condition").length > 1 ) {
93 if( $("#rules").find(".conditions > .condition").length > 1 ) {
96 new_rule.find('.remove_rule').click(function(e){
98 remove_rule_action( $(this) );
100 new_rule.find('.add_rule').remove();
104 $("a.add_block").click(function(e){
106 clone_block( $(this).parent() );
109 if( $("#rules").find(".rule").length < 1 ) {
111 $("#norules").show();
114 $("#rules .rule .blocks").each(function(){
115 if ( $(this).find(".block").length == 1 ) {
116 $(this).find("a.remove_block").hide();
120 jQuery.validator.addClassRules("age", {
124 $("#rules_form").validate();