
//  ----------------  EON_FUNCTIONS.JS  ----------------
//  eon_functions.js is an external javascript file used to 
//  insert the EonX active x control into web pages.
//             Last modified Jan 24th, 2005




//	  10	------------  CONTENTS  ------------
//	  30	DETECT PLATFORM, BROWSER, EON VERSION
//	  90	INSERT EON OBJECT
//	 250	EONX METHODS
//	 310	EONX PROPERTIES 			
//	 410	SENDING and RECIEVING EVENTS
//	 500	end of file













//  --- DETECT PLATFORM, BROWSER, EON VERSION ---

	var agt = navigator.userAgent.toLowerCase();
	var is_major = parseInt(navigator.appVersion); 

// --- PLATFORM ---
	var is_win95 = ((agt.indexOf("win95")!=-1) || (agt.indexOf("windows 95")!=-1));
	var is_win98 = ((agt.indexOf("win98")!=-1) || (agt.indexOf("windows 98")!=-1));
	var is_winnt = ((agt.indexOf("winnt")!=-1) || (agt.indexOf("windows nt")!=-1));
	var is_win32 = (is_win95 || is_winnt || is_win98 || 
			((is_major >= 4) && (navigator.platform == "Win32")) ||
			(agt.indexOf("win32")!=-1) || (agt.indexOf("32bit")!=-1));

// --- BROWSER --- 
	// Mozilla types (Netscape, Firefox)
	var MOZ  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
		&& (agt.indexOf('compatible')==-1) && (agt.indexOf('opera')==-1)
		&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
	// Internet Explorer
	var IE = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera")==-1));
	// Opera
	var OP = (agt.indexOf("opera")!=-1)


function VersionOK(VersionToCheck, SplitCharacter)
{
	var strVersionToCheck = String (VersionToCheck);
	var a = strVersionToCheck.split(SplitCharacter); // a = EON version installed on users computer
	var b = LatestEONVersion.split(',');			 // b = EON version required by this web page
	for (var i = 0; i < 4; i++)
	{
		a[i] = parseInt(a[i],10);
		b[i] = parseInt(b[i],10);
		if (a[i]>b[i]) return true; // checked version higher than required
		if (a[i]<b[i]) return false;// checked version lower than required
	}
	return true; // eon versions are the same.
}
	





















/*  --- INSERT EON OBJECT ---
  The EONInsert function reduces code in the webpage and allows different coding for different browsers.
	The <object> tag is used for all browsers. We no longer use <EMBED>.
  Mozilla can view the <object> tag if the classid is removed and type and data attributes are added.
  Internet Explorer (IE) requires classid attribute. For IE, we can leave out the codebase so that
   if EON does not exist it will display alternative text (how to get plugin) instead of downloading eonx.cab.  
  If (after inserting the EON object) we detect its version is old, the <object> is replaced with text saying go to plugin page.
  For browsers other than IE we expect EonXPlugin.dll in the browser's Plugins folder.
  Opera browser - similiar to Mozilla but to run, the codebase attribute must not exist.
*/
var EonXStatus = 0;
var curversion = "not installed";
var EONControl;
function EONInsert()
{
	if (!is_win32)
	{	EonXStatus=-1
		document.write(GetPluginText())
		return;
	}
	if (IE)
	{
		if (location.href.indexOf('installnow')!=-1) CheckAndHandleEonXStatus=0; 
		var tt=EONInsertText();
		
		document.write(tt);
		EONControl=document.getElementById("EONControl");
		if (CheckAndHandleEonXStatus==0) {return;}	//let EON update itself.
		if ((typeof EONControl.Version)!="undefined")
		{ 
			var va = EONControl.Version.toArray();
			if (va!=null)
			{	EonXStatus=2;
				curversion = va[0] + "." + va[1] + "." + va[2]+ "." + va[3];
				
				if (VersionOK(curversion, ".")) {return;}	// correct version so exit.
				EonXStatus=1;	// old version
			}
		}
		
		document.all('EONControl').outerHTML = GetPluginText()
		document.all('hiddiv').outerHTML = ""
		
	}
	else
	{
		navigator.plugins.refresh(true)
		var eonPlugin = navigator.plugins["EonX Plug-In"];
		if ((typeof eonPlugin)=="undefined")	// no plugin
		{	if (MOZ) document.write(EONInsertText())	// so that Mozilla displays its 'get plugin' bar.
			else document.write(GetPluginText())
		}
		else	// plugin exists - check version
		{	EonXStatus = 1;
			curversion=eonPlugin.description.substring(26) // extract from 26th characater onwards.
			if (curversion=="") curversion="3,0,1,75";
			if (!VersionOK(curversion, ",")) document.write(GetPluginText(curversion))
			else
			{	// correct plugin version exists
				EonXStatus = 2;
				document.write(EONInsertText())
			}
		}
	}
	
}

