
//log variables
var LOGCT_COMMON = "Common";
var LOGCT_RENDERING = "Rendering";
var LOGCT_XMLHTTP = "XmlHTTP";
var LOGCT_NAVIGATION = "Navigation";

//************************************* Log object *************************************************
function lc_Log()
{
	this.TimeLog = null;
	
	this.LogLinesInfo = null;
	this.LogLinesWarning = null;
	this.LogLinesError = null;
	this.LogLinesCritical = null;
	
	this.MaxLinesInfo = 10;
	this.MaxLinesWarning = 10;
	this.MaxLinesError = 1;
	this.MaxLinesCritical = 1;
	
	this.CurrentLinesInfo = 0;
	this.CurrentLinesWarning = 0;
	this.CurrentLinesError = 0;
	this.CurrentLinesCritical = 0;
	this.Enabled = true;
	
}
lc_Log.prototype.StartTimeLog= function(Caption)
{
	var strDate = new Date();
	this.EnsureLogDestination();
	
	var element  = Array();
	element.push(strDate);
	element.push(Caption);
	this.TimeLog.push(element);

}
lc_Log.prototype.EndTimeLog = function()
{
	var endDate = new Date();
	if(this.TimeLog==null)
		return;
	
	if(this.TimeLog.length==0) return;
	
	var element = this.TimeLog[this.TimeLog.length-1];
	var sec =  endDate - element[0];
	this.Info(element[1]+"\t["+sec+" : milliseconds]");
	this.TimeLog.splice(this.TimeLog.length-1, this.TimeLog.length-1);
}

lc_Log.prototype.EnsureLogDestination = function()
{
	if(this.LogLinesInfo == null)		this.LogLinesInfo		= new Array();
	if(this.LogLinesWarning == null)	this.LogLinesWarning	= new Array();
	if(this.LogLinesError == null)		this.LogLinesError		= new Array();
	if(this.LogLinesCritical == null)	this.LogLinesCritical	= new Array();
	if(this.TimeLog == null )			this.TimeLog			= new Array();
}

lc_Log.prototype.LogMessage = function(category,message,type)
{
	if (this.Enabled)
	{
		this.EnsureLogDestination();
		var str = "";
		var strM="";
		var strDate = new Date().toLocaleTimeString();
		if(message==null) strM="_NO_MESSAGE_";
		str = strDate + "^" + category + "^" + message;
		switch(type)
		{
			case 0: 
				this.LogLinesCritical.push(str);
				this.IncrementCritical();
				break
			case 1: 
				this.LogLinesError.push(str);
				this.IncrementError();
				break
			case 2: 
				this.LogLinesWarning.push(str);
				this.IncrementWarning();
				break
			case 3: 
				this.LogLinesInfo.push(str);
				this.IncrementInfo();
				break
		}
	}
}

lc_Log.prototype.Info = function(message)
{
	this.LogMessage("info",message,3);
}
lc_Log.prototype.Warning = function(message)
{
	this.LogMessage("Warning",message,2);
}
lc_Log.prototype.Error = function(message)
{
	this.LogMessage("Error",message,1);
}

lc_Log.prototype.Critical = function(message)
{
	this.LogMessage("Critical",message,0);
}

lc_Log.prototype.IncrementInfo = function()
{
	if(this.CurrentLinesInfo>=this.MaxLinesInfo)
	{
		this.CurrentLinesInfo = 0;
		this.SendToServer(this.LogLinesInfo,3);
	}
	this.CurrentLinesInfo += 1;
}

lc_Log.prototype.IncrementWarning = function()
{
	if(this.CurrentLinesWarning>=this.MaxLinesWarning)
	{
		this.CurrentLinesWarning = 0;
		this.SendToServer(this.LogLinesWarning,2);
	}
	this.CurrentLinesWarning += 1;
}

lc_Log.prototype.IncrementError = function()
{
	if(this.CurrentLinesError>=this.MaxLinesError)
	{
		this.CurrentLinesError = 0;
		this.SendToServer(this.LogLinesError,1);
	}
	this.CurrentLinesError += 1;
}

lc_Log.prototype.IncrementCritical = function()
{
	if(this.CurrentLinesCritical>=this.MaxLinesCritical)
	{
		this.CurrentLinesCritical = 0;
		this.SendToServer(this.LogLinesCritical,0);
	}
	this.CurrentLinesCritical += 1;
}
lc_Log.prototype.SendToServer = function(obj,level)
{
	this.Enabled = false;
	new httpcmd_SendLog(obj,level);
	this.Enabled = true;
}

//************************************* Log object *************************************************

/// Logging utilities

