//
//	Copyright (c) 2001 General Electric Company
//
//	Version control:
//		$Archive: /releases/FreeViewer_2.0/src/WebViewer/FreeViewer/HTML/app.js $
//		$Revision: 22 $
//		$Date: 3/10/03 8:47a $
//		$Author: Marceld $
//
//	Helper comment to enable syntax coloring in InterDev: <script>


// Configuration variabled not exported to config.txt
var APP_NAME      = "Centricity DICOM Viewer";
var APP_VERSION   = "2.1";
var APP_COPYRIGHT = "Copyright (c) 2001-2003 General Electric Company";


var p_LicenseKey = null;

var localedb = null;
var config = null;
var _strErrorMsg = null;

var SYSERROR_UNABLE_TO_OPEN_LOCALE_DATABASE   = '-3';
var SYSERROR_INVALID_LOCALE                   = '-4';
var SYSERROR_INCORRECT_BROWSER                = '-5';
var SYSERROR_BROWSER_UNTESTED                 = '-6';
var SYSERROR_EXISTING_WEBVIEWER               = '-7';

var mpSysErrors = new Object();
mpSysErrors[SYSERROR_UNABLE_TO_OPEN_LOCALE_DATABASE]   = 'Unable to open the locale database.';
mpSysErrors[SYSERROR_INVALID_LOCALE]                   = 'Locale ID not supported.';
mpSysErrors[SYSERROR_INCORRECT_BROWSER]                = 'Centricity Web requires Internet Explorer 5.5 Service Pack 2 or Internet Explorer 6.0.';
mpSysErrors[SYSERROR_BROWSER_UNTESTED]                 = 'Centricity Web has been tested with Internet Explorer 5.5 Service Pack 2 and Internet Explorer 6.0.\nCentricity Web is not guaranteed to work with the currently installed version.';
mpSysErrors[SYSERROR_EXISTING_WEBVIEWER]               = 'Cannot start Centricity Web twice in one Internet Explorer.';


//
// onload
//
// Purpose: Initialise the 'app'.
//
function window.onload()
{
	BrandTitle();
	loadframe.OnLoadComplete();

	// Check if the appframe could be loaded.
	if (HasErrors())
	{
		loadframe.SetErrorText(_strErrorMsg);
		return;
	}

	// Check if the dicomdirframe could be loaded.
	if (dicomdirframe.HasErrors())
	{
		loadframe.SetErrorText(dicomdirframe.GetErrorText());
		return;
	}

	// Check if the viewframe could be loaded.
	if (viewframe.HasErrors())
	{
		loadframe.SetErrorText(viewframe.GetErrorText());
		return;
	}
	if (viewframe.IsStaticPaletteMonitor())
	{
		loadframe.SetErrorText('This monitor configuration is not supported');
		return;
	}

	// Connect the dicomdirframe to the viewframe component.
	var IDicomDirView = dicomdirframe;
	viewframe.AttachDicomDirView(IDicomDirView);

	var licensekey = ShowLicenseDialog(new LicenseKey());
	if (licensekey != null)
	{
		viewframe.SetLicenseKey(licensekey);
		
		// Hide the 'loadframe' and show the 'application' frames.
		framesetAll.rows = '0, *';

		// Open the dicomdir at the root if opened with the file protocol.
		if (location.protocol == "file:")
		{
			var strRootDir = location.pathname.substr(1, 3);
		
			IDicomDirView.SetDicomDirPath(strRootDir);
		}
	}
	TranslateUIElements(dicomdirframe.document, "BUTTON");			// translate buttons in dicomdir frame
	TranslateUIElements(viewframe.viewcontrol.document, "BUTTON");	// translate buttons in viewcontrol frame
	
	viewframe.viewcontrol.labelAnnotations = GetLocaleString('labelAnnotations.caption');
	viewframe.viewcontrol.spanSpeed = GetLocaleString('spanSpeed.caption');

	viewframe.CineSpeedChange(viewframe.GetCineSpeed());

	focus();
}


//
// TranslateUIElements
//
function TranslateUIElements(objectContainer, strTag)
{ 
	ASSERT(strTag != 'INPUT'); // cannot update with innerText!
	var rgElem = objectContainer.all.tags(strTag);

	for (var i = 0; i < rgElem.length; i++)
	{
		var elem = rgElem[i];

		if (elem.id != null && elem.id != "")
		{
			var obj = FindString(elem.id + '.caption');
			if (obj.bFound)
			{
				SetElemText(elem, obj.strText);
			}
			elem.title = GetLocaleString(elem.id + '.title');
		}
	}
}

//
// BrandTitle
//
// Purpose: This function brands the title displayed in the caption bar with the text 
//          provided in the configuration file.
//
function BrandTitle()
{
	var strProvidedByTitle = config.ProvidedByTitle();
	if (strProvidedByTitle.length > 0)
	{
		document.title = APP_NAME + " - " + strProvidedByTitle;
	}
	else
	{
		document.title = APP_NAME;
	}
}


//
// ShowAboutDialog
//
// Purpose: Shows the modal dialog with version, copyright info and a link
//          to the Applicare website.
//
function ShowAboutDialog()
{
	showModalDialog("aboutdlg.htm", window,
	                "dialogHeight:300px; help:no; edge:raised; status:no; unadorned:yes" );
}


//
// oncontextmenu
//
// Purpose: Mark oncontextmenu events as handled to prevent that IE
//          displays its default menu.
//
function document.oncontextmenu()
{
	return false;
}


//
// ShowHelp
//
function ShowHelp()
{
	showModalDialog("help.htm", window, 
	                "help:no; edge:sunken; status:no; resizable:yes");
	return false;
}


//
// GetISettings()
//
function GetISettings()
{
	return window;
}


//
// GetConfiguration
//
function GetConfigurationItem(strKey)
{
	return window[strKey];
}


//
// GetApp
//
function GetApp()
{
  return self;
}


//
// ShowMessageID
// 
function ShowMessageID(strMsgId)
{
	//ShowMessageStr(mpSysErrors[strMsgId]);
	alert(mpSysErrors[strMsgId]);
}


//
// HasErrors
//
function HasErrors()
{
	try
	{
		return (typeof(localedb) != 'object');
	}
	catch (e)
	{
		return true;
	}
}


//
// LoadConfigurationFile
//
function LoadConfigurationFile()
{
	if (config == null)
	{
		config = new ConfigFile();
	}
}


//
// SetLocaleDB
//
function CreateLocaleDB(strLocale)
{
	if (localedb == null)
	{
		localedb = new CLocaleDb('lang');
	}

	if (!localedb.SetLocaleId(strLocale, '_') || localedb.GetXMLDb() == null)
	{
		_strErrorMsg = mpSysErrors[SYSERROR_UNABLE_TO_OPEN_LOCALE_DATABASE];
		return false;
	}

	if (!localedb.IsValidLocale())
	{
		_strErrorMsg = mpSysErrors[SYSERROR_INVALID_LOCALE];
		return false;
	}
	SetLocaleDB(localedb)

	return true;
}

LoadConfigurationFile();
CreateLocaleDB(config.LocaleId());
