
// *******************************
// * DOM and table Utilities
// ******************************* 

function getParent(node, name) {
	var currentNode=node;
	while (currentNode!=null) {
		if (currentNode.nodeName==name) return currentNode;
		currentNode=currentNode.parentNode;
	}
	return null;
}

/** We will try to get the first child by looking in the first branch.
*/
function getChildByName(node, name) {
	var currentNode=node;
	while (currentNode!=null) {
		if (currentNode.nodeName==name) return currentNode;

		if (currentNode.childNodes.length==0) {
			currentNode=currentNode.nextSibling;
		} else {
			currentNode=currentNode.firstChild;
		}
	}
	return null;
}


function insertAfter(newChild,refChild) {
  var parent=refChild.parentNode;
  if (parent.lastChild==refChild) return parent.appendChild(newChild);
  else return parent.insertBefore(newChild,refChild.nextSibling);
}

// We have some problem with style and the difference between internet-explorer and safari (and other)
// of course internet explorer is not complient ...
var tableRowStyle='table-row'; // this is the correct value is CSS2 ...
if (navigator.userAgent.toLowerCase().indexOf('ie')!=-1) tableRowStyle='block';
var tableCellStyle='table-cell'; // this is the correct value is CSS2 ...
if (navigator.userAgent.toLowerCase().indexOf('ie')!=-1) tableCellStyle='block';

/** Hide the current row and show the next row by changing the style */
function showRow(currentObject) {
	currentRow=getParent (currentObject, "TR");
	nextRow=currentRow.nextSibling;
	currentRow.style.display='none';
	nextRow.style.display=tableRowStyle;
}

/** Hide the current row and show the previous row by changing the style */
function hideRow(currentObject) {
	currentRow=getParent (currentObject, "TR");
	previousRow=currentRow.previousSibling;
	currentRow.style.display='none';
	previousRow.style.display=tableRowStyle;
}

/** Hide/show all objects with ID=power or name=power !!! (it seems it depends of browsers).
* We will use a funny way to find out if it is show or hide. We will check the value of the field
* and then change the value (replace Show/Hide or vice versa). */
/**
*function switchCellPowerName(currentObject) {
	if (currentObject.innerHTML.indexOf("Hide")!=-1) {
		newStyle='none';
		currentObject.innerHTML=currentObject.innerHTML.replace("Hide","Show");
	} else {
		newStyle=tableCellStyle;
		currentObject.innerHTML=currentObject.innerHTML.replace("Show","Hide");
	}
	powerObjects=document.getElementsByName("power");
	 for(i=0;i<powerObjects.length;i++) {
		powerObjects[i].style.display=newStyle;
	}
}
*/

/** We send a comma delimited list of object names */

function toggleRows(names) {
	allNames=names.split(",");
	for (var i=0; i<allNames.length; i++) {
		currentObject=document.getElementById(allNames[i]);
		if (currentObject) {
			toggleNextRows(currentObject);
		}
	}
}


/** Hide/show all the next rows that have the same id as the next row.
* Also invert the picture name (close.gif or open.gif).
*/
function toggleNextRows(currentObject) {
	currentRow=getParent (currentObject, "TR");
	// should we open or close ?
	isOpen=false;
	if (currentRow.innerHTML.indexOf("Hide")!=-1) {
		isOpen=true;
	} else if (currentRow.innerHTML.indexOf("close")!=-1) {
		isOpen=true;
	} 
	
	allText=getAllTextChildElements(currentRow);
	allImages=currentRow.getElementsByTagName('IMG');

	if (isOpen) {
		for(i=0;i<allText.length;i++) {
			allText[i].nodeValue=allText[i].nodeValue.replace("Hide", "Show");
		}
		for(i=0;i<allImages.length;i++) {
			allImages[i].src=allImages[i].src.replace("close", "open");
		}
	} else {
		for(i=0;i<allText.length;i++) {
			allText[i].nodeValue=allText[i].nodeValue.replace("Show", "Hide");
		}
		for(i=0;i<allImages.length;i++) {
			allImages[i].src=allImages[i].src.replace("open", "close");
		}
	}

	currentRow=currentRow.nextSibling;
	id=currentRow.id;

	while (id==currentRow.id) {
	
		if (isOpen) {
			currentRow.style.display='none';
		} else {
			currentRow.style.display=tableRowStyle;
		}
		currentRow=currentRow.nextSibling;
		if ((! currentRow) || (currentRow.id=="")) break;
	}
}
/** Hide/show all the cells from the current row that is not the current row.
* Also invert the picture name (close.gif or open.gif).
*/
/*
function toggleThisRow(currentObject) {
	// should we open or close ?
	currentRow=getParent(currentObject, "TR");
	currentCell=getParent(currentObject, "TD");
	if (currentObject.innerHTML.indexOf("Hide")!=-1) {
		newStyle='none';
		currentObject.innerHTML=currentObject.innerHTML.replace("Hide","Show");
	} else {
		newStyle=tableCellStyle;
		currentObject.innerHTML=currentObject.innerHTML.replace("Show","Hide");
	}
	 for(i=0;i<currentRow.childNodes.length;i++) {
		if (currentRow.childNodes[i].tagName=="TD" && currentRow.childNodes[i]!=currentCell) {
			currentRow.childNodes[i].style.display=newStyle;
		}
	}
}
*/

/** Hide/show all the cells from the current row that is not the current row.
* Also invert the picture name (close.gif or open.gif).
*/
/*
function toggleThisRowImage(imgSource) {
	// should we open or close ?
	source=imgSource.src;
	currentRow=getParent(imgSource, "TR");
	currentCell=getParent(imgSource, "TD");
	if (source.indexOf("close")!=-1) {
		imgSource.src=source.replace("close", "open");
	} else {
		imgSource.src=source.replace("open", "close");
	}
	 for(i=0;i<currentRow.childNodes.length;i++) {
		if (currentRow.childNodes[i].tagName=="TD" && currentRow.childNodes[i]!=currentCell) {
			if (source.indexOf("close")!=-1) {
				currentRow.childNodes[i].style.display='none';
			} else {
				currentRow.childNodes[i].style.display=tableCellStyle;
			}
		}
	}
}
*/

/** Hide/show all the cells from the current row that are after the current cell.
* Also invert the picture name (close.gif or open.gif).
*/
function toggleNextCellsImage(imgSource) {
	// should we open or close ?
	source=imgSource.src;
	currentRow=getParent(imgSource, "TR");
	currentCell=getParent(imgSource, "TD");
	if (source.indexOf("close")!=-1) {
		imgSource.src=source.replace("close", "open");
	} else {
		imgSource.src=source.replace("open", "close");
	}
	change=false;
	 for(i=0;i<currentRow.childNodes.length;i++) {
		if (currentRow.childNodes[i].tagName=="TD" && change) {
			if (source.indexOf("close")!=-1) {
				currentRow.childNodes[i].style.display='none';
			} else {
				currentRow.childNodes[i].style.display=tableCellStyle;
			}
		}
		if (currentRow.childNodes[i]==currentCell) change=true;
	}
}

/** Will count the number of TR tags at the first level (childNodes) and that contains cell of type TD */
function countLines(tabBody) {
	var counter=0;
	var i=0;
	 for(i;i<tabBody.childNodes.length;i++) {
		if (tabBody.childNodes[i].tagName=="TR") 
			if (getChildElements(tabBody.childNodes[i],"TD").length>0) counter++;
	}
	return counter;
}



function getRows(tabBody) {
	return getChildElements(tabBody, "TR");
}




