/* Given the id of a form, gets all input and select elements and serializes them into a query string. Note: a bit heavier than the true core extensions, modifying Array and such! Perhaps need a new .js file that's more appropriate? */ function serializeForm(form_element_id) { var form_element = document.getElementById(form_element_id); var input_elements = form_element.getElementsByTagName("input"); var select_elements = form_element.getElementsByTagName("select"); var serialized_form = ""; for(var i=0; i < input_elements.length; i++) { if (serialized_form != "") { serialized_form += "&"; } serialized_form += input_elements[i].name + "=" + encodeURIComponent(input_elements[i].value); } for(var i=0; i < select_elements.length; i++) { if (serialized_form != "") { serialized_form += "&"; } var selected_index = select_elements[i].selectedIndex; var selected_value = select_elements[i].options[selected_index].value; serialized_form += select_elements[i].name + "=" + encodeURIComponent(selected_value); } return serialized_form; } /* quick & easy style updater */ function update_visibility(elem_id, new_visibility) { elem_to_update = document.getElementById(elem_id) if (elem_to_update) { elem_to_update.style.visibility = new_visibility; } } //********************* RADIO BUTTON-RELATED EXTENSIONS ********** /* Selects a given element as the current radio button option, given the id or the element itself. */ function set_radio(radio_element) { radio_element = $(radio_element); radio_element.checked = true; } /* Sets a text field to the given value, or 0 by default. */ function clear_text(text_id, new_val) { element_to_clear = $(text_id) if (!new_val) { new_val = 0; } element_to_clear.value = new_val; } /* Attaches a numeric entry field to a radio button option; provides both validation (with auto-correction) of the numeric entry field and also selects the associated option whenever the value is set to a non-zero number... */ function associate_numeric_text_with_radio(text_element) { text_element = $(text_element); current_value = parseInt(text_element.value); // disallow non-numeric entry... if ( isNaN(current_value) ) { text_element.value = ''; return; } // ensure only proper integers entered into the text field:-) if (current_value.toString() != text_element.value) { text_element.value = current_value.toString(); } // don't change selection based on 0-valued number... if ( current_value == 0 ) { return; } // mark the associated radio choice after modifying the text value to a non-zero number... set_radio('rollover_fsa_has_balance'); } //****************** ARRAY EXTENSIONS ******************** /* extend the Array object type with a useful little contains method. */ Array.prototype.contains = function(check_for) { for(var i=0; i < this.length; i++) { if (this[i] == check_for) { return true; } } return false; } Array.prototype.add_unique = function(array_to_add) { for(var i=0; i < array_to_add.length; i++) { if (!this.contains(array_to_add[i])) { this.push(array_to_add[i]); } } } Array.prototype.add_all = function(array_to_add) { for(var i=0; i < array_to_add.length; i++) { this.push(array_to_add[i]); } } Array.prototype.remove = function(index_to_remove) { for (var i=index_to_remove; i < this.length - 1; i++) { this[i] = this[i + 1]; } this.length -= 1; } // a little helper method which returns a simple html list representing this array... Array.prototype.html_list = function() { var list_elem = document.createElement('list'); for (var i=0; i < this.length; i++) { var list_item_elem = document.createElement('li'); list_item_elem.appendChild(document.createTextNode(this[i])); list_elem.appendChild(list_item_elem); } return list_elem; }