/*
 * DNBasic.js
 * Basic HTML access and modification
 *
 * @author Jack Weinert (CodeMonkz)
 */
function DNHTMLServer()
{
	// GetElementByID ------------------------------------------------------
	// Return a html tag element given by its ID.
	//
	// @param
	// elementID - id of the element that should returned.
	//
	// @return
	// null - no element found
	// otherwise - element
	// ------------------------------------------------------ GetElementByID
	this.GetElementByID = function(elementID)
	{
		var returnValue = null;
		
		if (document.getElementById)
		{
			returnValue = document.getElementById(elementID);
		}
		else if (document.all)
		{
			returnValue = document.all[elementID];
		}
		else if (document.layers)
		{
			returnValue = document.layers[elementID];
		}
		
		return returnValue;
	};
	
	// GetSubElementByID ---------------------------------------------------
	// Return a sub element of a parent element.
	//
	// @param
	// parentElement - starting parent element
	// elementID - ID of the element that should returned
	//
	// @return
	// null - no element found
	// otherwise - element
	// --------------------------------------------------- GetSubElementByID
	this.GetSubElementByID = function(parentElement,elementID)
	{
		if ((parentElement) && (parentElement.hasChildNodes()))
		{
			var childTag = this.FirstChildTag(parentElement);
			while (childTag)
			{
				if ((childTag.id) && (childTag.id == elementID))
				{
					return childTag;
				}
				
				var ret = this.GetSubElementByID(childTag,elementID);
				if (ret)
				{
					return ret;
				}
				childTag = this.NextSiblingTag(childTag);
			}
		}
		return null;
	};
	
	// GetSubElementByClass ------------------------------------------------
	// Return a sub element of a parent element given by its class name.
	//
	// @param
	// parentElement - starting parent element
	// className - name of the class
	//
	// @return
	// null - no element found
	// otherwise - element
	// ------------------------------------------------ GetSubElementByClass
	this.GetSubElementByClass = function(parentElement,className)
	{
		if ((parentElement) && (parentElement.hasChildNodes()))
		{
			var childTag = this.FirstChildTag(parentElement);
			while (childTag)
			{
				if (this.HasElementClass(childTag,className) === true)
				{
					return childTag;
				}
				
				var ret = this.GetSubElementByClass(childTag,className);
				if (ret)
				{
					return ret;
				}
				childTag = this.NextSiblingTag(childTag);
			}
		}
		return null;
	};
	
	// GetSubElementByName -------------------------------------------------
	// Return a sub element of a parent element given by its name.
	//
	// @param
	// parentElement - starting parent element
	// name - name of the element that should returned
	//
	// @return
	// null - no element found
	// otherwise - element
	// ------------------------------------------------- GetSubElementByName
	this.GetSubElementByName = function(parentElement,name)
	{
		if ((parentElement) && (parentElement.hasChildNodes()))
		{
			var childTag = this.FirstChildTag(parentElement);
			while (childTag)
			{
				if ((childTag.name) && (childTag.name == name))
				{
					return childTag;
				}
				
				var ret = this.GetSubElementByName(childTag,name);
				if (ret)
				{
					return ret;
				}
				childTag = this.NextSiblingTag(childTag);
			}
		}
		return null;
	};
	
	// GetSubElementsByClass -----------------------------------------------
	// Return all sub element of a parent element that fits into a given
	// class.
	//
	// @param
	// parentElement - starting parent element
	// className - name of the class
	// (depth) - hierarchical levels to travel
	//
	// @return
	// array of elements; can be empty
	// ----------------------------------------------- GetSubElementsByClass
	this.GetSubElementsByClass = function(parentElement,className,_depth)
	{
		var depth = ((_depth === null) ? (-1) : (_depth));
		var returnValue = [];
		if ((depth != 0) && (parentElement) && (parentElement.hasChildNodes()))
		{
			var childTag = this.FirstChildTag(parentElement);
			while (childTag)
			{
				if (this.HasElementClass(childTag,className) === true)
				{
					returnValue.push(childTag);
				}
				
				var ret = this.GetSubElementsByClass(childTag,className,depth - 1)
				if (ret.length > 0)
				{
					for (var i in ret)
					{
						returnValue.push(ret[i]);
					}
				}
				childTag = this.NextSiblingTag(childTag);
			}
		} 
		
		return returnValue;
	};
	
	// GetAbsolutePosition -------------------------------------------------
	// Return the absolute position of an element.
	//
	// @return
	// array with 2 entries: [0] - left offset, [1] - top offset
	// ------------------------------------------------- GetAbsolutePosition
	this.GetAbsolutePosition = function(element)
	{
		var offsetLeft = 0;
		var offsetTop = 0;
		while (element)
		{
			offsetLeft += element.offsetLeft;
			offsetTop += element.offsetTop;
			
			element = element.offsetParent;
		}
		
		return [offsetLeft,offsetTop];
	};
	
	// HasElementClass -----------------------------------------------------
	// Check if an element has a specified class assigned or not.
	//
	// @param
	// element - reference element
	// className - class
	//
	// @return
	// false - class not assigned
	// true - class assigned
	// ----------------------------------------------------- HasElementClass
	this.HasElementClass = function(element,className)
	{
		if (element.className)
		{
			var classes = element.className.split(' ');
			for (var i in classes)
			{
				if (classes[i] == className)
				{
					return true;
				}
			}
		}
		return false;
	};
	
	// AppendElementClass --------------------------------------------------
	// Assign a specified class to an element.
	//
	// @param
	// element - reference element
	// className - class
	// -------------------------------------------------- AppendElementClass
	this.AppendElementClass = function(element,className)
	{
		if (element.className)
		{
			if (this.HasElementClass(element,className) === false)
			{
				element.className += ' ' + className;
			}
		}
		else
		{
			element.className = className;
		}
	};
	
	// RemoveElementClass --------------------------------------------------
	// Remove a specified class from an element.
	//
	// @param
	// element - reference element
	// className - class
	// -------------------------------------------------- RemoveElementClass
	this.RemoveElementClass = function(element,className)
	{
		if (element.className)
		{
			var classes = element.className.split(' ');
			var newClasses = [];
			for (var i in classes)
			{
				if (classes[i] != className)
				{
					newClasses.push(classes[i]);
				}
			}
			element.className = newClasses.join(' ');
		}
	};
	
	// ToggleElementClass --------------------------------------------------
	// Toggle between element classes.
	//
	// @param
	// element - reference element
	// className - class that will be assigned if no OR alternative class is
	//	assigned
	// alternative - alternative class, will be assigned when 'class' was
	//	assigned
	// -------------------------------------------------- ToggleElementClass
	this.ToggleElementClass = function(element,className,alternative)
	{
		var len = className.length;
		var aLen = alternative.length;
		if ((len > 0) && (this.HasElementClass(element,className)))
		{
			this.RemoveElementClass(element,className);
			if (aLen > 0)
			{
				this.AppendElementClass(element,alternative);
			}
		}
		else if ((aLen > 0) && (this.HasElementClass(element,alternative)))
		{
			this.RemoveElementClass(element,alternative);
			if (len > 0)
			{
				this.AppendElementClass(element,className);
			}
		}
		else
		{
			if (len > 0)
			{
				this.AppendElementClass(element,className);
			}
		}
	};
	
	// FirstChildTag -------------------------------------------------------
	// Return the first child tag of a given element.
	//
	// @param
	// element - starting/reference element
	//
	// @return
	// null - no child tag exists
	// otherwise - child tag
	// ------------------------------------------------------- FirstChildTag
	this.FirstChildTag = function(element)
	{
		if (element)
		{
			var child = element.firstChild;
			if (child)
			{
				if (child.tagName)
				{
					return child;
				}
				else
				{
					return this.NextSiblingTag(child);
				}
			}
		}
		return null;
	};
	
	// LastChildTag --------------------------------------------------------
	// Return the last child tag of a given element.
	//
	// @param
	// element - starting/reference element
	//
	// @return
	// null - no child tag exists
	// otherwise - child tag
	// -------------------------------------------------------- LastChildTag
	this.LastChildTag = function(element)
	{
		if (element)
		{
			var child = element.lastChild;
			if (child)
			{
				if (child.tagName)
				{
					return child;
				}
				else
				{
					return this.PreviousSiblingTag(child);
				}
			}
		}
		return null;
	};
	
	// NextSiblingTag ------------------------------------------------------
	// Return the next sibling tag.
	// This function is necessary because mozilla has a bug in returning the
	// next sibling of an element.
	//
	// @param
	// element - starting element
	//
	// @return
	// null - no next sibling tag exists
	// otherwise - next sibling tag
	// ------------------------------------------------------ NextSiblingTag
	this.NextSiblingTag = function(element)
	{
		if (element)
		{
			var sTag = element.nextSibling;
			if (sTag)
			{
				return ((sTag.tagName) ? (sTag) : (this.NextSiblingTag(sTag)));
			}
		}
		return null; 
	};
	
	// PreviousSiblingTag --------------------------------------------------
	// Return the previous sibling tag.
	// This function is necessary because mozilla has a bug in returning the
	// next sibling of an element.
	//
	// @param
	// element - starting element
	//
	// @return
	// null - no previous sibling tag exists
	// otherwise - previous sibling tag
	// -------------------------------------------------- PreviousSiblingTag
	this.PreviousSiblingTag = function(element)
	{
		if (element)
		{
			var sTag = element.previousSibling;
			if (sTag)
			{
				return ((sTag.tagName) ? (sTag) : (this.PreviousSiblingTag(sTag)));
			}
		}
		return null;
	};
}

var HTML = ((!HTML) ? (new DNHTMLServer()) : (HTML));

// DP_BuildEM ------------------------------------------------------------------
// Synthesize an email address and fill up the href attribute of an 'a' tag.
//
// @param
// element - 'a' tag that should get the email address to its 'href' attribute
// values - fragments of the email
// ------------------------------------------------------------------ DP_BuildEM
function DP_BuildEM(element,values)
{
	if ((element) && (element.tagName) && (element.tagName == 'A') && (values) && (values.length == 2))
	{
		var address = "mailto:" + values[1] + '@' + values[0];
		element.href = address;
		
		return true;
	}
	return false;
}

function DN_ValueToFixed(value, precision) {
    var power = Math.pow(10, precision || 0);
    return String(Math.round(value * power) / power);
}

