﻿function getElementHelper(whichLayer)
{
  var elem = null;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
    
  return elem;
}

function setLayer( elem, isDisplay )
{
  var vis;
  
  vis = elem.style;
  vis.display = (isDisplay)?'block':'none';
}

function collapseAll(prefix, img_prefix, collapseImageURL)
{
    var i = 1;
    var elem;
    do
    {
        elem = getElementHelper(prefix + i);
        
        if (elem != null)
        {
            setLayer(elem, false);
            swapImage(img_prefix + prefix + i, collapseImageURL);
        }
        i++;
    }
    while (i < 20);
    // Assuming the above has only max 20 categories / sub categories
    // Can increase ... just scrared will slow down in user browser.
    //while (elem != null);

}

function expand(prefix, index, img_prefix, expandImageURL, collapseImageURL)
{
    var elem = getElementHelper(prefix + index);        
    if (elem != null)
    {
        var toExpand = (elem.style.display == "none");
        
        collapseAll(prefix, img_prefix, collapseImageURL);
        
        if (toExpand)
        {
            setLayer(elem, true);
            swapImage(img_prefix + prefix + index, expandImageURL);
        }
    }
}

function swapImage(imgName, imgURL)
{
   var elem = getElementHelper(imgName);
   if (elem != null)
    elem.src = imgURL;
}
