if (typeof(visualize) == "undefined") throw new Error("errorHandler() requires visualize()");
if (typeof(stackDump) == "undefined") throw new Error("errorHandler() requires stackDump()");
window.onerror = function errorHandler(sError, sFile, sLineno) {
	// If the last node is a script tag with a src attribute, it is likely
	// to be the cause of the error, so display the src attribute's value:
	var oLastNode = document;
	while(oLastNode.childNodes.length > 0) {
		oLastNode = oLastNode.childNodes[oLastNode.childNodes.length - 1];
	}
	var sLastScript = oLastNode.tagName == "SCRIPT" && typeof(oLastNode.src) !== "undefined"?
		"(Last script: \"" + visualize(oLastNode.src) + "\")\r\n" : "";
	if (confirm(
		"JavaScript Error caught\r\n" +
		"Error: \"" + visualize(sError) + "\"\r\n" +
		"In line " + visualize(parseInt(sLineno)) + " of file \"" + visualize(sFile) + "\"\r\n" +
		sLastScript +
		"\r\n" +
		"(Press OK to ignore or CANCEL for more information)\r\n" +
		stackDump(1)
	) && confirm(
		"JavaScript Error caught\r\n" +
		"Error: \"" + visualize(sError) + "\"\r\n" +
		"In line " + visualize(parseInt(sLineno)) + " of file \"" + visualize(sFile) + "\"\r\n" +
		sLastScript +
		"\r\n" +
		"(Press OK to see ignore or CANCEL to terminate the script)\r\n" +
		stackDump(1, true)
	)) {
		return true;
	};
	return false;
}