function log_Logger(LType,CurrentLevel)
{
	this.TimeLog = null;
	this.KeysTimeLog = null;
	this.logtable = null;
	this.logwnd = null;
	this.enabled = false;
	this.typeLog = 0;
	this.ActiveCategory = "Show All";
	this.logLevel=(CurrentLevel == null)?0:CurrentLevel;
	
	if (LType == null){
		this.logtable = null;
		return;
	}
	else 
		this.typeLog = LType;
		
	this.enabled = (this.typeLog != 0);	
}

log_Logger.prototype.StartTime = function(message,category,logLevel){
	if (!this.enabled || !this.EnabledLevel(logLevel))return;
	try {
		this.EnsureLogDestination();
		this.AddInTimeArray(new Date(),message,category,null);
	}
	catch(e){};
} 

log_Logger.prototype.StartTimeByKey = function(Key,message,category,logLevel){
	if (!this.enabled || !this.EnabledLevel(logLevel))return;
	try {
		this.EnsureLogDestination();
		this.AddInTimeArray(new Date(),message,category,Key);
	}
	catch(e){};
}

log_Logger.prototype.AddInTimeArray = function(strDate,message,category,Key){
	var element = Array();
	element.push(strDate);
	element.push(message);
	element.push(category);
	this.LogMessage("Time",element[1]+"\t[Started]",element[2]);
	if (Key==null) 
		this.TimeLog.push(element);
	else 
		this.KeysTimeLog[Key]=element;
}

log_Logger.prototype.GetFromTimeArray = function(Key){
	return(Key==null)?this.TimeLog[this.TimeLog.length-1]:this.KeysTimeLog[Key];
}

log_Logger.prototype.DeleteLastFromTimeArray = function(Key){
	if (Key==null)
		this.TimeLog.splice(this.TimeLog.length-1, this.TimeLog.length-1);
	else 
		delete this.KeysTimeLog[Key];
} 

log_Logger.prototype.EndTime = function(logLevel){
	if (!this.enabled || !this.EnabledLevel(logLevel) || this.TimeLog==null || this.TimeLog.length==0)return;
	var endDate = new Date(), element = this.GetFromTimeArray(null);
	this.InsertTimeText(endDate,element);
}

log_Logger.prototype.EnabledLevel = function(logLevel){
	if (logLevel==null) logLevel = 0;
	return !(this.logLevel < logLevel);
}

log_Logger.prototype.EndTimeByKey = function(Key,logLevel){
	if (!this.enabled || !this.EnabledLevel(logLevel) || this.KeysTimeLog==null)return;
	var endDate = new Date(), element = this.GetFromTimeArray(Key);
	this.InsertTimeText(endDate,element);
}

log_Logger.prototype.InsertTimeText = function(endDate,element){
	var sec = endDate - element[0];
	this.LogMessage("Time",[element[1],"\t[Completed. Time = ",sec," : ms]"].join(''),element[2]);
	this.DeleteLastFromTimeArray();
}

log_Logger.prototype.Info = function(message,category,logLevel){
	this.LogMessage("Info", message, category,logLevel);
} 

log_Logger.prototype.ClearLogTable = function(){
	if(this.logtable!=null)this.logtable.removeChild(this.logtable.firstChild);
}

log_Logger.prototype.LogMessage = function(type,message,category,logLevel){
	if (this.enabled!=true || !this.EnabledLevel(logLevel))return;
	try {
		this.EnsureLogDestination();
		var row = this.logtable.insertRow();
		if(this.ActiveCategory != category && this.ActiveCategory != "Show All") row.style.display="none";
		switch(type){
			case "Info": 
				row.style.backgroundColor = "FFFFE1";
				break;
			case "Time": 
				row.style.backgroundColor = "ECF5FF";
				break;
		}

		var dateCell = row.insertCell();
			dateCell.style.width="90px";
			dateCell.noWrap = -1;
			dateCell.innerText = ToTimeString(new Date());

		var categoryCell = row.insertCell();
			categoryCell.style.width="50px";
			categoryCell.noWrap = -1;
			categoryCell.innerText = type;

		var categoryCell = row.insertCell();
			categoryCell.style.width="70px";
			categoryCell.noWrap = -1;
			categoryCell.innerText = (category == null ? LOGCT_COMMON : category);
			
			this.InsertCategory((category == null ? LOGCT_COMMON : category));
			
		var messageCell = row.insertCell();
			messageCell.style.width="100%";
			messageCell.innerText = message;
		//row.scrollIntoView();
	}
	catch(e){}; // if error required navigation.js
}

log_Logger.prototype.ShowCategory = function(category){
	this.ActiveCategory = this.categorySelect[this.categorySelect.selectedIndex].text;
	for(var i=0,iLen=this.logtable.rows.length;i<iLen;++i){
		row = this.logtable.rows[i];
		if (this.ActiveCategory != "Show All")
			row.style.display=(row.cells[2].innerText!=this.ActiveCategory)?"none":"block";
		else 
			row.style.display="block";
	}
}

