    /////////////////////////////////////////////////////////////////////////////////
   // Functions for the org.manager HTML export index page                        //
  //                                                                             //
 // (C) Copyright 2003-2008, Ingentis Softwareentwicklung. All rights reserved! //
/////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////
// classDebug - declaration
function classDebug()
{
	this.Start = Debug_Start;
	this.End = Debug_End;
	this.LogIt = Debug_LogIt;
	this.GetLogic = Debug_GetLogic;
	this.WriteCache = Debug_WriteCache;
	this.IsStarted = Debug_IsStarted;
	this.m_iMaxQueueSize	= 200;

	this.wndDebug = null;
	this.arrMessageQueue = new Array();
}

/////////////////////////////////////////////////
// classDebug - implementation
function Debug_GetLogic()
{
	var frameLogic = null;

	if ( self.orglogic )
		frameLogic = self.orglogic;
	else if ( self.orgframes && self.orgframes.orglogic )
		frameLogic = self.orgframes.orglogic;

	if ( frameLogic )
	{
		if ( frameLogic.bLogicLoaded && (frameLogic.bLogicLoaded == true) )
		{
			if ( frameLogic.bDataLoaded && (frameLogic.bDataLoaded == true) )
			{
				return frameLogic;
			}
		}
	}

	return null;
}

function Debug_Start(bReopen)
{
	var logic = this.GetLogic();

	if ( !logic )
		return false;
	if ( logic.theBrowser )
	{
		if ( (logic.theBrowser.sName == "Opera") && (logic.theBrowser.iVersion < 720) )
		{
			alert("The org.manager debug window isn't supported\nfor Opera version 7.11 and smaller!");
			return false;
		}
	}

	if ( !this.wndDebug || bReopen )
	{
		this.End();

		this.wndDebug = window.open(logic.globPath+"debug.html","dlgDebug","width=700, height=400, dependent=yes, titlebar=no, location=no, menubar=no, toolbar=no, resizable=yes, scrollbars=yes");

		var dateNow = new Date()
		var iYear = dateNow.getYear();
		if ( iYear < 999) iYear += 1900;
		var iMonth = dateNow.getMonth();
		iMonth++;

		this.arrMessageQueue.unshift("");
		this.arrMessageQueue.unshift("Used Browser: " + logic.theBrowser.sFullName);
		this.arrMessageQueue.unshift("Local time: " + dateNow.toLocaleString());
		this.arrMessageQueue.unshift("Initialize org.manager debug");
	}
	return true;
}

function Debug_End()
{
	if ( this.wndDebug )
	{
		this.wndDebug.close();
		this.wndDebug = null;
	}
}

function Debug_IsStarted()
{
	if ( this.wndDebug && this.wndDebug.bDebugLoaded && (this.wndDebug.bDebugLoaded == true) )
		return true;

	return false;
}

function Debug_LogIt(sLog)
{
	if ( this.wndDebug && !this.wndDebug.closed && this.wndDebug.LogIt )
	{
		this.WriteCache();

		this.wndDebug.LogIt(sLog);
	}
	else
	{
		if ( this.arrMessageQueue.length < this.m_iMaxQueueSize )	// put at least 200 log messages in the queue
		{
			this.arrMessageQueue[this.arrMessageQueue.length] = sLog;
		}
		else if ( this.arrMessageQueue.length == this.m_iMaxQueueSize )
		{
			this.arrMessageQueue[this.arrMessageQueue.length] = "Could not add log message, because message queue is full ("+this.m_iMaxQueueSize+" messages)";
		}
	}
}

function Debug_WriteCache()
{
	for ( var i = 0; i < this.arrMessageQueue.length; i++ )
		this.wndDebug.LogIt(this.arrMessageQueue[i]);
	this.arrMessageQueue.length = 0;
}

/////////////////////////////////////////////////
// classErrorInformation - declaration
function classErrorInformation(sMessage, sFile, iLine, dateTime)
{
	this.sMessage = sMessage;
	this.sFile = sFile;
	this.iLine = iLine;
	this.dateTime = dateTime;

	this.GetTime = ErrorInformation_GetTime;
	this.GetDateTime = ErrorInformation_GetDateTime
}

/////////////////////////////////////////////////
// classErrorInformation - implementation
function ErrorInformation_GetTime()
{
	var sTime = "";
	if ( this.dateTime.getHours() < 10 )
		sTime += "0";
	sTime += this.dateTime.getHours();

	sTime += ":";

	if ( this.dateTime.getMinutes() < 10 )
		sTime += "0";
	sTime += this.dateTime.getMinutes();

	sTime += ":";

	if ( this.dateTime.getSeconds() < 10 )
		sTime += "0";
	sTime += this.dateTime.getSeconds();

	return sTime;
}

function ErrorInformation_GetDateTime()
{
	return this.dateTime.toGMTString();
}

