//<!--
//
// $Id$
//
//Object oriented javascript for management of displaying one-of a collection.
//  Allowing for multiple, independent collections.
//Elements that are added, are added by name, and 'found' when needed in the
//  Display method.  This allows the tag to be written from IFrames to the
//  document ans stillwork properly.
//
function StateDisplay()
  {
  //**************************************************************************
  // PUBLIC attributes
  //**************************************************************************
  // Style to set when displaying a state.  Defaults to 'block'
  this.displayStyle = "block";
  
  //**************************************************************************
  // PRIVATE attributes
  //**************************************************************************

  // List of state names
  this.states = new Array();

  // List of elements.  The element at index I is assumed to be associated
  //   to the state named in states[I]
  this.stateElements = new Array();
  
  //**************************************************************************
  // PUBLIC methods
  //**************************************************************************
  // Add a state to this collection, by element reference
  // RETURNS: true if added successfully, false otherwise  

  // Adds a state to this collection, by id="XXX"
  // Returns: true if successfully added, false otherwise
  this.AddById = function(stateName, elementId)
                   {
                   if(this.find(stateName))
                     {
		     //Update element id
                     var state = this.get(stateName);
                     this.stateElements[state] = elementId;
                     }
                   else
                     {
		     //add new state
                     this.states[this.states.length] = stateName;
                     this.stateElements[this.stateElements.length] = elementId;
                     }

                   return this.find(stateName);
                   };

  // Displays (only) the named state.  Displays nothing if the named state has
  //   not been added to this collection.
  // Returns true;
  this.Display = function(stateName)
                   {
		   var i=0;
                   var e;
                   for(i=0; i < this.states.length; i++)
		     {
                     if(document.all)
                       {
                       e = eval('document.all["' + this.stateElements[i] + '"]');
                       }
                     else if(document.getElementById)
                       {
                       e = document.getElementById(this.stateElements[i]);
                       }
                     else
                       {
                       e = null;
                       }
		     
                     if(e != null)
                       {
                       if(this.states[i] == stateName)
                         {
                         e.style.display = this.displayStyle;
                         }
                       else
                         {
                         e.style.display = "none";
                         }
                       }
		     }
		   };

  this.HasState = function(stateName)
                    {
		    return this.find(stateName);
                    };

  this.Clear = function()
                 {
		 this.states = new Array();
		 this.stateElements = new Array();
		 };

  //**************************************************************************
  // PRIVATE methods
  //**************************************************************************
  
  // Checks if stated state name is found, with valid associated element
  this.find = function(stateName)
                {
                return (this.get(stateName) >= 0);
                };

  // Returns the index of the named state (with valid associated valid
  // element), or -1 if not present
  this.get = function(stateName)
                {
                var returnVal = -1;
                var i = 0;
                while(i < this.states.length &&
                      this.states[i] != stateName &&
                      i < this.stateElements.length)
                  {
                  i++;
                  }

                if(i < this.states.length && i < this.stateElements.length)
                  {
                  returnVal = i;
                  }

                return returnVal;
                };
  
  return this;
  }
//-->
