window.onerror = window_onerror;
var _lastErrorMessage = "";
var _lastErrorLineNumber = "";
var _lastErrorUrl = "";
var _errorCount = 0;
var _addedClientScriptBar = false;
var _clientExceptionOccured = false;

function window_onerror(clientError,url,lineNumber) {
	_clientExceptionOccured = true;
	_lastErrorMessage = clientError;
	_lastErrorUrl = url;
	_lastErrorLineNumber = lineNumber
	
	if (_errorCount == 0)
	{
		openClientErrorWindow();
	}
	showScriptErrorBar();	
	_errorCount++;
}

function openClientErrorWindow() {
	//if(window.basePageRemote && !window.basePageRemote.closed) {
	//	window.basePageRemote.AddError(escape(clientError));
	//	basePageRemote.focus();
	//} else {
		basePageRemote = window.open('?x_clientError=' + escape(_lastErrorMessage) + '&x_url=' + escape(_lastErrorUrl) + '&x_lineNumber=' + _lastErrorLineNumber,'_clientscriptErrorWindow','width=500,height=300,menubar=no,statusbar=no,scrollbars=yes');
	//}
}

function showScriptErrorBar() {
	if (!_addedClientScriptBar) {
		document.body.scrollTop = 0;
		
		try
		{
			_clientScriptErrorBar = document.createElement('div'); 
			//_clientScriptErrorBar.innerHTML = "<img src=\"?resource=smallalerticon.gif\"> There were script errors on this page that may prevent it from working properly.";
			_clientScriptErrorBar.innerHTML = "<img src=\"" + _errorAlertIcon + "\"> There were script errors on this page that may prevent it from working properly.";
			
			//_clientScriptErrorBar.setAttribute("style", "cursor:pointer;font-family:verdana;font-size:10pt;width:100%;height:25px;background:#FFFFE1;border:solid black 1px;padding:3px;");
			_clientScriptErrorBar.style.cursor = "pointer";
			_clientScriptErrorBar.style.fontSize = "10pt";
			_clientScriptErrorBar.style.fontFamily = "verdana, arial";
			_clientScriptErrorBar.style.width = "100%";
			_clientScriptErrorBar.style.height = "25px";
			_clientScriptErrorBar.style.background = "#FFFFE1";
			_clientScriptErrorBar.style.border = "solid black 1px";
			_clientScriptErrorBar.style.padding = "3px";
			_clientScriptErrorBar.id = "clientScriptErrorBarDiv";
			if(_clientScriptErrorBar.attachEvent) {
				_clientScriptErrorBar.attachEvent('onclick', openClientErrorWindow);
			} else {
				_clientScriptErrorBar.setAttribute("onclick", "openClientErrorWindow()"); 
			}
		}
		catch (e) {}
		
		document.body.insertBefore(_clientScriptErrorBar, document.body.childNodes[0]);
		//_clientScriptErrorBar = document.body.insertAdjacentHTML("afterBegin", scriptBarHtml);
		_addedClientScriptBar = true;
	}
}

/// This function is for people to test client side if there was an exception
function hasClientScriptException() {
	return _clientExceptionOccured;
}