// JavaScript DynamicTable class

function DTable(objectId) {
	this._table = document.getElementById(objectId);
	this._body  = this._table.getElementsByTagName('tbody').item(0);
	this._head  = this._table.getElementsByTagName('thead').item(0);
	this._minRows = parseInt(this._table.getAttribute("minrows"));
	this._cols  = parseInt(this._head.rows.item(1).cells.length);
	this._table.dTable = this;

	if (this._minRows != null && this._body.rows.length < this._minRows) {
		for (var i = this._body.rows.length; i < this._minRows; i++) {
			this.addRow();
		}
	}
	this.className = 'DTable';
	this.rowIndex  = '';

}

DTable.prototype.className;
DTable.prototype._table;
DTable.prototype._body;
DTable.prototype._rowIndex;
DTable.prototype.raw = false;


DTable.prototype.getObj = function() {
    return this._table;
}



	
DTable.prototype.removeRow = function(row) {
	
	this._table.deleteRow(row.rowIndex);
	for (var i=0; i< this._body.rows.length; i++) {
		row = this._body.rows.item(i);

		if (i % 2 == 0) {
		       if (row.className.indexOf("evenRow") == -1) {
				row.className = row.className + " evenRow";
			}
		}
		else {
			row.className = row.className.replace("evenRow","");
		}

	}
	if (this._minRows != null && this._body.rows.length < this._minRows) {
		this.addRow();
		
	}
}

DTable.prototype.addRow = function() {
	var newRow;
	var adjacentRow;
	
	
	if (arguments.length > 0) { //look for an empty row to fill
		for (var i=0; i< this._body.rows.length; i++) {
			if (this._body.rows.item(i).className.indexOf("emptyRow") > -1) { //found an empty
				newRow = this._body.rows.item(i);
				newRow.className = newRow.className.replace("emptyRow","");
				break;
			}
		}
	}
	if (newRow == null) {
  		newRow           = this._body.insertRow(-1);
		for (var i = 0; i < this._cols; i++) { //fill row with empty cells
			newRow.insertCell(i);
		}

		adjacentRow      = this._table.rows.item(parseInt(newRow.rowIndex - 1));
		newRow.className = "row_list";
		if (arguments.length == 0) { // empty row
			newRow.className = newRow.className + " emptyRow";
		}
			

	}


	if (adjacentRow != null && adjacentRow.className.indexOf("evenRow") == -1) {
		newRow.className = newRow.className + " evenRow";
	}


	for (var i = 0; i < (this._cols-1); i++) {
		var newTd = newRow.cells[i];
		if (i < arguments.length) {
			if (arguments[i] == null)
				continue;
			
			if (typeof(arguments[i]) == 'object') {
				switch(arguments[i].tagName.toLowerCase()) {
					case 'input':
					case 'textarea':
						/*
						var newInput = arguments[i].cloneNode(false);
						newInput.id = newInput.id + "[" + this.rowIndex + "]";
						newInput.name = newInput.id;
						newInput.setAttribute("type","hidden");
						*/
						var input  = document.createElement("input");
						input.setAttribute("id",arguments[i].id + "[" + this.rowIndex + "]");
						input.setAttribute("name",arguments[i].id + "[" + this.rowIndex + "]");
						input.setAttribute("value",arguments[i].value);


                        if (this.raw) {
    						input.setAttribute("type",arguments[i].type);
    						input.setAttribute("style",arguments[i].style);
    						input.setAttribute("size",arguments[i].size);
                            if (arguments[i].type == 'checkbox' && arguments[i].checked)
        						input.setAttribute("checked",arguments[i].checked);


                        }
                        else {
    						input.setAttribute("type","hidden");

    						var inputLabel = input.getAttribute("label");
    						if (inputLabel == null)
    							inputLabel = input.value;
					    	newTd.appendChild(document.createTextNode(inputLabel));
                        }


						newTd.appendChild(input);
						break;
					case 'select':
                        if (this.raw) {
                            var select = document.createElement("select");
    						select.setAttribute("id",arguments[i].id + "[" + this.rowIndex + "]");
    						select.setAttribute("name",arguments[i].id + "[" + this.rowIndex + "]");
                            for(j = 0; j < arguments[i].options.length; j++) {
                                option = document.createElement("option");
                                option.value = arguments[i].options[j].value;
                                option.text  = arguments[i].options[j].text;
                                select.appendChild(option);

                            }
                            select.selectedIndex = arguments[i].selectedIndex;
                            newTd.appendChild(select);

                        }
                        else {
    						var option = arguments[i].options[arguments[i].selectedIndex];
    						var input  = document.createElement("input");
    						input.setAttribute("type","hidden");
    						input.setAttribute("id",arguments[i].id + "[" + this.rowIndex + "]");
    						input.setAttribute("name",arguments[i].id + "[" + this.rowIndex + "]");
    						input.setAttribute("value",option.value);
    						newTd.appendChild(input);
    						newTd.appendChild(document.createTextNode(option.text));
                        }
						break;
				}

			}
			else {
				newTd.innerHTML = arguments[i];
			}
		}
		else {
			newTd.innerHTML = '';
		}
	}

	var buttonTd = newRow.cells[this._cols - 1];

	if (arguments.length > 0) { // there was some data passed in... add a delete button
		var button = document.createElement("input");
		button.setAttribute("type","button");
		button.setAttribute("value","X");
		button.onclick = function(){ row = this.parentNode.parentNode; table = row.parentNode.parentNode; table.dTable.removeRow(row) };
		buttonTd.appendChild(button);
	}
	//this.rowIndex++;

	return newRow;
}
