// -- PURPOSE: to change the selected row on a form
// -- Usage: changeSelected(id of row to highlight, class to change it to)

var oldId = null;
function changeSelected(newId, newClass, oldClass) {
	if (oldId) {
		highlightRow(oldId, oldClass);
	}
	oldId = newId;
	highlightRow(newId, newClass);
}

// -- PURPOSE: to collapse all elements
// -- Usage: collapseAll(list of items to collapse)

function collapseAll() {
	for (var i=0; i<collapseAll.arguments.length; i++) {
		var element = document.getElementById(collapseAll.arguments[i]);
		if (element) {
			element.style.display = "none";
		}
	}
}

// -- PURPOSE: to copy all style elements from one item to another.
// -- Usage: copyStyle(element to be changed, element to copy from)

function copyStyle(toElement, fromElement) {
	var myStyle = fromElement.getAttribute("style");
	// a bug in IE returns [object] when you do getAttribute("style").  setAttribute("style") doesn't work in IE, either, so we use .cssText
	// Netscape has a read-only .cssText, so we have to check against getAttribute and .cssText support
	if (fromElement.style.cssText && myStyle == "[object]") {
		toElement.style.cssText = fromElement.style.cssText;
	}
	else if (myStyle) {
		toElement.setAttribute("style", myStyle, 0);
	}
}

// -- PURPOSE: to expand all elements
// -- Usage: expandAll(list of items to expand)

function expandAll() {
	for (var i=0; i<expandAll.arguments.length; i++) {
		var element = document.getElementById(expandAll.arguments[i]);
		while (element && element.tagName != "BODY") {
			if (element.style) {
				element.style.display = "block";
			}
			element = element.parentNode;
		}
	}
}

// -- PURPOSE: to expand collapsed elements or collapse expanded elements
// -- Usage: expandCollapse(list of items to expand/collapse)

function expandCollapse() {
	for (var i=0; i<expandCollapse.arguments.length; i++) {
		var element = document.getElementById(expandCollapse.arguments[i]);
		if (element) {
			if ( (element.style.display == "" && element.className.indexOf("collapsed") >= 0) || element.style.display == "none") {
				expandAll(expandCollapse.arguments[i]);
			}
			else {
				element.style.display = "none";
			}
		}
	}
}

// -- PURPOSE: to expand an item and collapse many others
// -- Usage: expandFirst(item to expand, list of items to collapse)

function expandFirst() {
	expandAll(expandFirst.arguments[0]);
	for (var i=1; i<expandFirst.arguments.length; i++) {
		collapseAll(expandFirst.arguments[i]);
	}
}

// -- PURPOSE: to get a particular style property's value from an element (Cross-browser version)
// -- Usage: getStyle(element with style to retrieve, style property to retrieve)
// -- NOTE: This is a function you'd most likely not call from HTML, but only from other JS functions

function getStyle(myElement, styleTag) {
	if (myElement.style.getAttribute) {
		styleTag = resetStyleTag(styleTag);
		return myElement.style.getAttribute(styleTag);
	}
	else if (myElement.getAttribute) {
		var tempStyle = myElement.getAttribute("style");
		var searchString = new RegExp(".*" + styleTag + ": ", "i");
		tempStyle = tempStyle.replace(searchString, "");
		searchString = new RegExp(";.*", "i");
		tempStyle = tempStyle.replace(searchString, "");
		return tempStyle;
	}
}
	
// -- PURPOSE: to change a group of elements to hidden
// -- Usage: hideAll(list of elements to hide);

function hideAll() {
	for (var i=0; i<hideAll.arguments.length; i++) {
		var element = document.getElementById(hideAll.arguments[i]);
		if (element) {
			element.style.visibility = "hidden";
		}
	}
}

// -- PURPOSE: to highlight the cells in a row where the checkbox is checked
// -- Usage: highlightChecked(form with checkboxes, class used when not highlighted, class used when highlighted)

function highlightChecked(myForm, normal, highlighted) {
	// myElement should be a checkbox element nested in a <td>, if it is checked, the row will be highlighted
	for (var i=0; i<myForm.elements.length; i++) {
		var myElement = myForm.elements[i];
		if (myElement.type == "checkbox" && myElement.parentNode.tagName == "TD" || myElement.parentNode.tagName == "td") {
			var kids = myElement.parentNode.parentNode.childNodes;
			for (var j=0; j<kids.length; j++){
				if (myElement.checked) {
					kids[j].className = highlighted;
				} else {
					kids[j].className = normal;
				}
			}
		}
	}
}