function EONInsertText(n)
{
	var t='<OBJECT ID="EONControl"  TYPE="application/x-eonx"';
	if (!MOZ) t+=' CLASSID="' + eonxClassid + '" TITLE="EonX ActiveX Control"';
	if ((MOZ)&&(EonXStatus==0)) t+=' CODEBASE="' + eonxPluginsPage + '"';	// set codebase=pluginpage for Moz when no plugin
	if ((IE)&&(CheckAndHandleEonXStatus==0)) t+=' CODEBASE="' + eonxCodebaseIE + '"'; // let Eon download itself
	if (!IE) t+=' DATA="' + eonxSimulationFile + '"';
	t+=' HEIGHT="' + eonxHeight + '" WIDTH="' + eonxWidth + '">';
	t+=' <PARAM NAME="Codebase" Value="' + eonxCodebase2 + '">';
	t+=' <PARAM NAME="AutoPlay" Value="' + eonxAutoPlay + '">';
	t+=' <PARAM NAME="Background" Value="' + eonxBackground + '">';
	t+=' <PARAM NAME="Progressbar" Value="' + eonxProgressbar + '">';
	t+=' <PARAM NAME="SchemeValues" Value="' + eonxSchemeValues + '">';
	t+=' <PARAM NAME="SimulationFile" Value="' + eonxSimulationFile + '">';
	t+=' <PARAM NAME="PrototypebaseURL" Value="' + eonxPrototypebaseURL + '">';
	t+=' <PARAM NAME="ConfigurationScheme" Value="' + eonxConfigurationScheme + '">';
	if (n==null) t+=GetPluginText()	// this text shows if the browser cannot display the object
	t+='</OBJECT>';

	if (IE)
	{
		// The following code will insert hidden form fields which are neccessary 
		// to allow communication between vbscript and javascript and vice versa;
		// The tags should never be a problem becuase they are in a hidden div far
		// to the left of the page. (It hasn't been tested on early browsers.)
		// These fields allow all events from EON to be handled by one javascript 
		// function called EON_OnEvent(e,v) and they allow all events to EON to be 
		// sent using javascript only. (eon_functions.vbs external script is required.)
		t+='<div id="hiddiv" style="position:absolute;visibility:hidden; left:-1000 px;">';
		//t+='<form name="eonhiddenfields">';
		t+='<input type="hidden" name="EONInEventType">';
		t+='<input type="hidden" name="EONInEventName">';
		t+='<input type="hidden" name="EONInEventV1">';
		t+='<input type="hidden" name="EONInEventV2">';
		t+='<input type="hidden" name="EONInEventV3">';
		t+='<input type="hidden" name="EONInEventV4">';
		t+='<input type="hidden" name="EONOutEventType">';
		t+='<input type="hidden" name="EONOutEventName" onclick="EONOutEventName_OnClick()">';
		t+='<input type="hidden" name="EONOutEventV1">';
		t+='<input type="hidden" name="EONOutEventV2">';
		t+='<input type="hidden" name="EONOutEventV3">';
		t+='<input type="hidden" name="EONOutEventV4">';
		//t+='</form>';
		t+='</div>';
	}	
	if (eonxAutoPlay==1) isEONrunning = true;
	return t;
}

