

// ========================================================
// Handle uncaught errors
// ========================================================
// 
// Every function or method should catch the possible errors that may
// happen at its level.
// Whenever it is legitimate, the method itself may display an alert.
// However, the end user should not see directly all the mess that
// can happen in the code. Therefore, if there is no specific error
// handling for a particular method, it should call this function.
//
// Remember that javascript is procedural, if an error occurs, everything
// stops working ! It will not ignore the error and continue !
//
var __logErrors = true;
var __logDebug = true;
var __stackTraceLimit = 2;
function handleError(e)
{
	try
	{
		if( !__logErrors )
			return;
		
		if( !__logDebug )
		{
			status('error', e);
			return;
		}

		var callstack = [];
		if (e.stack) //Firefox
		{
			var lines = e.stack.split('\n');
			for (var i=0, len=lines.length; i<len; i++)
			{
				if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/))
					callstack.push(lines[i]);
			}
			//callstack.shift();
		}
		else if (window.opera && e.message) //Opera
		{
			var lines = e.message.split('\n');
			for (var i=0, len=lines.length; i<len; i++)
			{
				if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/))
				{
					var entry = lines[i];
					if (lines[i+1])
					{
						entry += " at " + lines[i+1];
						i++;
					}
					callstack.push(entry);
				}
			}
			callstack.shift();
		}
		else if( arguments )//IE and Safari
		{
			var currentFunction = arguments.callee.caller;
			while (currentFunction)
			{
				var fn = currentFunction.toString();
				var fname = fn.substring(fn.indexOf('function'), fn.indexOf(')') + 1) || 'anonymous';
				callstack.push(fname);
				currentFunction = currentFunction.caller;
			}
		}
		
		var message = "" + e;
		for( var i = 0; i < callstack.length; i++ )
		{
			if( i < __stackTraceLimit )
				message += "<br>#" + i + " " + callstack[i];
			else
			{
				message += "<br>#" + i + " ... (" + (callstack.length - __stackTraceLimit) + " more)";
				break;
			}
		}
		alert(message);
	}
	catch(e)
	{
		// Ignore errors in the last resort error handler !
		// By the way, this is the only location where it is allowed...
	}
}

var loading = null;

function load()
{
	try
	{
		if (document.getElementById('loading') == null)
		{
			loading = document.createElement('div');
			loading.id = 'loading';
			loading.className = 'loading';	
			loading.style.display = "block";
			loading.innerHTML = " Loading...";
			document.body.appendChild (loading);	
		}else
		{
			document.body.removeChild (document.getElementById('loading'));
			loading = null;
		}	
	} catch(e) { handleError(e); }
}

function chekBoxYN(id)
{
	if(!id.checked)
		id.value = 0;	
	else
		id.value = id.name;
}

