

var interval = 250;	// half second = 500
var timerID = setTimeout(Timer, 3000);
var bandwidth = "";
//var GetAppletValueError = "false";
var not_lowResUrl = true;
var not_reportEnviornment = true;
//var redrawLater = 0;


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()
{
	try {
		var x = null;
		if (navigator.plugins && navigator.plugins.length)
		{
			x = navigator.plugins["Shockwave Flash"];
			if (x)
			{
				if (x.description)
				{
					var 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 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 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 or applets are switched off, "+ e2.message);
		return;
	}
	if (javaVersionApplet === null)
		{ oldBrowser("This browser does not support LiveConnect, I am unable to find a Java applet"); 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."+unescape('%0a')+"Scripting of Java Applets may be switched off"+unescape('%0a')+ e3.message);
		return;
	}

	if (version === "")
		{ reportAlert("I am unable to check what version of Java is being used. This browser may be too 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
				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(e5)
		{
			reportProblem("Catch: We were unable to determine what version of Java is in use("+version+"): " + e5.message);
			alert("We were unable to determine what version of Java is in use("+version+"). It needs to be 1.4 or better");
		}
	}
}

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 Loaded()
{
	alert("loaded");
	TestJavaVersion();
	alert("back");
	var flash =  GetFlashMovie("Other");
	if (flash !== undefined && flash !== null)
		{ flash.focus(); }

	MM_preloadImages('cassandra2t.gif');
}

	//alert("build message:"+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)
		{
			var str = document.applet.GetValue(index);
			if (str === null || str=== undefined || str === "undefined")
				{ return null; }
			else
				{ return String(str); }
			//return str;
		}
		else
			{ alert("GetAppletValue no document"); }
	} 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 resizeWindowTo(w,h) {

	if (redrawLater != 0)
	{
		redrawLater = redrawLater - 1;
		if (redrawLater == 0)
		{	//make it redraw the window by resizing it
			var wide = top.outerWidth;
			var hight= top.outerHeight;
			//window.resizeTo
			resizeWindowTo(wide+10,hight);
			
			var wide2 = top.outerWidth-10;
			var hight2= top.outerHeight;
			resizeWindowTo(wide+(wide-wide2),hight+(hight-hight2));
			
			//var wide3 = top.outerWidth;
			//var hight3= top.outerHeight;
			//alert("Wide orig:"+wide+" after:"+wide2+" fixed:"+wide3+ "\rHi   orig:"+hight+" after:"+hight2+" fixed:"+hight3);
		}
	}

	
 if (parseInt(navigator.appVersion)>3)
 {
   if (navigator.appName=="Netscape") {
    top.outerWidth=w;
    top.outerHeight=h;
   } else
   { top.resizeTo(w,h); }
 }
}
*/

// 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  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 !== "")
	{
//alert("LoadFlash("+flashName+","+url+") A");
		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("path:"+window.location.pathname);
				var theUrl = window.location.protocol+'//'+window.location.host+window.location.pathname;
				var index = theUrl.lastIndexOf("/");
				if (index > 0)
					{ theUrl = theUrl.substring(0, index); }
				//if (theUrl.charAt(theUrl.length-1) == '/')
					//theUrl = theUrl.substring(0, theUrl.length-1);
				txt =  theUrl + txt;
			}
		
//alert("LoadFlash("+flashName+","+txt+") B");
			try {
				var current = flash.TGetProperty('/', 15);
				current = current.toLowerCase();
				if (current !== txt)
					{ flash.LoadMovie(0, txt); }
			} 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);
				flash.SetVariable("command", txt+ joiner+theMessage);
				return true;
			} else
				{ 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;
}

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(3);
	if (LoadFlash("Other", 	commands) === false)
		{ restoreAppletValue(3, 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/example2.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 FlashMessage(command,theMessage)
{
	switch(command) {
		case "gourl":	window.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);
}



