// 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 RADIO_NAME = 'totallyrad'; // this is being set via script
var TABLE_NAME = 'tblSample'; // 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;
	addRowToTable();
	addRowToTable();
}

// CONFIG:
// myRowObject is an object for storing information about the table rows
function myRowObject(m_txtObj, m_inpTxtObj1, m_inpTxtObj2, m_inpTxtObj3, m_inpTxtObj4, m_inpTxtObj5, m_inpChkbxObj, m_inpRadioObj)
{
	this.m_txtObj = m_txtObj; // text object
	this.m_inpTxtObj1 = m_inpTxtObj1; // input text object
	this.m_inpTxtObj2 = m_inpTxtObj2; // input text object
	this.m_inpTxtObj3 = m_inpTxtObj3; // input text object
	this.m_inpTxtObj4 = m_inpTxtObj4; // input text object
	this.m_inpTxtObj5 = m_inpTxtObj5; // input text object
	this.m_inpChkbxObj = m_inpChkbxObj; // input checkbox object
	this.m_inpRadioObj = m_inpRadioObj; // 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.m_inpRadioObj.getAttribute('type') == 'radio' && tbl.tBodies[0].rows[i].myRow.m_inpRadioObj.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) {
		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);
	
		// CONFIG: This whole section can be configured
		
		// cell 0 - text
		var cell0 = row.insertCell(0);
		var textNode = document.createTextNode(iteration-3);
		cell0.appendChild(textNode);
		
		// cell 1 - input text
		var cell1 = row.insertCell(1);
		var txtInp1 = document.createElement('input');
		txtInp1.setAttribute('type', 'text');
		//txtInp1.setAttribute('name', "outstandingwdl" + (iteration-3));
		txtInp1.setAttribute('id', "outstandingwdl" + (iteration-3)); //not accessable in IE without this!
		txtInp1.setAttribute('size', '11');
		txtInp1.setAttribute('maxlength', '11');
		txtInp1.setAttribute('value', '0.00'); // iteration included for debug purposes
//		txtInp1.onchange = function () {RecalcSheet()};  //firefox always complains that this function is not defined!
		cell1.appendChild(txtInp1);
		
		// cell 2 - input text
		var cell2 = row.insertCell(2);
		var txtInp2 = document.createElement('input');
		txtInp2.setAttribute('type', 'text');
		//txtInp2.setAttribute('name', "outstandingdep" + (iteration-3));
		txtInp2.setAttribute('id', "outstandingdep" + (iteration-3)); //not accessable in IE without this!
		txtInp2.setAttribute('size', '11');
		txtInp2.setAttribute('maxlength', '11');
		txtInp2.setAttribute('value', '0.00'); // iteration included for debug purposes
//		txtInp2.onchange = function () {RecalcSheet()};  //firefox always complains that this function is not defined!
		cell2.appendChild(txtInp2);
		
		// cell 3 - input text
		var cell3 = row.insertCell(3);
		var txtInp3 = document.createElement('input');
		txtInp3.setAttribute('type', 'text');
		//txtInp3.setAttribute('name', "outstandingdate" + (iteration-3));
		txtInp3.setAttribute('id', "outstandingdate" + (iteration-3)); //not accessable in IE without this!
		txtInp3.setAttribute('size', '14');
		txtInp3.setAttribute('maxlength', '10');
		var d=new Date();
		txtInp3.setAttribute('value', '//'+d.getFullYear()); // iteration included for debug purposes
		cell3.appendChild(txtInp3);
		
		// cell 4 - input text
		var cell4 = row.insertCell(4);
		var txtInp4 = document.createElement('input');
		txtInp4.setAttribute('type', 'text');
		//txtInp4.setAttribute('name', "outstandingnum" + (iteration-3));
		txtInp4.setAttribute('id', "outstandingnum" + (iteration-3)); //not accessable in IE without this!
		txtInp4.setAttribute('size', '11');
		txtInp4.setAttribute('maxlength', '11');
		txtInp4.setAttribute('value', ''); // iteration included for debug purposes
		cell4.appendChild(txtInp4);
		
		// cell 5 - input text
		var cell5 = row.insertCell(5);
		var txtInp5 = document.createElement('input');
		txtInp5.setAttribute('type', 'text');
		//txtInp5.setAttribute('name', "outstandingdesc" + (iteration-3));
		txtInp5.setAttribute('id', "outstandingdesc" + (iteration-3)); //not accessable in IE without this!
		txtInp5.setAttribute('size', '50');
		txtInp5.setAttribute('maxlength', '100');
		txtInp5.setAttribute('value', ''); // iteration included for debug purposes
		cell5.appendChild(txtInp5);
		
		// cell 6 - input button
		var cell6 = row.insertCell(6);
		var btnEl = document.createElement('input');
		btnEl.setAttribute('type', 'button');
		btnEl.setAttribute('value', 'Delete');
		btnEl.onclick = function () {deleteCurrentRow(this)};
		cell6.appendChild(btnEl);
		
		// cell 7 - input checkbox
		var cell7 = row.insertCell(7);
		var cbEl = document.createElement('input');
		cbEl.setAttribute('type', 'checkbox');
		cell7.appendChild(cbEl);
		
		// cell 8 - input radio
		var cell8 = row.insertCell(8);
		var raEl;
		try {
			raEl = document.createElement('<input type="radio" name="' + RADIO_NAME + '" value="' + iteration + '">');
			var failIfNotIE = raEl.name.length;
		} catch(ex) {
			raEl = document.createElement('input');
			raEl.setAttribute('type', 'radio');
			raEl.setAttribute('name', RADIO_NAME);
			raEl.setAttribute('value', iteration);
		}
		cell8.appendChild(raEl);
		
		// Pass in the elements you want to reference later
		// Store the myRow object in each row
		//row.myRow = new myRowObject(textNode, txtInp, cbEl, raEl);
		row.myRow = new myRowObject(textNode, txtInp1, txtInp2, txtInp3, txtInp4, txtInp5, cbEl, raEl);
	}
}

