// $Revision: 2475 $
function ReplaceInnerHtml(id, html) {
	try {
		if (id == null) {
			// Busy(false);
			errorReporter.Report("DynamicUpdate.js", "ReplaceInnerHtml: id is null");
			return;
		}
		var object = getElement(id);
		if (object == null) {
			// Busy(false);
			errorReporter.Report("DynamicUpdate.js", "ReplaceInnerHtml: id '" + id + "' not found");
			return;
		}
		object.innerHTML = unEscape(html); // Mozilla and IE, not for IE TABLE, TR, SELECT, etc.
	} catch(e) {
		Busy(false);
		errorReporter.Report("DynamicUpdate.js", "ReplaceInnerHtml('" + id + "', '" + html + "')", e);
	}
}

function ReplaceOuterHtml(id, html) {
	try {
		if (id == null) {
			// Busy(false);
			errorReporter.Report("DynamicUpdate.js", "ReplaceOuterHtml: id is null");
			return;
		}
		var remove = getElement(id);
		if (remove == null) {
			// Busy(false);
			errorReporter.Report("DynamicUpdate.js", "ReplaceOuterHtml: id '" + id + "' not found");
			return;
		}
		var parent = remove.parentNode;
		var before = remove.nextSibling;
		var div = document.createElement("div");
		div.innerHTML = unEscape(html);
		var scrollTop = parent.scrollTop;
		parent.removeChild(remove);
		while (div.childNodes.length > 0) {
			parent.insertBefore(div.childNodes[0], before);
		}
		var scrollable = parent.scrollHeight - parent.clientHeight;
		if (scrollTop > 0 && scrollable > 0) {
			parent.scrollTop = Math.min(scrollTop, scrollable);
		}
	} catch(e) {
		Busy(false);
		errorReporter.Report("DynamicUpdate.js", "ReplaceOuterHtml('" + id + "', '" + html + "')", e);
	}
}

function InsertHtmlBefore(id, html) {
	try {
		var object = getElement(id);
		var div = document.createElement("div");
		div.innerHTML = unEscape(html);
		while (div.childNodes.length > 0) {
			object.parentNode.insertBefore(div.childNodes[0], object);
		}
	} catch(e) {
		// Busy(false);
		errorReporter.Report("DynamicUpdate.js", "InsertHtmlBefore('" + id + "', '" + html + "')", e);
	}
}

function InsertHtmlAfter(id, html) {
	try {
		var object = getElement(id);
		var before = object.nextSibling;
		var div = document.createElement("div");
		div.innerHTML = unEscape(html);
		while (div.childNodes.length > 0) {
			object.parentNode.insertBefore(div.childNodes[0], before);
		}
		if (object.parentNode.id == null) {
			assignId(object.parentNode);
		}
	} catch(e) {
		// Busy(false);
		errorReporter.Report("DynamicUpdate.js", "InsertHtmlAfter('" + id + "', '" + html + "')", e);
	}
}

function InsertHtmlAtBeginning(containerId, objectId, html) {
	try {
		if (objectId != null && getElement(objectId) != null) {
			return ReplaceOuterHtml(objectId, html);
		}
		var container;
		if (containerId == null) {
			container = document.body;
		} else {
			container = getElement(containerId);
		}
		var before = container.firstChild;
		var div = document.createElement("div");
		div.innerHTML = unEscape(html);
		while (div.childNodes.length > 0) {
			container.insertBefore(div.childNodes[0], before);
		}
	} catch(e) {
		// Busy(false);
		errorReporter.Report("DynamicUpdate.js", "InsertHtmlAtBeginning('" + containerId + "', '" + objectId + "', '" + html + "')", e);
	}
}

function InsertHtmlAtEnd(containerId, objectId, html) {
	try {
		if (objectId != null && getElement(objectId) != null) {
			return ReplaceOuterHtml(objectId, html);
		}
		var container;
		if (containerId == null) {
			container = document.body;
		} else {
			container = getElement(containerId);
		}
		var div = document.createElement("div");
		div.innerHTML = unEscape(html);
		while (div.childNodes.length > 0) {
			container.insertBefore(div.childNodes[0], null);
		}
	} catch(e) {
		// Busy(false);
		errorReporter.Report("DynamicUpdate.js", "InsertHtmlAtEnd('" + containerId + "', '" + objectId + "', '" + html + "')", e);
	}
}

