// function to go back one page
function tweek_do_goback() {
	history.back();
}

// function to 'cancel' from a selected admin area page
function tweek_do_cancel(twk_t,twk_te)
{
    // declare varibale to contain path
    var path;
    
    // determine which path to use
    switch (twk_t)
    {
        default: path = '';
    }
    
    // redirect to required page
	document.location.href = cfgurl + path;
}

/**
* eTarget
* 
* Helper Function to get the target element of the fired event.
* @param	event	event	Event of the element to find target
* @return	object
*/
function eTarget(event) {
	
	// Ensure the event exists
	if (!event) event = window.event;

	// Reference the Object that fired the event
	return (event.srcElement) ? event.srcElement : event.target;
}

/**
* parentNode
* 
* Iterative loop up the DOM tree to find a specific node type.
* @param    object	child	Child node to start the search from
* @param	string	node	Node type to check for (i.e. 'DIV')
* @return   object|false
*/
function parentNode(child, node)
{
	// Abort if no child node was passed or the node type was not specified.
	if (!child || !node) return false;
	
	while (child.parentNode)
	{
		// Move up the DOM tree
		child = child.parentNode;
		
		// Check new node type
		if (child.nodeName == node) return child;
		
		// Continue loop...
	}
	
	// No parent node detected
	return false;
}

/**
* childNodes
* 
* Helper function to return only specific types of nodes from the original
* JavaScript childNodes call.
* @param    object	parent	Parent node to fetch children from
* @param	string	node	Type of node to fetch
* @return   array
*/
function childNodes(parent, node)
{
	var nodes = [];
	var children = parent.childNodes;
	for (var i in children)
	{
		if (children[i].nodeName == node)
		{
			nodes.push(children[i]);
		}
	}
	
	// Return the node array
	return nodes;
}

/**
* previousSibling
* 
* Wrapper function for the original JavaScript previousSibling function to
* prevent text nodes from being detected in IE.
* @param    object	node	Object to start the sibling check from
* @return   object|false
*/
function previousSibling(node)
{
	// If an invalid object node was passed, abort.
	if (!node) return false;
	
	// If the previousSibling is not valid, abort now
	if (!node.previousSibling) return false;
	
	// Loop through the previous siblings until a valid object node is detected.
	while (node.previousSibling)
	{
		// Mode up the sibling list
		node = node.previousSibling;
		
		// Check if the new node is a valid element
		if (node.nodeType == 1) return node;
	}
	
	// Sibling not found
	return false;
}

/**
* nextSibling
* 
* Wrapper function for the original JavaScript nextSibling function to
* prevent text nodes from being detected in IE.
* @param    object	node	Object to start the sibling check from
* @return   object|false
*/
function nextSibling(node)
{
	// If an invalid object node was passed, abort.
	if (!node) return false;
	
	// If the previousSibling is not valid, abort now
	if (!node.nextSibling) return false;
	
	// Loop through the previous siblings until a valid object node is detected.
	while (node.nextSibling)
	{
		// Mode up the sibling list
		node = node.nextSibling;
		
		// Check if the new node is a valid element
		if (node.nodeType == 1 && node.className != "lastNode") return node;
	}
	
	// Sibling not found
	return false;
}

/*******************************************************************************
 * SYSTEMATIC REVIEW OBEJECT
 ******************************************************************************/
