//  Parameters:
//    obj = reference to the object that will own the event, e.g. document.getElementById("text1").
//    event = the name of the event in quotation marks without an "on" prefix
//    function = reference to the function that will be attached
//    useCapture = true or false
function addEvent(obj, event, f, useCapture) {
    if (obj.addEventListener) {
        //  non-IE, DOM3 compliant browsers
        obj.addEventListener(event, f, useCapture);
    } else if (obj.attachEvent) {
        //  IE specific code
        x = obj.attachEvent("on" + event, f);
    } else {
        obj["on" + event] = f;
    }
}

function getIDFromEvent(e) {
	var id = e.target ? e.target.id : e.srcElement.id;
	return id;
}

function getTargetFromEvent(e) {
	var id = e.target ? e.target : e.srcElement;
	return id;
}

function openInfoPopup(url, name, height, width) {
    if (height==0) { height = 250; }
    if (width==0) { width = 500; }

    return window.open(url,name,"height=" + height + ",width=" + width + ",toolbar=no,titlebar=no,status=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no");
}

function removeAllChildNodes(parent) {
	while (parent.childNodes[0]) {
		parent.removeChild(parent.childNodes[0]);
	}
}

function removeChildNodesByTagName(parent, tag) {
	children = parent.getElementsByTagName(tag);
	for (i=children.length - 1; i>=0; i--) { 
		children[i].parentNode.removeChild(children[i]);
	} 
}

function random(lowerBound, upperBound, allowZero) {
	var result = 0;
	
	if (!allowZero) { 
		while (result == 0) {
			result = Math.ceil(Math.random() * (upperBound - lowerBound + 1) - Math.abs(lowerBound));
		}
	} else {
		result = Math.ceil(Math.random() * (upperBound - lowerBound + 1) - Math.abs(lowerBound));
	}

	return result;
}

//  Parameters:
//   tagName = the type of tag to be added, e.g. "a" or "span"
//   text = the text that should be displayed in the tag
//   parentID = the id of the tag to which the new tag should be added
//   Returns a reference to the new element.
function addNode(tagName, text, parent) {
    //	First create the new tag.
    var newNode = document.createElement(tagName);

    if (text != "") {
	    //	Create a new text node with the link's display text.
	    var newTextNode = document.createTextNode(text);
	
	    //	Add the text node as the link's first child.
	    newNode.appendChild(newTextNode);
    }
    
    //	Append the link to the paragraph in which it will be displayed.
    parent.appendChild(newNode);

    //	Return the node so the user can set any properties/attributes.
    return newNode;
}

function getCenteredTopPosition(obj) {
	return (document.body.clientHeight - obj.offsetHeight) / 2;
}

function getCenteredLeftPosition(obj) {
	return (document.body.clientWidth - obj.offsetWidth) / 2;
}