function addTableRows(id, html) {
	try {
		var table = getElement(id);
		if (table == null) {
			return;
		}
		if (table.nodeName != "TABLE") {
			errorReporter.Report("DynamicUpdate.js", "addTableRows: id '" + id + "' is not a table");
			return;
		}
		var div = document.createElement("div");
		div.innerHTML = "<table>" + unEscape(html) + "</table>";
		var rows = div.childNodes[0].tBodies[0].rows;
		var tbody = table.tBodies[0];
		if (rows != null && rows.length > 0) {
			iter = rows[0].getAttribute("iter");
			if (iter != null) {
				table.setAttribute("iter", iter);
			}
			if (isNs) {
				while (rows.length > 0) {
					tbody.insertBefore(rows[0], null);
				}
			} else {
				for (var i = 0; i < rows.length; i++) {
					var row = tbody.insertRow(-1);
					copyAttributes(rows[i], row);
					for (var j = 0; j < rows[i].cells.length; j++) {
						var cell = row.insertCell();
						copyAttributes(rows[i].cells[j], cell);
						cell.innerHTML = rows[i].cells[j].innerHTML;
					}
				}
			}
		}
	} catch(e) {
		errorReporter.Report("DynamicUpdate.js", "addTableRows('" + id + "', '" + html + "')", e);
	}
}

function insertTableRows(id, html) {
	try {
		// if id refers to a table, insert at the beginning
		var insertAtBeginning;
		var table = getElement(id);
		if (table == null) {
			errorReporter.Report("DynamicUpdate.js", "insertTableRows: id '" + id + "' not found");
			return;
		}
		if (table.tagName.toLowerCase == "table") {
			insertAtBeginning = true;
		} else {
			// else id must refer to an element in a row of the table
			var rowBefore = getTableRow(unEscape(id));
			if (rowBefore == null) {
				errorReporter.Report("DynamicUpdate.js", "insertTableRows: id '" + id + "' is not in a table row");
				return;
			}
			var table = getAncestorByTagName(rowBefore, "table");
			if (table.nodeName != "TABLE") {
				errorReporter.Report("DynamicUpdate.js", "insertTableRows: table ancestor of id '" + id + "' not found");
				return;
			}
		}
		var div = document.createElement("div");
		div.innerHTML = "<table>" + unEscape(html) + "</table>";
		var newRows = div.childNodes[0].tBodies[0].rows;
		var tbody = table.tBodies[0];
		var rows = tbody.rows;
		var rowAfterIndex;
		var rowAfter;
		if (insertAtBeginning) {
			rowAfterIndex = 0;
			rowAfter = rows[rowAfterIndex];
		} else {
			rowAfterIndex = -1;
			rowAfter = null;
			for (var i = 0; i < rows.length; i++) {
				if (rows[i] == rowBefore) {
					if (i < rows.length - 1) {
						rowAfterIndex = i + (1 - 0);
						rowAfter = rows[rowAfterIndex];
					}
					break;
				}
			}
		}
		if (newRows != null && newRows.length > 0) {
			iter = newRows[0].getAttribute("iter");
			if (iter != null) {
				table.setAttribute("iter", iter);
			}
			if (isNs) {
				while (newRows.length > 0) {
					tbody.insertBefore(newRows[0], rowAfter);
				}
			} else {
				for (var i = 0; i < newRows.length; i++) {
					var row = tbody.insertRow(rowAfterIndex);
					copyAttributes(newRows[i], row);
					for (var j = 0; j < newRows[i].cells.length; j++) {
						var cell = row.insertCell();
						copyAttributes(newRows[i].cells[j], cell);
						cell.innerHTML = newRows[i].cells[j].innerHTML;
					}
				}
			}
			if (table.id = null) {
				assignId(table);
			}
		}
	} catch(e) {
		errorReporter.Report("DynamicUpdate.js", "insertTableRows('" + id + "', '" + html + "')", e);
	}
}

