
var expandImg = "/img/directory_plus.gif";
var collapseImg = "/img/directory_minus.gif";
var totalSectionsCount;
var expandedSections;
var collapsedSections;

function setSectionCount(totalCount)
{
    totalSectionsCount = totalCount;
    resetCounts();
}

function resetCounts()
{
    expandedSections = 0;
    collapsedSections = 0;
}

function expandAll(groupName, count)
{
     for(i=1; i <= count; i++) {
        var elementId = groupName + i;
        expand(elementId);
     }
     resetCounts();
}

function collapseAll(groupName, count)
{
     for(i=1; i <= count; i++) {
        var elementId = groupName + i;
        collapse(elementId);
     }
     resetCounts();
}

function expand(elementId)
{
    var currRow = document.getElementById(elementId);
    currRow.style.display="";
    document.getElementById(elementId+"img").src = collapseImg;
    expandedSections += 1;
}

function collapse(elementId)
{
    var currRow = document.getElementById(elementId);
    currRow.style.display="none";
    document.getElementById(elementId+"img").src = expandImg;
    collapsedSections += 1;
}

function expandOrCollapse(elementId)
{
    var currRow = document.getElementById(elementId);
	if (currRow.style.display=="none") {
		expand(elementId);
	} else {
		collapse(elementId);
	}
	resetTopLevelControl();
}

function resetTopLevelControl(){
    if(expandedSections == totalSectionsCount) {
        document.getElementById('expandAll').style.display='none';
        document.getElementById('collapseAll').style.display='inline';
        resetCounts();
    } else if (collapsedSections == totalSectionsCount) {
        document.getElementById('expandAll').style.display='inline';
        document.getElementById('collapseAll').style.display='none';
        resetCounts();
    }
}