// tabledeleterow.js version 1.2 2006-02-21
// mredkj.com

// 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

var TABLE_NAME = 'products'; // 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 checkbox object
	this.four = four; // input radio object
}

/*
 * insertRowToTable
 * Insert and reorder
 */
function insertRowToTable()
{
	if (hasLoaded) {
		var tbl = document.getElementById(TABLE_NAME);
		var rowToInsertAt = tbl.tBodies[0].rows.length;
		for (var i=0; i<tbl.tBodies[0].rows.length; i++) {
			if (tbl.tBodies[0].rows[i].myRow && tbl.tBodies[0].rows[i].myRow.four.getAttribute('type') == 'radio' && tbl.tBodies[0].rows[i].myRow.four.checked) {
				rowToInsertAt = i;
				break;
			}
		}
		addRowToTable(rowToInsertAt);
		reorderRows(tbl, rowToInsertAt);
	}
}

/*
 * 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) {
		if(document.getElementById('tmp_quantity').value == '') { alert('Enter a quantity'); return false; }
		if(document.getElementById('tmp_product_code').selectedIndex == 0 && document.getElementById('tmp_manual_product_code').value == '') { alert('Select a product code'); return false; }
		if(document.getElementById('tmp_description').selectedIndex == 0 && document.getElementById('tmp_manual_description').value == '') { alert('Select a category/description'); return false; }

		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: This whole section can be configured
		var quantity = document.order_form.tmp_quantity.value;
		var product_code;
		if(document.order_form.tmp_manual_product_code.value != '') {
			product_code = document.order_form.tmp_manual_product_code.value
		} else {
			product_code = document.order_form.tmp_product_code.value
		}
		var description;
		if(document.order_form.tmp_manual_description.value != '') {
			description = document.order_form.tmp_manual_description.value;
		} else {
			description = document.order_form.tmp_description.value;
		}

		// cell 0 - text
		var cell0 = row.insertCell(0);
		var textNode = document.createTextNode(iteration);
		cell0.appendChild(textNode);

		var input0 = document.createElement('A');
		input0.setAttribute('href', '#');
		input0.innerHTML = ' (delete)';
		input0.onclick = function () {deleteCurrentRow(this)};
		cell0.appendChild(input0);

		// cell 1 - input text
		var cell1 = row.insertCell(1);
		cell1.setAttribute('align', 'left');
		var txtInp1 = document.createElement('input');
		txtInp1.setAttribute('type', 'hidden');
		txtInp1.setAttribute('name', 'Quantity[]'); //+iteration);
		txtInp1.setAttribute('value', quantity); // iteration included for debug purposes
		cell1.appendChild(txtInp1);
		cell1.appendChild(document.createTextNode(quantity));

		// cell 2 - input text
		var cell2 = row.insertCell(2);
		cell2.setAttribute('align', 'left');
		var txtInp2 = document.createElement('input');
		txtInp2.setAttribute('type', 'hidden');
		txtInp2.setAttribute('name', 'ProductCode[]'); //+iteration);
		txtInp2.setAttribute('value', product_code); // iteration included for debug purposes
		cell2.appendChild(txtInp2);
		cell2.appendChild(document.createTextNode(product_code));

		// cell 3 - input text
		var cell3 = row.insertCell(3);
		var txtInp3 = document.createElement('input');
		txtInp3.setAttribute('type', 'hidden');
		txtInp3.setAttribute('name', 'Description[]'); //+iteration);
		txtInp3.setAttribute('value', description); // iteration included for debug purposes
		cell3.appendChild(txtInp3);
		cell3.appendChild(document.createTextNode(description));

		// Pass in the elements you want to reference later
		// Store the myRow object in each row
		row.myRow = new myRowObject(textNode, txtInp1, txtInp2, txtInp3);

		// Reset values
		document.getElementById('tmp_quantity').value = '';
		document.getElementById('tmp_product_code').selectedIndex = 0;
		document.getElementById('tmp_manual_product_code').value = '';
		document.getElementById('tmp_description').selectedIndex = 0;
		document.getElementById('tmp_manual_description').value = '';
	}
}

// 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++) {
				// CONFIG: next line is affected by myRowObject settings
				tbl.tBodies[0].rows[i].myRow.one.data = count; // text

				// CONFIG: next line is affected by myRowObject settings
				//tbl.tBodies[0].rows[i].myRow.two.name = 'Quantity'+count; // input text

				// CONFIG: next line is affected by myRowObject settings
				//tbl.tBodies[0].rows[i].myRow.three.name = 'ProductCode'+count; // for debug purposes

				// CONFIG: next line is affected by myRowObject settings
				//tbl.tBodies[0].rows[i].myRow.four.name = 'Description'+count; // input radio

				count++;
			}
		}
	}
}

function deleteRows(rowObjArray)
{
	if (hasLoaded) {
		for (var i=0; i<rowObjArray.length; i++) {
			var rIndex = rowObjArray[i].sectionRowIndex;
			rowObjArray[i].parentNode.deleteRow(rIndex);
		}
	}
}
