/*
	Error handler component - stores JavaScript errors for logging.
	
	Writes the javascript errors happening on website 
	to a cookie (errLogCookie) which lasts 7 days. 
	Server-side components then empty this cookie and write its
	contents into a log file. Thus, we are able to log
	javascript errors.	
*/

function setCookie(cookieName, cookieValue, expires, path, domain, secure) 
{
	if (!expires)
		expires = new Date(2036, 1, 1); // Far in the future

	document.cookie = 
		escape(cookieName) + '=' + escape(cookieValue) 
		+ (expires ? '; EXPIRES=' + expires.toGMTString() : '')
		+ (path ? '; PATH=' + path : '')
		+ (domain ? '; DOMAIN=' + domain : '')
		+ (secure ? '; SECURE' : '');
}

function getCookie(cookieName) 
{
	var cookieValue = null;
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
	
	if (posName != -1) 
	{
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);
		if (endPos != -1)
			cookieValue = unescape(document.cookie.substring(posValue, endPos));
		else
			cookieValue = unescape(document.cookie.substring(posValue));
	}
	
	return cookieValue;
}

function deleteCookie(name, path, domain)
{
	if (getCookie(name))
	{
		document.cookie = name + "=" +
		";expires=Thu, 01-Jan-70 00:00:01 GMT" +
		((path) ? ";path=" + path :"") +
		((domain) ? ";domain=" + domain :"");
	}
}


function onerrorHandler(desc, page, line)	
{ 
	exps = new Date;
	exps.setDate(exps.getDate() + 7); // save error information for next 7 days 
	var errLogCookie = getCookie("errLogCookie");
	var contentErr = "\n\nJavaScript error occurred!\nClient Browser: " + navigator.appName + " " + navigator.appVersion + "\nClient platfrorm: " + navigator.platform + "\nError description: \t"+desc +"\nPage address:			\t"+page +"\nLine number:			 \t"+line;
	
	if (!errLogCookie)
	{
		setCookie("errLogCookie", contentErr, exps);
	}
	else
	{
		setCookie("errLogCookie", errLogCookie + contentErr, exps);
	}
	//alert(errLogCookie+contentErr)
	return false;
}

window.onerror=onerrorHandler


