
//  ----------------  EON_FUNCTIONS.JS  ----------------
//  eon_functions.js is an external javascript file used to 
//  insert the EonX active x control into web pages.
//             Last modified Feb 28th, 2008




//	  10	------------  CONTENTS  ------------
//	  30	DETECT PLATFORM, BROWSER, EON VERSION
//	  90	INSERT EON OBJECT
//	 290	EONX METHODS
//	 370	EONX PROPERTIES 			
//	 490	SENDING and RECIEVING EVENTS
//   610	METHODS FOR GETTING IN/OUT EVENT INFORMATION
//	 655	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.
*/

// declaring 'EONControl' as a global variable for non IE browsers, IE will instead have object ID set to 'EONControl'.
if (!IE) eval("var EONControl = null"); 
var EonXStatus = 0;
var curversion = "not installed";
function EONInsert()
{
	if (!is_win32) {
		EonXStatus=-1
		document.write(GetPluginText(curversion))
		return;
	}
	if (location.href.indexOf('installnow')!=-1) CheckAndHandleEonXStatus=0; 
	
	if (IE) {
		// Insert the EON control object and then check its version afterwards.
		document.write(EONInsertText());
		
		if (CheckAndHandleEonXStatus==0) return; // let EON update itself.	
		
		// Get version info and set EonXStatus
		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, ",")) EonXStatus=1;	// old version
			}
		}
		if (EonXStatus!=2)	// replace EON object with text on how to get plugin.
		{
			document.getElementById('EONControl').outerHTML = GetPluginText(curversion)
			document.getElementById('hiddiv').outerHTML = ""
		}
	}
	else
	{
		// check version number in description of plugin before inserting EON object.
		navigator.plugins.refresh(false)
		var eonPlugin = navigator.plugins["EonX Plug-In"];	// write 'about:plugins' in your addressbar to see details.
		if ((typeof eonPlugin)!="undefined") // plugin exists - check version
		{
			curversion=eonPlugin.description.substring(26) // extract from 26th characater onwards.			
			if (curversion=="") curversion="3,0,1,75";
			if (VersionOK(curversion, ",")) EonXStatus = 2; // correct version installed.
			else EonXStatus = 1; // old version installed
		}
		
		if (EonXStatus<=1 && CheckAndHandleEonXStatus!=0)
		{
			document.write(GetPluginText(curversion)) 
		}
		else
		{
			document.write(EONInsertText());
			EONControl = document.getElementById('EONObject');
				
			EONControl.PluginsPage				= eonxPluginsPage							
			EONControl.Codebase						= eonxCodebase2;					
			EONControl.AutoPlay						= eonxAutoPlay;					
			EONControl.Background					= eonxBackground;
			EONControl.Progressbar				= eonxProgressbar;
			EONControl.SchemeValues				= eonxSchemeValues;
			EONControl.SimulationFile			= eonxSimulationFile;
			EONControl.PrototypebaseURL		= eonxPrototypebaseURL;
			EONControl.ConfigurationScheme= eonxConfigurationScheme;
				
			// workaround for AutoPlay
			if(eonxAutoPlay != 0) EONControl.Start();
		}		
	}		
}

