// There are still a few bugs in this with regards to keeping the basic and advanced views in sync --
// the plus and minus boxes get out of synch, but the basic functionality with regards to opening and closing trees,
// and only cascading the open/expand  up what was open before, seems to work. 
function toggleRows(elm) {
    var rows = document.getElementsByTagName("TR");
    // elm.style.backgroundImage = "url(images/plus.gif)";
    elm.className = "folder-closed";
    var newDisplay = "none";
    var parentRow = elm.parentNode.parentNode.parentNode;
    var thisID = parentRow.id + "-";
    // Are we expanding or contracting? If the first child is hidden, we expand
     for (var i = 0; i < rows.length; i++) {
        var r = rows[i];
        if (matchStart(r.id, thisID, true)) {
            if (r.style.display == "none") {
                if (document.all) {
                    newDisplay = "block"; //IE4+ specific code
                } else {
                    newDisplay = "table-row"; //Netscape and Mozilla
                }
                // elm.style.backgroundImage = "url(images/minus.gif)";
                elm.className = "folder-open";
            }
            break;
        }
    }
    
    // When expanding, only expand one level.    Collapse all desendants.
    // var matchDirectChildrenOnly = (newDisplay != "none");
    var matchDirectChildrenOnly = false;
    
    var expanding = newDisplay != "none";
    var closedNode = "";
    
    for (var j = 0; j < rows.length; j++) {
        var s = rows[j];
        if (matchStart(s.id, thisID, matchDirectChildrenOnly)) {
        	var parentIsClosed = parentClosed(closedNode, s.id);
        	if ( expanding && parentIsClosed ) {
        		continue;
        	} else {
                s.style.display = newDisplay;
                // var cell = s.getElementsByTagName("TD")[0];
                // var tier = cell.getElementsByTagName("DIV")[0];
                // var folder = tier.getElementsByTagName("A")[0];
                // if (folder.getAttribute("onclick") != null) {
                    // folder.style.backgroundImage = "url(images/plus.gif)";
                // }
            }
            if ( s.className == "row-closed" ) {
            	closedNode = s.id;
            } else {
            	closedNode = "";
        	}
        }
    }
    if ( expanding ) {
    	parentRow.className = "row-open";
    } else {
    	parentRow.className = "row-closed";
	}
}

function parentClosed(parentId, thisId) {
	var parent = document.getElementById( parentId );
	if ( ! parent ) {
		return false;
	}
	var isChild = matchStart(thisId, parentId, false)
	return isChild && parent.className == "row-closed";
}

// match start expects pattern to be "n-m-" whereis target is complete id.
function matchStart(target, pattern, matchDirectChildrenOnly) {
    var pos = target.indexOf(pattern);
    if (pos != 0) return false;
    if (!matchDirectChildrenOnly) return true;
    if (target.slice(pos + pattern.length, target.length).indexOf("-") >= 0) return false;
    return true;
}

function collapseAllRows() {
    var rows = document.getElementsByTagName("TR");
    for (var j = 0; j < rows.length; j++) {
        var r = rows[j];
        if (r.id.indexOf("-") >= 0) {
            r.style.display = "none";        
        }
    }
}