function GetPluginText(vers)
{
	if (eonxWidth<240) eonxWidth=240;
	var t = '<table id="PluginText" width=' + eonxWidth + ' height=' + eonxHeight + ' border=0 cellspacing=0 cellpadding=3 bgcolor="#ccccff"><tr><td><div ';
	if (IE) t+='style="position:relative;overflow:auto;height:100%"';
	t+='><table width=' + (parseInt(eonxWidth)-6) + ' height=' + (parseInt(eonxHeight)-6) + ' border=0 cellspacing=0 cellpadding=3 bgcolor="#eeeeff">';
	t+='<tr><td align="center"><font size=1 color="#000066" face="Verdana,Arial,Helvetica,san-serif"><b><font size=2>';
	t+='EON Reality Web plug-in required!<br><br></font>To display the 3D content on this page,<br>the EON Reality Web plug-in is required.';
	t+='<br><font size=2><br>Go to the <a href="' + eonxPluginsPage + '">Plugin Page</a></font>';
	if (IE) t+='<br><br><i>Version required:' + '<a href="javascript:InstallHere()">_</a> ' + formatVersion(LatestEONVersion) + '</i>';
	else t+='<br><br><i>Version required:' + formatVersion(LatestEONVersion) + '</i>';
	if ((!IE)&&(EonXStatus==1)) t+='<br><i>Current Version:' + formatVersion(vers) + '</i>';
	t+='<br><br><br><br>Content: <a href="' + eonxSimulationFile + '">' + eonxSimulationFile + '</a>';
	if (!is_win32) t+='<br><br><font size=2><b>Note: EON Reality Web plug-in requires a Windows platform!</b></font>';
	t+='</b></font></td></tr></table></div></td></tr></table>';
	return t;
}

function formatVersion(vers)
{	
	if (vers=="not installed") return vers;
	var b = vers.split(",")
	return b[0]+ "." + b[1] + "." + b[2] + " (build " + b[3] + ")"
}

function InstallHere()
{
	var msg="        -- EonX Plugin --"
	msg+="\n\nCurrent  Version: " + formatVersion(curversion)
	msg+="\nRequired version: " + formatVersion(LatestEONVersion)
	msg+="\n\nDo you want to install EonX Plugin here and now"
	msg+="\nwithout going to a plugin page?"
	if (window.confirm(msg)) location.href = location.href + "?installnow"
}



















// --- EONX METHODS --- (Start, Stop, Pause, ShowSettingsDialog, Fullsize, SaveSnapshot)

//  This variable is used in the SimulationFile function to determine 
//  if the simulation should be stopped and restarted. 
var isEONrunning = false;

// This function starts a simulation
function EONStart()
{
	EONControl.Start();
	isEONrunning = true;
}
// This function stops a simulation
function EONStop()
{
	EONControl.Stop();
	isEONrunning = false;
	EONBackground(eonxBackground);
}

function EONPause(v){EONControl.Pause(v)}
function EONShowSettingsDialog(){EONControl.ShowSettingsDialog()}

// This function can both set the FullSize Mode and get its value
function EONFullsize(v)
{
	if (v == null){return EONControl.GetFullsize()}
	else{EONControl.SetFullsize(v)}
}

// This function saves a picture of the running simulation to a specific location
// Press 444 to automatically call this function
// use this to see the file: '<img src="file:///c://eonx.png">'
function EONSaveSnapshot(v)
{
	if (v==null) EONControl.SaveSnapshot("C://eonx.png");
	else EONControl.SaveSnapshot(v)
}
document.onkeypress = KeyPressed;
var fourcount = 0;
function KeyPressed(e)
{
	e = window.event;
	if (e.keyCode == 52) {fourcount ++;}
	else {fourcount = 0;}
	if (fourcount==3)
	{
		fourcount=0;
		EONSaveSnapshot();
	}
}









//  --- EONX PROPERTIES --- (ProgressBar, SimulationFile, Background, Codebase, Width, Height, Version) 

// This function can both set or get the value of the progressbar
function EONProgressbar(v)
{
	if (v == null)
	{	if (EONControl.Progressbar == -1) return 1;
		return EONControl.Progressbar ;	
	}
	else{EONControl.Progressbar = v}
}


// When setting it checks that the simulation file is different from the existing
// file and if the simulation is running it will stop it, change files and restart it.
// If it isn't running it will only change the file.
function EONSimulationFile(v)
{
	if (v == null)
	{
		return EONControl.SimulationFile
	}
	else	// Setting the value of the Simulation file
	{
		if (v != (EONSimulationFile()))
		{
			var s = false;
			if (isEONrunning) 
			{
				EONStop();
				var s = true;
			}
			EONControl.SimulationFile = v
			if (s) EONStart();
		}
	}
}

function EONBackground(v)
{
	if (v == null) return EONControl.Background
	else
	{
		if (isEONrunning) eonxBackground = v;
		else EONControl.Background = v
	}
}

