/**
 *  looks for an item with the given ID in the DOM and sets its CSS
 *  {@code visibility} to the value given in the status parameter
 *
 *  @param id          the DOM element for which the CSS attribute is to be set
 *  @param status      the new value of the CSS style visibility. May be
 *                     a literal String or one of the following values
 *                      1: visible
 *                      0: hidden, i.e. element is hidden but still occupies
 *                         the space to be displayed
 *                     -1: collapse, i.e. element is hidden and the required
 *                         space is freed
 */
function setVisibility (id, status) {
    if (id)
    {
        var item  = document.getElementById (id);
        if (typeof (item) == 'undefined')
            return;

        if (status != null) {
            if (status == 1)
                status = 'visible';
            if (status == 0)
                status = 'hidden';
            if (status == -1)
                status = 'collapse';
            item.style.visibility = status;
        }
    }
    return;
}

/**
 *  looks for an item with the given ID in the DOM and sets its CSS
 *  {@code visibility} to {@code 'hidden'} if the element was previously
 *  displayed, and to {@code 'visible'} if it was previously not displayed
 *
 *  @param id          the DOM element for which the CSS attribute is to be
 *                     changed
 */
function toggleVisibility (id) {
    if (id)
    {
        var item  = document.getElementById (id);
        if (typeof (item) == 'undefined')
            return;

        status = item.style.visibility;
        if (!status || status == 'collapse' || status == 'hidden') {
            item.style.visibility = 'visible';
        } else {
            item.style.visibility = 'hidden';
        }
    }
    return;
}


var dynItems = new Array( );
var dynItemParents = new Array( );

function changeAllItemVisibility()
{
}

/** This function changes the visibility of the given
 *  item to the value indicated by the parameter show.
 *
 * @itemId	Some sort of ID to identify the item at question.
 *	 	Usually the value (name) of the id-attribute of a
 *		<div>-field that is to be shown or hidden.
 * @show	Parameter that determines the state of visibility that
 *		is to be switched to. The following values are possible:
 *  		undefined - toggle current state
 *		0 - switch visibility to 0 (not visible)
 *		1 - switch visibility to 1 (visible)
 *		2 - toggle current state; hide all other items so that
 *		    only one item is visible at a time.
 *
 */
function changeItemVisibility( itemId, show )
{
  if( itemId )
  {
    var item  = document.getElementById( itemId );
    if( typeof( item ) == 'undefined' )
      return;
    var itemParent;

    //Variable to indicate if only a single item shall be displayed at a time
    var hideAllOtherItems = false;	// default is false

    //A value of 2 in parameter show indicates that a maximum of one item is
    //to be displayed at a time.
    if (show == 2)
    {
      hideAllOtherItems = true;
    }

    // If no value for the parameter show is given, or if the value is 2
    // we are to toggle the state
    // of the visibility-flag. In order to do that, we have
    // to determine the current state of that flag first.
    if( (typeof( show ) == 'undefined')  || (show == 2) )
    {
      // If the item at hand hasn't been used in our dynItems-array yet,
      // or if its value in the dynItems array is not 0
      // then its visibility must obviously be set to 1 (visible) at the moment.
      if( (dynItems[ itemId ] != 0) && (typeof(dynItems[ itemId ]) != 'undefined') )
      {
        show = 1;
      }
      else
      {
        show = 0;
      }
    }

    //If the
    if( hideAllOtherItems == true )
    {    var tmpArray = new Array( );
      var i = 0;
      for( var item in allItems )
      {
        tmpArray[ i ] = item;
        i++;
      }
      for( i=i; i>0; i-- )
      {
        changeItemVisibility (tmpArray[i-1], 0);
      }
    }

    //At this point, we know the state of visibility we have to change to,
    //either explicitly indicated by the given show parameter, or due to
    //our considerations in the section above. Now we can go on and make
    //the desired changes.
    if( show )
    {
      if ((dynItems[ itemId ] != 0) && (typeof(dynItems[ itemId ]) != 'undefined'))
      {
        itemParent = dynItemParents[ itemId ];
        itemParent.appendChild( dynItems[ itemId ] );
        dynItems[ itemId ] = 0;
      }
    }
    else
    {
      if ((dynItems[ itemId ] == 0) || (typeof(dynItems[ itemId ]) == 'undefined'))
      {
        itemParent = item.parentNode;
        dynItems[ itemId ] = itemParent.removeChild( item );
        dynItemParents[ itemId ] = itemParent;
      }
    }
  }
  //alert( obj.style.visibility );
  return true;
}

function hideItems( itemsToHide )
{
  itemsToHide.sort();
  var tmpArray = new Array( );
  var i = 0;
  for( var item in itemsToHide )
  {
    tmpArray[ i ] = item;
    i++;
  }
  for( i=i; i>0; i-- )
  {
    changeItemVisibility( tmpArray[i-1], 0 );
  }
}


/** changes the visibility of all items on or off.
 *  need following variables to be declared on the page:
 *    allItems - array with all items
 *    allItemsDisplayed - boolean state (on or off)
 */
function changeAllItems ()
{
  // alert ("changeAllItems allItemsDisplayed="+allItemsDisplayed);
  var tmpArray = new Array( );
  var i = 0;
  for( var item in allItems )
  {
    tmpArray[ i ] = item;
    i++;
  }
  for( i=i; i>0; i-- )
  {
    if (allItemsDisplayed)
    {
      changeItemVisibility (tmpArray[i-1], 0);
    }
    else
    {
      changeItemVisibility (tmpArray[i-1], 1);
    }
  }
  if (allItemsDisplayed)
  {
    allItemsDisplayed=0;
  }
  else
  {
    allItemsDisplayed=1;
  }
}


/** displays all items (turns them on).
 *  need following variables to be declared on the page:
 *    allItems - array with all items
 *    allItemsDisplayed - boolean state (on or off)
 *  @param checkBoxName name of the checkbox which should be checked
 */
function displayAllItems (checkBoxName)
{
  // alert ("changeAllItems allItemsDisplayed="+allItemsDisplayed);
  var tmpArray = new Array ();
  var i = 0;
  for (var item in allItems)
  {
    tmpArray[i] = item;
    i++;
  }
  for (i=i; i>0; i--)
  {
    changeItemVisibility (tmpArray[i-1], 1);
  }
  allItemsDisplayed=1;
  // alert ("checkBoxName=" + checkBoxName);
  if (checkBoxName)
  {
    checkbox = document.getElementsByName (checkBoxName)[0];
    // alert ("checkbox=" + checkbox);
    if ((checkbox != 0) && (typeof(checkbox) != 'undefined') )
    {
      checkbox.checked = true;
    }
  }
}
