
var interval = 250;	// half second = 500
var timerID = setTimeout(Timer, 3000);
var timerFocus = setTimeout(TimerFocus, 2500);
var bandwidth = "";
var not_lowResUrl = true;
var not_reportEnviornment = true;
var getAppletValueErrorFlag = false;

function log(message) {
/*
    if (!log.window_ || log.window_.closed) {
        var win = window.open("", null, "width=400,height=200," +
                              "scrollbars=yes,resizable=yes,status=no," +
                              "location=no,menubar=no,toolbar=no");
        if (!win) { return; }
        var doc = win.document;
        doc.write("<html><head><title>Debug Log</title></head>" +
                  "<body></body></html>");
        doc.close();
        log.window_ = win;
    }
    var theMessage = message;	//paranoia
    var logLine = log.window_.document.createElement("div");
    logLine.appendChild(log.window_.document.createTextNode(theMessage));
    log.window_.document.body.appendChild(logLine);
*/
}
//How big is the screen?
var screenW = 640, screenH = 480;
if (parseInt(navigator.appVersion,10)>3) {
 screenW = screen.width;
 screenH = screen.height;
}
else if (navigator.appName == "Netscape" && parseInt(navigator.appVersion,10)==3  && navigator.javaEnabled()) 
{
 var jToolkit = java.awt.Toolkit.getDefaultToolkit();
 var jScreenSize = jToolkit.getScreenSize();
 screenW = jScreenSize.width;
 screenH = jScreenSize.height;
}

function GetEnviornment()
{
	var msg = "";
	try {
		if (navigator.appCodeName !== undefined)
			{ msg += ", appCodeName = "+navigator.appCodeName; }
		if (navigator. appName !== undefined)
			{ msg += ", appName = "+navigator.appName; }
		if (navigator. appVersion !== undefined)
			{ msg += ", appVersion = "+navigator. appVersion; }
		if (navigator.cpuClass !== undefined)
			{ msg += ", cpuClass = "+navigator.cpuClass; }
		if (navigator. platform !== undefined)
			{ msg += ", platform = "+navigator. platform; }
		if (navigator. language !== undefined)
			{ msg += ", language = "+navigator. language; }
		if (navigator. browserLanguage !== undefined)
			{ msg += ", browserLanguage = "+navigator. browserLanguage; }
		if (navigator. systemLanguage !== undefined)
			{ msg += ", systemLanguage = "+navigator. systemLanguage; }
		if (navigator.userLanguage !== undefined)
			{ msg += ", userLanguage = "+navigator.userLanguage; }
		if (navigator.userAgent !== undefined)
			{ msg += ", userAgent = "+navigator.userAgent; }
		if (document.referrer !== undefined)
			{ msg += ", referrer = "+document.referrer; }
	 } catch (e) {}
	return msg;
}
function ReportEnviornment()
{
	try {
		document.applet.FlashMessage("msg", "AINote," + GetEnviornment());
		not_reportEnviornment = false;
	} catch(e)
	{ }
}

function getAppletValue(index)
{
	try {
		if (document !== null && document.applet !== null)
		{
			/*
			if (! getAppletValueErrorFlag)
			{
				getAppletValueErrorFlag = true;
				try {
					var version = java.lang.System.getProperty("java.version");
					if (version < "1.5")
					{
						if (confirm("You will need install a newer version than "+version+" of Java to run this. Would you like to get it now?"))
							{ location.href = "http://java.com/en/download/index.jsp"; }	//load java
					}
				} catch(e)
				{
					//if (confirm("You will need install Java or a newer version of Java to run this. Would you like to get it now?"))
					//	{ location.href = "http://java.com/en/download/index.jsp"; }	//load java
				}
			}
			*/
			var str = document.applet.GetValue(index);
			if (str === null || str === undefined || str == "undefined")
				{ return null; }
			else
				{ return String(str); }
			//return str;
		}
		else if (! getAppletValueErrorFlag)
		{
				getAppletValueErrorFlag = true;	//only present the error message once
				var message = "";
				if (document === null)
					{ message = "getAppletValue: document is null You probably need to install a newer version of Java Would you like to get Java?"; }
				else
					{ message = "getAppletValue: document.applet is null You probably need to either enable Java for your browser or install a newer version of Java Would you like to get Java?"; }
				if (confirm(message))
					{ location.href = "http://java.com/en/download/index.jsp"; }	//load java
		}
	} catch(e)
	{
		//if ( GetAppletValueError == "false")
		//{
		//	GetAppletValueError = "true";
		//	alert("getAppletValue catch");
		//}
	}
	return null;
}

//function ClearAppletValue(index)
function RestoreAppletValue(index, commands)
{
	try {
		if (document !== null && document.applet !== null)
			{ document.applet.RestoreValue(index, commands); }
			//document.applet.ClearValue(index);
	} catch(e)
	{}

}

