// 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_CURSO = 'curso_'; // this is being set via script
var INPUT_NAME_PREFIX_CURSO2 = 'formacionId_'; // this is being set via script
var TABLE_NAME_CURSO = 'tblCursos'; // 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:
// myRowObjectCurso is an object for storing information about the table rows
function myRowObjectCurso(one, two)
{
	this.one = one; // text object
	this.two = two; // input text object
	this.three = three; // input text object	
}

/*
 * addRowToTableCurso
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */
function addRowToTableCurso(num)
{
	if (hasLoaded) {
		var tbl = document.getElementById(TABLE_NAME_CURSO);
		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('textarea');
		txtInp.name = INPUT_NAME_PREFIX_CURSO + iteration;
		txtInp.id = INPUT_NAME_PREFIX_CURSO + iteration;
		txtInp.setAttribute('style', 'width:100%; padding:0px;');
		cell1.appendChild(txtInp);

//		// cell 2 - hidden
		var cell2 = row.insertCell(2);
		var txtInp2 = document.createElement('input');
		txtInp2.type = 'hidden';
		txtInp2.name = INPUT_NAME_PREFIX_CURSO2 + iteration;
		txtInp2.id = INPUT_NAME_PREFIX_CURSO2 + iteration;
		txtInp2.value = '';
		cell2.appendChild(txtInp2);

		// cell 3 - delete button
		var cell3 = row.insertCell(3);
		var btnEl = document.createElement('input');
		btnEl.type = 'button';
		btnEl.value = 'Borrar';
		btnEl.onclick = function () {deleteCurrentRowCurso(this)};
		cell3.appendChild(btnEl);
				
		// Pass in the elements you want to reference later
		// Store the myRow object in each row
		row.myRow = new myRowObjectCurso(textNode, txtInp, txtInp2);
	}
}

// If there isn't an element with an onclick event in your row, then this function can't be used.
function deleteCurrentRowCurso(obj)
{
	if (hasLoaded) {
		var delRow = obj.parentNode.parentNode;
		var tbl = delRow.parentNode.parentNode;
		var rIndex = delRow.sectionRowIndex;
		var rowArray = new Array(delRow);
		deleteRowsCurso(rowArray);
		reorderRowsCurso(tbl, rIndex);
	}
}

function reorderRowsCurso(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; 
	
	
				var _textarea = myRow.getElementsByTagName("td")[1].getElementsByTagName("textarea")[0];
				_textarea.setAttribute("id", INPUT_NAME_PREFIX_CURSO + count);
				_textarea.setAttribute("name", INPUT_NAME_PREFIX_CURSO + count);


				myRow.getElementsByTagName("td")[2].firstChild.name = INPUT_NAME_PREFIX_CURSO2 + count; 
				myRow.getElementsByTagName("td")[2].firstChild.id = INPUT_NAME_PREFIX_CURSO2 + count; 
			
				count++;
			}
		}
	}
}

function deleteRowsCurso(rowObjArray)
{
	if (hasLoaded) {
		for (var i=0; i<rowObjArray.length; i++) {
			var rIndex = rowObjArray[i].sectionRowIndex;
			rowObjArray[i].parentNode.deleteRow(rIndex);
		}
	}
}


