//CentricsAttributeAPI.js
//This file deals with the setting/getting of attributes, and searching by those attributes

//Gets a node's attribute value by its attribute name
function getNodeAttribute(node, attributeName)
{
   if (node == null)
   {
      alert("getNodeAttribute received a null node!");
   }
      
   //W3C (mozilla & netscape)
   if (node.hasAttribute && node.hasAttribute(attributeName))
   {
      return node.getAttribute(attributeName);
   }
   //IE only
   else
   {
      return node[attributeName];
   }
}

//Sets a nodes attribute value (will create attribute if it doesn't already exist)
function setNodeAttribute(node, attributeName, value)
{
   if (node == null)
   {
      alert("setNodeAttribute received a null node!");
   }
   
   //W3C (mozilla & netscape)
   if (node.hasAttribute)
   {
      node.setAttribute(attributeName, value);
   }
   //IE only
   else
   {
      node[attributeName] = value;
   }
}

//Checks to see if a node has an attribute
function nodeHasAttribute(node, attributeName)
{
   if (node == null)
   {
      alert("nodeHasAttribute received a null node!");
   }
   
   //W3C (mozilla & netscape)
   if (node.hasAttribute)
   {
      return node.hasAttribute(attributeName);
   }
   //IE only
   else
   {
      return node[attributeName] != null;
   }
}

//Checks to see if a node's attribute matches the value passed in
function nodeAttributeMatchesValue(node, attributeName, value)
{
   if (node == null)
   {
      alert("NodeAttributeMatchesValue received a null node!");
      return false;
   }
   
   if (nodeHasAttribute(node, attributeName))
   {
      return (value == getNodeAttribute(node, attributeName));
   }
   else
   {
      return false;
   }
}

//Finds a child node by an attribute name & value
function findNodeByAttributeRecurse(node, attribName, value)
{       
   if (node == null)
   {
      alert("FindNodeByAttribute received a null node!");
      return null;
   }
        
   var iter
   if(node.childNodes)
   {            
      var len = node.childNodes.length;
      for (var index = 0; index < len; index++)
      { 
         var iter = node.childNodes.item(index);
         
         if (nodeAttributeMatchesValue(iter, attribName, value) )
         {
            return iter;
         }

         var found = findNodeByAttributeRecurse(iter, attribName, value);
         if(found != null) return found;                     
      }
      
   }
   
   return null;
}

//Finds a parent node by attribute name & value
function findParentNodeByAttribute(node, attribName, value)
{
   if (node == null)
   {
      alert("findParentNodeByAttribute received a null node!");
   }
   
   var myparent = node.parentNode;
   
   while (myparent != null && !nodeAttributeMatchesValue(myparent, attribName, value))
   {
      myparent = myparent.parentNode;
   }
   return myparent;
}

function findChildNodeByNodeName(node, nodeName)
{
   if (node == null)
   {
      alert("FindChildNodeByNodeName received a null node!");
   }
   
   if (node.nodeName != nodeName)
   {
      if (node.childNodes)
      {
		   for (childIndex = 0; childIndex < node.childNodes.length; childIndex++)
		   {
		      var child = node.childNodes[childIndex];
		      if (child.nodeName == nodeName)
		      {
		         return child;
		      }
		   }
		}
   }
   else
   {
      return node;
   }
   return null;
}

function findAllNodesByAttributeRecurse(node, attribName, value, arr)
{
   if (node == null)
   {
      alert("findAllNodesByAttributeRecurse received a null node!");
      return null;
   }
        
   var iter
   if(node.childNodes)
   {            
      var len = node.childNodes.length;
      for (var index = 0; index < len; index++)
      { 
         var iter = node.childNodes.item(index);
         
         if (nodeAttributeMatchesValue(iter, attribName, value) )
         {
            arr.push(iter);
         }

         findAllNodesByAttributeRecurse(iter, attribName, value, arr);
      }
   }
   
   return;
}