function getFlashMovie(name)
{
	if (window.document[name] !== null)
		{ return window.document[name]; }
	if (navigator.appName.indexOf("Microsoft Internet")==-1)
	{
		if (document.embeds && document.embeds[name] !== null)
			{ return document.embeds[name]; }
		else
			{ return null; }
	} else
		{ return document.getElementById(name); }
}

function  trimn(str)	// Strip leading and trailing white-space
{
	var str2 = str;
	return str2.replace(/^\s*|\s*$/g, "");
}	

function LoadFlash(flashName, url)
{
	if (url !== undefined && url !== null && url !== "")
	{
		var flash =  getFlashMovie(flashName);
		if (flash !== null)
		{
			var txt = trimn(url);
			txt = txt.toLowerCase();
			var starts = txt.substring(0,1);
			if (starts == "/" || starts == "\\")
				{ txt = "http://gate1.mcfiii.com/clientfiles"+ txt; }
//alert("LoadFlash("+flashName+","+txt+")");
			try {
				flash.LoadMovie(0, txt);
				//alert("LoadFlash("+ txt + ") Done");
			} catch(e)
			{ alert("LoadFlash("+ txt +") Error "+ e.message); }
			return true;
		}	else
		{
			alert("LoadFlash unknown "+flashName);
			return false;
		}
	} else
		{ return true; }
}

function PassAppletMessages(flashName, theMessage)
{
	if (theMessage !== null && theMessage !== "")
	{
		//alert("PassAppletMessages("+ flashName+") += " + theMessage);
		var flash =  getFlashMovie(flashName);
		if (flash !== null)
		{
			var txt;
			try
			{ txt = flash.GetVariable("command"); } //_root.command
			catch(e)
			{ txt = null; }

			//if the variable exists at this time
			if (txt !== null) 
			{
				var joiner = "";
				if (txt !== "")
					{ joiner = ","; }
				//alert("url = "+txt+ joiner+theMessage);
				log("PassAppletMessages command = "+txt+ joiner+theMessage);
				flash.SetVariable("command", txt+ joiner+theMessage);
				return true;
			} else	{
				log("PassAppletMessages no command var");
				return false;
			}
			//	alert("BAD flash:"+flashName+"_root.command === null");	//movie hasn't run first frame yet
		} else	{
			alert("Can't find flash movie:"+ flashName); 
			return false;
		}
		//shouldn't happen
	}
	return true;
}

// lowResUrl=http://gate1.mcfiii.com/clientfiles/example4.html
function SetFlashVar(flashName, varName, value)
{
	var flash =  getFlashMovie(flashName);
	if (flash === null)
		{ return false; }
	var txt;
	try
	{
		txt = flash.GetVariable(varName);
		flash.SetVariable(varName, value);
		return true;	//succeeded
	}
	catch(e)
	{ return false; }	////variable doesn't exists at this time
}

function IsSignedOn()
{
	try {
		return document.applet.IsSignedOn();
	} catch(e)
	{ return false; }
}

function Timer()
{
	//read messages from applet and give them to their
	//flash  movie pass commands
	//only clear the value if we can give it to the flash movie

	var commands = getAppletValue(1);
	if (LoadFlash("Face", 	commands) === false)
		{ RestoreAppletValue(1, commands); }

	commands = getAppletValue(2);
	if (PassAppletMessages("Face",  commands) === false)
		{ RestoreAppletValue(2, commands); }

	commands = getAppletValue(4);
	if (PassAppletMessages("Other",	commands) === false)
		{ RestoreAppletValue(4, commands); }

	commands = getAppletValue(5);
	//if (commands !== undefined && commands !== null && commands !== "")
	//	window.location.href = commands;
	if (commands !== undefined && commands !== null && commands !== "")
	{	//window.location.href = commands;
		
		var parameters = commands.split(",");
		//alert("split: "+parameters.length + "  "+commands);
		if (parameters.length == 1)
			{ window.location.href = commands; }	//baskward compatable
		else
		{
		
			var index = 0;
			for(index = 0; index < parameters.length-1; index+=2)
			{
				var cmdName = parameters[index].toLowerCase();
				//alert("for "+index+" "+cmdName);
				if (cmdName == "gourl")	//go to a url
				{
					window.location.href = parameters[index+1];
				} else if (cmdName == "focus")	//set the focus to a html element
				{
					var flash =  getFlashMovie(parameters[index+1]);
					if (flash === undefined || flash=== null)
						{ alert("focus "+parameters[index+1]+" is missing"); }
					else
					{
						//alert("SetFocus");
						flash.focus();
					}
				}
			}
		}
	}

	if (not_lowResUrl === true)	//lowResUrl=http://gate1.mcfiii.com/clientfiles/example4.html
	{
		if (SetFlashVar("Other", "lowResUrl", "http://gate1.mcfiii.com/clientfiles/example2.html"))
			{ not_lowResUrl = false; }	//once we set the var once stop doing this
	}
	if (not_reportEnviornment === true && IsSignedOn() === true)
		{ ReportEnviornment(); }

	timerID  = setTimeout(Timer, interval);
}

