/* Utilities for javascript tree. */


/*
 Redirect the given url to the entire page.
*/
function loadPage(url)
{
   window.parent.location.href = url;
}

/*
 Load the given url into the given frame. The frame
 should be denoted by its name, as defined in the frameset.
*/
function loadFrame(url, frameName)
{
   window.parent.frames[frameName].location.href = url;
}

/*
 Highlight the selected tree node.  This is called in treeview.js.
 @node  a node object in the Yahoo tree
*/
function highlightNode(node)
{ 
	// Note: The node could represent the actual tree node or the
	// label associated with it. The highlighting should be applied to
	// the label, i.e. the "a" tag, not "div", otherwise the entire
	// tree will be highlighted.

	var selectedNode = window.parent.frames["toc"].selectedNode;
	
   // deselect the previous selected node
   if (selectedNode != null && selectedNode != "undefined")
   {
		var selElem = document.getElementById(selectedNode.labelElId);
		if (selElem == null)
			selElem = selectedNode.getLabelEl();
		
   	selElem.style.border = "none";
   	selElem.style.backgroundColor = "#ffffff";
   }

	// node is the "a" tag, i.e. the label, so just use the "id"
	var elem = document.getElementById(node.id);
	if (elem == null)
		elem = node.getLabelEl();
	
   if (elem != null && elem != "undefined")
   {
   	elem.style.border = "1px solid black";
   	elem.style.backgroundColor = "#ffffe4";
	}
	
	// set the current selected node
   window.parent.frames["toc"].selectedNode = node;
}   


/*
 Remove children of given DOM element.
*/
function removeChildNodes(elemObj)
{
   if (elemObj.hasChildNodes())
   {
      var children = elemObj.childNodes;
      for (var i = children.length-1; i >= 0; i--)
      {
         elemObj.removeChild(elemObj.childNodes[i]);
      }
   }
}

/**
 Set the hidden encrypted password value.
*/
function encryptPassword()
{
	var userName = document.getElementById("userName").value;
	var unencryptedPassword = document.getElementById("unencryptedPassword").value;

	if (userName == null || userName == undefined || userName == "")
	{
		alert("The username must be inserted.");
		return false;
	}
	if (unencryptedPassword == null || unencryptedPassword == undefined || unencryptedPassword == "")
	{
		alert("The password must be inserted.");
		return false;
	}
		
	// trim spaces
	userName = trim(userName);
	unencryptedPassword = trim(unencryptedPassword);
	
	var encryptedPassword = hex_md5(userName + ":SysRealm:" + unencryptedPassword);
	document.loginForm.encryptedPassword.value = encryptedPassword;
}

/**
 Trims spaces from strings.
*/
function trim(str)
{
   return str.replace(/^\s+/,'').replace(/\s+$/,'');
} 

