function buildField(nm, tp) {
	var theField = document.createElement("input");
	theField.setAttribute("name", nm);
	if (tp != null) {
		theField.setAttribute("type", tp);
	}
	return theField;
}

function buildTextField(nm) {
	return buildField(nm, "text");
}

function buildSearchField(nm) {
	return buildField(nm, "search");
}

function buildCheckbox(nm) {
	return buildField(nm, "checkbox");
}

function buildLabeledField(field, lab, fieldFirst) {
	if (fieldFirst == null) {
		fieldFirst = false;
	}
	var theLine = document.createElement("div");
	var theLabel = document.createElement("label");
	theLabel.setAttribute("for", field.getAttribute("name"));
	theLabel.appendChild(document.createTextNode(lab));
	if (fieldFirst) {
		theLine.appendChild(field);
		theLine.appendChild(theLabel);
	} else {
		theLine.appendChild(theLabel);
		theLine.appendChild(field);
	}
	return theLine;
}

function buildLabeledRow(table, field, lab) {
	var theRow = table.insertRow(-1);
		// build label cell
	var theLabelCell = theRow.insertCell(-1);
	theLabelCell.className = "label";
	var theLabel = document.createElement("label");
	theLabel.setAttribute("for", field.getAttribute("name"));
	theLabel.appendChild(document.createTextNode(lab));
	theLabelCell.appendChild(theLabel);
		// build field cell
	var theFieldCell = theRow.insertCell(-1);
	theFieldCell.appendChild(field);
}

function createFieldset(leg) {
	var theFieldset = document.createElement("fieldset");
	if (leg != null) {
		var theLegend = document.createElement("legend");
		theLegend.appendChild(document.createTextNode(leg));
		theFieldset.appendChild(theLegend);
	}
	return theFieldset;
}

function findChildWithId(par, ident, deep) {
	if (deep == null) {
		deep = false;
	}
	try {
		for (var curr = par.firstChild; curr != null; curr = curr.nextSibling) {
			if (curr.id && curr.id == ident) {
				return curr;
			} else if (deep) {
				if (curr.hasChildNodes()) {
					var childResult = findChildWithId(curr, ident, deep);
					if (childResult != null) {
						return childResult;
					}
				} // otherwise continue looking in this level
			}
		}
		return null;
	} catch (m) {
		alert("caught exception in findChildWithId: " + m.toString());
	}
}

function findChildWithName(par, ident, deep) {
	if (deep == null) {
		deep = false;
	}
	try {
		for (var curr = par.firstChild; curr != null; curr = curr.nextSibling) {
			if (curr.hasAttributes() && curr.hasAttribute("name") && curr.getAttribute("name") == ident) {
				return curr;
			} else if (deep) {
				if (curr.hasChildNodes()) {
					var childResult = findChildWithName(curr, ident, deep);
					if (childResult != null) {
						return childResult;
					}
				} // otherwise continue looking in this level
			}
		}
		return null;
	} catch (m) {
		alert("caught exception in findChildWithName: " + m.toString());
	}
}

