function DHTMLTable(sID) {
  this.oTable = null;
  this.sID = sID;
  this.indexes = new Array();
  this.rowPos = new Array();
  this.colSort = -1;
  this.sortOrder = 'asc';
  this.cellContent = new Array();
  this.nbFirstLinesIgnored = 0;
  this.nbLastLinesIgnored = 0;
}


DHTMLTable.prototype.init = function () {
  if (this.oTable == null) {
  	this.oTable = document.getElementById(this.sID);
  	this.nbLines = this.oTable.rows.length - this.nbFirstLinesIgnored - this.nbLastLinesIgnored;
    for (var row=0; row<this.nbLines; row++) {
      this.cellContent[row] = this.getCellsContentOfRow(row + this.nbFirstLinesIgnored);
    }
  }
  return (this.oTable);
}


DHTMLTable.prototype.swapCells = function (iRow1, iCol1, iRow2, iCol2) {
  if (!this.init()) return (false);
  var c1 = this.oTable.rows[iRow1].cells[iCol1];
  var c2 = this.oTable.rows[iRow2].cells[iCol2]
  if (c1.colSpan != c2.colSpan || c1.rowSpan != c2.rowSpan || c1.tagName != c2.tagName) return (false);
  var sTmp = c2.innerHTML;
  c2.innerHTML = c1.innerHTML;
  c1.innerHTML = sTmp;
  return (true);
}


DHTMLTable.prototype.swapRows = function (iRow1, iRow2) {
  if (!this.init()) return;
  for (var i=0; i<this.oTable.rows[iRow1].cells.length; i++) {
    if (this.swapCells(this.rowPos[iRow1], i, this.rowPos[iRow2], i) == false) return (false);
  }
  return (true);
}


DHTMLTable.prototype.getCellsContentOfRow = function (row) {
  if (!this.init()) return;
  var result = new Array();
  for (var i=0; i<this.oTable.rows[row].cells.length; i++) {
    result[i] = this.oTable.rows[row].cells[i].innerHTML;
  }
  return (result);
}


DHTMLTable.prototype.setCellsContentOfRow = function (row, content) {
  if (!this.init()) return;
  for (var i=0; i<this.oTable.rows[row].cells.length; i++) {
    this.oTable.rows[row].cells[i].innerHTML = content[i];
  }
}


DHTMLTable.prototype.swapCols = function (iCol1, iCol2) {
  if (!this.init()) return;
  for (var i=0; i<this.oTable.rows.length; i++) {
    if (this.swapCells(i, iCol1, i, iCol2) == false) return (false);
  }
  return (true);
}


function DHTMLTable_setIndexes(iCol) {
	var argv = DHTMLTable_setIndexes.arguments;
	this.indexes[iCol] = new Array();
	for (var i=1; i<argv.length; i++) {
		this.indexes[iCol][this.indexes[iCol].length] = argv[i];
	}
}
DHTMLTable.prototype.setIndexes = DHTMLTable_setIndexes;


function DHTMLTable_sort(iCol) {
  if (!this.init()) return;
	var argv = DHTMLTable_sort.arguments;
	switch (argv[1]) {
		case 'asc':
		  this.sortOrder = 'asc';
		break;
		case 'desc':
		  this.sortOrder = 'desc';
		break;
    default :
    if (this.colSort == iCol) {
      this.sortOrder = this.sortOrder == 'asc' ? 'desc' : 'asc';
    } else {
      this.sortOrder = 'asc';
    }
  }

  for (var row=0; row<this.nbLines; row++) {

    if (this.sortOrder == 'asc') {
      index = row + this.nbFirstLinesIgnored;
    } else {
      index = this.nbFirstLinesIgnored + (this.nbLines - row - 1);
    }
    this.setCellsContentOfRow(index, this.cellContent[this.indexes[iCol][row]]);
  }
  this.colSort = iCol;
}
DHTMLTable.prototype.sort = DHTMLTable_sort;