// This codebase is where the simulation files and background files are kept.
function EONCodebase(v)
{
	if (v == null) return EONControl.Codebase
	else {if (!isEONrunning) EONControl.Codebase = v}
}

function EONWidth(v)
{
	if (v == null) return EONControl.width
	else EONControl.width = v
}

function EONHeight(v)
{
	if (v == null) return EONControl.height
	else EONControl.height = v
}

//  This function will return, as an array, which version of EON is installed.
function EONGetVersionArray()
{
	var verArray = EONControl.Version;
	if(verArray==null) return new Array("not available")
	else return verArray.toArray()
}
//  This function will return a string stating which version of EON is installed.
function EONGetVersion()
{
	var verArray = EONGetVersionArray()
	if (verArray[0] == "not available")	{return "EonX version not available";}
	else {return "EonX version " + verArray[0] + "." + verArray[1] + "." + verArray[2]+ "." + verArray[3];}
}



















//  --- SENDING and RECIEVING EVENTS ---
// This function sends events to EON. Only Internet Explorer supports this!
// Netscape 4.x supported sending events but this webpage does not!
function EONSendEvent(type, e, v1, v2, v3, v4)
{
	// Send error if type is not recognized
		type=type.toUpperCase()
		datatypes = "SFBOOL, SFCOLOR, SFFLOAT, SFIMAGE, SFINT32, SFNODE, SFROTATION, SFSTRING, SFTIME, SFVEC2F, SFVEC3F";
		if (datatypes.indexOf(type)==-1) 
		{
			alert("Unrecognized EON datatype '" + type + "'");
		}

	if ((typeof(v1)=="string")&&(v2==null))
	{
		var simpletypes = "SFBOOL, SFFLOAT, SFIMAGE, SFINT32, SFNODE, SFSTRING, SFTIME"
		if (simpletypes.indexOf(type)!=-1)
		{
			if (type=="SFBOOL")
			{
				if ((v1=="true")||(v1=="True")||(v1=="TRUE")) {v1=true}else{v1=false}
			}
			if ((type=="SFFLOAT")||(type=="SFINT32")||(type=="SFTIME")){v1=Number(v1)}
		}
		else
		{
			var b = v1.split(" ");
			if ((b.length>=2)&&(b.length<=4))
			{
				v1=Number(b[0]);
				v2=Number(b[1]);
				if ((b.length==3)&&((type=="SFCOLOR")||(type=="SFVEC3F"))) v3=Number(b[2]);
				if ((b.length==4)&&(type=="SFROTATION")) {v3=Number(b[2]);v4=Number(b[3]);} 
			}
		}
	}
	// The data is sent via HTML Elements to then be picked up by vbscript and then 
	//  sent to EON. (See the EONInEventName_OnClick subroutine in eon_functions.vbs)
	document.forms[0].EONInEventType.value = type;
	document.forms[0].EONInEventName.value = e;
	document.forms[0].EONInEventV1.value = v1
	document.forms[0].EONInEventV2.value = v2;
	document.forms[0].EONInEventV3.value = v3;
	document.forms[0].EONInEventV4.value = v4;
	document.forms[0].EONInEventName.click();
}

// This next function is for receiving events from EON when using IE.
// The event has already been accepted by VBScript and put into the hidden field
// This function gets the values and sends them to a common function for both
// Netscape and IE browsers called EON_OnEvent(e,v).
function EONOutEventName_OnClick()
{
	var t = document.forms[0].EONOutEventType.value;
	var e = document.forms[0].EONOutEventName.value;
	
	if (t == 0) 
	{
		var v = document.forms[0].EONOutEventV1.value;
		EON_OnEvent(e,v);
	}
	else
	{
		var v = new Array(t + 1);
		if (t == 1)
		{
			v[0] = document.forms[0].EONOutEventV1.value
			v[1] = document.forms[0].EONOutEventV2.value
		}
		if (t == 2)
		{
			v[0] = document.forms[0].EONOutEventV1.value
			v[1] = document.forms[0].EONOutEventV2.value
			v[2] = document.forms[0].EONOutEventV3.value
		} 
		if (t == 3) 
		{
			v[0] = document.forms[0].EONOutEventV1.value
			v[1] = document.forms[0].EONOutEventV2.value
			v[2] = document.forms[0].EONOutEventV3.value
			v[3] = document.forms[0].EONOutEventV4.value
		}
		EON_OnEvent(e, v);
	}
}





//End of file
