// mredkj.com
// 2006-07-21
// Adapted by Matt Law for Sparrow Dry Cleaners
// 10-March-2008

var ROW_BASE = 1;
var INPUT_NAME_PREFIX = 'txt';


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
}

function insertRowPHP(num)
{
	var tbl = document.getElementById('tblInsertRowPHP');
	 	
	var nextRow = tbl.tBodies[0].rows.length;
	
	var iteration = nextRow + ROW_BASE;

	num = nextRow 
	iteration = num + ROW_BASE;
	
		
	// add the row
	var newRow = tbl.tBodies[0].insertRow(num);
 
	var idxCell = newRow.insertCell(0);
	var textNode = document.createTextNode(iteration);
	idxCell.appendChild(textNode);
	
	var newCell = newRow.insertCell(1);
	var newCell1 = newRow.insertCell(2);
	var newCell2 = newRow.insertCell(3);
	
	var txtQuantity = document.createElement('input');
	txtQuantity.type = 'text';
	txtQuantity.name = 'txtQuantity[]';
	txtQuantity.id = 'txtQuantity' + iteration;
	txtQuantity.className = "small";
	newCell.appendChild(txtQuantity);
	txtQuantity.focus();
	
	var txtItem = document.createElement('input');
	txtItem.type = 'text';
	txtItem.name = 'txtItem[]';
	txtItem.id = 'txtItem' + iteration;
	txtItem.className = "large";
	newCell1.appendChild(txtItem);
	
	var txtInstructions = document.createElement('input');
	txtInstructions.type = 'text';
	txtInstructions.name = 'txtInstructions[]';
	txtInstructions.id = 'txtInstructions' + iteration;
	txtInstructions.className = "large";
	newCell2.appendChild(txtInstructions);
	
	newRow.myRow = new myRowObject(textNode,txtQuantity,txtItem, txtInstructions);
	
	// cell 3 - delete button
	var cmdCell = newRow.insertCell(4);
	var btnEl = document.createElement('input');
	btnEl.type = 'button';
	btnEl.value = 'Delete';
	btnEl.onclick = function () {deleteCurrentRow(this)};
	cmdCell.appendChild(btnEl);
  
}

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

function deleteCurrentRow(obj)
{
	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 (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
		 
			 
			
			count++;
		}
	}
	 
}


function deleteAllRows(tblId)
{
  var tbl = document.getElementById(tblId);
  for (var i=tbl.tBodies[0].rows.length-1; i>=0; i--) {
    tbl.tBodies[0].deleteRow(i);
  }
}

function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}