function CreateListViewTab(index, tabName, lvObj, trObj, enabled)
{
  var obj = trObj.insertCell(index);
  obj.innerHTML = tabName;
  obj.lvObj = lvObj;
  obj.sortOrder = "ASCENDING";
  outset(obj);

  function inset(tabObj)
  {
    tabObj.style.borderTop = "1px solid #808080";
    tabObj.style.borderLeft = "1px solid #808080";
    tabObj.style.borderRight = "1px solid #ffffff";
    tabObj.style.borderBottom = "1px solid #ffffff";
    tabObj.style.padding = "2px 4px 1px 5px";
  }

  function outset(tabObj)
  {
    tabObj.style.borderTop = "1px solid #ffffff";
    tabObj.style.borderLeft = "1px solid #ffffff";
    tabObj.style.borderRight = "1px solid #808080";
    tabObj.style.borderBottom = "1px solid #808080";
    tabObj.style.padding = "1px 5px 2px 4px";
  }

  function msOver()
  {
    this.style.cursor = "hand";
  }

  function msOut()
  {
    this.style.cursor = "default";
    this.outset(this);
  }

  function msDown()
  {
    this.inset(this);
  }

  function msUp()
  {
    this.outset(this);
    this.lvObj.sort(this.cellIndex, this.sortOrder);
    this.sortOrder = this.sortOrder == "ASCENDING" ? "DESCENDING" : "ASCENDING";
  }

  if (enabled)
  {
    obj.inset = inset;
    obj.outset = outset;
    obj.onmouseover = msOver;
    obj.onmouseout = msOut;
    obj.onmousedown = msDown;
    obj.onmouseup = msUp;
  }
}

function CreateListViewCell(index, cellContent, trObj)
{
  var obj = trObj.insertCell(index);
  obj.style.padding = "2px 4px 2px 4px";
  obj.innerHTML = cellContent;
}

function CreateListViewTabRow(tabNames, lvObj, enabledTabs)
{
  var obj = lvObj.insertRow(0);
  obj.style.fontFamily = "verdana";
  obj.style.fontWeight = "bold";
  obj.style.backgroundColor = "#d4d0c8";

  for (var i = 0; i < tabNames.length; i++)
  {
    CreateListViewTab(i, tabNames[i], lvObj, obj, enabledTabs[i]);
  }
}

function CreateListViewCellRow(index, cellContents, lvObj)
{
  var obj = lvObj.insertRow(index);
  obj.style.backgroundColor = index % 2 ? "#f8f8f8" : "#ffffff";

  for (var i = 0; i < cellContents.length; i++)
  {
    CreateListViewCell(i, cellContents[i], obj);
  }
}

function CreateListView(containerId)
{
  var obj = document.createElement("table");
  obj.cellPadding = 0;
  obj.cellSpacing = 0;
  //obj.width = "100%";
  obj.style.fontFamily = "arial";
  obj.style.fontSize = "10pt";
  obj.style.border = "1px solid #808080";
  obj.containerId = containerId;
  obj.lvRows = new Array();

  function addTabs(tabNames, enabledTabs)
  {
    CreateListViewTabRow(tabNames, this, enabledTabs);
  }

  function addRow(cellContents)
  {
    this.lvRows[this.lvRows.length] = cellContents;
    CreateListViewCellRow(this.rows.length, cellContents, this);
  }

  function display()
  {
    document.getElementById(this.containerId).appendChild(this);
  }

  function sort(tabIndex, sortOrder)
  {
    for (var i = this.lvRows.length - 1; i > 0; i--)
    {
      for (var j = 0; j < i; j++)
      {
        if ( (this.lvRows[j][tabIndex] > this.lvRows[j + 1][tabIndex] && sortOrder == "ASCENDING") ||
             (this.lvRows[j][tabIndex] < this.lvRows[j + 1][tabIndex] && sortOrder == "DESCENDING") )
        {
          var tmpRow = this.lvRows[j];
          this.lvRows[j] = this.lvRows[j + 1];
          this.lvRows[j + 1] = tmpRow;
        }
      }
    }

    for (var i = 0; i < this.lvRows.length; i++)
    {
      for (var j = 0; j < this.lvRows[0].length; j++)
      {
        this.rows[i + 1].cells[j].innerHTML = this.lvRows[i][j];
      }
    }
  }

  obj.addTabs = addTabs;
  obj.addRow = addRow;
  obj.display = display;
  obj.sort = sort;
  return obj;
}
