function autoRow (strTableName,nID) {

	//The table we are working on
	var oTBody=document.getElementById(strTableName+'TBody');

	//check whether we are on the last row
	if (lastVisibleRow(strTableName)==strTableName+'Row'+nID) {
		addRow(strTableName);
	}
}

function addRow (strTableName) {

	//The table we are working on
	var oTBody=document.getElementById(strTableName+'TBody');

	//insert a new row into the table
	myTR=oTBody.insertRow();

	//set the name
	myTR.id=strTableName+'Row'+eval('n'+strTableName+'Rows');

	//get the template row
	oTemplateRow=document.getElementById(strTableName+'RowTemplate');

	//for each templateRow child element
	for (i=0;i<oTemplateRow.cells.length;++i) {

			//clone the element
			var oTD = oTemplateRow.cells(i).cloneNode(true);

			//replace any ZZZZidZZZZ placeholder with the new row ID
			var re = new RegExp ('ZZZZidZZZZ', 'gi') ;
			oTD.innerHTML = oTD.innerHTML.replace(re, eval('n'+strTableName+'Rows')) ;

			//append the element to the new row
			myTR.appendChild(oTD);
	}
	//increase the row counter
	eval('++n'+strTableName+'Rows');
}

function deleteRow(strTableName,nID){

	var oTBody=document.getElementById(strTableName+'TBody');
	//oTBody.removeChild(document.getElementById(strTableName+'Row'+nID));
	
	//set it to bDelete=true and hide it
	document.getElementById("bDelete"+strTableName+nID).value="true";
	document.getElementById(strTableName+'Row'+nID).style.display="none";
	
	//if there are no more rows, add one (2 accounts for headers)
	if (visibleRowCount(strTableName)==0) addRow(strTableName);
}

function lastVisibleRow(strTableName) {

	var oTBody=document.getElementById(strTableName+'TBody');

	var count=0;
	for (i=0; i<eval('n'+strTableName+'Rows'); ++i) {
		if (document.getElementById("bDelete"+strTableName+i)) {
			if (document.getElementById("bDelete"+strTableName+i).value=="false") {
				rowID=oTBody.rows(strTableName+'Row'+i).id;
			}
		}
	}
	return rowID;
	
}

function visibleRowCount(strTableName) {
	var count=0;
	for (i=0; i<eval('n'+strTableName+'Rows'); ++i) {
		if (document.getElementById("bDelete"+strTableName+i)) {
			if (document.getElementById("bDelete"+strTableName+i).value=="false") ++count;
		}
	}
	return count;
}