function TimerFocus()
{
	log("TimerFocus()");
//	timerFocus = setTimeout("TimerFocus()", 2500);
	document.applet.focus();
	//$(applet).focus();
	//document.applets[0].focus();

	//redraw it
	//document.applet.style.display = 'none';//Set the display property to none
	//document.applet.style.display = 'block';//and then return it to block
	document.applet.click();
}


function FlashMessage(command,theMessage)
{
	log("FlashMessage("+command+","+theMessage);
	//alert("FlashMessage("+command+", "+theMessage);
	switch(command.toLowerCase()) {
		case "gourl":
			if (theMessage !== "" && theMessage !== null)
			{
				var s1 = location.href.toLocaleLowerCase();
				var s2 = theMessage.toLocaleLowerCase();
				if (s1 != s2)
					{ location.href = theMessage; }
			}
			break;
		case "alert":   alert("FlashMessage("+theMessage+")");break;
		default:
			try {
				document.applet.FlashMessage(command,theMessage);
			} catch(e)
			{ }
		break;
	}
}

function Other_DoFSCommand(command, theMessage)
{
	FlashMessage(command,theMessage);
}
function face_FSCommand(command,theMessage)
{
	FlashMessage(command,theMessage);
}

//These should work for IE,Firefox,Netscape,Safari
//Ref: www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
function sortResults(inWin, docelcw, bodycw) {
	var result = inWin ? inWin : 0;
	if (docelcw) {
		if  (! result || (result>docelcw))
			{ result = docelcw; }
	}
	if (! bodycw)
		{ return false; }
	else if  (! result || (result>bodycw)) 
		{ return bodycw; }
	else
		{ return result; }
}

function windowWidth() {
	return sortResults (window.innerWidth ? window.innerWidth : 0,
						document.documentElement ? document.documentElement.clientWidth : 0,
						document.body ? 			document.body.clientWidth : 0);
}
function windowHeight() {
	return sortResults (window.innerHeight ? 		window.innerHeight : 0,
					document.documentElement ? document.documentElement.clientHeight : 0,
					document.body ? 			document.body.clientHeight : 0);
}

var resizeTimeRef = 0;
function centerWindow()
{
	var horizMargin	= 33;	// FF:-18;
	var vertMargin	= 92;	// FF:+22; if "find" bar open
	var width = windowWidth() + horizMargin;
	var height = windowHeight() + vertMargin;
	log(  "centerWindow()  Screen width = "+screenW+"<br>" +"Screen height = "+screenH+
		"width = "+ width + "height = "+ height);

	//minimum window size

		var minWide	= 990;	//640
		var minHi  	= 715;  //480
		if (minWide > screenW-25) { minWide = screenW-25; }
		if (minHi   > screenH-60) { minHi   = screenH-60; }
		var resizeFlag = false;
		if (width < minWide)
		{ log("Adjust width == "+ width +" ["+ minWide +"]");  width = minWide; resizeFlag = true; }

		if (height < minHi)
		{ log("Adjust height == "+ height +" ["+ minHi +"]");   height = minHi;  resizeFlag = true; }


	if (resizeFlag === true)
	{
		var newX = parseInt((screenW-width)/2,10);
		if (newX<0) { newX = 0; }
		var newY = parseInt( ((screenH-30) - (height+60))/3+30,10 );	//20 for top menu bar
		if (newY <0) { newY = 0; }

		moveTo(newX, newY);
		log("Move:"+ newX +", "+ newY);
		if (resizeTimeRef !== 0 )
			{ clearTimeout( resizeTimeRef ); }
		resizeTimeRef  = setTimeout("resizeWindow("+ width +","+ height+")",200);
	}

}
// navigator.appName - Gives the name of the browser (application Name)
// navigator.appVersion - Gives the browser version (application Version)
// navigator.appCodeName - Gives the browser codename (application CodeName)
// navigator.platform -
//navigator.product = "Gecko"	navigator.productSub = "20030107"
//navigator.vendor = "Apple Computer, Inc."
/*

var growingTimerRef = 0;
function growingWindow()	
//this will be hit many times while streaching the windwow
{
	if (growingTimerRef != 0 )
		{ clearTimeout( growingTimerRef ); }
	growingTimerRef  = setTimeout(" centerWindow()",300);
}

*/
function resizeWindow(wide,hi)
{
	if (navigator.appName == "Netscape" && navigator.product != "Gecko")
	{				
		top.outerWidth = wide;
		top.outerHeight= hi;
		log( "resizeWindow("+ wide +", " + hi+") Netscape");
	} else {
		top.resizeTo( wide,hi );
		log( "resizeWindow("+ wide +", " + hi+")");
	}
}


