/* 
For use with checkboxes, this will show/hide the elements listed in elements arr depending on whether the checkbox is checked or not, 
if show_if_checked is true, then the fields will be revealed if the box is checked and hidden if it's unchecked, if it's false then 
the fields will be hidden if the box is checked and revealed if the box is unchecked
*/
function toggle_elements_based_on_checkbox(form_id, check_box_id, elements_arr, show_if_checked) {
	form = document.getElementById(form_id);
	checkbox = document.getElementById(check_box_id);
	if(form && checkbox) {
		var display_value = '';
		switch(checkbox.checked) {
			case true:
				if(show_if_checked) {			
					/* Reveal the fields specified in elements_arr */
					display_value = 'inline';
				} else {
					display_value = 'none';
				}
				break;
				
			case false:
				if(show_if_checked) {			
					/* Hide the fields specified in elements_arr */
					display_value = 'none';
				} else {
					display_value = 'inline';
				}	
				break;		
		}
		toggle_elements(display_value, elements_arr);
	}
}

/* 
For use with a form element (element_id), this will show/hide the elements listed in elements arr (elements_arr) depending on whether 
the elements value matches a given value (target_value)
show_if_matches can be used to invert the behaviour
*/
function toggle_elements_based_on_element_value(element_id, target_value, elements_arr, show_if_matches) {
	if(element = document.getElementById(element_id)) {
		var display_value = '';
		if(element.value == target_value) {
			if(show_if_matches) {
				/* Reveal the fields specified in elements_arr */
				display_value = 'inline';
			} else {
				display_value = 'none';
			}
		} else {
			if(show_if_matches) {
				/* Hide the fields specified in elements_arr */
				display_value = 'none';
			} else {
				display_value = 'inline';
			}
		}
		toggle_elements(display_value, elements_arr);
	}
}

/* This will set the display value of all elements (which should be an array of element id's) in elements_arr to display_value */
function toggle_elements(display_value, elements_arr) {
	for(var count = 0; count < elements_arr.length; count++) {
		field_name = elements_arr[count];
		if(document.getElementById(field_name)) {
			document.getElementById(field_name).style.display = display_value;
		}
		/* Some forms fields have associated labels, hide these to */
		if(document.getElementById(field_name+'_label')) {
			document.getElementById(field_name+'_label').style.display = display_value;
		}
	}
}