// 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 = 'nombre_'; // this is being set via script
var INPUT_NAME_PREFIX2 = 'apellidos_'; // this is being set via script
var INPUT_NAME_PREFIX3 = 'contactoId_'; // this is being set via script
var TABLE_NAME = 'tblContacto'; // 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:
// myRowObject is an object for storing information about the table rows
function myRowObject(one, two, three, four)
{
	this.one = one; // text object
	this.two = two; // input text object
	this.three = three; // input text object
	this.four = four; // input text object
}

/*
 * addRowToTable
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */
function addRowToTable(num)
{
	if (hasLoaded) {
		var tbl = document.getElementById(TABLE_NAME);
		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 + iteration;
		txtInp.id = INPUT_NAME_PREFIX + iteration;
		txtInp.size = 40;
		txtInp.value = '';
		cell1.appendChild(txtInp);

		// cell 2 - input text
		var cell2 = row.insertCell(2);
		var txtInp2 = document.createElement('input');
		txtInp2.type = 'text';
		txtInp2.name = INPUT_NAME_PREFIX2 + iteration;
		txtInp2.id = INPUT_NAME_PREFIX2 + iteration;
		txtInp2.size = 40;
		txtInp2.value = '';
		cell2.appendChild(txtInp2);

//		// cell 3 - hidden
		var cell3 = row.insertCell(3);
		var txtInp3 = document.createElement('input');
		txtInp3.type = 'hidden';
		txtInp3.name = INPUT_NAME_PREFIX3 + iteration;
		txtInp3.id = INPUT_NAME_PREFIX3 + iteration;
		txtInp3.value = '';
		cell3.appendChild(txtInp3);
		
		// cell 4 - delete button
		var cell4 = row.insertCell(4);
		var btnEl = document.createElement('input');
		btnEl.type = 'button';
		btnEl.value = 'Borrar';
		btnEl.onclick = function () {deleteCurrentRow(this)};
		cell4.appendChild(btnEl);
				
		// Pass in the elements you want to reference later
		// Store the myRow object in each row
		row.myRow = new myRowObject(textNode, txtInp, txtInp2, txtInp3);
	}
}

// If there isn't an element with an onclick event in your row, then this function can't be used.
function deleteCurrentRow(obj)
{
	if (hasLoaded) {
		var delRow = obj.parentNode.parentNode;
		var tbl = delRow.parentNode.parentNode;
		var rIndex = delRow.sectionRowIndex;
		var rowArray = new Array(delRow);
		deleteRows(rowArray);		
		reorderRows(tbl, rIndex);
	}
}

function reorderRows(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 + count; 
				myRow.getElementsByTagName("td")[1].firstChild.id = INPUT_NAME_PREFIX + count; 
				
				myRow.getElementsByTagName("td")[2].firstChild.name = INPUT_NAME_PREFIX2 + count; 
				myRow.getElementsByTagName("td")[2].firstChild.id = INPUT_NAME_PREFIX2 + count; 
				
				myRow.getElementsByTagName("td")[3].firstChild.name = INPUT_NAME_PREFIX3 + count; 
				myRow.getElementsByTagName("td")[3].firstChild.id = INPUT_NAME_PREFIX3 + count; 
				
				count++;
			}
		}
	}
}



function deleteRows(rowObjArray)
{
	if (hasLoaded) {
		for (var i=0; i<rowObjArray.length; i++) {
			var rIndex = rowObjArray[i].sectionRowIndex;
			rowObjArray[i].parentNode.deleteRow(rIndex);
		}
	}
}