function copyAttributes(from, to) {
	if (!from) {
		return;
	}
	if (from.className) {
		to.className = from.className;
	}
	if(from.style){
		to.style.cssText = from.style.cssText;
	}
	if (!from.attributes) {
		return;
	}
	for (var i=0; i<from.attributes.length; i++) {
		var attr = from.attributes[i];
		if (attr.specified) {
			to.setAttribute(attr.nodeName, attr.nodeValue);
		}
	}
}

function RemoveElement(id) {
	try {
		var object = getElement(id);
		if (object == null) {
			return;
		}
		DoRemoveElement(object);
	} catch(e) {
		// Busy(false);
		errorReporter.Report("DynamicUpdate.js", "RemoveElement('" + id + "', '" + relation + "')", e);
	}
}

function DoRemoveElement(object) {
	try {
		var parent = object.parentNode;
		parent.removeChild(object);
	} catch(e) {
		// Busy(false);
		errorReporter.Report("DynamicUpdate.js", "DoRemoveElement('" + object.id + "')", e);
	}
}

function RemoveContents(id) {
	try {
		var object = getElement(id);
		if (object == null) {
			return;
		}
		DoRemoveContents(object);
	} catch(e) {
		// Busy(false);
		errorReporter.Report("DynamicUpdate.js", "RemoveContents('" + id + "', '" + relation + "')", e);
	}
}

function DoRemoveContents(object) {
	try {
		for (var n = object.childNodes.length - 1; n >= 0; n--) {
			object.removeChild(object.childNodes[n]);
		}
	} catch(e) {
		// Busy(false);
		errorReporter.Report("DynamicUpdate.js", "DoRemoveContents('" + object.id + "')", e);
	}
}

function getTableRow(id) {
	var element = getElement(id);
	if (element == null) {
		errorReporter.Report("DynamicUpdate.js", "getTableRow: id '" + id + "' not found");
		return null;
	}
	return getAncestorByTagName(element, "tr");
}

function removeTableRow(id) {
	var row = getTableRow(id);
	var parent;
	if (row != null) {
		parent = row.parentNode;
		parent.removeChild(row);
	}
}

function replaceTableRow(id, html) {
	try {
		var oldRow = getTableRow(unEscape(id));
		if (oldRow == null) {
			errorReporter.Report("DynamicUpdate.js", "replaceTableRow: row containing id '" + id + "' not found");
			return;
		}
		var table = getAncestorByTagName(oldRow, "table");
		if (table == null) {
			errorReporter.Report("DynamicUpdate.js", "replaceTableRow: table ancestor of id '" + id + "' not found");
			return;
		}
		var div = document.createElement("div");
		div.innerHTML = "<table>" + unEscape(html) + "</table>";
		var rows = div.childNodes[0].tBodies[0].rows;
		var tbody = table.tBodies[0];
		if (rows != null && rows.length > 0) {
			iter = rows[0].getAttribute("iter");
			if (iter != null) {
				table.setAttribute("iter", iter);
			}
			if (isNs) {
				while (rows.length > 0) {
					tbody.insertBefore(rows[0], oldRow);
				}
			} else {
				var oldRowIndex = oldRow.rowIndex;
				for (var i = 0; i < rows.length; i++) {
					var row = tbody.insertRow(oldRowIndex);
					copyAttributes(rows[i], row);
					for (var j = 0; j < rows[i].cells.length; j++) {
						var cell = row.insertCell();
						copyAttributes(rows[i].cells[j], cell);
						cell.innerHTML = rows[i].cells[j].innerHTML;
					}
				}
			}
			var parent = oldRow.parentNode;
			parent.removeChild(oldRow);
			if (table.id == null) {
				assignId(table);
			}
		}
	} catch(e) {
		errorReporter.Report("DynamicUpdate.js", "replaceTableRow('" + id + "', '" + html + "')", e);
	}
}
