function addRowToTable()
{
	
	var tbl = document.getElementById('multipleSearchTable');
	var lastRow = tbl.rows.length;
	
	if (lastRow <= 9) {
		var row = tbl.insertRow(lastRow);
	} else {
		alert('Only 10 fields may be searched at a time.');
		return false;
	}
	
	// fieldSelect cell
	var cellFieldSelect = row.insertCell(0);
	var fs = document.createElement('select');
	fs.name = 'fieldSelect[]';
	fs.options[0] = new Option('persons/groups', 'pg');
	fs.options[1] = new Option('locations', 'l');
	fs.options[2] = new Option('items/things', 'it');
	fs.options[3] = new Option('summary', 'g');
	fs.options[4] = new Option('phrases', 'p');
	overwriteAttribute(fs, 'class', 'selectoption');
	cellFieldSelect.appendChild(fs);
	
	// fieldSelect cell
	var cellTypeSelect = row.insertCell(1);
	var ts = document.createElement('select');
	ts.name = 'typeSelect[]';
	ts.options[0] = new Option('contains', 'c');
	ts.options[1] = new Option('does not contain', 'dnc');
	overwriteAttribute(ts, 'class', 'selectoption');
	cellTypeSelect.appendChild(ts);
	
	// searchInput cell
	var cellSearchInput = row.insertCell(2);
	var si = document.createElement('input');
	si.type = 'text';
	si.name = 'searchInput[]';
	si.size = 50;
	overwriteAttribute(si, 'id', 'fieldtext');
	cellSearchInput.appendChild(si);
	
	// Remove Row button
	var removeRowCell = row.insertCell(3);
	var removeRowButton = document.createElement("input");
	removeRowButton.type = "button";
	removeRowButton.id = "removeRow";
	removeRowButton.value = "-";
	overwriteAttribute(removeRowButton, 'class', 'searchbutton');
	// This works in Firefox 2 but not IE 7
	//removeRowButton.setAttribute("onclick", "deleteRow(this);");
	// This works in Firefox 2 and IE 7
	removeRowButton.onclick = new Function('deleteRow(this);');
	removeRowCell.appendChild(removeRowButton);
	
	// Add row button
	var addRowCell = row.insertCell(4);
	var addRowButton = document.createElement("input");
	addRowButton.type = "button";
	addRowButton.id = "addRow";
	addRowButton.value = "+";
	overwriteAttribute(addRowButton, 'class', 'searchbutton');
	addRowButton.onclick = new Function('addRowToTable();');
	addRowCell.appendChild(addRowButton);

	// Add a remove row button to the first delete cell if two rows exist.
	if (tbl.rows.length == 2) {
		var firstDeleteCell = document.getElementById('multipleSearchTable').rows[0].cells[3];
		var removeRowButton = document.createElement("input");
		removeRowButton.type = "button";
		removeRowButton.id = "removeRow";
		removeRowButton.value = "-";
		overwriteAttribute(removeRowButton, 'class', 'searchbutton');
		removeRowButton.onclick = new Function('deleteRow(this);');
		firstDeleteCell.appendChild(removeRowButton);
	}
}

// Used to correct IE's poor handling of setAttribute() in the DOM.
function overwriteAttribute(obj, attributeName, attributeValue)
{
	if (obj.getAttributeNode(attributeName)) {
		for (var i = 0; i < obj.attributes.length; i++) {
			var attrName = obj.attributes[i].name.toUpperCase();
			if (attrName == attributeName.toUpperCase()) {
				obj.attributes[i].value = attributeValue;
			}
		}
	} else {
		obj.setAttribute(attributeName, attributeValue);
	}
}

function deleteRow(delButton)
{
	// Delete the requested table row.
	var tbl = delButton.parentNode.parentNode.parentNode;
	var tr = delButton.parentNode.parentNode;
	tbl.removeChild(tr);
	
	// Delete the remove row button from the first delete cell if only one row exists.
	var tbl = document.getElementById('multipleSearchTable');
	if (tbl.rows.length == 1) {
		var firstDeleteCell = document.getElementById('multipleSearchTable').rows[0].cells[3];
		firstDeleteCell.innerHTML = '';
	}
}
