/*
// $RCSfile: postgradjs.js,v $
// $Source: /cvs/postgradweb-live/postgradjs.js,v $, $Revision: 1.3 $, $Date: 2007/02/26 04:56:33 $, $State: Exp $ 
*/
// Expand and Collapse Tables

window.onload=function(){
	initExpandableTables(""); // no default open
}

// set global variables
var thisTable = "";
var thisBody = "";
var arrRows = "";

var expandAllText = "Expand All";
var expandAllClass = "expandAll";
var hideAllText = "Hide All";
var hideAllClass = "hideAll";

function initExpandableTables(defaultOpen)
{
	if(!document.getElementById) return false;
	if(!document.getElementById("expandingTable")) return false;
	thisTable = document.getElementById("expandingTable");
	
	// Get and save current table class
	var savedTableClass = thisTable.className;
	
	//temporarily hide the table
	thisTable.setAttribute("class","hide");
	thisTable.className = "hide"; //For IE - stupid browser!
	
	// Do row manipulation
	thisBody = thisTable.tBodies[0];
	arrRows = thisBody.rows;

	var currentSection = "";
	//arrRows = thisBody.getElementsByTagName("tr");
	
	for(i=0; i < arrRows.length; i++){
		if(arrRows[i].getAttribute("id")){
			currentSection = arrRows[i].getAttribute("id");
			// Create "a" element
			var aTag = document.createElement("a");
			aTag.setAttribute("href","#");
			aTag.setAttribute("id",currentSection+"A");
			aTag.setAttribute("title",currentSection);
			aTag.setAttribute("class","expand");
			aTag.className = "expand"; //For IE - stupid browser!
			
			// Create "span" element
			var spanTag = document.createElement("span");
			spanTagText = document.createTextNode("Expand");
			spanTag.appendChild(spanTagText);
			
			spanTag.setAttribute("class","hide");
			spanTag.className = "hide"; //For IE - stupid browser!
			
			aTag.appendChild(spanTag);

			// Set onClick event
			aTag.onclick = function(){
				showHideChildren(this);
				return false;
			}
			
			// Place new "a" element as first child of first TD
			firstTdTag = arrRows[i].getElementsByTagName("td")[0];
			firstTdTag.insertBefore(aTag,firstTdTag.firstChild);
		}
		else {
			if(!arrRows[i].getAttribute("title")){
				arrRows[i].setAttribute("title",currentSection);
			}
			arrRows[i].setAttribute("class", "hide");
			arrRows[i].className = "hide"; //For IE - stupid browser!
		}
	}
	
	// If there is a default open value set then open it
	if(defaultOpen){
		currentSection = defaultOpen;
		for(i=0; i < arrRows.length; i++){
			var thisRowTitle = arrRows[i].getAttribute("title");
			if(thisRowTitle == currentSection) {
				arrRows[i].setAttribute("class","");
				arrRows[i].className = ""; //For IE - stupid browser!	
			}
		}
		headerTr = document.getElementById(defaultOpen+"A");
		headerTr.setAttribute("class","contract");
		headerTr.className = "contract"; //For IE - stupid browser!
	}
	
	
	// Create and Add Expand/Hide all link
	var aTagExpandAll = document.createElement("a");
	aTagExpandAll.setAttribute("href","#");
	aTagExpandAll.setAttribute("class",expandAllClass);
	aTagExpandAll.className = expandAllClass; //For IE - stupid browser!
	aTagExpandAllText = document.createTextNode(expandAllText);
	aTagExpandAll.appendChild(aTagExpandAllText);
	
	var aTagHideAll = document.createElement("a");
	aTagHideAll.setAttribute("href","#");
	aTagHideAll.setAttribute("class",hideAllClass);
	aTagHideAll.className = hideAllClass; //For IE - stupid browser!
	aTagHideAllText = document.createTextNode(hideAllText);
	aTagHideAll.appendChild(aTagHideAllText);
	
	//Set onclick events 
	aTagExpandAll.onclick = function(){
		showHideAll(this);
		return false;
	}
	aTagHideAll.onclick = function(){
		showHideAll(this);
		return false;
	}
	
	// Get table head row
	firstThTag = thisTable.tHead.rows[0].cells[0]; 

	// Place the new "a" elements in the first TH
	firstThTag.insertBefore(aTagExpandAll,firstThTag.firstChild);
	firstThTag.insertBefore(aTagHideAll,firstThTag.firstChild);
	
	//replace saved table class
	thisTable.setAttribute("class",savedTableClass);
	thisTable.className = savedTableClass; //For IE - stupid browser!
	
	setSecondLevelLinks();
}

function showHideChildren(thisA){

	currentSection = thisA.getAttribute('title');
	for (var i=0; i<arrRows.length; i++) {         
		var thisRowTitle = arrRows[i].getAttribute("title");
		if(thisRowTitle == currentSection) {
			
			if(arrRows[i].getAttribute("class") == "hide"){
				if(thisRowTitle != "secondlevel"){
					arrRows[i].setAttribute("class","");
					arrRows[i].className = ""; //For IE - stupid browser!
					thisA.setAttribute("class","contract");
					thisA.className = "contract"; //For IE - stupid browser!
				}
			} else {
				arrRows[i].setAttribute("class","hide");
				arrRows[i].className = "hide"; //For IE - stupid browser!
				thisA.setAttribute("class","expand");
				thisA.className = "expand"; //For IE - stupid browser!
			}
		}
	}
}

function showHideAll(thisA){
	action = thisA.firstChild.nodeValue;
	
	for (var i=0; i<arrRows.length; i++) {
		if(arrRows[i].getAttribute("title")){
			if(action == expandAllText) {
				arrRows[i].setAttribute("class","");
				arrRows[i].className = ""; //For IE - stupid browser!
			} else {
				arrRows[i].setAttribute("class","hide");
				arrRows[i].className = "hide"; //For IE - stupid browser!	
			}				 
		} else {
			if(action == "Expand All"){
				arrRows[i].getElementsByTagName("a")[0].className = "contract";
			} else {
				arrRows[i].getElementsByTagName("a")[0].className = "expand";
			}
		}
	}
}

function showHideSecondLevel(thisA){
	//Get Node to show or hide
	secondLevel = thisA.parentNode.parentNode.nextSibling;
	if(secondLevel.className == "") {
		secondLevel.className = "hide";
	} else {
		secondLevel.className = "";
	}
	
	if(thisA.className == "expand level2"){
		thisA.className = "contract level2";
	} else {
		thisA.className = "expand level2";	
	}
}

function setSecondLevelLinks(){
	//Get all a's within expandable table
	thisTable = document.getElementById("expandingTable");
	allAs = thisTable.getElementsByTagName("a");
	
	for (var i=0; i<allAs.length; i++) {
		if(allAs[i].getAttribute("rel") == "secondLevelLink"){
			allAs[i].className = "expand level2";
			allAs[i].onclick = function(){
				showHideSecondLevel(this);
			}
		}
	}
}