﻿
$(document).ready(function() {
	window.onerror = addAlert;
	document.onerror = addAlert;
});

function loadDivAlert() {
	css = document.createElement("link");
	css.setAttribute("rel", "stylesheet");
	css.setAttribute("href", gAppPath + "/elexiajax/errorhandler.css?releaseid=" + gAppReleaseID);
	document.getElementsByTagName("head")[0].appendChild(css);
	s = "<div id='divAlert'><div id='divAlertTitle'>An error has ocurred.<div align=right id='divClose' onclick='javascript:closeAlert();'>Close</div></div><div id='divAlertMessage'></div></div>";
	document.write(s);
	$('#divAlert').keypress(function() {
		if (event.keyCode == 27) closeAlert();
	});
}

function addAlert(sText, url, line, showAlert) {
	if (typeof sText == "undefined") return true;
	if (sText == "undefined") return true;
	if (sText.contains("J.readyState")) return true;
	
	if (typeof url == "undefined") url = window.location.href;
	sText += "<br>" + url;
	if (typeof line != "undefined") sText += " - line: " + line;

	re = /function\W+([\w-]+)/i;
	var func = arguments.callee;
	var s = "";
	while (func) {
		var functionName = (re.exec(func))[1];
		if (functionName != "addAlert") {
			s += functionName + "(";
			for (L = 0; L < func.arguments.length; L++) {
				if (L) s += ", ";
				s += typeof func.arguments[L];
				if (typeof func.arguments[L] != "object") {
					s += " ";
					if (typeof func.arguments[L] == "string") s += '"';
					s += String(func.arguments[L]).substring(0, 50);
					if (typeof func.arguments[L] == "string") s += '"';
				}
			}
			s += ")<br>";
		}
		func = func.arguments.callee.caller;
	}
	sText += "<br><br>" + s;

	if (elm('divAlertMessage').innerHTML != "") elm('divAlertMessage').innerHTML += String.fromCharCode(13);
	elm('divAlertMessage').innerHTML += "<pre>" + sText + "</pre>";

	elm('divAlert').style.top = document.documentElement.scrollTop + 20;
	$('#divAlert').show();
	$('#divAlert').focus();

	if (gLogJavaScriptErrors) {
		var s = "<Error>";
		s += addNode("ErrorDateTime", getCurrentDate(true));
		s += addNode("Page", url);
		s += addNode("Message", sText);
		if (gAppUserID) s += addNode("UserID", gAppUserID);
		s += addNode("Type", "JavaScript");
		s += "</Error>";
		runXMLInsert_async(s, null);
	}
	if (gEmailJavaScriptErrors) {
		s = addParam("ErrorDateTime", getCurrentDate(true));
		s += addParam("Page", url);
		s += addParam("Message", sText);
		if (gAppUserID) s += addParam("UserID", gAppUserID);
		s += addParam("Type", "JavaScript");

		var emailURL = gAppPath + "/elexiajax/aspx/processform.aspx?action=email";
		emailURL += addParam("to", "mark@elexient.com");
		emailURL += addParam("from", "natlcustom@elexient.com");
		emailURL += addParam("fromname", "Elexient Errors");
 		emailURL += addParam("title", "Javascript Error");
		$.post(emailURL, s, function(response) { return false; });
	}
	return true;
}

function closeAlert() {
	$('#divAlert').hide();
	elm('divAlertMessage').innerHTML = "";
	if (typeof appStatus == "function") appStatus("", true);
}