function EONInsertText(n)
{
	if (!IE)
	{
		var t='<OBJECT ID="EONObject"  TYPE="application/x-eonx"';
		if (EonXStatus==0 && CheckAndHandleEonXStatus==0) t+=' CODEBASE="' + eonxPluginsPage + '"';	// set codebase=pluginpage for non IE browsers
		t+=' HEIGHT="' + eonxHeight + '" WIDTH="' + eonxWidth + '">';
		if (EonXStatus==0 || CheckAndHandleEonXStatus==1) t+=GetPluginText(curversion)	// this text shows if the browser cannot display the object
		t+='</OBJECT>';	
	}
	else
	{
		var t='<OBJECT ID="EONControl"  TYPE="application/x-eonx"';
		t+=' CLASSID="' + eonxClassid + '" TITLE="EonX ActiveX Control"';
		if (CheckAndHandleEonXStatus==0) t+=' CODEBASE="' + eonxCodebaseIE + '"'; // let Eon download itself
		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 + '">';
		t+=GetPluginText(curversion)	// this text shows if the browser cannot display the object
		t+='</OBJECT>';
		
		// 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></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 Viewer required!<br><br></font>To display the 3D content on this page,<br>your browser needs the EON Viewer plugin.';
	t+='<br><font size=2><br>Go to the <a href="' + eonxPluginsPage + '">Plugin Page</a></font>';
	t+='<br><br><i>Version required:'
	if (IE) t+='<a href="javascript:InstallHere()">_</a> '
	else t+=" ";
	t+=formatVersion(LatestEONVersion) + '</i>';
	if (vers || ((!IE)&&(EonXStatus<=1))) t+='<br><i>Current Version: ' + formatVersion(vers) + '</i>';
	t+='<br><br>'
	if (EonXStatus==1) t+='<a href="javascript:TryWithOldVersion()">Try with old version</a>'
	t+='<br><br>Content: <a href="' + eonxSimulationFile + '">' + eonxSimulationFile + '</a>';
	if (!is_win32) t+='<br><br><font size=2 color="#ff0000"><b>Note: EON Viewer plugin 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="        -- EON Viewer Required! --"
	msg+="\n\nClick 'OK' if you want to install the ActiveX Control way from this page."
	msg+="\nThis may be quicker but it will not be a full installation"
	msg+=" and it may not be the latest version."
	msg+="\n\nTo get the fullest and latest version, click 'Cancel' and then click"
	msg+=" the link to the Plugin Page." 
	if (window.confirm(msg)) location.href = location.href + "?installnow"
}

function TryWithOldVersion()
{
	var msg="        -- EonX Plugin --"
	msg+="\n\nCurrent Version: " + formatVersion(curversion)
	msg+="\nRequired Version: " + formatVersion(LatestEONVersion)
	msg+="\n\nSometimes 3D content works with older versions."
	msg+="\n\nClick OK to try. (Press 'Back' button if it fails.)"
	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);
	EONCodebase(eonxCodebase2);
}
// This function can pause or unpause the simulation or get paused value.
function EONPause(v)
{
	if (v==null) return EONControl.Pause();
	EONControl.Pause(v);
}
function EONShowSettingsDialog(){EONControl.ShowSettingsDialog()}

// This function can set the FullSize Mode
function EONFullsize(v)
{
	if (v==null) return EONControl.Fullsize()
	else
	{
		if (IE) EONControl.SetFullSize(v)
		else EONControl.FullSize(v)
	}
	return;
}

// This function saves a picture of the running simulation to a specific location
// Pressing 444 will trigger this function
// To see the file in the web page use: '<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)
{
	if(!e) e=window.event;
  	var key = e.keycode ? e.keycode : e.which;
  	if (key == 52) {fourcount ++;}
	else {fourcount = 0;}
	if (fourcount==3)
	{
		fourcount=0;
		EONSaveSnapshot();
	}
}

















//  --- EONX PROPERTIES --- (ProgressBar, SimulationFile, Background, Codebase, PrototypebaseURL, ConfigurationScheme, SchemeValues, 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.
// Note, if relative path given, plugin will search via the codebase property.
function EONSimulationFile(v)
{
	if (v == null) {return EONControl.SimulationFile}		
	if (v == EONControl.SimulationFile) return; //ignore if no change in sim file.
	var s = false;
	if (isEONrunning) 
	{
		EONStop();
		s = true;
	}
	EONControl.SimulationFile = v
	if (s) EONStart();
}

// This function sets or gets the Background property
function EONBackground(v)
{
	if (v == null) return EONControl.Background
	eonxBackground = v;
	// don't set while running.
	if (!isEONrunning) EONControl.Background = v
}

// This codebase is where the simulation files and background files are kept.
function EONCodebase(v)
{
	if (v == null) return EONControl.Codebase
	eonxCodebase2 = v;
	// don't set while running.
	if (!isEONrunning) EONControl.Codebase = v
}

// This function changes the location of where prototypes are downloaded from.
function EONPrototypebaseURL(v)
{
	if (v==null) return EONControl.PrototypebaseURL
	EONControl.PrototypebaseURL = v;
}

function EONConfigurationScheme(v)
{
	if (v==null) return EONControl.ConfigurationScheme
	eonxConfigurationScheme = v;
	if (!isEONrunning) EONControl.ConfigurationScheme = v
}

function EONSchemeValues(v)
{
	if (v==null) return EONControl.SchemeValues
	else EONControl.SchemeValues = 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 verArray[0] + "," + verArray[1] + "," + verArray[2]+ "," + verArray[3];}
}

function EONVersion()
{
	if (IE) return EONGetVersion();
	else return EONControl.Version;
}



















