function byId(id)
{
var elt;

if(document.getElementById)
    elt = document.getElementById(id);
else
if(document.all)
    elt = document.all[id];
else
if(document[id])
    elt=document[id];

if(typeof(elt) != undefined)
	return(elt);
else
	alert("No element with ID = \"" + id + "\"!");
}


// alternate form of isInitialized()

function isInitialized()
{
return(LG3D.getVersionNumber() > 0);
}

var LG3D, LGready = false, LGreadyId = 0, LGreadyCount = 0;
var toggleSpinId = 0;
var readyFlag = false;
var readyDelayTime = 1; // but gets doubled each time to prevent runaway condition

var LGdetectMode = 0;

var initialViewVerticalX, initialViewVerticalY, initialViewVerticalZ;
var viewVerticalX, viewVerticalY, viewVerticalZ;
var initialViewPointX, initialViewPointY, initialViewPointZ;
var viewPointX, viewPointY, viewPointZ;
var initialSpinX, initialSpinY;
var spinX, spinY;
 
function live3Dsetup()
{
clearInterval(LGreadyId);
readyFlag = true;

// get some initial values
getSpin();
getViewVertical();
getViewPoint();

initialViewVertcalX = viewVerticalX;
initialViewVertcalY = viewVerticalY;
initialViewVertcalZ = viewVerticalZ;
initialViewPointX = viewPointX; 
initialViewPointY = viewPointY; 
initialViewPointZ = viewPointZ; 
initialSpinX = spinX;
initialSpinY = spinY;

if(typeof(live3Dspeak) != undefined)
   live3Dspeak();
}

function live3DtryAgain()
{
LGreadyCount++; 
LGready = isInitialized(); 
if(LGready) 
	live3Dsetup();
else
	{
	if(readyDelayTime > 15000)
		{
		readyDelayTime *= 2;
		alert("Cannot communicate with applet after " + readyDelayTime + "milliseconds (and " + LGreadyCount + "attempts, doubling the delay time with each attempt). You should probably abort and close the browser window. Sorry. This page uses Javascript to communicate with the Java applet and something is wrong. Feel free to contact me at <rmabry AT lsus DOT edu> to alert me of this condition. Tell me the type of browser and system you are using and anything else that might help. Thanks, R. Mabry");
		}
	}
}

function live3Dready()
{
if(document.LiveGraphics3D)
	{
	LG3D = document.LiveGraphics3D; LGdetectMode = 1;
	}
else
if(document.getElementById)
	{
  LG3D = document.getElementById("LiveGraphics3D"); LGdetectMode = 2;
	}
else
if(document.all)
	{
   LG3D = document.all["LiveGraphics3D"]; LGdetectMode = 3;
	}
else
if(document.applets)
	{
    LG3D = document.applets["LiveGraphics3D"]; LGdetectMode = 4;
	}
else
if(document["LiveGraphics3D"])
	{
    LG3D = document["LiveGraphics3D"]; LGdetectMode = 5;
	}

if(LGdetectMode > 0)
	LGreadyId = setInterval("live3DtryAgain()", readyDelayTime);
else
	alert("No element with ID = \"LiveGraphics3D\"!");
}

function toggleLockSpin()
{
if(toggleSpinId == 0)
    {
    toggleSpinId = setInterval("LG3D.setSpin(0.0,0.0);", 1);
		document.LiveGraphics3DForm.toggleSpin.value = "Allow Spin";
		}
else
    {
    clearInterval(toggleSpinId);
		document.LiveGraphics3DForm.toggleSpin.value = "Prevent Spin";
		}
}


function tell(func)
{
if(!readyFlag)
   alert("Applet not yet initialized! One moment please...");
else
   func();
}

function tellIsInitialized()
{
alert("isInitialized() required " + LGreadyCount + " tries.");
}

function tellVersionNumber()
{
var ver = LG3D.getVersionNumber();
alert("Version number is " + ver);
}

function getViewPoint()
{
viewPointX = LG3D.getViewPointX();
viewPointY = LG3D.getViewPointY();
viewPointZ = LG3D.getViewPointZ();
}

function tellViewPoint()
{
getViewPoint();
alert("ViewPoint = (" + viewPointX + ",    " + viewPointY + ",   " + viewPointZ + ")");
}

function getViewVertical()
{
viewVerticalX = LG3D.getViewVerticalX();
viewVerticalY = LG3D.getViewVerticalY();
viewVerticalZ = LG3D.getViewVerticalZ();
}

function tellViewVertical()
{
getViewVertical();
alert("ViewVertical = (" + viewVerticalX + ",   " + viewVerticalY + ",   " + viewVerticalZ + ")");
}

function getSpin()
{
spinX = LG3D.getSpinX();
spinY = LG3D.getSpinY();
}

function tellSpin()
{
getSpin();
alert("Spin = (" + spx + ",   " + spy + ")");
}

function tellMagnification()
{
var mag = LG3D.getMagnification();
alert("Magnification = " + mag);
}

function tellStereoDistance()
{
var sd = LG3D.getStereoDistance();
alert("StereoDistance = " + sd);
}

function tellStrippedPrimitives()
{
var sp = LG3D.getStrippedPrimitives();
alert("StrippedPrimitives = " + sp);
}

function getFrame()
{
frame = LG3D.getFrame();
}

function tellFrame()
{
getFrame();
alert("Frame = " + frame);
}

function setSpin(sx,sy)
{
var ss = LG3D.setSpin(sx,sy);
if(!ss)
	alert("Error: setSpin failed.");
return ss;	
}
function setGraphics3D(g)
{
var sg = LG3D.setGraphics3D(g);
if(!sg)
	alert("Error: setGraphics3D failed.");
return sg;	
}

function setViewPointAndViewVertical(vpx, vpy, vpz, vvx, vvy, vvz)
{
var sv = LG3D.setViewPointAndViewVertical(vpx, vpy, vpz, vvx, vvy, vvz);
if(!sv)
	alert("Error: setViewPointAndViewVertical failed.");
return sv;	
}

function setViewPoint(vpx, vpy, vpz)
{
getViewVertical();
return(setViewPointAndViewVertical(vpx, vpy, vpz, viewVerticalX, viewVerticalY, viewVerticalZ));
}

function setFrame(fr)
{
var sf = LG3D.setFrame(fr);
if(!sf)
	alert("Error: setFrame(" + fr + ") failed.");
return sf;	
}

function setInputFile(file, archive)
{
var sif = LG3D.setInputFile(file, archive);
if(!sif)
	alert("Error: setInputFile(" + file + "," + archive + ") failed.");
return sif;	
}

function rotateToViewPointAndViewVertical(vpx, vpy, vpz, vvx, vvy, vvz)
{
var rv = LG3D.rotateToViewPointAndViewVertical(vpx, vpy, vpz, vvx, vvy, vvz);
if(!rv)
	alert("Error: rotateToViewPointAndViewVertical failed.");
return rv;	
}

function getVariable(name)
{
return LG3D.getVariable(name);
}

function setVariable(name, value)
{
var sv = LG3D.setVariable(name,value);
if(!sv)
	alert("Error: setVariable(" + name + ", " + value + ") failed.");
return sv;	
}

