////////////////////////
// General Functions
////////////////////////
function createNewImage()
{
	var captcha_image = document.getElementById("captcha_image");
	captcha_image.src = "/images/captcha.php?width=125&height=40&chars=6&rand="+Math.random();
}


function showLayer(layerID)
{
	if ( document.getElementById )
	{
		// If a string, get the object
		if ( typeof(layerID) == "string" ) obj = document.getElementById(layerID);

		// If the object itself was passed
		else if ( typeof(layerID) == "object" ) obj = layerID;

		if ( obj ) obj.style.visibility = "visible";
	}
	// NS4
	else if ( document.layers ) document.layers[layerID].visibility = "show";
}


function hideLayer(layerID)
{
	if ( document.getElementById )
	{
		// If a string, get the object
		if ( typeof(layerID) == "string" ) obj = document.getElementById(layerID);

		// If the object itself was passed
		else if ( typeof(layerID) == "object" ) obj = layerID;

		if ( obj ) obj.style.visibility = "hidden";
	}
	// NS4
	else if ( document.layers ) document.layers[layerID].visibility = "hidden";
}


function toggleVisibility(obj)
{
	// If the ID was passed, find the object
	if ( typeof(obj) == "string" )
	{
		obj = document.getElementById(obj);
	}

	if ( obj )
	{
		if ( obj.style.visibility == "hidden" ) obj.style.visibility = "visible";
		else obj.style.visibility = "hidden";
	}
}



function moveLayer(layerID, x, y)
{
	var obj = null;

	if ( document.getElementById )
	{
		// If a string, get the object
		if ( typeof(layerID) == "string" ) obj = document.getElementById(layerID);

		// If the object itself was passed
		else if ( typeof(layerID) == "object" ) obj = layerID;

		if ( obj )
		{
			if ( x != null ) obj.style.left = x+"px";
			if ( y != null ) obj.style.top = y+"px";
		}
	}
	// NS4
	else if ( document.layers ) document.layers[layerID].moveTo(x,y);
}


// Get the width of an object
function getWidth(obj)
{
	var width = null;

	if ( document.getElementById )
	{
		// If the ID was passed, find the object
		if ( typeof(obj) == "string" )
		{
			obj = document.getElementById(obj);
		}

		if ( typeof(obj) == "object" )
		{
//			width = getElementStyle(obj, "width", "width");						// unreliable.  make sure display != "none" and use offsetWidth
			width = "auto";

			if ( width == "auto" && document.all )
			{
				width = obj.offsetWidth;										// IE
				if ( width == 0 ) width = obj.clientWidth;						// IE backup
			}
			else if ( width == "auto" ) width = obj.offsetWidth;				// MOZ

			width = parseInt(width,10);
		}
	}
	return width;
}


// Get the height of an object
function getHeight(obj)
{
	var height = null;

	if ( document.getElementById )
	{
		// If the ID was passed, find the object
		if ( typeof(obj) == "string" )
		{
			obj = document.getElementById(obj);
		}

		if ( typeof(obj) == "object" )
		{
//			height = getElementStyle(obj, "height", "height");					// unreliable.  make sure display != "none" and use offsetHeight
			height = "auto";

			if ( height == "auto" && document.all )
			{
				height = obj.offsetHeight;										// IE
				if ( height == 0 ) height = obj.clientHeight;					// IE backup
			}
			else if ( height == "auto" ) height = obj.offsetHeight;				// Moz

			height = parseInt(height,10);
		}
	}
	return height;
}


// Determines an object's screen positin by looking at all of its parents positions
function findPosition(obj)
{
	var ret_obj = new Object;
	if ( obj.offsetParent )
	{
		for (var pos_x = 0, pos_y = 0; obj.offsetParent; obj = obj.offsetParent)
		{
			pos_x += obj.offsetLeft;
			pos_y += obj.offsetTop;
		}
		ret_obj.x = pos_x;
		ret_obj.y = pos_y;
		return ret_obj;
	}
	else
	{
		ret_obj.x = obj.x;
		ret_obj.y = obj.y;
		return ret_obj;
	}
}


function setClass(dest, newClass)
{
	var obj = null;

	if ( document.getElementById )
	{
		// If an object, set its class directly
		if ( typeof(dest) == "object" ) dest.className = newClass;

		// If a string, get the object and set its class
		else if ( typeof(dest) == "string" )
		{
			if ( obj = document.getElementById(dest) ) obj.className = newClass;
		}
	}
}


function setDisplay(dest, vis)
{
	var obj = null;

	if ( document.getElementById )
	{
		// If an object, set its display directly
		if ( typeof(dest) == "object" ) dest.style.display = vis;

		// If a string, get the object and set its display
		else if ( typeof(dest) == "string" )
		{
			if ( obj = document.getElementById(dest) ) obj.style.display = vis;
		}
	}
}


function setStatus(status)
{
	window.status = status;
	return true;
}


function toggleDisplay(dest)
{
	var obj = null;

	if ( document.getElementById )
	{
		// If a string, get the object and set its display
		if ( typeof(dest) == "string" ) dest = document.getElementById(dest);

		if ( dest )
		{
			if ( dest.style.display == "none" ) setDisplay(dest, "block");
			else setDisplay(dest, "none");
		}
	}
}


function toggleDisplay(dest)
{
	var elem, vis;

	// If a string, get the object and set its display
	if ( typeof(dest) == "string" )
	{
		if( document.getElementById )	elem = document.getElementById(dest);	// this is the way the standards work
		else if( document.all )			elem = document.all[dest];				// this is the way old msie versions work
	}
	vis = elem.style;

	// if the style.display value is blank we try to figure it out here
	if (vis.display=='' && elem.offsetWidth!=undefined && elem.offsetHeight!=undefined )
		vis.display = (elem.offsetWidth!=0 && elem.offsetHeight!=0) ? 'block' : 'none';
	vis.display = (vis.display=='' || vis.display=='block') ? 'none' : 'block';
}