// -- PURPOSE: to highlight the cells in a row
// -- Usage: highlightRow(id of row to highlight, class to be assigned to each td in the row)

function highlightRow(rowId, rowClass) {
	var myRow = document.getElementById(rowId);
	if (myRow) {
		if (myRow.tagName == "TR" || myRow.tagName == "tr") {
			var kids = myRow.childNodes;
			for (var i=0; i<kids.length; i++){
				
				var pass = (!kids[i].id) ? true : (kids[i].id.indexOf("static") == -1) ? true : false;
				if (pass) {
					kids[i].className = rowClass;
				}
			}
		}
	}
}

// -- PURPOSE: resets a style property from hyphenated form to uppercase form (background-color to backgroundColor).
// -- NOTE: This is an internal function used be setStyle and getStyle and shouldn't be used directly.  It is only used for IE

function resetStyleTag(styleTag) {
	// IE uses the backgroundColor form instead of background-color when using setAttribute on element.style, so we have to parse it.
	// don't call this in Netscape because you have to user element.setAttribute("style") instead and it uses the background-color form
	var searchString = new RegExp("-([a-z])", "g");
	if (searchString.test(styleTag)) {
		return styleTag.replace(searchString, RegExp.$1.toUpperCase());
	}
	return styleTag;
}

// -- PURPOSE: to set the style of a particular item (Cross-browser version)
// -- Usage: setStyle(element to be changed, style property to be set, value to set property to)
// -- NOTE: This is a function you'd most likely not call from HTML, but only from other JS functions

function setOrderByClass(orderByFull,ascClass,descClass) {
	var searchString = new RegExp("([+ ]|%20|%2520)+DESC", "i");
	var orderBy = orderByFull.replace(searchString,"");
	var orderByElement = document.getElementById("order_by_"+orderBy);
	if (orderByElement) {
		if (orderBy != orderByFull) {
			orderByElement.className = descClass;
		} else {
			orderByElement.className = ascClass;
		}
	}
}
	
function setStyle(myElement, styleTag, styleValue) {
	// IE uses element.style.setAttribute and element.style.getAttribute
	if (currentElement.style.setAttribute && currentElement.style.removeAttribute) {
		styleTag = resetStyleTag(styleTag);
		if (styleValue) {
			currentElement.style.setAttribute(styleTag, styleValue);
		}
		else {
			currentElement.style.removeAttribute(styleTag, 0);
		}
	}
	// Netscape uses element.setAttribute("style") and element.getAttribute("style")
	else if (currentElement.setAttribute && currentElement.getAttribute) {
		var tempStyle = " " + currentElement.getAttribute("style");
		var searchString = new RegExp(" " + styleTag + "[^\;\"]+.", "i");
		if (searchString.test(tempStyle)) {
			var replaceString = (styleValue) ? styleTag + ": " + styleValue + "; " : "";
			tempStyle = tempStyle.replace(searchString, replaceString);
		}
		else {
			tempStyle = styleTag + ": " + styleValue + ";" + tempStyle;
		}
		currentElement.setAttribute("style", tempStyle, 0);
	}
}

// -- PURPOSE: to change a group of elements to visible
// -- Usage: showAll(list of elements to show);

function showAll() {
	for (var i=0; i<showAll.arguments.length; i++) {
		var element = document.getElementById(showAll.arguments[i]);
		if (element) {
			element.style.visibility = "visible";
		}
	}
}

// -- PURPOSE: to make an element visible and a list of elements invisible.
// -- Usage: showFirst(element to make visible, list of elements to hide)

function showFirst() {
	showAll(showFirst.arguments[0]);
	for (var i=1; i<showFirst.arguments.length; i++) {
		hideAll(showFirst.arguments[i]);
	}
}

// -- PURPOSE: to change a group of elements from visible to hidden or vice versa
// -- Usage: showHide(list of elements to show/hide);

function showHide() {
	for (var i=0; i<showHide.arguments.length; i++) {
		var element = document.getElementById(showHide.arguments[i]);
		if (element) {
			element.style.visibility = (element.style.visibility == "hidden") ? "visible" : "hidden";
		}
	}
}