function ReportProblem(message)
{
	try {
		document.Warning.src = "http://gate1.mcfiii.com/cgi-bin/WriteLog.cgi?Log="+escape(message); 
	} catch(e) {}
}
function ReportAlert(message)
{
	try {
		document.Warning.src = "http://gate1.mcfiii.com/cgi-bin/WriteLog.cgi?Log="+escape(message); 
	} catch(e) {}
	alert(message);
}
function OldBrowser(cause)
{
	ReportProblem("OldBrowser:"+cause);
	//document.Warning.src = "http://gate1.mcfiii.com/cgi-bin/WriteLog.cgi?Log=OldBrowser:"+escape(cause); 
	//note: we need some time for the line above to trigger the cgi application before changing web pages if inothing else
	alert("This browser is too old to run the demo.  Please either update it or repelace it with a diferent one."+unescape('%0a')+cause);
	location.href = "http://gate1.mcfiii.com/clientfiles/OldBrowser.html";
}
function GetFlashVers()
{
	var x = "";
	var y = "";
	try {
		//MSDetect = "false";
		if (navigator.plugins && navigator.plugins.length)
		{
			x = navigator.plugins["Shockwave Flash"];
			if (x)
			{
				if (x.description)
				{
					y = x.description;
					return "flash "+ y.charAt(y.indexOf('.')-1) + " installed";
				} else
					{ return "flash is installed"; }
			}
			else
			{
				if (navigator.plugins["Shockwave Flash 2.0"])
					{ return "Shockwave Flash 2.0"; }
				else 
					{ return "no flash"; }
			}
		}
		else if (navigator.mimeTypes && navigator.mimeTypes.length)
		{
			x = navigator.mimeTypes['application/x-shockwave-flash'];
			if (x && x.enabledPlugin)
				{ return "flash installed"; }
			else
				{ return "no flash"; }
		}
		else
			{ return "flash?"; }
	}	catch(e)
	{ return "flash detect catch:"+ e.message; }
}

function TestJavaVersion()
{
	//Java on ?
	try{
		if (navigator=== null )
		{
			OldBrowser("Unable to find navigator");
			return;
		} else
			//if (document.getElementById("TestJV")=== null && document.TestJV=== null)
			if (!navigator.javaEnabled())
			{	ReportAlert("Java needs to be switched on for applets on this web page to run"); return; }	//else the applet has loaded
	}	catch(e)
	{ OldBrowser("JavaScript is unable to check if java is enabled. "+ e.message); return; }

	//get the TestJV applet
	var javaVersionApplet = null;
	try {
		javaVersionApplet = document.TestJV;
	} catch(e2)
	{	OldBrowser("This browser does not support LiveConnect, "+ e2.message); return; }
	if (javaVersionApplet=== null)
		{ OldBrowser("This browser does not support LiveConnect"); return; }
	
	//Get Java version
	var version = null;
	try {
		version = javaVersionApplet.GetJavaVersion() +"";	//note: +"" because we want a copy made from the string returned by the applet
	} catch(e3)
	{ 	OldBrowser("Unable to call function within an applet. "+ e3.message); return; }

	if (version === "")
		{ ReportAlert("I am unable to check what version of Java is being used. This browser may be to old. "); return; }

	//check java version
	var parts = version.split(".");
	if (parts === undefined || parts.length < 2)
	{
		ReportAlert("We were unable to determine what version of Java is in use("+version+"). It needs to be 1.4 or better");
	} else
	{
		try {
			if (parseInt(parts[0],10) < 1 || (parseInt(parts[0],10) == 1 && parseInt(parts[1],10) < 4))
			{	
				ReportProblem("You will need install a newer version of Java than "+version+" to view this web page. Would you like to get it now?");
				if (confirm("You will need install a newer version of Java than "+version+" to view this web page."+unescape('%0a')+"Would you like to get it now?"))
					{ location.href = "http://java.com/en/download/index.jsp"; }
				else
					{ ReportProblem("Declined newer Java"); }
			} else
			{	//alert("Java version is good "+version);
				//lets report back what version of java and flash is being used
				//lets report back what version of java and flash is being used
				var page = "";
				try {
					page =window.location.pathname.toString();
				} catch(e4) { page = e4.message; }
				ReportProblem("page="+page+" Java version = "+version+ "  "+GetFlashVers()+ " Enviornment:" + GetEnviornment());	//no problem - report version
			}
		}	catch(e4)
		{
			ReportProblem("Catch: We were unable to determine what version of Java is in use("+version+"): " + e4.message);
			alert("We were unable to determine what version of Java is in use("+version+"). It needs to be 1.4 or better");
		}
	}
}