// Gets an element's style assigned via a stylesheet or link
function getElementStyle(obj, IEStyleProp, CSSStyleProp)
{
	if ( document.getElementById )
	{
		// If the ID was passed, find the object
		if ( typeof(obj) == "string" )
		{
			obj = document.getElementById(obj);
		}
	
		if ( obj.currentStyle )
		{
			return obj.currentStyle[IEStyleProp];
		}
		else if ( window.getComputedStyle )
		{
			var compStyle = window.getComputedStyle(obj, "");
			return compStyle.getPropertyValue(CSSStyleProp);
		}
		else if (document.defaultView && document.defaultView.getComputedStyle)		// for safari
		{
			var compStyle = document.defaultView.getComputedStyle(obj,"")
			return compStyle.getPropertyValue(CSSStyleProp);
		}
	}
	return "";
}


// Finds the first child node of the given tag type with the given id.
// If the node does not have an id, then the name will be checked.
// Returns the node if found, null otherwise
//  [compare_type] is optional.  It determines how the ids are compared.
//    undefined = "equal" - the ids must match completely
//    "starts_with"       - the id must begin with the node_id value
function findChildByTagAndId(node, tag, node_id, compare_type)
{
	if ( compare_type === undefined ) compare_type = "equals";
	else compare_type = trim(compare_type.toLowerCase());

	if ( document.getElementById )
	{
		var nodes = node.getElementsByTagName(tag);
		for ( var i=0; i<nodes.length; i++ )
		{
			if ( nodes[i].id || nodes[i].name )
			{
				switch (compare_type)
				{
					case "starts_with":
						if ( (nodes[i].id && nodes[i].id.indexOf(node_id) == 0) || (nodes[i].name && nodes[i].name.indexOf(node_id) == 0) )
						{
							return nodes[i];
							break;
						}
					case "equals":
					default:
						if ( (nodes[i].id && nodes[i].id == node_id) || (nodes[i].name && nodes[i].name == node_id) )
						{
							return nodes[i];
							break;
						}
				}
			}
		}
	}
	return null;
}

// Finds the children nodes of the given parent node with the given id.
// If the nodes do not have an id, then their names will be checked.
// Returns an array of nodes if found, null otherwise.
//  [depth] is how deep in the tree to look.  If undefined, then no limit.
//  [compare_type] is optional.  It determines how the ids are compared.
//    undefined = "equal" - the ids must match completely
//    "starts_with"       - the id must begin with the node_id value
function findChildrenById(node, node_id, compare_type, depth)
{
	var results = new Array();

	if ( compare_type === undefined ) compare_type = "equals";
	else compare_type = trim(compare_type.toLowerCase());

	if ( depth === undefined ) depth = -1;
	else if ( depth > 0 ) depth--;

	if ( !node || !node.hasChildNodes() ) return results;
	else
	{
		var child = node.firstChild;
		while (child != null )
		{
			if ( child.id || child.name )
			{
				switch (compare_type)
				{
					case "starts_with":
						if ( (child.id && child.id.indexOf(node_id) == 0) || (child.name && child.name.indexOf(node_id) == 0) )
						{
							results[results.length] = child;
						}
						break;
					case "ends_with":
						if ( (child.id && child.id.lastIndexOf(node_id) == (child.id.length - node_id.length))
						  || (child.name && child.name.lastIndexOf(node_id) == (child.name.length - node_id.length)) )
						{
							results[results.length] = child;
						}
						break;
					case "equals":
					default:
						if ( (child.id && child.id == node_id) || (child.name && child.name == node_id) )
						{
							results[results.length] = child;
						}
						break;
				}
			}
			if ( child.hasChildNodes() && depth != 0 )
			{
			    result = findChildrenById(child, node_id, compare_type, depth);
				results = results.concat(result);
			}
			child = child.nextSibling;
		}
		return results;
	}
}


// Prevents an event from bubbling up any more.  Used on elements that catch an event (onclick),
// use the event, and want the event from not being applied to any containing elements that have 
// an event handler.
function endEventBubble(evt)
{
	var evt = (evt) ? evt : ((window.event) ? event : null);
	evt.cancelBubble = true;
}


// Custom hasAttribute function since Moz supports it and IE6 does not
function hasAttribute(obj, attribute)
{
	if ( obj.hasAttribute ) return obj.hasAttribute(attribute);
	else if ( obj.getAttribute )
	{
		var value = obj.getAttribute(attribute);
		if ( value == null ) return false;
		else return true;
	}
	else return false;
}

// Returns true if the object has the given attribute with the given value.  False
// if the attriute does not exist or if the value is different.
function hasAttributeValue(obj, attribute, val)
{
	if ( obj.getAttribute )
	{
		var value = obj.getAttribute(attribute);
		if ( value == null ) return false;
		else
		{
			if ( typeof val == "number" )
			{
				if ( parseInt(value) == val ) return true;
				else return false;
			}
			else if ( typeof val == "string" )
			{
				if ( value.toLowerCase() == val.toLowerCase() ) return true;
				else return false;
			}
		}
	}
	else return false;
}


// Strip whitespace (space, tabs) from the beginning and end of a string
function trim(txt)
{
	// Handle special cases
	if ( !txt || txt == "" || txt == null || txt === undefined) return "";
	else
	{
		txt = txt.replace(/^[\s\t]*/, "");
		txt = txt.replace(/[\s\t]*$/, "");
		return txt;
	}
}


function isArray(obj)
{
	return( typeof(obj.length) == "undefined" ) ? false : true;
}
