function showBranch(branch)
{
	var objBranch = document.getElementById(branch).style;
	if(objBranch.display=="block")
		objBranch.display="none";
	else
		objBranch.display="block";
}

function swapFolder(img)
{
   objImg = document.getElementById(img);

   if(objImg.src.indexOf('fw/images/plus.gif')>-1)
      objImg.src = openImg.src;
   else
      objImg.src = closedImg.src;
}


var selectedSpan = null;
var selectedSpanForColor = null;
var selectedColor = "#375491";

function toggleSelection(span)
{
   if(span.style.backgroundColor==selectedColor)
   {
      if(span.getAttribute("disableItem") != "true")
      {
      	span.style.color = 'black';
      	var childrenNodes = span.childNodes;
         	for(i=0; i<childrenNodes.length; i++)
         	{
         		if(childrenNodes[i].style != null)
               		childrenNodes[i].style.color = 'black';
         	}
         	
      }
         

      span.style.backgroundColor = 'white';
      span.style.border = "1px white solid";
      selectedSpan = null;
      selectedSpanForColor = null;
   }
   else
   {
      if(selectedSpan != null)
      {
         if(selectedSpan.getAttribute("disableItem") != "true")
         {
         	selectedSpanForColor.style.color = 'black';
         	var childrenNodes = selectedSpanForColor.childNodes;
         	for(i=0; i<childrenNodes.length; i++)
         	{
         		if(childrenNodes[i].style != null)
	         		childrenNodes[i].style.color = 'black';
            }
         }
            

         selectedSpanForColor.style.backgroundColor = 'white';
         selectedSpanForColor.style.border = "1px white solid";
         selectedSpanForColor = null;
         selectedSpan = null;
      }

      span.style.backgroundColor = selectedColor;
      span.style.border = "1px black solid";


      if(span.getAttribute("disableItem") != "true")
      {
         span.style.color = 'white';
         var childrenNodes = span.childNodes;
         for(i=0; i<childrenNodes.length; i++)
         {
            if(childrenNodes[i].style != null)
         		childrenNodes[i].style.color = 'white';
         }
         
         
      }

      selectedSpan = span.id.indexOf("chapterName_")>=0 ? span.parentNode : span;
      selectedSpanForColor = span;
   }
}



function moveUp(span)
{
   if(span != null)
   {
   	var staticNode= span.previousSibling;
   
   
   	if(staticNode)
   	{
    	var parentNode = span.parentNode;
      	var spanCopy = span.cloneNode(true);

     	parentNode.removeChild(span);
      	parentNode.insertBefore(spanCopy, staticNode);

      	selectedSpan = spanCopy;
      	selectedSpanForColor = spanCopy.id.indexOf("chapter_")>=0 ? spanCopy.childNodes[0] : spanCopy;
   	}
   }
}

function moveDown(span)
{
	if(span != null)
	{
   		if(span.nextSibling)
   		{
      		var parentNode = span.parentNode;
      		var nextCopy = span.nextSibling.cloneNode(true);

      		parentNode.removeChild(span.nextSibling);
      		parentNode.insertBefore(nextCopy, span);
      	}
   }
}

function walkTree(root, pageList, disabledList)
{
   if(root.id && root.id.indexOf("pageNode_") > -1)
   {
      var index = parseInt(root.id.substring(9, root.id.length));
      pageList[pageList.length] = "" + pageIds[index];

      if(root.getAttribute("disableItem") == "true")
      {
         disabledList[disabledList.length] = "" + pageIds[index];
      }
   }
   else if(root.childNodes != null)
   {
      for(var i=0; i<root.childNodes.length; i++)
      {
         walkTree(root.childNodes[i], pageList, disabledList);
      }
   }
}

function arrayContains(list, item)
{
   for(var i=0; i<list.length; i++)
   {
      if(list[i] == item)
      {
         return true;
      }
   }

   return false;
}


function disableTree(root, disabledList)
{
   if(root.id && root.id.indexOf("pageNode_") > -1)
   {
      var index = parseInt(root.id.substring(9, root.id.length));

      if(arrayContains(disabledList, pageIds[index]))
      {
         var childrenNodes = root.childNodes;
         for(i=0; i<childrenNodes.length; i++)
         {
         		if(childrenNodes[i].style != null)
         		{
	         		childrenNodes[i].style.color = '#aaaaaa';
	         		childrenNodes[i].setAttribute("origTitle", childrenNodes[i].innerHTML);
	         		childrenNodes[i].innerHTML = childrenNodes[i].innerHTML + "(Disabled)";
	         	}
         }
         root.setAttribute("disableItem", "true");
         root.setAttribute("origTitle", root.title);
         root.title = root.title + ":Disabled"; 
      }
   }
   else if(root.childNodes != null)
   {
      for(var i=0; i<root.childNodes.length; i++)
      {
         disableTree(root.childNodes[i], disabledList);
      }
   }
}
