// 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 INPUT_NAME_PREFIX = 'inputName'; // this is being set via script
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(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) {
		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);
		//cell0.appendChild(textNode);
		
		// cell qty - input text
		var cell0 = row.insertCell(0);
		var txtInp0 = document.createElement('input');
		txtInp0.type = 'text';
		txtInp0.name = 'qty[]';
		txtInp0.id = 'qty' + iteration;
		txtInp0.size = 2; // iteration included for debug purposes
		cell0.appendChild(txtInp0);
		
		// cell manufacturer - input text
		var cell1 = row.insertCell(1);
		var txtInp1 = document.createElement('input');
		txtInp1.type = 'text';
		txtInp1.name = 'manufacturer[]';
		txtInp1.value = jsvar;
		txtInp1.id = 'manufacturer' + iteration;
		txtInp1.size = 12; // iteration included for debug purposes
		cell1.appendChild(txtInp1);
		
		// cell partnumber - input text
		var cell2 = row.insertCell(2);
		var txtInp2 = document.createElement('input');
		txtInp2.type = 'text';
		txtInp2.name = 'PartNumber[]';
		txtInp2.id = 'PartNumber' + iteration;
		txtInp2.size = 12; // iteration included for debug purposes
		cell2.appendChild(txtInp2);
		
		// cell description- input text
		var cell3 = row.insertCell(3);
		var txtInp3 = document.createElement('input');
		txtInp3.type = 'text';
		txtInp3.name = 'Description[]';
		txtInp3.id = 'Description' + iteration;
		txtInp3.size = 12; // iteration included for debug purposes
		cell3.appendChild(txtInp3);

		// cell Price- input text
		var cell4 = row.insertCell(4);
		var txtInp4 = document.createElement('input');
		txtInp4.type = 'text';
		txtInp4.name = 'fault[]';
		txtInp4.id = 'fault' + iteration;
		txtInp4.size = 12; // iteration included for debug purposes
		cell4.appendChild(txtInp4);
		
		
		// cell 2 - input button
		var cell6 = row.insertCell(5);
		var btnEl = document.createElement('input');
		btnEl.setAttribute('type', 'button');
		btnEl.setAttribute('value', 'Delete');
		btnEl.onclick = function () {deleteCurrentRow(this)};
		cell6.appendChild(btnEl);

	}
}

// 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.three.getAttribute('type') == 'checkbox' && tbl.tBodies[0].rows[i].myRow.three.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.one.data = count; // text
				
				// CONFIG: next line is affected by myRowObject settings
				tbl.tBodies[0].rows[i].myRow.two.name = INPUT_NAME_PREFIX + count; // input text
				
				// CONFIG: next line is affected by myRowObject settings
				var tempVal = tbl.tBodies[0].rows[i].myRow.two.value.split(' '); // for debug purposes
				tbl.tBodies[0].rows[i].myRow.two.value = count + ' was' + tempVal[0]; // for debug purposes
				
				// CONFIG: next line is affected by myRowObject settings
				tbl.tBodies[0].rows[i].myRow.four.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 FillBilling(f) {
  if(f.billingtoo.checked == true) {
    f.billcompany.value = f.company.value;
   	 f.billName.value = f.Name.value;
	 	f.billaddress.value = f.address.value;
	  		f.billaddress2.value = f.address2.value;
	 			f.billcity.value = f.city.value;
					f.billstate.value = f.state.value;
	 					f.billzipCode.value = f.zipCode.value;
	  						f.billcountry.value = f.country.value;
	   							f.billphone.value = f.phone.value;
	    							f.billfax.value = f.fax.value;
    /* If more fields are used, just expand the parameters				
       above to include the additional fields. */	  
  }
}