/*
Program:
Husky Mounted Detection System (HMDS)

Source:
EOIR Technologies
PO Box 1240
Spotsylvania, VA  22553

HMDS Development Team
USA CECOM Night Vision and Electronic Sensors Directorate (NVESD)
10221 Burbeck Rd., STE 430
Fort Belvoir, VA 22060

Conditions for Use:
This software is Copyright(C) EOIR Technologies, Inc.

EOIR Technologies has all commercial interest and rights.
NVESD has full rights, title, and interest.

This software is provided "as is", without warranty by 
EOIR Technologies, Inc and NVESD. In no event shall EOIR 
Technologies, Inc. or NVESD be liable for any loss or for
any indirect, special, punitive, exemplary, incidental, or 
consequential damages arising from the use, possession, or
performance of this software.
*/

// Depends on Utils

var bFirst = true;

var EOIR = EOIR ? EOIR : function()
{
	var obj =
	{
	};

	return obj;

} ();


EOIR.External = function ()
{
	var obj =
	{
		get: function ()
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					if(navigator.userAgent.indexOf(EOIR.Utils.getAppName()) != -1)
					{
						//ROCBrowserGECKO
						try
						{
							//set permissions to allow XPCOM to run
							netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

							//connect to and file down the XPCOM object
							// var strClass = "@eoir.com/rocv/rocv;1";
							var strClass = "@eoir.com/ctr/ctrdefault;1";
							var rocvObj = Components.classes[strClass].getService();
							//rocvObj = rocvObj.QueryInterface(Components.interfaces.nsIROCV);
							rocvObj = rocvObj.QueryInterface(Components.interfaces.nsICTRDefault);

							EOIR.Utils.setProperty("netscapeExternalOk", rocvObj != null);

							return rocvObj;
						}
						catch(e)
						{
							alert("exception: " + e);

							EOIR.Utils.setProperty("netscapeExternalOk", false);
						}
					}
					else
					{
						//FBCB2
						try
						{
							//set permissions to allow XPCOM to run
							netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

							//connect to and file down the XPCOM object
							var strClass = "@eoir.com/ctr/ctrdefault;1"; //this will change eventually
							var rocvObj = Components.classes[strClass].getService();
							rocvObj = rocvObj.QueryInterface(Components.interfaces.nsICTRDefault); //this too (will change)

							EOIR.Utils.setProperty("netscapeExternalOk", rocvObj != null);

							return rocvObj;
						}
						catch(e)
						{
							EOIR.Utils.setProperty("netscapeExternalOk", false);
						}
					}
				}
			}
			catch(e)
			{
			}

			if(window.external)
			{
				return window.external;
			}

			return null;
		},

		getUserHomeDir: function ()
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return "file:///" + this.get().UserHomeDir();
				}
				else
				{
					return this.get().UserHomeDir;
				}
			}
			catch(e)
			{
				return null;
			}
		},

		getProduct: function ()
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return this.get().Product();
				}
				else
				{
					return this.get().Product;
				}
			}
			catch(e)
			{
				return null;
			}
		},

		getConfigurationFilename: function ()
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return "file:///" + this.get().GetConfigurationFilename();
				}
				else
				{
					return this.get().ConfigurationFilename;
				}
			}
			catch(e)
			{
				return null;
			}
		},

		getSimulationLocation: function ()
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return "file:///" + this.get().GetSimulationLocation();
				}
				else
				{
					return this.get().SimulationLocation;
				}
			}
			catch(e)
			{
				return null;
			}
		},

		getSearchFilesLocation: function ()
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return "file:///" + this.get().GetSearchFilesLocation();
				}
				else
				{
					return this.get().SearchFilesLocation;
				}
			}
			catch(e)
			{
				return null;
			}
		},

		getBCFilesLocation: function ()
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return "file:///" + this.get().GetBCFilesLocation();
				}
				else
				{
					return this.get().BCFilesLocation;
				}
			}
			catch(e)
			{
				return null;
			}
		},

		getConfusionFilename: function ()
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return "file:///" + this.get().GetConfusionFilename();
				}
				else
				{
					return this.get().ConfusionFilename;
				}
			}
			catch(e)
			{
				return null;
			}
		},

		getUserInfoFilename: function ()
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return "file:///" + this.get().GetUserInfoFilename();
				}
				else
				{
					return this.get().UserInfoFilename;
				}
			}
			catch(e)
			{
				return null;
			}
		},

		getUserContentFolder: function ()
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return "file:///" + this.get().GetUserContentFolder();
				}
				else
				{
					return this.get().UserContentFolder;
				}
			}
			catch(e)
			{
				return null;
			}
		},


		msgBox: function (strMessage, strCaption, nButtons)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return this.get().MsgBox(strMessage, strCaption, nButtons);
				}
				else
				{
					return this.get().MsgBox(strMessage, strCaption, nButtons);
				}
			}
			catch(e)
			{
				//return null;
				throw e;
			}
		},


		run: function (strPath, bWait)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return this.get().Run(strPath, bWait);
				}
				else
				{
					return this.get().Run(strPath, bWait);
				}
			}
			catch(e)
			{
				return null;
			}
		},

		runProcess: function (strPath)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return this.get().RunProcess(strPath);
				}
				else
				{
					return this.get().RunProcess(strPath);
				}
			}
			catch(e)
			{
				return null;
			}
		},

		getIsProcessRunning: function (hProcess)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return this.get().IsProcessRunning(hProcess);
				}
				else
				{
					return this.get().IsProcessRunning(hProcess);
				}
			}
			catch(e)
			{
				return null;
			}
		},

		closeProcess: function (hProcess)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return this.get().CloseProcess(hProcess);
				}
				else
				{
					return this.get().CloseProcess(hProcess);
				}
			}
			catch(e)
			{
				return null;
			}
		},

		generatePreview: function (strFile, strPreview, cx, cy)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return this.get().GeneratePreview(strFile, strPreview, cx, cy);
				}
				else
				{
					return this.get().GeneratePreview(strFile, strPreview, cx, cy);
				}
			}
			catch(e)
			{
				return null;
			}
		},


		encrypt: function (str)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return this.get().Encrypt(str);
				}
				else
				{
					return this.get().Encrypt(str);
				}
			}
			catch(e)
			{
				return str;
			}
		},

		getProgramParameter: function (strParam)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return this.get().GetConfigurationParameter(strParam);
				}
				else
				{
					return this.get().ConfigurationParameter(strParam);
				}
			}
			catch(e)
			{
				//alert("getprogramparam failed: " + e);
				throw e;
			}
		},

		setProgramParameter: function (strParam, value)
		{
			try
			{
				this.get().SetConfigurationParameter(strParam, value);
			}
			catch(e)
			{
				//alert("setprogramparam failed: " + e);
				throw e;
			}
		},

		resetIE: function ()
		{
			try
			{
				this.get().resetIE();
				return true;
			}
			catch(e)
			{
				//alert("resetie failed: " + e);
			}

			return false;
		},

		unloadUnusedDLLs: function ()
		{
			try
			{
				this.get().unloadUnusedDLLs();
			}
			catch(e)
			{
				//alert("unloadunuseddlls failed: " + e);
			}
		},

		resetWindow: function ()
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					this.get().ResetWindow();
				}
				else
				{
					this.get().ResetWindow();
				}
			}
			catch(e)
			{
				//alert("resetwindow failed: " + e);
			}
		},

		debugWrite: function (strMessage)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					this.get().DebugWrite(strMessage);
				}
				else
				{
					return this.get().DebugWrite(strMessage);
				}
			}
			catch(e)
			{
				//alert("debugwrite failed: " + e);
			}

			try
			{
				window.top.console.log(strMessage);
				return;
			}
			catch(e)
			{
			}

			try
			{
				dump(strMessage);
				return;
			}
			catch(e)
			{
			}

		},

		isDebug: function ()
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return this.get().IsDebug();
				}
				else
				{
					return this.get().IsDebug();
				}
			}
			catch(e)
			{
				//alert("isdebug failed: " + e);
				return false;
			}
		},

		minimizeWindow: function ()
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					this.get().MinimizeBrowser();
				}
				else
				{
					this.get().MinimizeBrowser();
				}
			}
			catch(e)
			{
				//alert("minimizeWindow failed: " + e);
			}
		},

		closeWindow: function ()
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					this.get().ExitBrowser();
				}
				else
				{
					this.get().ExitBrowser();
				}
			}
			catch(e)
			{
				window.close();
			}
		},

		launchDocument: function (strDoc, params)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					this.get().LaunchDocument(strDoc, params);
					return true;
				}
				else
				{
					this.get().LaunchDocument(strDoc, params);
					return true;
				}
			}
			catch(e)
			{
				//alert("launchdocument failed: " + e);
				return false;
			}
		},

		showWaitCursor: function (bShow)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					this.get().ShowWaitCursor(bShow);
				}
				else
				{
					this.get().ShowWaitCursor(bShow);
				}
			}
			catch(e)
			{
			}
		},

		saveText: function (strFilename, strText)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					this.get().SaveText(strFilename, strText);
				}
				else
				{
					this.get().SaveText(strFilename, strText);
				}
			}
			catch(e)
			{
			}
		},

		filenameFromCommonDlg: function (bOpenDialog, strFilter, strDefaultFile, strDefaultExt)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return this.get().FilenameFromCommonDlg(bOpenDialog, strFilter, strDefaultFile, strDefaultExt);
				}
				else
				{
					return this.get().FilenameFromCommonDlg(bOpenDialog, strFilter, strDefaultFile, strDefaultExt);
				}
			}
			catch(e)
			{
				throw e;
			}
		},

		invertImageNS: function (img)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					//get the new filename
					var input_filename = img.src;
					var output_filename = new Object();
					this.get().FlipImage(input_filename, output_filename);

					this.debugWrite("FlipImage " + input_filename + "  " + output_filename.value);

					//create the replacement
					img.src = output_filename.value;
				}
			}
			catch(e)
			{
				//alert("invertimagens failed: " + e);
			}
		},

		getTargetListXML: function ()
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					if(navigator.userAgent.indexOf(EOIR.Utils.getAppName()) != -1)
					{
						//ROCBrowserGECKO
						netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

						var output_xml = this.get().GetTargetListXMLString();

						var parser = new DOMParser();
						return parser.parseFromString(output_xml, "text/xml");
					}
					else
					{
						//FBCB2

						//set permissions to allow XPCOM to run
						netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
						var output_xml = new Object();
						this.get().GetTargetXML(output_xml);

						var parser = new DOMParser();
						return parser.parseFromString(output_xml.value, "text/xml");
					}
				}
				else
				{
					return this.get().TargetListXML;
				}
			}
			catch(e)
			{
				//alert("gettargetlistxml failed: " + e);
			}

			return null;
		},

		getTargetXML: function (strShortName)
		{
			// NOT IMPLEMENTED!
			try
			{
				return this.get().TargetXML(strShortName);
			}
			catch(e)
			{
				//alert("gettargetxml failed: " + e);
			};

			return null;
		},

		setInDragArea: function (bDrag)
		{
			return this.get().InDragArea = bDrag;
		},

		downloadUrl: function (strUrl, strFile)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					this.get().DebugWrite("DownloadUrl " + strUrl + "  " + strFile);
					this.get().DownloadUrl(strUrl, strFile);
				}
				else
				{
					this.get().DownloadUrl(strUrl, strFile);
				}
				return true;
			}
			catch(e)
			{
				//alert("downloadurl failed: " + e);
				return false;
			}
		},

		cacheUrl: function (strUrl)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return this.get().CacheUrl(strUrl);
				}
				else
				{
					return this.get().CacheUrl(strUrl);
				}
			}
			catch(e)
			{
				//alert("cacheurl failed: " + e);
				return false;
			}
		},

		resolvePath: function (strPath)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return this.get().ResolvePath(strPath);
				}
				else
				{
					return this.get().ResolvePath(strPath);
				}
			}
			catch(e)
			{
				//alert("resolvepath failed: " + e);
				return false;
			}
		},

		makeSureDirExists: function (strDir)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return this.get().MakeSureDirExists(strDir);
				}
				else
				{
					return this.get().MakeSureDirExists(strDir);
				}
			}
			catch(e)
			{
				//alert("makeSureDirExists failed: " + e);
				return false;
			}
		},

		showHelp: function (id)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return this.get().ShowHelp(id);
				}
				else
				{
					return this.get().ShowHelp(id);
				}
			}
			catch(e)
			{
				//alert("showHelp error: " + e.description);
				return false;
			}
		},

		strCmpLogical: function (a, b)
		{
			try
			{
				if(EOIR.Utils.canEnableNetscapePrivileges())
				{
					//set permissions to allow XPCOM to run
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

					return this.get().LogicalStrCmp(a, b);
				}
				else
				{
					return this.get().LogicalStrCmp(a, b);
				}
			}
			catch(e)
			{
				//alert("strcmplogical error: " + e);
				throw e;
			}
		}

	};

	return obj;
} ();

