/* ------------------------------------------------------------------------------- +

	File:		dom.js
	Author:		Ben Nadel
	Date:		January 20, 2005
	Desc:		This has functions for accessing the document object model.

+ ------------------------------------------------------------------------------- */


// Gets the first child of this node with the given class name
function GetChildWithClassName(objNode, strClassName){
	var objChildNode = null;
	
	// Check to see if we are working with a valid node
	if (objNode && (objNode.childNodes.length > 0)){
		// Loop through the children and check for tag name
		for (var i = 0 ; i < objNode.childNodes.length ; i++){
			// Check to see if this child is the one we are looking for
			if (
				objNode.childNodes[i] && 
				(objNode.childNodes[i].nodeType == 1) &&
				(objNode.childNodes[i].className.toLowerCase() == strClassName.toLowerCase())
				){
				// We found the correct child, so return it
				return(objNode.childNodes[i]);
			} else {
				// We didn't find the correct node, so run through its children
				objChildNode = GetChildWithClassName(objNode.childNodes[i], strClassName);
				
				// Check to see if we found the child
				if (objChildNode){
					return(objChildNode);
				}
			}
		}	
		
		// We have not found it at this point, so return null
		return(null);
	} else {
		return(null);
	}
}


// Gets the first child of this node with the given tag name
function GetChildWithTagName(objNode, strTagName){
	var objChildNode = null;
	
	// Check to see if we are working with a valid node
	if (objNode && (objNode.childNodes.length > 0)){
		// Loop through the children and check for tag name
		for (var i = 0 ; i < objNode.childNodes.length ; i++){
			// Check to see if this child is the one we are looking for
			if (
				objNode.childNodes[i] && 
				(objNode.childNodes[i].nodeType == 1) &&
				(objNode.childNodes[i].tagName.toLowerCase() == strTagName.toLowerCase())
				){
				// We found the correct child, so return it
				return(objNode.childNodes[i]);
			} else {
				// We didn't find the correct node, so run through its children
				objChildNode = GetChildWithTagName(objNode.childNodes[i], strTagName);
				
				// Check to see if we found the child
				if (objChildNode){
					return(objChildNode);
				}
			}
		}	
		
		// We have not found it at this point, so return null
		return(null);
	} else {
		return(null);
	}
}


// Gets the next sibling with the given class name
function GetNextSiblingWithClassName(objNode, strClassName){
	var objSibling = objNode.nextSibling;
	strClassName = strClassName.toLowerCase();
	 
	// Loop over siblings until you get null or the correct node
	while (objSibling){
		if (
			(objSibling.nodeType == 1) &&
			(objSibling.className.toLowerCase() == strClassName)
			){
			return(objSibling);
		} else {
			objSibling = objSibling.nextSibling;
		}
	}	
	 
	return(objSibling);
}


// Gets the first parent of this node with the given class name
function GetParentWithClassName(objNode, strClassName){
	// Check to see if we are working with a valid node
	if (objNode && objNode.parentNode){
		// Get the parent node
		objNode = objNode.parentNode;
		
		// Check to see if the node is the one we are looking for
		if (
			(objNode.nodeType == 1) &&
			(objNode.className.toLowerCase() == strClassName.toLowerCase())
			){
			// We found the correct node, so return it
			return(objNode);
		} else {
			// We didn't find it, so search higher
			return(GetParentWithClassName(objNode, strClassName));
		}
	} else {
		return(null);
	}
}


// Gets the first parent of this node with the given tag name
function GetParentWithTagName(objNode, strTagName){
	// Check to see if we are working with a valid node
	if (objNode && objNode.parentNode){
		// Get the parent node
		objNode = objNode.parentNode;
		
		// Check to see if the node is the one we are looking for
		if (
			(objNode.nodeType == 1) &&
			(objNode.tagName.toLowerCase() == strTagName.toLowerCase())
			){
			// We found the correct node, so return it
			return(objNode);
		} else {
			// We didn't find it, so search higher
			return(GetParentWithTagName(objNode, strTagName));
		}
	} else {
		return(null);
	}
}


// Gets the previous sibling with the given class name
function GetPreviousSiblingWithClassName(objNode, strClassName){
	var objSibling = objNode.previousSibling;
	strClassName = strClassName.toLowerCase();
	 
	// Loop over siblings until you get null or the correct node
	while (objSibling){
		if (
			(objSibling.nodeType == 1) &&
			(objSibling.className.toLowerCase() == strClassName)
			){
			return(objSibling);
		} else {
			objSibling = objSibling.previousSibling;
		}
	}	
	 
	return(objSibling);
}