// toggles visibility of element
// returns true if element is show, false if hidden
function toggleDisplay(element) {
	var el = (typeof(element) == "string") ? document.getElementById(element) : element;
	el.style.display = (el.style.display == "none") ? el.style.display = "" : el.style.display = "none";
	
	return (el.style.display == "");
}

// hides element
function hide(element) {
	var el = (typeof(element) == "string") ? document.getElementById(element) : element;
	el.style.display = "none";
}

// shows element
function show(element) {
	var el = (typeof(element) == "string") ? document.getElementById(element) : element;
	el.style.display = "";
}

// adds CSS class to element
function addClass(element, classValue) {
    if (element == null)
        return false;
	var pattern = new RegExp("(^| )" + classValue + "( |$)");

	if (!pattern.test(element.className)) {
		if (element.className == "") {
			element.className = classValue;
		} else {
			element.className += " " + classValue;
		}
		
		return true;
	} else {
		return false;
	}
}

// removes CSS class from element
function removeClass(element, classValue) {
	var removedClass = element.className;
	var pattern = new RegExp("(^| )" + classValue + "( |$)");

	removedClass = removedClass.replace(pattern, "$1");
	removedClass = removedClass.replace(/ $/, "");

	element.className = removedClass;

	return true;
}

function IntOnlyCheck(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    status = "";
    return true;
}

// gets array of elements that have given CSS class
// via http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
function getElementsByClassName(oElm, strTagName, oClassNames){
	var arrElements = (strTagName == "*" && document.all) ? document.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var arrRegExpClassNames = new Array();
	if (typeof oClassNames == "object"){
		for (var i = 0; i < oClassNames.length; i++){
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
		}
	} else {
		arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
	}
	var oElement;
	var bMatchesAll;
	for (var j=0; j<arrElements.length; j++) {
		oElement = arrElements[j];
		bMatchesAll = true;

		for (var k=  0; k < arrRegExpClassNames.length; k++) {
			if (!arrRegExpClassNames[k].test(oElement.className)) {
				bMatchesAll = false;
				break;                      
			}
		}
		
		if (bMatchesAll) {
			arrReturnElements.push(oElement);
		}
	}
	
	return (arrReturnElements)
}

// toggles li element CSS class
/*
	listel		: id of the container UL
	anchorel	: anchor element that was clicked
	cssClass	: css class name to be applied
*/
function toggleULNav(listel, anchorel, cssClass) {
	var ul = document.getElementById(listel);
	
	// get all anchor tags inside list
	var items = ul.getElementsByTagName("a");
	
	// remove cssClass from each child
	for (i = 0; i < items.length; i++) {
		removeClass(items[i], cssClass);
	}
	
	// add cssClass to itemel (of type LI)
	addClass(anchorel, cssClass);
}

// shows element at cursor location
function showElementAtCursor(element, event) {
	var el = (typeof(element) == "string") ? document.getElementById(element) : element;

	var winScrollY = (isNaN(parseInt(document.documentElement.scrollTop))) ? 0 : parseInt(document.documentElement.scrollTop);

	var x = event.clientX + 20;
	var y = event.clientY + winScrollY;
	
	var winW = document.body.offsetWidth;
	var winH = document.body.offsetHeight;
	
	var elementW = (isNaN(parseInt(el.style.width))) ? 0 : parseInt(el.style.width);
	var elementH = (isNaN(parseInt(el.style.height))) ? 0 : parseInt(el.style.height);
	
	// determine x position
	if (x - elementW < 0) {
		x += 10;
	} else {
		x -= (elementW + 10);
	}
	
	el.style.display = "";
	el.style.top = y + "px";
	el.style.left = x + "px";
}

// add functions to window onload event
function addWindowLoadEvent(func) {
	var oldonload = window.onload;
	
	if (typeof(window.onload) != "function") {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

// inserts element after target element in DOM
function insertAfter(newElement, targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement, targetElement.nextSibling);
	}
}

// returns next element
// usage - getNextElement(element.nextSibling)
function getNextElement(node) {
	if (node.nodeType == 1)
		return node;
		
	if (node.nextSibling)
		return getNextElement(node.nextSibling);
		
	return null;
}

// adds CSS class to every other row in given table
function stripeTable(tableID, stripeClass) {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById(tableID)) return false;
	
	var table = document.getElementById(tableID);
	var odd = false;
	var rows = table.getElementsByTagName("tr");
	for (var j = 0; j < rows.length; j++) {
		if (odd == true) {
			addClass(rows[j], stripeClass);
			odd = false;
		} else {
			odd = true;
		}
	}
}

// adds CSS class to <TR> for mouse over effect
function highlightRows(onClass, offClass) {
	if (!document.getElementsByTagName) return false;
	
	var rows = document.getElementsByTagName("tr");
	for (var i = 0; i < rows.length; i++) {
		rows[i].onmouseover = function() {
			addClass(this, onClass);
			removeClass(this, offClass);
		}
		
		rows[i].onmouseout = function() {
			addClass(this, offClass);
			removeClass(this, onClass);
		}
	}
}

// animate element
function moveElement(elementID, final_x, final_y, interval) {
	if (!document.getElementById) return false;
	if (!document.getElementById(elementID)) return false;
	
	var elem = document.getElementById(elementID);
	
	if (elem.movement)
		clearTimeout(elem.movement);
		
	if (!elem.style.left)
		elem.style.left = "0px";
	if (!elem.style.top)
		elem.style.top = "0px";
	
	var xpos = parseInt(elem.style.left);
	var ypos = parseInt(elem.style.top);
	var dist;
	
	if (xpos == final_x && ypos == final_y)
		return true;
		
	if (xpos < final_x) {
		dist = Math.ceil((final_x - xpos)/10);
		xpos = xpos + dist;
	}
	if (xpos > final_x) {
		dist = Math.ceil((xpos - final_x)/10);
		xpos = xpos - dist;
	}
	if (ypos < final_y) {
		dist = Math.ceil((final_y - ypos)/10);
		ypos = ypos + dist;
	}
	if (ypos > final_y) {
		dist = Math.ceil((ypos - final_y)/10);
		ypos = ypos - dist;
	}
		
	elem.style.left = xpos + "px";
	elem.style.top = ypos + "px";
	
	var repeat = "moveElement('" + elementID + "', " + final_x + ", " + final_y + ", " + interval + ")";
	elem.movement = setTimeout(repeat, interval);
}