log_Logger.prototype.InsertCategory = function(category){
	if (category==null)return;
	var len = this.categorySelect.options.length;
	for(var i=1; i<len;++i)
		if (this.categorySelect.options[i].text == category) return;
	var oOption=(this.logwnd == null)?document.createElement("OPTION"):this.logwnd.document.createElement("OPTION");
	oOption.text = category;
	oOption.value = len;
	this.categorySelect.options.add(oOption);
}

log_Logger.prototype.EnsureLogDestination = function(){
	if (!this.enabled )return;
	if (this.TimeLog == null)this.TimeLog = [];
	if (this.KeysTimeLog == null)this.KeysTimeLog = [];
	if (this.logtable != null)return;
	if (this.typeLog==2) { // inline code
		var obj = document.getElementById("divTRCont");
		if (obj != null) {
			obj.style.display="block";
			if (typeof(htr_log_show)!="undefiend") htr_log_show.style.display="";
			this.logtable = document.getElementById("LogTrac");
			this.categorySelect = document.getElementById("select_category");
		}
	}
	else if (this.typeLog==1 && typeof(LogStrPopup)!='undefined') { //popup window
		this.logwnd = window.open("about:blank", "Logger", "height=500,width=700,resizable=yes,scrollbars=yes,toolbar=no,titlebar=no,menubar=no");
		this.logwnd.document.body.style.padding = "0px";
		this.logwnd.document.body.style.margin = "0px";
		this.logwnd.document.body.style.fontFamily = "Verdana, Arial, Helvetica, sans-serif";
		this.logwnd.document.body.innerHTML = LogStrPopup;
		this.logtable = this.logwnd.document.getElementById("LogTrac");
		var obj = this.logwnd.document.getElementById("divTRCont");
		this.categorySelect = this.logwnd.document.getElementById("select_category");
		if (obj != null){
			obj.style.display="block";
			obj.style.overflow = "auto";
			obj.style.height="475px";
		}
	}
	this.logtable.style.fontSize = 9;
}

function StartTimeLog(Caption,Category,logLevel){
	if (typeof(ClientSideLogEnabled)=="undefined") return;
	var WinObj = dom_GetTopLevelWindow();
	if(PopupOrParent == true && Category != null) Category = "p:"+Category;
	if (typeof(LogC)!="undefined" && LogC!=null)
		LogC.StartTimeLog(Caption);
	else if (typeof(WinObj.Log)!="undefined") 
		WinObj.Log.StartTime(Caption,Category,logLevel);
}

function EndTimeLog(logLevel){
	if (typeof(ClientSideLogEnabled)=="undefined") return;
	var WinObj = dom_GetTopLevelWindow();
	if (typeof(LogC)!="undefined" && LogC!=null)
		LogC.EndTimeLog();
	else if (typeof(WinObj.Log)!="undefined") 
		WinObj.Log.EndTime(logLevel);
}

function StartTimeLogByKey(Key,Caption,Category,logLevel){
	if (typeof(ClientSideLogEnabled)=="undefined") return;
	var WinObj = dom_GetTopLevelWindow();
	if(PopupOrParent == true && Category != null) Category = "p:"+Category;
	if (Key==null || typeof(WinObj.Log)=="undefined") return;
	if (typeof(WinObj.Log)!="undefined") 
		WinObj.Log.StartTimeByKey(Key,Caption,Category,logLevel);
}

function EndTimeLogByKey(Key,logLevel){
	if (typeof(ClientSideLogEnabled)=="undefined") return;
	var WinObj = dom_GetTopLevelWindow();
	if (Key==null || typeof(WinObj.Log)=="undefined" ) return;
	if (typeof(WinObj.Log)!="undefined") 
		WinObj.Log.EndTimeByKey(Key,logLevel);
}

function LogInfo(message,category,logLevel){
	if (typeof(LogC)!="undefined" && LogC!=null)
		LogC.Info(message);
	else
	{
		if (typeof(ClientSideLogEnabled)=="undefined") return;
		var WinObj = dom_GetTopLevelWindow();
		if(PopupOrParent == true && category != null) category = "p:"+category;
		if (typeof(WinObj.Log)!="undefined") WinObj.Log.Info(message,category,logLevel);
	}
}

function WarningToServer(message){
	if (typeof(LogC)!="undefined" && LogC!=null)
		LogC.Warning(message);
}

function ErrorToServer(message){
	if (typeof(LogC)!="undefined" && LogC!=null)
		LogC.Error(message);
}

function CriticalErrorToServer(message){
	if (typeof(LogC)!="undefined" && LogC!=null)
		LogC.Critical(message);
}