/*
javascript DOM manipulation functions

AddElement(Parent, element, content, attributeList)
SetAttributes(node, attributeList)
SetContent(node, content)
LocateChildNodebyName(parentObject, targetNodeName)
LocateChildNodebyType(parentObject, targetNodeType)
ListChildren(node)
DeleteChildren(node)
*/
function setNodeCSSClass()
{
}

function AddElement(Parent,element,content,attributeList)
{
	// insert new element into the document
	new_element = document.createElement(element);
	new_node = Parent.appendChild(new_element);
	
	SetAttributes(new_element,attributeList);
	SetContent(new_element,content);
	
	return new_node;
}

function insertAfter(newNode,markerNode)
{
	ParentNode = markerNode.parentNode;
	positionNode = markerNode.nextSibling;
	if(positionNode != null)
	{
		newNode = ParentNode.insertBefore(newNode, positionNode);
	}
	else
	{
		newNode = ParentNode.appendChild(newNode);
	}
	return newNode;
}

function SetAttributes(node,attributeList)
{
	if(attributeList != '')
	{
		attributePairs = attributeList.split(";");
		for(pairIndex in attributePairs)
		{
			atrributes = attributePairs[pairIndex].split(":");	
			
			// fix for IE's lack of support for setAttribute / class
			atrributes[0] = ((ie && !opera) && atrributes[0] == 'class') ? 'className' : atrributes[0];
			
			node.setAttribute(atrributes[0],atrributes[1]);
		}
		return node;
	}
	else
		return false;
}

function SetContent(node,content)
{
	if(content != '')
	{
		// insert text into the new element
		text_node = document.createTextNode(content);
		node.appendChild(text_node);
		return node;
	}
	else
		return false;
}

// locate first instance of a given tag inside a given parent node
function LocateChildNodebyName(parentObject,targetNodeName)
{
	nodeIndex = 0;
	while(nodeIndex < parentObject.childNodes.length)
	{
		currentNode = parentObject.childNodes[nodeIndex]
		if(currentNode.nodeName == targetNodeName)
			return currentNode;
		nodeIndex++;
	}
	return false;
}

/*
select node by nodeType property
1 ELEMENT_NODE
2 ATTRIBUTE_NODE
3 TEXT_NODE
4 CDATA_SECTION_NODE
5 ENTITY_REFERENCE_NODE
6 ENTITY_NODE
7 PROCESSING_INSTRUCTION_NODE
8 COMMENT_NODE
9 DOCUMENT_NODE
10 DOCUMENT_TYPE_NODE
11 DOCUMENT_FRAGMENT_NODE
12 NOTATION_NODE
*/
function LocateChildNodebyType(parentObject,targetNodeType)
{
	nodeIndex = 0;
	while(nodeIndex < parentObject.childNodes.length)
	{
		currentNode = parentObject.childNodes[nodeIndex]
		if(currentNode.nodeType == targetNodeType)
			return currentNode;
		nodeIndex++;
	}
	return false;
}

// list the child nodes of a specified node
function ListChildren(node)
{
	test = '';
	nodeIndex = 0;
	while(nodeIndex < node.childNodes.length)
	{
		test += 'node[' + nodeIndex + '] => ' + node.childNodes[nodeIndex].nodeName + ' (' + node.childNodes[nodeIndex].nodeValue + ')\n';
		nodeIndex++;
	}
	return test;
}

// delete a node's child nodes
function DeleteChildren(node)
{
	nodeIndex = node.childNodes.length - 1;
	
	// start from *LAST* node and work forwards as removing first node changes the indices of the following nodes
	while(nodeIndex >= 0)
	{
		removedNode = node.removeChild(node.childNodes[nodeIndex]);
		nodeIndex--;
	}
	return node.childNodes.length;
}