$Review = {
	
	/**
	* ToggleTree
	* 
	* Toggles the collapse and expand of a tree list item.
	* @param    event	e			Event that fired the call.
	* @param	object	force		Force the function to use this object
	* 								instead of fetching the object via the
	* 								event action.
	* @param	boolean	forceType	If Force is used, this determines if the
	* 								force is to collapse (1) or expand (0).
	* @param	boolean	checkCookie	Determines if the function will check for
	* 								the state setting from the cookie.
	* @param	boolean	forceCookie	Force the function to override the stored
	* 								cookie regardless of the force state.
	* @return   nil
	*/
	ToggleTree: function(e, force, forceType, checkCookie, forceCookie)
	{
		// Find the event target
		if (!forceType) forceType = false;
		if (!forceCookie) forceCookie = false;
		var target = (force !== undefined)
			? force
			: eTarget(e);
		if (!target) return false;
		
		// Only trigger the event for specific elements
		switch (target.tagName)
		{
			case "SPAN":
			case "IMG":
				// Continue the event
			break;
			
			default:
				// Invalid element
				return false;
		}
		
		// Find the parent table row, abort if not found.
		var LI = parentNode(target, 'LI');
		var Source = parentNode(target, 'UL');
		if (!Source) return false;
		
		if (Source.className == "") Source.className = "expanded";
		
		// Check state settings from the cookie
		if (checkCookie && Source.id)
		{
			var Cookie = $Cookie.Get(Source.id, false);
			if (Cookie !== false && Source.className == Cookie)
			{
				// The cookie state says not to check this node.
				return true;
			}
		}
		
		// Toggle the expand/collapse class
		if (force !== undefined)
		{
			// Check if the current state of the node is different than the
			// forced version, if it is the same, don't toggle the change.
			if ((forceType && Source.className == "expanded") ||
				(!forceType && Source.className == "collapsed"))
			{
				// Abort toggle
				return false;
			}
		}
		Source.className = (Source.className == "collapsed")
			? "expanded" : "collapsed";
			
		// Store the state in a cookie for reload
		if (forceCookie || force === undefined)
		{
			if (Source.id) $Cookie.Set(Source.id, Source.className);
		}
			
		// Change the plus/minus icon to the opposite
		var IMG = childNodes(LI, "IMG");
		var IMGPrefix = cfgurl + "images/nav/";
		var IMGOrig = [
			"minus.gif",
			"minustop.gif",
			"minusbottom.gif",
			"plus.gif",
			"plustop.gif",
			"plusbottom.gif",
			"folder.gif",
			"folder-expanded.gif"
		];
		var IMGSwap = [
			"plus.gif",
			"plustop.gif",
			"plusbottom.gif",
			"minus.gif",
			"minustop.gif",
			"minusbottom.gif",
			"folder-expanded.gif",
			"folder.gif"
		];
		
		for (var i in IMG)
		{
			for (var j in IMGOrig)
			{
				if (IMG[i].src == IMGPrefix + IMGOrig[j])
				{
					IMG[i].src = IMGPrefix + IMGSwap[j];
					break;
				}
			}
		}
	},
	
	/**
	* Collapse
	* 
	* Forces all tree nodes to collapse
	* @param    boolean		checkCookie		Check for cookie state
	* @param    boolean		forceCookie		Force the cookie to update
	* @return   nil
	*/
	Collapse: function(checkCookie, forceCookie)
	{
		if (!checkCookie) checkCookie = false;
		if (!forceCookie) forceCookie = false;
		var Diseases = document.getElementById("diseases");
		if (!Diseases) return false;
		
		var Folders = Diseases.getElementsByTagName("SPAN");
		for (var i in Folders)
		{
			this.ToggleTree(false, Folders[i], false, checkCookie, forceCookie);
		}
	},
	
	/**
	* Expand
	* 
	* Forces all tree nodes to expand
	* @param    boolean		checkCookie		Check for cookie state
	* @param    boolean		forceCookie		Force the cookie to update
	* @return   nil
	*/
	Expand: function(checkCookie, forceCookie)
	{
		if (!checkCookie) checkCookie = false;
		if (!forceCookie) forceCookie = false;
		var Diseases = document.getElementById("diseases");
		if (!Diseases) return false;
		
		var Folders = Diseases.getElementsByTagName("SPAN");
		for (var i in Folders)
		{
			this.ToggleTree(false, Folders[i], true, checkCookie, forceCookie);
		}
	}
};

/*******************************************************************************
 * COOKIE MANAGEMENT
 ******************************************************************************/
$Cookie = {
	
	/**
	* Set
	* 
	* Creates a with the provided information
	* @param    str		name	Name of the cookie variable to create
	* @param	mixed	value	Value to store in the cookie
	* @param	int		days	Number of days to store the cookie
	* @return   nil
	*/
	Set: function (name, value, days)
	{
		
		// Default to 1 year store time
		if (!days) days = 365;
		
		// Create the expires timestamp
	    var date = new Date();
	        date.setTime(date.getTime()+(days*24*60*60*1000));
	    var expires = "; expires="+date.toGMTString();
	    
	    // Store the cookie
	    document.cookie = name+"="+value+expires+"; path=/";
	    
	},
	
	/**
	* Get
	* 
	* Reads the value of a saved cookie
	* @param    str		name		Name of the cookie variable to load
	* @param	mixed	dValue		Default value to return
	* @return   mixed
	*/
	Get: function (name, dValue)
	{
		
		// Prepare the cookie information
	    var nameEQ = name + "=";
	    var ca = document.cookie.split(';');
	    
	    // Find the cookie value
	    for(var i = 0; i < ca.length; i++) {
	    
	        var c = ca[i];
	        while (c.charAt(0)==' ') c = c.substring(1,c.length);
	        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	        
	    }
	    
	    // Return the value
	    return dValue;
	    
	},
	
	/**
	* Clear
	* 
	* Erases a stored cookie
	* @param    str		name		Name of the cookie variable to erase
	* @return   nil
	*/
	Clear: function (name) {
		
		// Replace the stored cookie with a blank string
    	this.Set(name,"",-1);
    	
	}
};