/**
 * Prototype-Plus
 * Author: Randy Boland
 * Documentation: http://www.brockli.com/projects/prototype/
 * License: This work is licensed under the Creative Commons Attribution-ShareAlike 2.5 License. To view a copy of this license, 
 * visit http://creativecommons.org/licenses/by-sa/2.5/.  Basically, you can use this code for anything you like, as long as 
 * you include this copyright information.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
 * NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 *
 * The following are some functions that I find useful.  In order to use these, you'll need the 
 * Prototype framework (http://prototype.conio.net/).
 */ 
// Make sure prototype.js is available
if(Object.extend) {
	Object.extend(Element, {
		// Remove all children from HTML elements.  Arguments should be the IDs of elements to remove children from.
		removeChildren: function() {
			for (var i = 0; i < arguments.length; i++) {
				var element = $(arguments[i]);
				if(element) {
					if(element.childNodes) {
						while(element.childNodes.length > 0) {
							element.removeChild(element.childNodes[0]);
						}
					}
				}
			}
		}
	});
			
	Object.extend(Form.Element, {
		// Get the value of a set of radio buttons, given the name.  Returns false if nothing is checked.
		getRadioValue: function(radioName) {
			var els = document.getElementsByTagName("input");
			for(var i = 0; i < els.length; i++) {
				if(els[i].type == "radio" && els[i].name == radioName) {
					if(els[i].checked) return els[i].value;
				}
			}
			return false;
		}
	});
	
	// Append children to a node.  Can have several arguments, and any can be arrays
	Object.extend(HTMLElement.prototype, {
		appendChildren: function() {
			for (var i = 0; i < arguments.length; i++) {
				var arg = arguments[i];
				if(arg.constructor != Array) arg = [arg];
				for(var k = 0; k < arg.length; k++) {
					this.appendChild(arg[k]);
				}
			}
		}
	});
}
