/** 
 * Common Javascript functions library
 * 
 * Functions in this file:
 * 
 * function addEvent(elementObject, eventType, functionString);
 * function catchEnter(evt)
 * function getMousePositionRelativeToDocument(evt)
 * function removeElementIfExists(tempElementId)
 * function createDropDownBoxSmooth(posX, posY, sizeX, sizeY, elementId, parentId, timeToDraw, layerLevel, followupFunction, boxStyle, boxClass)
 * function createDropDownBoxSmoothHelper(currentX, currentY, incrementX, incrementY, elementId, iterationLeft, timeOut, finalX, finalY, followupFunction)
 * function createDropDownBoxFadeIn(posX, posY, sizeX, sizeY, startOpacity, finalOpacity, elementId, parentId, timeToDraw, layerLevel, followupFunction, boxStyle, boxClass)
 * function createDropDownBoxFadeInHelper(currentOpacity, incrementOpacity, elementId, iterationLeft, timeOut, finalOpacity, followupFunction)
 * 
 */

function addEvent(elementObject, eventType, functionString, useCapture)
{
	if (elementObject == null)
	{
		return false;
	}
	if (document.addEventListener)
	{
		// Used by All Mozilla, Safari, Opera 7+ and DOM 2 Compliant
		elementObject.addEventListener(eventType, functionString, useCapture);
		return true;
	}
	if (document.attachEvent)
	{
		// Used by IE 5+ and Opera 7+
		elementObject.attachEvent(("on" + eventType), functionString);
		return true;
	}
	return false;
}

/**
 * Returns true if the key pressed was "Enter"
 * Prevents default action from being executed as well 
 *
 * Suggested usage: onkeypress="if (catchEnter(event)) { doSomething(); }"
 */
function catchEnter(evt)
{
	// 13 is the ASCII value of an "Enter"
	if (evt.keyCode == 13)
	{
		// Prevents default action from Occuring in IE5+
		if (evt.returnValue)
		{
			evt.returnValue = false;
		}

		// Prevents default action from Occuring in NN6+
		if (evt.preventDefault)
		{
			evt.preventDefault();
		}
		return true;
	}
	
	// Key was not "Enter" so return false
	return false;
}

/**
 * Returns an Array with the mouse position relative to the document
 * First element is the X position, Second Element is the Y position
 *
 * Suggested usage: onclick="pos = getMousePositionRelativeToDocument(event); alert(pos[0] + ',' + pos[1]);"
 */
