function getCategoryHierarchy()
{
  hideAllSubs('div', 'categories','categorycontainer');
  getElementsByClass('h1', 'cattop','categorycontainer');
}


function getElementsByClass(tag, categoryClass, containerName)
{	
  var categories = document.getElementById(containerName);	
  if (categories)
    categories=categories.getElementsByTagName(tag);
  
  if(categories)
  {
    for( x = 0; x < categories.length; x++ )
  	{
           if(categories[x].className == categoryClass)
           {
           	try
            {
             categories[x].firstChild.onclick = toggleDisplay;
           	 categories[x].lastChild.onclick = addToCatBox;
            }
       			catch( err )
      			{
      				alert( x );
      			}
           }
    }
  }
}

function toggleDisplay()
{  
 var clickedElement = this.parentNode;

 var toggler = getNextElementSibling(clickedElement); 
 if (toggler)
 {
  while( toggler && !(toggler.tagName.toLowerCase() == "div" && toggler.className == "categories" ))
  {
  	toggler = getNextElementSibling(toggler);

  }     
  if (toggler.style.display == "")
  {
      toggler.style.display= "none";
  }
  else
  {
      toggler.style.display = "";
  }   
   this.innerHTML = toggler.style.display == "none" ? "+" : "-";
 }  
}


function addToCatBox()
{
  var categoryBox = document.getElementById("categorydisplay");
  if (categoryBox)
  {
    categoryBox.value = this.innerHTML;
    if( this.previousSibling.type && this.previousSibling.type.toLowerCase() == "radio" )
    {
      this.previousSibling.checked = true;
    }
  }
}

function getNextElementSibling( element )
{
  var theElement = element.nextSibling;
  

   while ( theElement && theElement.nodeType != 1 )
  {
		theElement = theElement.nextSibling;
  }

  return theElement;
}

function hideAllSubs(tagName, elementName, catContainer)
{
  var subContainers = document.getElementById(catContainer);
  if (subContainers)
    subContainers = subContainers.getElementsByTagName(tagName);

  if(subContainers)
  {
    for (x = 0; x < subContainers.length; x++)
    {
      if(subContainers[x].className == elementName)
        subContainers[x].style.display = "none";
    }
  }
}