/////////////////////////////////////////////////
// classError - declaration
function classError()
{
	this.NewError = Error_NewError;
	this.GetError = Error_GetError;
	this.GetCount = Error_GetCount;

	this.arrErrors = new Array();
}

/////////////////////////////////////////////////
// classError - implementation
function Error_NewError(sMessage, sFile, iLine)
{
	this.arrErrors[this.GetCount()] = new classErrorInformation(sMessage, sFile, iLine, new Date());

	return this.GetCount()-1;
}

function Error_GetError(iIndex)
{
	if ( (iIndex < 0) || (iIndex >= this.GetCount()) )
		return null;

	return this.arrErrors[iIndex];
}

function Error_GetCount()
{
	return this.arrErrors.length;
}

/////////////////////////////////////////////////
// global properties
var sStartParam = "";
var sStartField = "ID";
var bParamDebug = false;
var sParamLanguage = "";
var sParamOpenSearch = "";
var bLoadEmpTable = false;
var sParamTree = "";
var sParamDetails = "";
var sParamChart = "";
var sParamList = "";
var sParamSearch = "";
var sParamNavigable = "";
var sParamExpandTree = "";
var sParamProfile = "";
var sParamLayer = "";
var globDebug = new classDebug();
var globError = new classError();
var orgErrorDlg = null;

function parseURI()
{
	var sParam = location.search;

	if ( sParam.length == 0 )
		return;

	if ( sParam.charAt(0) == "?" )
		sParam = sParam.substr(1,sParam.length-1);

	var arrParams = new Array();
	var arrValues = new Array();

	var arrParams = sParam.split("&");

	for ( i = 0; i < arrParams.length; i++ )
	{
		arrValues = arrParams[i].split("=");

		if ( arrValues.length == 2 )
		{
			var sParam = arrValues[0].toUpperCase();
			var sValue = arrValues[1];

			if ( sParam == "DEBUG" )
			{
				if ( sValue == "1" )
				{
					bParamDebug = true;
					globDebug.Start(true);
				}
			}
			if ( sParam == "TREE" )
			{
				if ( sValue.length > 0 )
					sParamTree = sValue;
			}
			if ( sParam == "DETAILS" )
			{
				if ( sValue.length > 0 )
					sParamDetails = sValue;
			}
			if ( sParam == "CHART" )
			{
				if ( sValue.length > 0 )
					sParamChart = sValue;
			}
			if ( sParam == "LIST" )
			{
				if ( sValue.length > 0 )
					sParamList = sValue;
			}
			if ( sParam == "SEARCH" )
			{
				if ( sValue.length > 0 )
					sParamSearch = sValue;
			}
			if ( sParam == "OPENSEARCH" )
			{
				if ( sValue.length > 0 )
					sParamOpenSearch = sValue;
			}
			if ( sParam == "NAVIG" )
			{
				if ( sValue.length > 0 )
					sParamNavigable = sValue;
			}
			if ( sParam == "LANG" )
			{
				if ( sValue.length > 0 )
					sParamLanguage = sValue;
			}
			if ( sParam == "EXPANDTREE" )
			{
				if ( sValue.length > 0 )
					sParamExpandTree = sValue;
			}
			if ( sParam == "PROFILE" )
			{
				if ( sValue.length > 0 )
					sParamProfile = sValue;
			}
			if ( sParam == "LAYER" )
			{
				if ( sValue.length > 0 )
					sParamLayer = sValue;
			}
			if ( (sParam == "ID") || (sParam == "TREENAME") || (sParam == "ENTRY") || (sParam == "ORG") || (sParam == "EMP") || (sParam == "NETWORK") )
			{
				if ( sParam == "EMP" )
					bLoadEmpTable = true;

				sStartField = sParam;
				sStartParam = sValue;
			}
		}
	}
	if ( (sStartField != "ID") && (sStartField != "TREENAME") && (sStartField != "ENTRY") && (sStartField != "ORG") && (sStartField != "EMP") && (sStartField != "NETWORK") )
		sStartParam = "";
}

function HandleError(sMessage, sFile, iLine)
{
	sErrorMessage = sMessage;
	sErrorFile = sFile;
	iErrorLine = iLine;

	ReportError();

	return true;
}

function ReportError()
{
	var bIsOpen = false;

	if ( orgErrorDlg )
	{
		if ( !orgErrorDlg.closed )
		{
			bIsOpen = true;
		}
	}

	if ( bIsOpen )
	{
		orgErrorDlg.newError();
		orgErrorDlg.focus();
	}
	else
	{
		orgErrorDlg = window.open("error.html","orgerror","width=420, height=260, dependent=yes, titlebar=no, location=no, menubar=no, toolbar=no, resizable=yes, status=no");
	}
}

window.onerror = HandleError;

parseURI();