//  --- SENDING and RECIEVING EVENTS ---
// This function sends events to EON. Now works in Firefox as well as IE
function EONSendEvent(typ, e, v1, v2, v3, v4)
{
	// Send error if type is not recognized
	typ=typ.toUpperCase()
	datatypes = "SFBOOL, SFCOLOR, SFFLOAT, SFIMAGE, SFINT32, SFNODE, SFROTATION, SFSTRING, SFTIME, SFVEC2F, SFVEC3F";
	if (datatypes.indexOf(typ)==-1) 
	{
		alert("Unrecognized EON datatype '" + typ + "'");
	}
	
	var vectortype = false;
	var simpletypes = "SFBOOL, SFFLOAT, SFIMAGE, SFINT32, SFNODE, SFSTRING, SFTIME"
	if (simpletypes.indexOf(typ)==-1) vectortype = true;
	
	if ((typeof(v1)=="string")&&(v2==null))
	{
		if (vectortype)
		{
			var b = v1.split(" ");
			if ((b.length>=2)&&(b.length<=4))
			{
				v1=Number(b[0]);
				v2=Number(b[1]);
				if ((b.length==3)&&((typ=="SFCOLOR")||(typ=="SFVEC3F"))) v3=Number(b[2]);
				if ((b.length==4)&&(typ=="SFROTATION")) {v3=Number(b[2]);v4=Number(b[3]);} 
			}
		}
		else
		{
			if (typ=="SFBOOL")
			{
				if ((v1=="true")||(v1=="True")||(v1=="TRUE")) {v1=true}else{v1=false}
			}
			if ((typ=="SFFLOAT")||(typ=="SFINT32")||(typ=="SFTIME")){v1=Number(v1)}
		}
	}
	if (IE)
	{
		// 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.eonhiddenfields.EONInEventType.value = typ;
		document.eonhiddenfields.EONInEventName.value = e;
		document.eonhiddenfields.EONInEventV1.value = v1
		document.eonhiddenfields.EONInEventV2.value = v2;
		document.eonhiddenfields.EONInEventV3.value = v3;
		document.eonhiddenfields.EONInEventV4.value = v4;
		document.eonhiddenfields.EONInEventName.click();
	}
	else
	{
		if (vectortype)
		{
			var args = new Array(typ, v1, v2, v3, v4);
			EONControl.SendEvent(e, args);
		} 
		else
		{
			EONControl.SendEvent(e, v1);
		}		
	}	
}

// 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.eonhiddenfields.EONOutEventType.value;
	var e = document.eonhiddenfields.EONOutEventName.value;
	
	if (t == 0) 
	{
		var v = document.eonhiddenfields.EONOutEventV1.value;
		EON_OnEvent(e,v);
	}
	else
	{
		var v = new Array(t + 1);
		if (t == 1)
		{
			v[0] = document.eonhiddenfields.EONOutEventV1.value
			v[1] = document.eonhiddenfields.EONOutEventV2.value
		}
		if (t == 2)
		{
			v[0] = document.eonhiddenfields.EONOutEventV1.value
			v[1] = document.eonhiddenfields.EONOutEventV2.value
			v[2] = document.eonhiddenfields.EONOutEventV3.value
		} 
		if (t == 3) 
		{
			v[0] = document.eonhiddenfields.EONOutEventV1.value
			v[1] = document.eonhiddenfields.EONOutEventV2.value
			v[2] = document.eonhiddenfields.EONOutEventV3.value
			v[3] = document.eonhiddenfields.EONOutEventV4.value
		}
		EON_OnEvent(e, v);
	}
}

// This is here just in case it might help for other browsers in future.
// It will send the event on to the common function called EON_OnEvent(e,v)
function EONObject_OnEvent(e,v) {EON_OnEvent(e, v);}














// --- METHODS FOR GETTING IN/OUT EVENT INFORMATION ---

// This returns the number of IN events for the running simulation.
function EONGetNumberOfInEvents()
{
	return EONControl.GetNumberOfInEvents();
}

// This returns the number of OUT events for the running simulation.
function EONGetNumberOfOutEvents()
{
	return EONControl.GetNumberOfOutEvents();
}

// This function gets the name of an inevent given its position in the list of inevents.
function EONGetInEventName(i)
{
	if (EONGetNumberOfInEvents()<i) return -1;
	return EONControl.GetInEventName(i);
}

// This function gets the name of an outevent given its position in the list of outevents.
function EONGetOutEventName(i)
{
	if (EONGetNumberOfOutEvents()<i) return -1;
	return EONControl.GetOutEventName(i);
}

// Given the events position in the list of inevents, this function gets 
// an integer of an inevent which represents its datatype.
function EONGetInEventType(i)
{
	if (EONGetNumberOfInEvents()<i) return -1;
	return EONControl.GetInEventType(i);
}

// Given the events position in the list of outevents, this function gets 
// an integer of an outevent which represents its datatype.
function EONGetOutEventType(i)
{
	if (EONGetNumberOfOutEvents()<i) return -1;
	return EONControl.GetOutEventType(i);
}


//End of file
