// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

  String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
  }



	function toggle_div(div_id, hide_now) {
		if (hide_now || $(div_id).visible()) {
			hide_div(div_id)
		} else {
			show_div(div_id)
		}
	}
	
	function hide_div(div_id) {
		$(div_id).hide(); 
		Form.getElements($(div_id)).each(function(element) { element.disable() } )
	}
	
	function show_div(div_id) {
		$(div_id).show(); 
		Form.getElements($(div_id)).each(function(element) { element.enable() } )
	}
	
	function remove_fields(link) {  
    $(link).previous("input[type=hidden]").value = "1";  
    $(link).up(".nested_fields").hide();  
  }

    function add_fields(link, association, content) {  
      var new_id = new Date().getTime();  
      var regexp = new RegExp("new_" + association, "g");  
      $(link).up().insert({  
        before: content.replace(regexp, new_id)  
      });  
    }

      function append_fields(link, association, content) {  
        var new_id = new Date().getTime();  
        var regexp = new RegExp("new_" + association, "g");  
        $(link).up().insert({  
          after: content.replace(regexp, new_id)  
        });  
      }
  
  function add_to_list(existing_list, value, confirm) {
    array_of_values = existing_list.split(',');
    if (confirm) {
      array_of_values[array_of_values.length] = value;
    }
    else
    {
      var i = 0;
      while(i < array_of_values.length)
      {
        if (array_of_values[i] == value) {
          array_of_values.splice(i, 1);
        } else {
          i++;
        }
      }
    }
    return array_of_values.join(',');
  }
