function DTable (tableId, maxRows, firstRow) {
	this.tabela = document.getElementById(tableId);
	this.maxRows = typeof maxRows == 'undefined' ? 99 : maxRows;
	this.firstRow = typeof firstRow == 'undefined' ? 1 : firstRow;
	this.clearAllRows(false);
}

DTable.prototype.addRows = DTable_addRows;
DTable.prototype.clearAllRows = DTable_clearAllRows;
DTable.prototype.clearRow = DTable_clearRow;
DTable.prototype.deleteAllRows = DTable_deleteAllRows;
DTable.prototype.deleteRow = DTable_deleteRow;
DTable.prototype.getRowCount = DTable_getRowCount;

function DTable_addRows (numRows, clean) {
	numRows = Number(numRows); 
	if (numRows <= 0) alert('Por favor indique um n?mero de linhas superior a zero.');
	if (this.getRowCount() + 1 > this.maxRows) {
		if (this.maxRows > 1)
			alert('Esta tabela n?o pode ter mais do que '+this.maxRows+' linhas.');
		else
			alert('Esta tabela n?o pode ter mais do que '+this.maxRows+' linha.');
	}
	for (var i = 0; i < numRows && this.getRowCount() + 1 <= this.maxRows; i++) {
		var row = this.tabela.rows[this.firstRow].cloneNode(true);
		this.tabela.tBodies[0].appendChild(row);
		this.clearRow(this.getRowCount(), clean);
	}
	if (i > 0 && i < numRows) {
		if (i > 1) {
			if (this.maxRows > 1)
				alert('Apenas foram adicionadas '+i+' linhas porque a tabela s? permite um m?ximo de '+this.maxRows+' linhas.');
			else
				alert('Apenas foram adicionadas '+i+' linhas porque a tabela s? permite um m?ximo de '+this.maxRows+' linha.');
		} else {
			if (this.maxRows > 1)
				alert('Apenas foi adicionada '+i+' linha porque a tabela s? permite um m?ximo de '+this.maxRows+' linhas.');
			else
				alert('Apenas foi adicionada '+i+' linha porque a tabela s? permite um m?ximo de '+this.maxRows+' linha.');
		}
	}
}

function DTable_clearAllRows (clean) {
	var total = this.getRowCount();
	for (var i = 1; i <= total; i++) this.clearRow(i, clean);
}

function DTable_clearRow (i, clean) {
	if (typeof clean == 'undefined') clean = true;
	var row = this.tabela.rows[Number(i) + this.firstRow - 1];
	DTable_clearInputs(row.getElementsByTagName('INPUT'), i, clean);
	DTable_clearInputs(row.getElementsByTagName('SELECT'), i, clean);
	DTable_clearInputs(row.getElementsByTagName('BUTTON'), i, clean);
	DTable_clearInputs(row.getElementsByTagName('TEXTAREA'), i, clean);
}

function DTable_clearInputs (inputs, i, clean) {
	for (var j = 0; j < inputs.length; j++) {
		inputs[j].id = inputs[j].name = inputs[j].name.replace(/\d+$/, i);
		inputs[j].row = i;
		if (inputs[j].type.toLowerCase() == 'button') continue;
		if (inputs[j].tagName.toLowerCase() == 'button') continue;
		if (typeof inputs[j].inputValue != 'undefined') initInput(inputs[j]);
		if (clean) setInputValue(inputs[j], '');
	}
}

function DTable_deleteAllRows () {
	var total = this.getRowCount();
	for (var i = total; i > 1; i--) this.tabela.deleteRow(Number(i) + this.firstRow - 1);
	this.clearRow(1);
}

function DTable_deleteRow (i) {
	if (i == 1 && this.getRowCount() == 1) {
		this.clearRow(i);
		return;
	}
	this.tabela.deleteRow(Number(i) + this.firstRow - 1);
	for (var k = i; k <= this.getRowCount(); k++) this.clearRow(k, false);
}

function DTable_getRowCount () {
	return this.tabela.rows.length - this.firstRow;
}
