///////////////////////////////////////////////////////////////////////////////////
//  Tree Management Section
///////////////////////////////////////////////////////////////////////////////////

// Expand or Collapse Single Tree Node
function toggle(item)
{
    var obj = document.getElementById(item);

    if (obj.style.display != "none")
    {
        collapseNode(item);
    } 
    else 
    {
	expandNode(item);
    }

    document.getElementById("all").checked = areAllChecked();

    // if all nodes are expanded, then change the main node to minus
    if (areAllNodesExpanded(g_categories)) 
    {
        setImage("allCategoryImage", "minus");
    }

    // if all nodes are collapsed, then change the main node to plus
    if (areAllNodesCollapsed(g_categories)) 
    {
        setImage("allCategoryImage", "plus");
    }

    return false;
}

// collapse a tree node
function collapseNode(item)
{
    var obj = document.getElementById(item);
    obj.style.display = "none";
    setImage("ximg"+item, "plus");
}

// expand a tree node
function expandNode(item)
{
    var obj = document.getElementById(item);
    obj.style.display = "block";
    setImage("ximg"+item, "minus");
}

// find out whether or not all of the nodes in the array are expanded
function areAllNodesExpanded(categoryArray) {
    for(var i = 0; i < categoryArray.length; i++) {
        if (document.getElementById(categoryArray[i]).style.display == "none") {
            return false;
        }
    }
    return true;
}

// find out whether or not all of the nodes in the array are collapsed
function areAllNodesCollapsed(categoryArray) {
    for(var i = 0; i < categoryArray.length; i++) {
        if (document.getElementById(categoryArray[i]).style.display == "block") {
            return false;
        }
    }

    return true;
}

// toggles all of the categories in the array
function toggleAll(categoryArray)
{
    var img = document.images["allCategoryImage"];
    if(img != null) {
      if (String(img.src).indexOf("plus.gif") > 0)
      {
          expandAll(categoryArray);
      } 
      else 
      {
          collapseAll(categoryArray);
      }
    }

    return false;
}

function expandAll(categoryArray)
{
    setImage("allCategoryImage", "minus");
    for (var i = 0; i < categoryArray.length; i++) 
    {
        expandNode(categoryArray[i]);
    }    
}

function collapseAll(categoryArray)
{
    setImage("allCategoryImage", "plus");
    for(var i = 0; i < categoryArray.length; i++)
    {
        collapseNode(categoryArray[i]);
    }
}

///////////////////////////////////////////////////////////////////////////////////
//  Checkbox Management Section
///////////////////////////////////////////////////////////////////////////////////

function checkAllSources()
{
    if (typeof(document.search.all) != "undefined")
    {
//        alert("setting all sources");
        document.search.all.checked = areAllChecked();
    }
}

// Check All Nodes Under a Group
function check_group(item)
{
    category = document.getElementById("z"+item);
    collections = document.getElementById(item).getElementsByTagName("input");
    for (i = 0; i < collections.length; i++) {
	if (category.checked === true) {
	    collections[i].checked = true;
	} else {
	    collections[i].checked = false;
	}
    }

    checkAllSources();
}

// Checks Status of a Group
function check_status(item)
{
    // get the category element identified by "item"
    var category = document.getElementById("z"+item);

    // get all of this category element's children
    var collections = document.getElementById(item).getElementsByTagName("input");

    // If there are no child nodes, then return
    if (collections.length === 0)
    {
        return;
    }

    // Count the children that are "checked"

    var check_count = 0;

    for (i = 0; i < collections.length; i++) {
	if (collections[i].type == 'checkbox') {
	    if (collections[i].checked === true) {
		check_count++;
	    }
	}
    }

    // if all the children are checked, then the parent is checked
    if (check_count > 0 && check_count == collections.length)
    {
	category.checked = true;
    }
    else
    {
	category.checked = false;
	// if some, but not all of the categories are checked, then expand this category node
	if (check_count !== 0) {
	    expandNode(item);
	}
    }

    checkAllSources();
}

// Sets the checked state of the checkboxes
function _setAllCheckBoxes(checked)
{
    if(checked === true) {
//        alert("checkingeow"+document.getElementById('eow'));
        if(document.getElementById('eowid')) {
            document.getElementById('eowid').value = "73";
        }
    }
    var root = document.getElementById("categoryBlock");
    var nodes = root.getElementsByTagName("input");
    if (nodes)
    {
    	// set the check value for all check boxes
	for (var i = 0; i < nodes.length; i++)
        {
	    var element = nodes[i];
	    if (element.type == "checkbox")
            {
		element.checked = checked;
	    }
	}
    }
}