// CONFIG: this entire function is affected by myRowObject settings
// If there isn't a checkbox in your row, then this function can't be used.
function deleteChecked()
{
	if (hasLoaded) {
		var checkedObjArray = new Array();
		var cCount = 0;
	
		var tbl = document.getElementById(TABLE_NAME);
		for (var i=0; i<tbl.tBodies[0].rows.length; i++) {
			if (tbl.tBodies[0].rows[i].myRow && tbl.tBodies[0].rows[i].myRow.m_inpChkbxObj.getAttribute('type') == 'checkbox' && tbl.tBodies[0].rows[i].myRow.m_inpChkbxObj.checked) {
				checkedObjArray[cCount] = tbl.tBodies[0].rows[i];
				cCount++;
			}
		}
		if (checkedObjArray.length > 0) {
			var rIndex = checkedObjArray[0].sectionRowIndex;
			deleteRows(checkedObjArray);
			reorderRows(tbl, rIndex);
		}
	}
}

// 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.m_txtObj.data = (count-3); // text
				
				// CONFIG: next line is affected by myRowObject settings
				tbl.tBodies[0].rows[i].myRow.m_inpTxtObj1.id = "outstandingwdl" + (count-3); // input text
				// CONFIG: next line is affected by myRowObject settings
				tbl.tBodies[0].rows[i].myRow.m_inpTxtObj2.id = "outstandingdep" + (count-3); // input text
				// CONFIG: next line is affected by myRowObject settings
				tbl.tBodies[0].rows[i].myRow.m_inpTxtObj3.id = "outstandingdate" + (count-3); // input text
				// CONFIG: next line is affected by myRowObject settings
				tbl.tBodies[0].rows[i].myRow.m_inpTxtObj4.id = "outstandingnum" + (count-3); // input text
				// CONFIG: next line is affected by myRowObject settings
				tbl.tBodies[0].rows[i].myRow.m_inpTxtObj5.id = "outstandingdesc" + (count-3); // input text
				
//				// CONFIG: next line is affected by myRowObject settings
//				var tempVal = tbl.tBodies[0].rows[i].myRow.m_inpTxtObj1.value.split(' '); // for debug purposes
//				tbl.tBodies[0].rows[i].myRow.m_inpTxtObj1.value = count + ' was' + tempVal[0]; // for debug purposes
//				var tempVal = tbl.tBodies[0].rows[i].myRow.m_inpTxtObj2.value.split(' '); // for debug purposes
//				tbl.tBodies[0].rows[i].myRow.m_inpTxtObj2.value = count + ' was' + tempVal[0]; // for debug purposes
//				var tempVal = tbl.tBodies[0].rows[i].myRow.m_inpTxtObj3.value.split(' '); // for debug purposes
//				tbl.tBodies[0].rows[i].myRow.m_inpTxtObj3.value = count + ' was' + tempVal[0]; // for debug purposes
//				var tempVal = tbl.tBodies[0].rows[i].myRow.m_inpTxtObj4.value.split(' '); // for debug purposes
//				tbl.tBodies[0].rows[i].myRow.m_inpTxtObj4.value = count + ' was' + tempVal[0]; // for debug purposes
//				var tempVal = tbl.tBodies[0].rows[i].myRow.m_inpTxtObj5.value.split(' '); // for debug purposes
//				tbl.tBodies[0].rows[i].myRow.m_inpTxtObj5.value = count + ' was' + tempVal[0]; // for debug purposes
				
				// CONFIG: next line is affected by myRowObject settings
				tbl.tBodies[0].rows[i].myRow.m_inpRadioObj.value = count; // input radio
				
				// CONFIG: requires class named classy0 and classy1
				tbl.tBodies[0].rows[i].className = 'classy' + (count % 2);
				
				count++;
			}
		}
	}
}

function deleteRows(rowObjArray)
{
	if (hasLoaded) {
		for (var i=0; i<rowObjArray.length; i++) {
			var rIndex = rowObjArray[i].sectionRowIndex;
			rowObjArray[i].parentNode.deleteRow(rIndex);
		}
	}
}

function openInNewWindow(frm)
{
	// open a blank window
	var aWindow = window.open('', 'TableAddRow2NewWindow',
	'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
	
	// set the target to the blank window
	frm.target = 'TableAddRow2NewWindow';
	
	// submit
	frm.submit();
}