﻿// Translate:NetLanguage
/********************************************************************
* File: Applets.js
*
* Author:
*   Matrikon Consulting Inc.
* Suite 1800
* 10405 Jasper Avenue
* Edmonton, Alberta, Canada
* T5J 3N4
* Ph: (780) 448-1010
* Fax: (780) 448-9191
*   E-Mail: support@matrikon.com
*
* Date created:
*     April 11, 2006
*
* Copyright notice:
*     Any use of this software without the written
*     consent of Matrikon Consulting Inc. is
*     strictly prohibited.
*********************************************************************/

//g_lAppletLoadTimeDelay_ms sets the time in milliseconds to wait after applets are activated, before checking if they are ready
//The default value of 250 ms works well for most reasonable machines and VMs.  For slower computer, raise this value (e.g. 500)
//Excessively large values will slow down the loading of any pages or dialogs requiring applet activation
var g_lAppletLoadTimeDelay_ms = 250;


function DoWaitAppletsAsync()
{
	try {
		var lArgumentsLength = arguments.length;
		var lIndex = 0;
		var sArgument;
		var aCallbackArgs;
		var lArgsIndex;
		var sCommand;
		for (lIndex = 0; lIndex < lArgumentsLength; ++lIndex) {
			sArgument = arguments[lIndex].toString();
			if (sArgument.indexOf("(") < 0) {
				if (!DoCheckAppletReadyState(sArgument)) {
					//Not ready, do the callback
					aCallbackArgs = new Array();
					for (lArgsIndex = 0; lArgsIndex < lArgumentsLength; ++lArgsIndex) {
						aCallbackArgs.push(arguments[lArgsIndex]);
					}
					sCommand = "DoWaitAppletsAsync('" + aCallbackArgs.join("', '") + "', '"+sArgument+"');";
					window.setTimeout(sCommand, g_lAppletLoadTimeDelay_ms);
					break;
				}
			} else {
				//Well, all the applets to this point are fine, and now we have the callback so just call it!!!
				try {
					eval(sArgument);
				} catch (e) {
					alert("DoWaitAppletsAsync() : Error: Callback threw an unhandled exception: " + e.description)
				}
				break;
			}
		}
	} catch (e) {
		alert("DoWaitAppletsAsync() : Error: " + e.description);
	}
	return;
}
      
function DoCheckAppletReadyState(sID)
{
	var isAppletReady = false;
	var oApplet;
	try {
		oApplet = document.getElementById(sID);
		if (oApplet != null) {
			if (oApplet.readyState == 4) {
				//If readyState is 4 it's good to go, unless it's sun
				//in which case we need a slightly deeper check of the applet
				try {
					//Added this call per sun's advice: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5073549
					//Unfortunately this call seems to alwasy return true, or will hang if the applet is not ready
					//So either way it's pointless to call isActive until they fix it properly
					//The probolem is further reported here: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4837333
					//This issue was still present on 1.5.0_10 and 1.6.0_03.
					//if (oApplet.isActive() == true) {
						isAppletReady = true;
					//}
				} catch (E) {
					//Should only fail if it's not a Java applet
					isAppletReady = true;
				}
			}
		}
	} catch (e) {
		alert("DoCheckAppletReadyState() : Error: " + e.description);
	}
	return isAppletReady;
}
      
//Takes applet names as arguments including a callback function as the last argument
function DoLoadDIVAppletSet()
{
	try {
		var lArgumentsLength = arguments.length;
		var lIndex = 0;
		var aCallbackArgs = null;
		var sArgument;
		var sCommand;
		for (lIndex = 0; lIndex < lArgumentsLength; ++lIndex) {
			sArgument = arguments[lIndex].toString();
			if (sArgument.indexOf("(") < 0) {
				DoLoadDIVAppletByID(sArgument);
				if (aCallbackArgs == null) {
					aCallbackArgs = new Array();
				}
				aCallbackArgs.push(sArgument);
			} else {
				//Found the callback.  Do not process any arguments after the callback argument.
				sCommand = "";
				if (aCallbackArgs == null) {
					sCommand = "DoWaitAppletsAsync('" + sArgument + "');"
				} else {
					sCommand = "DoWaitAppletsAsync('" + aCallbackArgs.join("', '") + "', '" + sArgument + "');"
				}
				window.setTimeout(sCommand, g_lAppletLoadTimeDelay_ms);	//Must wait a small moment before checking if applets are ready to go
			}
		}
	} catch (e) {
		alert("DoLoadDIVAppletSet() : Error:  " + e.description);
	}
	return;
}

function DoLoadDIVAppletByID(sID)
{
	try {
		var oDIVApplet = null;
		var aElements;
		oDIVApplet = document.getElementById(sID);
		if (oDIVApplet == null) {
			aElements = document.getElementsByName(sID);
			if (aElements.length > 0) {
				oDIVApplet = aElements[0];
			}
		}
		if (oDIVApplet != null) {
			DoLoadDIVApplet(oDIVApplet);
		} else {
			alert("DoLoadDIVAppletByID() : Applet definition not found: " + sID);
		}
	} catch (e) {
		alert("DoLoadDIVAppletByID() : Error: " + e.description);
	}
	return;
}

function DoLoadDIVApplet(oDIVApplet)
{
	try {
		var sDefinition;
		var sTargetType;
		if (oDIVApplet != null) {
			sTargetType = oDIVApplet.getAttribute("targetType");
			if (sTargetType == null || sTargetType == "") {
				sTargetType = "APPLET";
			}
			sDefinition = oDIVApplet.outerHTML;
			sDefinition = ReplaceAllIC(sDefinition,"<" + oDIVApplet.tagName, "<" + sTargetType);
			sDefinition = ReplaceAllIC(sDefinition,"</" + oDIVApplet.tagName, "</" + sTargetType);
			oDIVApplet.outerHTML = sDefinition;
		} else {
			alert("DoLoadDIVApplet() : Applet DIV is null");
		}
	} catch (e) {
		alert("DoLoadDIVApplet() : Error: " + e.description);
	}
	return;
}

function ReplaceAllIC( sSource, sThis, sWithThis ) {
  return (sSource+"").replace(new RegExp(sThis, "gi"), sWithThis); // g flag on RegExp specifies global replacement
}
