// 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_TITULO = 'titulo_'; // this is being set via script
var INPUT_NAME_PREFIX_TITULO2 = 'grado_'; // this is being set via script
var INPUT_NAME_PREFIX_TITULO3 = 'rama_'; // this is being set via script
var INPUT_NAME_PREFIX_TITULO4 = 'tituloId_'; // this is being set via script
var TABLE_NAME_TITULO = 'tblTitulos'; // 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:
// myRowObjectTitulo is an object for storing information about the table rows
function myRowObjectTitulo(one, two, three, four, five)
{
	this.one = one; // text object
	this.two = two; // input text object
	this.three = three; // input text object
	this.four = four; // input text object	
	this.five = five; // input text object	
}

/*
 * addRowToTableTitulo
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */
function addRowToTableTitulo(num)
{
	if (hasLoaded) {
		var tbl = document.getElementById(TABLE_NAME_TITULO);
		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_TITULO + iteration;
		// Don't refer to ids
		txtInp.id = INPUT_NAME_PREFIX_TITULO + iteration;
		txtInp.size = 35;
		txtInp.value = '';
		cell1.appendChild(txtInp);
		
		// select cell
		var cell2 = row.insertCell(2);
		var sel = document.createElement('select');
		sel.name = INPUT_NAME_PREFIX_TITULO2 + iteration;
		sel.id = INPUT_NAME_PREFIX_TITULO2 + iteration;
		esId = 1
		for (var i = 0, j = 0, len = arrayGrado.length; i < len; i++) {
			//alert("elem i: " + i + ", esId: " + esId);
			if (esId == 1)
			{
				sel.options[j] = new Option(arrayGrado[i], arrayGrado[i+1]);
				esId = 0;
				j++;
			}
			else
			{
				esId = 1;
			}
		}
		cell2.appendChild(sel);
		
		// cell 3 - input text
		var cell3 = row.insertCell(3);
		var txtInp3 = document.createElement('input');
		txtInp3.type = 'text';
		txtInp3.name = INPUT_NAME_PREFIX_TITULO3 + iteration;
		// Don't refer to ids
		txtInp3.id = INPUT_NAME_PREFIX_TITULO3 + iteration;
		txtInp3.size = 35;
		txtInp3.value = '';
		cell3.appendChild(txtInp3);

		// cell 4 - hidden
		var cell4 = row.insertCell(4);
		var txtInp4 = document.createElement('input');
		txtInp4.type = 'hidden';
		txtInp4.name = INPUT_NAME_PREFIX_TITULO4 + iteration;
		// Don't refer to ids
		txtInp4.id = INPUT_NAME_PREFIX_TITULO4 + iteration;
		//txtInp3.size = 40;
		txtInp4.value = '';
		cell4.appendChild(txtInp4);


		// cell 5 - delete button
		var cell5 = row.insertCell(5);
		var btnEl = document.createElement('input');
		btnEl.type = 'button';
		btnEl.value = 'Borrar';
		btnEl.onclick = function () {deleteCurrentRowTitulo(this)};
		cell5.appendChild(btnEl);
				
		// Pass in the elements you want to reference later
		// Store the myRow object in each row
		row.myRow = new myRowObjectTitulo(textNode, txtInp, sel, txtInp3, txtInp4);
	}
}

// If there isn't an element with an onclick event in your row, then this function can't be used.
function deleteCurrentRowTitulo(obj)
{
	if (hasLoaded) {
		var delRow = obj.parentNode.parentNode;
		var tbl = delRow.parentNode.parentNode;
		var rIndex = delRow.sectionRowIndex;
		var rowArray = new Array(delRow);
		deleteRowsTitulo(rowArray);
		reorderRowsTitulo(tbl, rIndex);
	}
}

function reorderRowsTitulo(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_TITULO + count; 
				myRow.getElementsByTagName("td")[1].firstChild.id = INPUT_NAME_PREFIX_TITULO + count; 
				
				
				var _select = myRow.getElementsByTagName("td")[2].getElementsByTagName("select")[0];
				_select.setAttribute("id", INPUT_NAME_PREFIX_TITULO2 + count);
				_select.setAttribute("name", INPUT_NAME_PREFIX_TITULO2 + count);
				

				myRow.getElementsByTagName("td")[3].firstChild.name = INPUT_NAME_PREFIX_TITULO3 + count; 
				myRow.getElementsByTagName("td")[3].firstChild.id = INPUT_NAME_PREFIX_TITULO3 + count; 

				myRow.getElementsByTagName("td")[4].firstChild.name = INPUT_NAME_PREFIX_TITULO4 + count; 
				myRow.getElementsByTagName("td")[4].firstChild.id = INPUT_NAME_PREFIX_TITULO4 + count; 

				count++;
			}
		}
	}
}

function deleteRowsTitulo(rowObjArray)
{
	if (hasLoaded) {
		for (var i=0; i<rowObjArray.length; i++) {
			var rIndex = rowObjArray[i].sectionRowIndex;
			rowObjArray[i].parentNode.deleteRow(rIndex);
		}
	}
}


