//
// PREGO LIBRARY
// Powered by Prego (http://www.pregowebdesign.com)
//
// Copyright (c) 2006 George Brocklehurst
// You may only use this file as part of the web site with which it was provided.
// Within the context of that website, you may modify the contents as required.
// All other rights reserved.
//
// DOM manipulation functions
//


// CREATE ELEMENT
// Creates an element with the specified tag name and contents
// e.g. createElement('strong', 'Hello') is like writing the HTML <strong>Hello</strong>
function pregoCreateElement(tagName, contents)
{
	var node = document.createElement(tagName);
	
	if(contents)
		pregoUniversalAppend(node, contents);
	
	return node;
}


// CREATE IMAGE
function pregoCreateImage(src, alt)
{
	var node = document.createElement('IMG');
	node.src = src;
	node.alt = alt;
	
	return node;
}


// CREATE LINK
function pregoCreateLink(href, contents)
{
	var node = pregoCreateElement('A', contents);
	node.href = href;
	
	return node;
}

// CREATE INPUT
function pregoCreateInput(type, name, value)
{
	if(type == 'textarea')
	{
		var textarea = pregoCreateElement('TEXTAREA', value);
		textarea.name = name;
		return textarea;
	}
	else
	{
		var input = document.createElement('INPUT');
		input.type = type;
		input.name = name;
		input.value = value;
		return input;
	}
}


// UNIVERSAL APPEND
// Appends any child to any parent node
// If the child is an array, it's members are appended individually
// If the child is a DOM node, it is appended as is
// Any other type of child is enclosed in a DOM text node and then appended
function pregoUniversalAppend(parent, child)
{
	// Arrays - add each member individually
	if(typeof(child) == 'object' && child.length && !child.nodeType)
	{
		for(var i = 0; i < child.length; i++)
			pregoUniversalAppend(parent, child[i]);
	}
	
	// Nodes - add as they are
	else if(typeof(child) == 'object' && child.nodeType)
		parent.appendChild(child);
		
	// Anything else - treat as a string and enclose in a text node
	else
		parent.appendChild(document.createTextNode(child));
}


// REMOVE CHILDREN
// Removes all the children from a DOM node
function pregoRemoveChildren(parent)
{
	while(parent.firstChild)
		parent.removeChild(parent.firstChild);
}


// REPLACE CHILDREN
// Removes all the children from a DOM node and replaced them with 'contents'
function pregoReplaceChildren(parent, contents)
{
	while(parent.firstChild)
		parent.removeChild(parent.firstChild);
	if(contents)
		pregoUniversalAppend(parent, contents);
}



// SELECT MOVE UP
// Moves the selected item in a <select> up
// Argument can be the select node itself, or a select's ID
function pregoSelectMoveUp(select)
{
	if(typeof(select) == 'string')
		select = document.getElementById(select);
	
	if(!select || select.tagName != 'SELECT' || select.selectedIndex <= 0)
		return;
	
	var si = select.selectedIndex;
	var optAfter = select.options[si];
	select.remove(si);
	select.add(optAfter, select.options[si-1]);
}


// SELECT MOVE DOWN
// Moves the selected item in a <select> down
// Argument can be the select node itself, or a select's ID
function pregoSelectMoveDown(select)
{
	if(typeof(select) == 'string')
		select = document.getElementById(select);
	
	if(!select || select.tagName != 'SELECT' || select.selectedIndex >= select.length - 1)
		return;
	
	var si = select.selectedIndex;
	var optAfter = select.options[si + 1];
	select.remove(si + 1);
	select.add(optAfter, select.options[si]);
}