function GroupAddGroupRow(tableName, orderField, idfield, masterrow, maxrows) {
	//get table element
	
	var table = document.getElementById(tableName);
	//select lastOrder field, else -1
	var lastOrderFieldIndex = document.getElementsByName(orderField).length-2 < 0 ? -1 : document.getElementsByName(orderField).length-2;
	var lastRow = -1;
	//if lastOrderField get last row number
	if(lastOrderFieldIndex > -1){
		lastRow = parseInt(document.getElementsByName(orderField)[lastOrderFieldIndex].value);
	}
	//get max rows allowed
	maxrowTest = parseInt(maxrows) + parseInt(masterrow);
	//if maxrows is not reached, add extra row
	if(table.rows.length-1 < maxrowTest || maxrows == 0){
		//select row template
		var newRowTemplate = document.getElementById('row_template_' + tableName);
		//create new row element
		var newRow = table.insertRow(table.rows.length-1);
		//copy all cell's one by one because bill gates is a bastart.		
		for(i=0; i < newRowTemplate.cells.length; i++){
			var newCell = newRow.insertCell(i);
			newCell.innerHTML = newRowTemplate.cells[i].innerHTML.replace(/#templateid/g, (lastRow+1).toString());
		}
		//run kpwindows js includes
		//set classes for rows (display)
		GroupSetGroupOrder(tableName, orderField, idfield);
	}
}

function GroupDeleteGroupOrder(tableName, orderField, idfield, masterrow, orderObj) {
	var rownum = document.getElementById(orderObj).parentNode.parentNode.rowIndex;
	var tabel = document.getElementById(tableName);
	if(tabel.rows.length-1 > parseInt(masterrow)+1){
		tabel.deleteRow(parseInt(rownum));
	}
	else {
		var newRow = tabel.insertRow(tabel.rows.length-1);
		for(i=0; i < tabel.rows[masterrow].cells.length; i++){
			
			var newCell = newRow.insertCell(i);
			var innerHTML = tabel.rows[masterrow].cells[i].innerHTML;
			if(innerHTML.match(/class=\"{0,1}text_item\"{0,1}/) != null){
				var txtNewOrd = 'name=';
				var txtOldOrd = /value=.* name=/gi;
				innerHTML = innerHTML.replace(txtOldOrd, txtNewOrd);
			}
			
			if(innerHTML.match(/class=\"{0,1}datepicker\"{0,1}/) != null){
				var txtNewOrd = 'name='
				var txtOldOrd = /value=.* name=/gi;
				innerHTML = innerHTML.replace(txtOldOrd, txtNewOrd);
			}
			
			if(innerHTML.match(/cols/) != null){
				var areaNewOrd = '><';
				var areaOldOrd = />.*</gi;	
				innerHTML = innerHTML.replace(areaOldOrd, areaNewOrd);
			}
			innerHTML = innerHTML.replace(/checked/gi,"");
			innerHTML = innerHTML.replace(/selected/gi,"");
			newCell.innerHTML = innerHTML;
		}
		tabel.deleteRow(parseInt(rownum));
	}
	
	GroupSetGroupOrder(tableName, orderField, idfield);
}

function GroupSetGroupOrder(tableName, orderField, idfield){
	var tabel = document.getElementById(tableName);
	for(i=1; i < tabel.rows.length-1; i++) {	
		if(i%2){
			tabel.rows[i].className = 'table_colrow';	
		}
		else{
			tabel.rows[i].className = 'table_nocolrow';	
		}
	}
	//alert('call KPWindowRunComponentJS(' +KPWindowFindParent(tabel).id+');');
	KPWindowRunComponentJS(KPWindowFindParent(tabel));
}