// Table Delete Row
// mredkj.com
// based off tabledeleterow.js version 1.2 2006-02-21
// tabledeleterow-calendar.js version 1.2.1 2006-05-04
// tabledeleterow-calendar.js version 1.2.2 2006-10-10

// CONFIG notes. Below are some comments that point to where this script can be customized.
// Note: Make sure to include a <tbody></tbody> in your table's HTML
// Note: Cannot use this with any calendar script that references object ids
//		 The way the rows are added and deleted, ids aren't reset.

var INPUT_NAME_PREFIX_PROGRAMA = 'programa_'; // this is being set via script
var INPUT_NAME_PREFIX_PROGRAMA2 = 'nivel_'; // this is being set via script
var INPUT_NAME_PREFIX_PROGRAMA3 = 'programaId_'; // this is being set via script
var TABLE_NAME_PROGRAMA = 'tblProgramas'; // this should be named in the HTML
var ROW_BASE = 1; // first number (for display)
var hasLoaded = false;

window.onload=fillInRows;


function fillInRows()
{
	hasLoaded = true;
}

// CONFIG:
// myRowObjectPROGRAMA is an object for storing information about the table rows
function myRowObjectPrograma(one, two, three)
{
	this.one = one; // text object
	this.two = two; // input text object
	this.three = three; // input text object	
	this.four = four; // input text object	
}

/*
 * addRowToTablePrograma
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */
function addRowToTablePrograma(num)
{
	if (hasLoaded) {
		var tbl = document.getElementById(TABLE_NAME_PROGRAMA);
		var nextRow = tbl.tBodies[0].rows.length;
		var iteration = nextRow + ROW_BASE;
		if (num == null) { 
			num = nextRow;
		} else {
			iteration = num + ROW_BASE;
		}
		
		// add the row
		var row = tbl.tBodies[0].insertRow(num);
		
		// CONFIG: requires classes named classy0 and classy1
//		row.className = 'classy' + (iteration % 2);
		row.className = 'classy1';
	
		// CONFIG: This whole section can be configured
		
		// cell 0 - text
		var cell0 = row.insertCell(0);
		var textNode = document.createTextNode(iteration);
		cell0.appendChild(textNode);
		
		// cell 1 - input text
		var cell1 = row.insertCell(1);
		var txtInp = document.createElement('input');
		txtInp.type = 'text';
		txtInp.name = INPUT_NAME_PREFIX_PROGRAMA + iteration;
		txtInp.id = INPUT_NAME_PREFIX_PROGRAMA + iteration;
		txtInp.size = 80;
		txtInp.value = '';
		cell1.appendChild(txtInp);

		// select cell
		var cell2 = row.insertCell(2);
		var sel = document.createElement('select');
		sel.name = INPUT_NAME_PREFIX_PROGRAMA2 + iteration;
		sel.id = INPUT_NAME_PREFIX_PROGRAMA2 + iteration;
		esId = 1
		for (var i = 0, j = 0, len = arrayNivel.length; i < len; i++) {
			if (esId == 1)
			{
				sel.options[j] = new Option(arrayNivel[i], arrayNivel[i+1]);
				esId = 0;
				j++;
			}
			else
			{
				esId = 1;
			}
		}
		cell2.appendChild(sel);
		
		// cell 3 - hidden
		var cell3 = row.insertCell(3);
		var txtInp2 = document.createElement('input');
		txtInp2.type = 'hidden';
		txtInp2.name = INPUT_NAME_PREFIX_PROGRAMA3 + iteration;
		// Don't refer to ids
		txtInp2.id = INPUT_NAME_PREFIX_PROGRAMA3 + iteration;
		//txtInp3.size = 40;
		txtInp2.value = '';
		cell3.appendChild(txtInp2);

		// cell 4 - delete button
		var cell4 = row.insertCell(4);
		var btnEl = document.createElement('input');
		btnEl.type = 'button';
		btnEl.value = 'Borrar';
		btnEl.onclick = function () {deleteCurrentRowPrograma(this)};
		cell4.appendChild(btnEl);
				
		// Pass in the elements you want to reference later
		// Store the myRow object in each row
		row.myRow = new myRowObjectPrograma(textNode, txtInp, sel, txtInp2);
	}
}

// If there isn't an element with an onclick event in your row, then this function can't be used.
function deleteCurrentRowPrograma(obj)
{
	if (hasLoaded) {
		var delRow = obj.parentNode.parentNode;
		var tbl = delRow.parentNode.parentNode;
		var rIndex = delRow.sectionRowIndex;
		var rowArray = new Array(delRow);
		deleteRowsPrograma(rowArray);
		reorderRowsPrograma(tbl, rIndex);
	}
}

function reorderRowsPrograma(tbl, startingIndex)
{
	if (hasLoaded) {
		if (tbl.tBodies[0].rows[startingIndex]) {
			var count = startingIndex + ROW_BASE;
			for (var i=startingIndex; i<tbl.tBodies[0].rows.length; i++) {

				myRow = tbl.tBodies[0].rows[i];
				myRow.getElementsByTagName("td")[0].innerHTML = count; 
				
				myRow.getElementsByTagName("td")[1].firstChild.name = INPUT_NAME_PREFIX_PROGRAMA + count; 
				myRow.getElementsByTagName("td")[1].firstChild.id = INPUT_NAME_PREFIX_PROGRAMA + count; 


				var _select = myRow.getElementsByTagName("td")[2].getElementsByTagName("select")[0];
				_select.setAttribute("id", INPUT_NAME_PREFIX_PROGRAMA2 + count);
				_select.setAttribute("name", INPUT_NAME_PREFIX_PROGRAMA2 + count);


				myRow.getElementsByTagName("td")[3].firstChild.name = INPUT_NAME_PREFIX_PROGRAMA3 + count; 
				myRow.getElementsByTagName("td")[3].firstChild.id = INPUT_NAME_PREFIX_PROGRAMA3 + count; 

				count++;
			}
		}
	}
}

function deleteRowsPrograma(rowObjArray)
{
	if (hasLoaded) {
		for (var i=0; i<rowObjArray.length; i++) {
			var rIndex = rowObjArray[i].sectionRowIndex;
			rowObjArray[i].parentNode.deleteRow(rIndex);
		}
	}
}