// unchecks all checkboxes in the search form
function uncheckAll()
{
//    alert("called uncheck");
    _setAllCheckBoxes(false);
}

// checks all checkboxes in the search form
function checkAll()
{
    _setAllCheckBoxes(true);
}

//-----------------------------------------------------------------------------
// Finds out if all of the checkboxes in the search form are checked
//-----------------------------------------------------------------------------
function areAllChecked()
{
    var root = document.getElementById("categoryBlock");
    var nodes = root.getElementsByTagName("input");
    if (!nodes)
    {
        return false;
    }

    for (var i = 0; i < nodes.length; i++) {
        if (nodes[i].type == "checkbox") {
            if (nodes[i].id != "all" && nodes[i].checked === false) {
                return false;
            }
        }
    }

    return true;
}

//-----------------------------------------------------------------------------
// Finds out if any of the checkboxes in the search form are checked
//-----------------------------------------------------------------------------
function areAnyChecked()
{
//    alert("called areAnyChecked");
    var root = document.getElementById("categoryBlock");
    var nodes = root.getElementsByTagName("input");
    if (!nodes)
    {
        return false;
    }

    for (var i = 0; i < nodes.length; i++)
    {
        if (nodes[i].type == "checkbox") 
        {
            if (nodes[i].checked === true) 
            {
                return true;
            }
        }
    }

    return false;
}

//-----------------------------------------------------------------------------
// Extracts the collection id from a string that is the combination of category
// id and collection id.
//-----------------------------------------------------------------------------
function extractId(value)
{
    var underIndex = value.indexOf("_");
    if (underIndex > 0) {
        return value.substr(underIndex + 1, value.length - 1);
    }
}


// Checks no Collections by Default
function setupTree(collectionIds)
{
    if (collectionIds.length === 0) 
    {        
        uncheckAll();
    }
    else if (collectionIds[0] == "all") {
        checkAll();
    }
    else
    {
        var collectionCheckBoxes = document.search.selectedCollections;
        if (typeof(collectionCheckBoxes) == "undefined")
        {
            collectionCheckBoxes = document.search.collections;
        }
        
        for (var i = 0; i < collectionCheckBoxes.length; i++)
        {
            var collectionCheckbox = collectionCheckBoxes[i];
            collectionCheckbox.checked = false;
            for (var j = 0; j < collectionIds.length; j++) {
                if (collectionCheckbox.value == collectionIds[j] ||
                    extractId(collectionCheckbox.value) == collectionIds[j] )
                {
                    collectionCheckbox.checked = true;
                }
            }
        }

        // go through each category and decided if we need to show it
        for (var i = 0; i < g_categories.length; i++)
        {
            var categoryName = "cat" + (i + 1);
            if (document.getElementById("z" + categoryName)) 
            {
                check_status(categoryName);
            }
        }
    }

    checkAllSources();
}

// check all of the coll checkboxes
function selectAll(collections)
{
//    alert("called selectAll");
    if (!collections)
    {
	return;
    }

    // set the check value for all check boxes
    for (var i = 0; i < collections.length; i++) 
    {
	collections[i].checked = true;
    }
}

// uncheck all of the collections checkboxes
function deselectAll(collections)
{
//    alert("called deselectAll");
    if (!collections)
    {
	return;
    }

    // set the check value for all check boxes
    for (var i = 0; i < collections.length; i++) 
    {
	collections[i].checked = false;
    }
}

// set the image identified by imgId to the image with name: imgName
// this assumes that there is an array of images named "imageCache"
// that contains the image to set.
function setImage(imgId, imgName)
{
    if (document.images && imageCache)
    {
        document.images[imgId].src   = imageCache[imgName].src;
        document.images[imgId].alt   = imageCache[imgName].alt;
        document.images[imgId].title = imageCache[imgName].title;
        return true;
    }

    return false;
}

function toggleSelect(chkAllElement)
{
//    alert("called toggleSelect");
    if (document.images && chkAllElement)
    {
        if (chkAllElement.checked === true)
        {
            selectAll(document.search.selectedCollections);
            checkAll();
            //Eprints Special Collections - Eprints on websites
            //73 is the drop down value for All Categories
            if(document.getElementById('eowid')) {
//              document.getElementById('eowid').value = "73";
            }
        }
        else
        {
            deselectAll(document.search.selectedCollections);
            uncheckAll();
            if(document.getElementById('eowid')) {
              document.getElementById('eowid').value = "";
            }
        }
    }
}

  
