function addrow(){
	var thediv = document.getElementById('item-ma');
	if (thediv){
		var formlen = thediv.getElementsByTagName('table'); // get the table
		var divlen = thediv.getElementsByTagName('div'); // determine the number of rows in the form
		var lastdiv = divlen.length - 1; // find the ID of the final row
		var thelastdiv = document.getElementById(lastdiv); // get the last row and store
		var newid = formlen.length; // get the new ID for the new row
		var oldrow = document.getElementById('0'); // find the initial row
		
		var newrow = oldrow.cloneNode(true); // clone a new row from the initial row
		
		var newinpts = newrow.getElementsByTagName('select'); // get the select inputs
		
		for (var i=0; i<newinpts.length; i++){ // loop through and replace each select's name with a new one for this row
			var thename = newinpts[i].name
			thename = thename.replace(/\w+_/, '');
			thename = "vwattr" + newid + "_" + thename;
			
			newinpts[i].name = thename;
		}
		
		var tds = newrow.getElementsByTagName('td'); // get the cell the qty input is in
		var qtybox = tds[0].getElementsByTagName('input'); // get the qty input
		
		qtybox[0].name = "vwquantity" + newid; // change the name for the new row
		qtybox[1].name = "vwitem" + newid; // change the item for the new row
		
		newrow.id = newid; // change the ID of this cloned row to that of the new one
		insertAfter(newrow,thelastdiv); // insert the new row into the DOM
	}
	
	var dellink = document.getElementById('delrow'); // "turn on" the delete previous row link
	dellink.style.display = "block";
}

function removerow(){
	var thediv = document.getElementById('item-ma');
	if (thediv){
		var divlen = thediv.getElementsByTagName('div'); // determine the number of rows in the form
		var lastdiv = divlen.length - 1; // find the ID of the final row
		var thelastdiv = document.getElementById(lastdiv); // get the last row and store
		
		thediv.removeChild(thelastdiv);
	}
	
	var divlen2 = thediv.getElementsByTagName('div');
	
	if (divlen2.length == 1){
		var dellink = document.getElementById('delrow'); // "turn on" the delete previous row link
		dellink.style.display = "none";
	}
}

function getElementsByClassName(classname, node)  {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function insertAfter(newElement,targetElement) {
	var parent = targetElement.parentNode;
	if(parent.lastchild == targetElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement, targetElement.nextSibling);
	}
}
