function GUI_Table()
{
  // inherit from GUI_Object ...
  this.prototype = new GUI_Object();
  for (prop in this.prototype)
    this[prop] = this.prototype[prop];



  this.Clear = function()
  {
    while (this.m_htmlObject.rows.length)
        this.m_htmlObject.deleteRow(0);
  }


  this.SetNumColumns = function(numColumns)
  {
    this.m_numColumns = numColumns;
    this.Rebuild();
  }


  this.OnXmlLoaded = function(xmlDoc)
  {
    this.m_owner.Clear();

    // get column definitions
    var columns = xmlDoc.getElementsByTagName('column');
    if (columns)
    {
      var numColumns = columns.length;
      var colWidth = new Array();
      var colTitle = new Array();

      for (var i = 0; i < numColumns; i++)
      {
        var columnObject = columns[i];
        var numAttr = columnObject.attributes.length;
        var minWidth = 0;

        for (var j = 0; j < numAttr; j++)
        {
          switch (columnObject.attributes[j].name)
          {
            case 'minWidth':
              minWidth = columnObject.attributes[j].value;
              break;
          }
        }
        
        var columnTitle = columnObject.firstChild.nodeValue;
        colWidth.push(minWidth);
        colTitle.push(columnTitle);
      }

      this.m_owner.DefineColumns(numColumns, colWidth, colTitle);
    }
    else
    {
      // error state.  there are no columns defined in the
      // XML file.
      return;
    }

    var entries = xmlDoc.getElementsByTagName('entry');
    if (entries)
    {
      var numEntries = entries.length;
      for (var i = 0; i < numEntries; i++)
      {
        var entryObject = entries[i];
	var values = entryObject.getElementsByTagName('value');
	this.m_owner.AddRow();
        for (var j = 0; j < values.length; j++)
          if (values[j].firstChild)
            this.m_owner.SetContent(j, i + 1, values[j].firstChild.nodeValue);
          else
            this.m_owner.SetContent(j, i + 1, '');
      }
    }
  }



  this.LoadXML = function(xmlFile)
  {
     this.m_xmlLoader.Load(xmlFile, true);
  }



  this.SetContent = function(colNum, rowNum, object)
  {
    if (colNum >= this.m_numColumns)
      return;

    if (rowNum >= this.m_numRows)
      return;

    var target = this.m_htmlObject.rows[rowNum].cells[colNum];

    while (target.firstChild)
      target.removeChild(target.firstChild);

    target.innerHTML = object;
  }



  this.DefineColumns = function(numColumns, columnWidthArray, columnTitleArray)
  {
    this.SetNumColumns(numColumns);
    this.AddRow();

    for (var i = 0; i < numColumns; i++)
    {
      var col = this.m_htmlObject.rows[0].cells[i];
      col.style.cursor = 'default';
      col.style.width = columnWidthArray[i];
      col.style.backgroundColor = this.m_bannerColor;
      col.style.color = this.m_bannerFontColor;
      col.style.font = this.m_bannerFont;
      col.style.paddingRight = this.m_paddingRight;
      col.style.paddingLeft = this.m_paddingLeft;
      col.style.height = this.m_bannerHeight;
      var textNode = document.createTextNode('');
      textNode.nodeValue = columnTitleArray[i];
      col.appendChild(textNode);
    }
    this.m_htmlObject.style.width = this.m_style['width'];
  }



  this.AddRow = function(cellValues)
  {
    var tr = document.createElement('tr');
    this.m_tbody.appendChild(tr);
    for (var i = 0; i < this.m_numColumns; i++)
    {
      var td = document.createElement('td');
      tr.appendChild(td);
      td.style.paddingRight = this.m_paddingRight;
      td.style.paddingLeft = this.m_paddingLeft;
      td.style.paddingTop = this.m_paddingTop;
      td.style.paddingBottom = this.m_paddingBottom;
      if (this.m_numRows & 1)
      {
        td.style.color = this.m_oddRowFontColor;
        td.style.backgroundColor = this.m_oddRowBgColor;
      }
      else
      {
        td.style.color = this.m_evenRowFontColor;
        td.style.backgroundColor = this.m_evenRowBgColor;
      }
    }

    this.m_numRows++;
    return tr;
  }

  


  this.Rebuild = function()
  {
    this.Clear();
    for (var r = 0; r < this.m_numRows; r++)
    {
      var tr = document.createElement('tr');
      this.m_tbody.appendChild(tr);
      for (var c = 0; c < this.m_numColumns; c++)
      {
        var td = document.createElement('td');
        td.noWrap = true;
        tr.appendChild(td);
      }
    }
  }



  this.OnSetStyle = function(style, value)
  {
    switch (style)
    {
      case 'style':
        this.SetStyle(g_resourceManager.GetResource(value));
        break;

      case 'bannerColor':
        if (this.m_htmlObject.rows.length)
          for (var i = 0; i < this.m_htmlObject.rows[0].cells.length; i++)
            this.m_htmlObject.rows[0].cells[i].style.backgroundColor = value;
        this.m_bannerColor = value;
        break;

      case 'bannerFont':
        if (this.m_htmlObject.rows.length)
          for (var i = 0; i < this.m_htmlObject.rows[0].cells.length; i++)
            this.m_htmlObject.rows[0].cells[i].style.font = value;
        this.m_bannerFont = value;
        break;


      case 'bannerFontColor':
        if (this.m_htmlObject.rows.length)
          for (var i = 0; i < this.m_htmlObject.rows[0].cells.length; i++)
            this.m_htmlObject.rows[0].cells[i].style.color = value;
        this.m_bannerFontColor = value;
        break;


      case 'evenRowBgColor':
        this.m_evenRowBgColor = value;
        break;

      case 'oddRowBgColor':
        this.m_oddRowBgColor = value;
        break;

      case 'evenRowFontColor':
        this.m_evenRowFontColor = value;
        break;

      case 'oddRowFontColor':
        this.m_oddRowFontColor = value;
        break;

      case 'bannerHeight':
        this.m_bannerHeight = value;
        break;

      case 'paddingLeft':
        this.m_paddingLeft = value;
        break;

      case 'paddingRight':
        this.m_paddingRight = value;
        break;


      case 'paddingTop':
        this.m_paddingTop = value;
        break;

      case 'paddingBottom':
        this.m_paddingBottom = value;
        break;
    }
  }



  //
  this.m_bannerHeight = 0;
  this.m_paddingLeft = 0;
  this.m_paddingRight = 0;
  this.m_paddingBottom = 0;
  this.m_paddingTop = 0;
  this.m_bannerColor = 'transparent';
  this.m_evenRowBgColor = 'transparent';
  this.m_oddRowBgColor = 'transparent';
  this.m_evenRowFontColor = 'rgb(0, 0, 0)';
  this.m_oddRowFontColor = 'rgb(0, 0, 0)';
  this.m_bannerFont = '12px verdana';
  this.m_bannerFontColor = 'rgb(0, 0, 0)';

  this.m_htmlObject = document.createElement('table');
  this.m_htmlObject.cellPadding = 0;
  this.m_htmlObject.cellSpacing = 0;
  this.m_tbody = document.createElement('tbody');
  this.m_htmlObject.appendChild(this.m_tbody);
  this.m_numColumns = 0;
  this.m_numRows = 0;

  this.m_xmlLoader = new XML_Loader();
  this.m_xmlLoader.m_onXmlLoaded = this.OnXmlLoaded;
  this.m_xmlLoader.m_owner = this;
  this.m_onSetStyle = this.OnSetStyle;
}




function GUI_CreateTable(parent, numColumns, colWidths, colTitles, numEntries, entries)
{
  // parent is parent HTML object.
  // numColumns is number of columns in the table
  // colWidths is array of size numColumns defining the min width of each column
  // colTitles is array of size numColumns with the caption for each column
  // numEntries is number of rows to add (not including header row)
  // entries is array of size numEntries.  each element in the array is an array of size
  //   numColumns.  Each entry in that array is an html object to insert into the
  //   correct location of the table (in that row).

  var table = new GUI_Table();
  table.SetParent(parent);
  table.DefineColumns(numColumns, colWidths, colTitles);

  for (var i = 0; i < numEntries; i++)
  {
     table.AddRow();
     for (var j = 0; j < numColumns; j++)
     {
       if (entries[i][j] != undefined)
         table.SetContent(j, i + 1, entries[i][j]);
     }
  }

  return table;
}