function getMousePositionRelativeToDocument(evt)
{
	var returnArray = new Array();
	returnArray[0] = 0;
	returnArray[1] = 0;
	
	// Let's make sure the event exists.
	// If it doesn't, we'll try to pull the event from window.event
	if (!evt)
	{
		var evt = window.event;
	}
	
	// This code segment works for Opera 7 and all Mozilla, Safari browsers
	if (evt.pageX || evt.pageY)
	{
		returnArray[0] = evt.pageX;
		returnArray[1] = evt.pageY;
	}
	
	// While this should work in most browsers, the scrollLeft & scrollTop is more of kludge for IE
	else if (evt.clientX || evt.clientY)
	{
		returnArray[0] = evt.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		returnArray[1] = evt.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	
	return returnArray;
}

/**
 * Removes an Element from the DOM
 *
 * Compatible with IE5+, Mozilla, Safari, Opera 7+, and DOM 2.0
 */
function removeElementIfExists(tempElementId)
{
	// Sanity Check
	if ( (document.getElementById(tempElementId) != null) && (document.getElementById(tempElementId).parentNode != null) )
	{
		// Element exists, as does parentNode, so let's remove element
		document.getElementById(tempElementId).parentNode.removeChild(document.getElementById(tempElementId));
	}
}

/**
 * Creates a smooth drop down box
 * 
 * Variables:
 * posX - X coordinate Position, in pixels, relative to body of document
 * posY - Y coordinate Position, in pixels, relative to body of document
 * sizeX - Full width, in pixels, of desired box
 * sizeY - Full height, in pixels of desired box
 * elementId - the ID tag of the div box
 * parentId - ID of the parent container
 * timeToDraw - time in ms to draw the box - WARNING: Isn't accurate, suggest giving 500 if you wanted 1000ms
 * layerLevel - z-index value of the div box.  Needed to determine how "important" the box is
 * followupFunction - string of a function to run once the box is fully enlarged
 * boxStyle - string of style settings to be applied to the div
 * boxClass - string of class settings to be applied to the div
 * boxPosition - position value of box - defaults to "absolute"
 *
 * Suggested usage: 
 * onclick="createDropDownBoxSmooth(100, 200, 120, 240, 'sillyDiv', 'parentBody', 500, 50, 'doSomething();', 'background-color: red;', 'sillyDivClass');"
 */
function createDropDownBoxSmooth(posX, posY, sizeX, sizeY, elementId, parentId, timeToDraw, layerLevel, followupFunction, boxStyle, boxClass, boxPosition)
{
	// Variable declarations
	var tempDiv;
	var deltaX;
	var deltaY;
	var steps;
	var timeBetweenSteps = 10;
	
	// Find out what the increment will be and how many draws there will be
	deltaX = (sizeX * timeBetweenSteps / timeToDraw);
	deltaY = (sizeY * timeBetweenSteps / timeToDraw);
	steps = Math.floor(timeToDraw / timeBetweenSteps);
	
	// Let's make sure the element is actually unique
	if (document.getElementById(elementId))
	{
		return false;
	}

	// Let's draw the box
	tempDiv = document.createElement("div");
	tempDiv.setAttribute("id", elementId);
	tempDiv.setAttribute("class", boxClass);
	tempDiv.style.cssText = boxStyle;
	if (boxPosition == "")
	{
		tempDiv.style.position = "absolute";
	}
	else
	{
		tempDiv.style.position = boxPosition;
	}
	tempDiv.style.left = (posX + "px");
	tempDiv.style.top = (posY + "px");
	tempDiv.style.height = 0 + "px";
	tempDiv.style.width = 0 + "px";
	tempDiv.style.zIndex = layerLevel;
	document.getElementById(parentId).appendChild(tempDiv);

	// Set timeout that will call helper function to enlarge the box
	setTimeout(("createDropDownBoxSmoothHelper(0, 0, " + deltaX + ", " + deltaY + ", '" + elementId + "', " + steps + ", " + timeBetweenSteps + ", " + sizeX + ", " + sizeY + ", '" + followupFunction + "');"), timeBetweenSteps);
}

/**
 * Helper function to help enlarge a div box
 * 
 * Variables:
 * currentX - Current width in pixels
 * currentY - Current height in pixels
 * incrementX - increment to width, in pixels, that should be applied on every iteration
 * incrementY - increment to height, in pixels, that should be applied on every iteration
 * elementId - the ID tag of the div box
 * iterationLeft - the number of iterations left to be done
 * timeOut - the time in ms between each step - WARNING this is not that accurate
 * finalX - Full width, in pixels, of desired box
 * finalY - Full height, in pixels of desired box
 * followupFunction - string of a function to run once the box is fully enlarged
 *
 * Suggested usage: 
 * setTimeout(("createDropDownBoxSmoothHelper(0, 0, 10, 20, 'sillyDiv', 12, 19, 120, 240, 'doSomething();');"), 10);
 */
function createDropDownBoxSmoothHelper(currentX, currentY, incrementX, incrementY, elementId, iterationLeft, timeOut, finalX, finalY, followupFunction)
{
	
	if (document.getElementById(elementId) == null)
	{
		// The div cannot be found - no need to throw an error
		return false;
	}
	
	// Retrieve div
	var tempDiv = document.getElementById(elementId);

	if (iterationLeft <= 0)
	{
		// Last iteration - ensure that the width and height were the final desired height/width
		tempDiv.style.width = Math.ceil(finalX) + "px";
		tempDiv.style.height = Math.ceil(finalY) + "px";
		if (followupFunction != "")
		{
			// If followupFunction is set, we should evaluate it
			eval(followupFunction);
		}
		return;
	}

	// currentX & currentY should be considered float values
	currentX = currentX + incrementX;
	currentY = currentY + incrementY;
	
	// Round up for actual drawing
	tempDiv.style.width = Math.ceil(currentX) + "px";
	tempDiv.style.height = Math.ceil(currentY) + "px";
	
	// Set timeout that will call itself again to enlarge the box again
	setTimeout(("createDropDownBoxSmoothHelper(" + currentX + ", " + currentY + ", " + incrementX + ", " + incrementY + ", '" + elementId + "', " + (iterationLeft - 1) + ", " + timeOut + ", " + finalX + ", " + finalY + ", '" + followupFunction + "');"), timeOut);

}

/**
 * Creates a fade in drop down box
 * 
 * Variables:
 * posX - X coordinate Position, in pixels, relative to body of document
 * posY - Y coordinate Position, in pixels, relative to body of document
 * sizeX - Width, in pixels, of box
 * sizeY - Height, in pixels, of box
 * startOpacity - Start Opacity of the box
 * finalOpacity - Final Opacity of the box
 * elementId - the ID tag of the div box
 * parentId - ID of the parent container
 * timeToDraw - time in ms to draw the box - WARNING: Isn't accurate, suggest giving 500 if you wanted 1000ms
 * layerLevel - z-index value of the div box.  Needed to determine how "important" the box is
 * followupFunction - string of a function to run once the box is fully enlarged
 * boxStyle - string of style settings to be applied to the div
 * boxClass - string of class settings to be applied to the div
 * boxPosition - position value of box - defaults to "absolute"
 *
 * Suggested usage: 
 * onclick="createDropDownBoxFadeIn(100, 200, 240, 360, 0, 0.5, 'sillyDiv', 'parentBody', 500, 50, 'doSomething();', 'background-color: blue;', 'sillyDivClass');"
 */
function createDropDownBoxFadeIn(posX, posY, sizeX, sizeY, startOpacity, finalOpacity, elementId, parentId, timeToDraw, layerLevel, followupFunction, boxStyle, boxClass, boxPosition)
{
	
	// Variable declarations
	var tempDiv;
	var deltaOpacity;
	var steps;
	var timeBetweenSteps = 10;
	
	// Find out what the increment will be and how many draws there will be
	deltaOpacity = ((finalOpacity-startOpacity) * timeBetweenSteps / timeToDraw);
	steps = Math.floor(timeToDraw / timeBetweenSteps);
	
	// Let's make sure the element is actually unique
	if (document.getElementById(elementId))
	{
		return false;
	}

	// Let's draw the box
	tempDiv = document.createElement("div");
	tempDiv.setAttribute("id", elementId);
	tempDiv.setAttribute("class", boxClass);
	tempDiv.style.cssText = boxStyle;
	if (boxPosition == "")
	{
		tempDiv.style.position = "absolute";
	}
	else
	{
		tempDiv.style.position = boxPosition;
	}
	tempDiv.style.left = (posX + "px");
	tempDiv.style.top = (posY + "px");
	tempDiv.style.width = sizeX + "px";
	tempDiv.style.height = sizeY + "px";
	
	if (tempDiv.style.opacity != null)
	{
		// Compatible with Mozilla 1.7.2+, Safari 1.2+, Opera 9+, CSS3
		// Preferred method
		tempDiv.style.opacity = startOpacity;
	}
	else if (tempDiv.style.MozOpacity != null)
	{
		// Compatible with all Mozilla
		tempDiv.style.MozOpacity = startOpacity;
	}
	else if (tempDiv.style.filter != null)
	{
		// Compatible with IE5.5+
		tempDiv.style.filter = "alpha(opacity=" + Math.ceil(startOpacity*100) + ")";
	}
	tempDiv.style.zIndex = layerLevel;
	document.getElementById(parentId).appendChild(tempDiv);

	// Set timeout that will call itself again to adjust opacity of the box again
	setTimeout(("createDropDownBoxFadeInHelper(" + startOpacity + ", " + deltaOpacity + ", '" + elementId + "', " + steps + ", " + timeBetweenSteps + ", " + finalOpacity + ", '" + followupFunction + "');"), timeBetweenSteps);

}

/**
 * Helper function to help fade in a div box
 * 
 * Variables:
 * currentOpacity - Current opacity of the box
 * incrementOpacity - increment to opacity that should be applied on every iteration
 * elementId - the ID tag of the div box
 * iterationLeft - the number of iterations left to be done
 * timeOut - the time in ms between each step - WARNING this is not that accurate
 * finalOpacity - Final Opacity of the box
 * followupFunction - string of a function to run once the box is fully enlarged
 *
 * Suggested usage: 
 * setTimeout(("createDropDownBoxSmoothHelper(0, 0.05, 'sillyDiv', 10, 10, 0.5, 'doSomething();');"), 10);
 */
function createDropDownBoxFadeInHelper(currentOpacity, incrementOpacity, elementId, iterationLeft, timeOut, finalOpacity, followupFunction)
{
	
	if (document.getElementById(elementId) == null)
	{
		// The div cannot be found - no need to throw an error
		return false;
	}
	
	// Retrieve div
	var tempDiv = document.getElementById(elementId);

	if (iterationLeft <= 0)
	{
		// Last iteration - ensure that the width and height were the final desired height/width
		if (tempDiv.style.opacity != null)
		{
			// Compatible with Mozilla 1.7.2+, Safari 1.2+, Opera 9+, CSS3
			// Preferred method
			tempDiv.style.opacity = finalOpacity;
		}
		else if (tempDiv.style.MozOpacity != null)
		{
			// Compatible with all Mozilla
			tempDiv.style.MozOpacity = finalOpacity;
		}
		else if (tempDiv.style.filter != null)
		{
			// Compatible with IE5.5+
			tempDiv.style.filter = "alpha(opacity=" + Math.ceil(finalOpacity*100) + ")";
		}

		if (followupFunction != "")
		{
			// If followupFunction is set, we should evaluate it
			eval(followupFunction);
		}
		return;
	}

	// currentOpacity & incrementOpacity should be considered float values
	currentOpacity = currentOpacity + incrementOpacity;
	
	if (tempDiv.style.opacity != null)
	{
		// Compatible with Mozilla 1.7.2+, Safari 1.2+, Opera 9+, CSS3
		// Preferred method
		tempDiv.style.opacity = currentOpacity;
	}
	else if (tempDiv.style.MozOpacity != null)
	{
		// Compatible with all Mozilla
		tempDiv.style.MozOpacity = currentOpacity;
	}
	else if (tempDiv.style.filter != null)
	{
		// Compatible with IE5.5+
		tempDiv.style.filter = "alpha(opacity=" + Math.ceil(currentOpacity*100) + ")";
	}
	
	// Set timeout that will call itself again to enlarge the box again
	setTimeout(("createDropDownBoxFadeInHelper(" + currentOpacity + ", " + incrementOpacity + ", '" + elementId + "', " + (iterationLeft - 1) + ", " + timeOut + ", " + finalOpacity + ", '" + followupFunction + "');"), timeOut);

}

