function Log(type)
{
    this.IsTeleBetUser = top.global.isTeleBetUser;
    this.type = type;
    this.functionEntries = new Object();
	
	var log = "";
	var indent = "";
	var maxLength = 3000;

	this.FunctionEntry = function(name)
	{
	    if(this.IsTeleBetUser || this.type == 'logger')
	    {
	        var stringToLog = name + "(";
	        
	        for (var i = 1; i < arguments.length; i++)
	        {
	            stringToLog += arguments[i];
	            if (i != arguments.length - 1) stringToLog += ", ";
	        }
	        
	        stringToLog += ")" + " - ENTRY ";
	        
	        if (this.type == 'logger')
	        {
	            if (this.IsClientLoggingEnabled(name))
	            	this.functionEntries[name] = stringToLog;
	        }
	        else this.Log(stringToLog);
		}
	};
	
	this.IsClientLoggingEnabled = function(name)
	{
	    if (top.getTopWindow().loggingConfiguration.ClientLogging.Enabled == true)
	    {
	        for (var i = 0; i < top.getTopWindow().loggingConfiguration.ClientLogging.LoggingItems.length; i++)
	        {
	            var currentItem = top.getTopWindow().loggingConfiguration.ClientLogging.LoggingItems[i];
	            if (currentItem.Name == name)
	            {
	                return currentItem.Enabled;
	            }
	        }
	    }
	    return false;
	};

	this.FunctionExit = function(name, returnValue)
	{
	    if(this.IsTeleBetUser || this.type == 'logger')
	    {
		    if (indent.length >= 2)
			    indent = indent.substring(2);
    		
    		stringToLog = '';
    		
		    if (typeof(returnValue) != "undefined")
			    stringToLog = name + "()" + " - " + returnValue + " - EXIT";
		    else stringToLog = name + "()" + " - EXIT";
			
			if (this.type == 'logger')
	        {
	            if (this.IsClientLoggingEnabled(name))
	                this.functionEntries[name] = null;
	        }
	        else this.Log(stringToLog);
		}
	};

	this.Log = function(text)
	{	
	    if(this.IsTeleBetUser || this.type == 'logger')
	    {
		    var theTime = new Date();
		    var d  = theTime.getDate();
		    var day = (d < 10) ? '0' + d : d;
		    var m = theTime.getMonth() + 1;
		    var month = (m < 10) ? '0' + m : m;
		    var yy = theTime.getYear();
		    var year = (yy < 1000) ? yy + 1900 : yy;

		    var theTimeString = day + "-" + month + "-" + year + " " + theTime.getHours() + ":" + theTime.getMinutes() + ":" + theTime.getSeconds() + "." + theTime.getMilliseconds();
		    log += theTimeString + "  " + indent + " " + text + "\n";
    		
		    if (log.length > maxLength)
		    {
			    log = log.substring(log.length-maxLength, log.length);
		    }		
		}
	};

	this.Display = function()
	{		
	    if(this.IsTeleBetUser)
		    alert(log);	
	};
	
	this.LogToServer = function(name, clearLogAfter, addAdditionalInformation)
	{
	    if (typeof(addAdditionalInformation) == "undefined" || addAdditionalInformation == null)
	        addAdditionalInformation = true; /*default value is true -> log additional information*/
	        
	    if (this.IsClientLoggingEnabled(name))
	    {
	        var request = getNewXMLHttpRequest();
	        log += 'Func entry: ';
	        for (var i in this.functionEntries)
	        {
	            if (this.functionEntries[i] != null)
	                log += this.functionEntries[i];
	        }
	        var sentData = log;
	        if (addAdditionalInformation == true)
	        {
	            var topWindow = top.getTopWindow();
                sentData += ", telebet = " + ((typeof(topWindow.GBE_header.isTeleBetLoggedIn) != "undefined" && topWindow.GBE_header.isTeleBetLoggedIn == true) ? "true" : "false");
		        sentData += ", punter handle= " + topWindow.global.punterId;
		        sentData += ", preferences = " + topWindow.prefs.GetPrefsString();
		        
	        }
	        sentData = escape(sentData);
	        sentData = 'log=' + sentData;
            request.open('POST', GBE_Web_UI_Util_Root + '/Common/ClientLog/ClientLogger.aspx?currentPreferences=' + top.GetPreferencesURLPostfix(), false);
            request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            request.setRequestHeader("Content-length", sentData.length);
            request.setRequestHeader("Connection", "close");
	        request.send(sentData);
	    }
	    
	    if (clearLogAfter == true) this.ClearLog();
	};
	

	
	this.ClearLog = function()
	{
	    log = "";
	};
	
	this.CreateErrorString = function(e, name, str)
	{
	    if (this.IsClientLoggingEnabled(name))
	    {
	        var error = "Client Logger : "+ str;
	        for (var i in e)
            {
                error += " " + i + ": " + e[i];
            }
            log += error;
        }
	};
}
/*used for telebet users*/
var theLog = new Log('telebet');

/*error catching on client*/
var clientLog = new Log('logger');

