var Distance;
var ImageWidthMax;
var ImageDiamMax;
var ImageWidthMin;
var ImageDiamMin;
var ImageWidth;
var ImageDiam;
var DistanceMax;
var DistanceMin;
var VerticalOffsetMin;
var VerticalOffsetMax;

//*** globals
var Win_1 = null;	// to refer to whther it is open or closed
var infinity = 10000000000000  // 10^13;
var windowcropTally = 5; // will not crop windows if more than this number visible
var browserName = navigator.appName;
var browserVersion = navigator.appVersion;
if ( (browserName == "Netscape") && (parseInt(browserVersion) >= 3)) browserName = "N";
else if ( (browserName == "Microsoft Internet Explorer") && (parseInt(browserVersion) >= 3) ) browserName = "M";
var e = 2.718281828459045;
var pi = 3.141592653589793;
// var x = 0;
var A = 0;
var tab = unescape( "%09" );	// these are now the appropriate strings;
var cr = unescape( "%0D" );	
var fractionMode = false;
var MaxNumValues = 15; // maximum # of values of x
var GraphWinOpened = false; // window not openend
windowFull = false; // true when window must be cleared
var heights = new Array(); // heights of bars in the graph
var numCats = 1; // number of bars in the graph
var catName = new Array(); // labels for bars in the graph
var okToRoll = true;
var theString = "";
var a = 0;
var b = 0; // end points
var theFunction = ""; // the function
var numSigDig = 10; // default for rounding
var ExampleString = "Some Examples:" + cr  + "EXPRESSION" + tab +tab + "FORMAT (spaces ignored)" + cr
ExampleString += "Linear Function"+tab + "2*x-4    or    2x-4"+cr;
ExampleString += "Exponents"+tab +tab + "2^(x-1)"+cr;
ExampleString += "Polynomial"+tab +tab + "3*x^2+4*x-5  or  3x^2+4x-5"+cr;
ExampleString += "Exponential"+tab +tab + "e^(2*x)"+cr;
ExampleString += "ln|4x-1|"+tab+tab + "ln(abs(4x-1))"+cr;
ExampleString += "Ratios"+tab +tab +tab+ "(3x^2 - 2)/(4x - 1)"+cr;
ExampleString += "Trig Function"+tab + "sin(pi*x-4)   NOT   sin(pix-4)";

window.onerror = myErrorTrap;
// *** end globals

// ****************** ERROR HANDLER *************
function myErrorTrap(message,url,linenumber) {
alert("Whoops! I can't process this!" + cr +" Press 'Example' for formatting information.");
return (true);
} // end of on error
// ***********************************************


// ******* Graphing routine
// *** the following functions are required for the grapher

function myParse(expression)
{
		theString = stripSpaces(expression);		
		with (Math)
			{
		// now convert formatting from GC formatting		
		theString = putProduct(theString);
		theString = replaceSubstring(theString,"log","(1/log(10))*log");
		theString = replaceSubstring(theString,"ln","log");
			while (powCheck(theString))
				{
				theString = powFix2(theString);
				// alert (theString);
				}
		theString = replaceChar(theString,"X","x");
			} // with Math
	return(theString);
} // myParse


function readBasics() {
// get the information from the web page
	for (var k = 1; k <= 1; k++)
	{
	//theFunction = document.theForm.fn.value; 
	theFunction = myParse(theFunction);
	//if (theFunction == "") {document.theForm.feedback.value = "You must first enter a function."; okToRoll = false; break;} 
	// alert ("here");
	//var aa = document.theForm.a.value; 
	//if (aa == "") { document.theForm.feedback.value = "You have not entered a number for the "+cr+"left end-point."; okToRoll = false; break;}
	//a = eval(aa);
	//if (isNaN(a) ) { document.theForm.feedback.value = "You have not entered a number for the  "+cr+"left end-point."; okToRoll = false; break;}
	//var bb = document.theForm.b.value; 
	//if (bb == "") { document.theForm.feedback.value = "You have not entered a number for the  "+cr+"right end-point."; okToRoll = false; break}
	//b = eval(bb); 
	//if (isNaN(b) ) { document.theForm.feedback.value = "You have not entered a number for the  "+cr+"right end-point."; okToRoll = false; break;}
	//if ( (okToRoll) && (a >= b)) { document.theForm.feedback.value = "The right end-point should be larger  "+cr+"than the left end-point"; okToRoll = false; break;}
	} // end of single loop
}

// ********* utilities

function looksLikeANumber(theString) {
// returns true if theString looks like it can be evaluated
var result = true;
var length = theString.length;
var x = ""
var y = "1234567890-+^*./ "
var yLength = y.length;
for (var i = 0; i <= length; i++)
	{ 
	x = theString.charAt(i);
		result = false;
		for (var j = 0; j <= yLength; j++) 
			{
			if (x == y.charAt(j)) {result = true; break}
			} // j
	if (result == false) return(false);
	} // i
return(result);
} // looks like a number


function shiftRight(theNumber, k) {
	if (k == 0) return (theNumber)
	else
		{
		var k2 = 1;
		var num = k;
		if (num < 0) num = -num;
		for (var i = 1; i <= num; i++)
			{
			k2 = k2*10
			}
		}
	if (k>0) 
		{return(k2*theNumber)}
	else 
		{return(theNumber/k2)}
	}

function roundSigDig(theNumber, numDigits) {
	with (Math)
		{
		if (theNumber == 0) return(0);
		else if(abs(theNumber) < 0.000000000001) return(0);
// WARNING: ignores numbers less than 10^(-12)
		else
			{
			var k = floor(log(abs(theNumber))/log(10))-numDigits
			var k2 = shiftRight(round(shiftRight(abs(theNumber),-k)),k)
			if (theNumber > 0) return(k2);
			else return(-k2)
			} // end else
		}
	}

function stripSpaces (InString)  {
	OutString="";
	for (Count=0; Count < InString.length; Count++)  {
		TempChar=InString.substring (Count, Count+1);
		if (TempChar!=" ")
			OutString=OutString+TempChar;
		}
	return (OutString);
	}

function replaceChar (InString,oldSymbol,newSymbol)  {
	OutString="";
	for (Count=0; Count < InString.length; Count++)  {
		TempChar=InString.substring (Count, Count+1);
		if (TempChar!=oldSymbol)
			OutString=OutString+TempChar
		else OutString=OutString+newSymbol;
	}
	return (OutString);
}

function replaceSubstring (InString,oldSubstring,newSubstring)  {
	OutString="";
	var sublength = oldSubstring.length;
	for (Count=0; Count < InString.length; Count++)  {
		TempStr=InString.substring (Count, Count+sublength);
		TempChar=InString.substring (Count, Count+1);
		if (TempStr!= oldSubstring)
			OutString=OutString+TempChar
		else 
			{
			OutString=OutString+ newSubstring;
			Count +=sublength-1
			}

	}
	return (OutString);
}

function reverse (InString)  {
	OutString="";
	var Length = InString.length;
	for (Count=Length; Count > -1; Count--)  {
		TempChar=InString.substring (Count, Count+1);
		if (TempChar == "(") {TempChar = ")"}
		else if  (TempChar == ")") {TempChar = "("}
		OutString=OutString+TempChar;
		}
	return (OutString);
	}

function toFrac(x, maxDenom, tol) {
// tolerance is the largest errror you will tolerate before resorting to 
// expressing the result as the input decimal in fraction form
// suggest no less than 10^-10, since we round all to 15 decimal places.
	var theFrac = new Array();
	theFrac[1] = 0;
	theFrac[2] = 0;
	var p1 = 1;
	var p2 = 0;
	var q1 = 0;	
	var q2 = 1;	
	var u =0;
	var t = 0;
	var flag = true;
	var negflag = false;
	var a = 0;
	var xIn = x; // variable for later

	if (x >10000000000) return(theFrac);
while (flag)
	{
	if (x<0) {x = -x; negflag = true; p1 = -p1}
	var intPart = Math.floor(x);
	var decimalPart = roundSigDig((x - intPart),15);

	x = decimalPart;
	a = intPart;
	
	t = a*p1 + p2;
	u = a*q1 + q2;
	if  ( (Math.abs(t) > 10000000000 ) || (u > maxDenom ) ) 
		{
			n = p1;
			d = q1;
			break;
		}

		p = t;
		q = u;
			
//		cout << "cf coeff: " << a << endl; // for debugging
//		cout << p << "/" << q << endl;	// for debugging
		
	if ( x == 0 )
		{
		n = p;
		d = q;
		break;
		}

		p2 = p1;
		p1 = p;
		q2 = q1;
		q1 = q;
		x = 1/x;
	
	} // while ( true );
	
	theFrac[1] = n;
	theFrac[2] = d;
	if (Math.abs(xIn-(n/d)) > tol) return (roundSigDig(xIn, numSigDig-1).toString());
	if (theFrac[2] == 1) return (theFrac[1].toString());
	return (theFrac[1] + "/" + theFrac[2]);

} // toFrac

function breakApart(InString) {
	// ******* Input: any string such as aaa*bbb or (aa*aa)*bbb
	// *** This Routine Retuns two pieces
	// *** bbb starts at the left-most operation *, /,  - or +
	// *** if it sees a paren, it stops after closure.

	theString = InString;
	var Length = theString.length;
	var outArray = new Array();
	outArray[1] = theString;
	outArray[2] = "";
	var parenCount = 0;
	var parenWatch = false;
	var looking = true;
	
	
	if (theString.substring (0,1) == "(")
		{
		parenCount++;
		parenWatch = true;
		looking = false;
		}
	// Look for operators
	
	for (Count=1; Count < Length; Count++)  
		{
		TempChar=theString.substring (Count, Count+1);
		if (TempChar == "(") 
			{parenCount ++;}
		else if (TempChar == ")") {parenCount = parenCount-1};

		if ((parenCount == 0) && (parenWatch == true))
			 {
			parenWatch = false;
			outArray[1] = theString.substring (0, Count+1);
			outArray[2] = theString.substring (Count+1, Length);
			}

		if (looking == true)
			{
		if ( ( (TempChar == "*") || (TempChar == "/") || (TempChar == "-")  ) || ( (TempChar == "+") || (TempChar == ')' )  )  )
				{ 
				
					{
					// alert(Count);
					looking = false;
					outArray[1] = theString.substring (0, Count);
					outArray[2] = theString.substring (Count, Length);
					// alert (outArray[1]);
					// alert(outArray[2]);
					} // end if hit one
				} 

			} // end if looking

		} // end of loop


	return (outArray);

} // end of breakApart

function isNumberChar (InString)  {
	if(InString.length!=1) 
		return (false);
	RefString="1234567890";
	if (RefString.indexOf (InString, 0)==-1) 
		return (false);
	return (true);
}

function isCharHere (InString, RefString)  {
	if(InString.length!=1) 
		return (false);
	if (RefString.indexOf (InString, 0)==-1) 
		return (false);
	return (true);
}

function putProduct(InString) {
OutString="";
for (Count=0; Count < InString.length; Count++)  {
		TempChar=InString.substring (Count, Count+1);
		if (!isCharHere(TempChar,"xXeslcap") || (Count == 0) )
			{OutString=OutString+TempChar}
		else 
			{
			if (isNumberChar(InString.substring(Count-1,Count)))
				{OutString=OutString+"*"+TempChar}
			else OutString=OutString+TempChar
			}
	}
	return (OutString);
}


function powFix2(InString) {
	// ****Replaces one "^" by "pow"

	theString = InString;
	var Length = theString.length;
	outString = theString; 
		// in case nothing happens
	
		// Look for wedge
	var looking = true;
	for (Count=0; Count < Length; Count++)  
		{
		if (looking)
			{
			TempChar=theString.substring (Count, Count+1);
			if (TempChar == "^")
				{
				looking = false;
				rightStr = theString.substring (Count+1,Length);
				leftStr = theString.substring (0,Count);
				// deal with right-hand string
				Aray = breakApart(rightStr);
				Arg2 = Aray[1];
				rightRest = Aray[2];
			
				backString = reverse(leftStr);
				Aray = breakApart(backString);
				Arg1 = reverse(Aray[1]);
				leftRest = reverse(Aray[2]);
				outString = leftRest+"pow("+Arg1+","+Arg2+")"+rightRest;
				
				} // end hif hit a wedge

			} // end of looking for a wedge
		} // end of loop

// ****** testing *******
// document.Extra.diagnostics.value += outString + cr;
// ***** end testing *****

return (outString);

} // end of powFix2


function powCheck(InString) {
	// ****checks for ^
	
	theString = InString;
	var Length = theString.length;
	
	// Look for wedge
	var found = false;
	for (Count=0; Count < Length; Count++)  
		{
		TempChar=theString.substring (Count, Count+1);
		if (TempChar == "^")
			{
			found = true;
			} // end if hit a wedge
		} // end of looking for a wedge
	return(found);


} // end of powCheck


function calc(xVal, theFunction){
	fractionMode = false;
	var num = calc.arguments[0];

	// first get the function info
	for (var k = 1; k <= 1; k++)
	{
		theFunction = myParse(theFunction);
		// alert(theFunction);
		var accuracydig = 5;
		if (!looksLikeANumber(accuracydig)) {
			//document.theForm.feedback.value = "You must enter a value for the accuracy" + br + "in the range 1-13"; okToRoll = false; break;
			alert("No accuracy entered!");
		}
		else numSigDig = eval(accuracydig);
	} 
	if (okToRoll) {	
		with (Math)
		{
			var x1 = xVal;
			theString = replaceChar(theString,"X","A");
			theString = replaceChar(theString,"x","A");
		
			A = eval(x1);			// global
// ****** testing *******
// document.Extra.diagnostics.value += "about to evaluate the string " + theString + cr;
// ***** end testing *****
			ret = eval(theString);

// ****** testing *******
// document.Extra.diagnostics.value += "about to round sigdig " + ret + cr;
// ***** end testing *****
	
// ****** testing *******
// alert(ret);
// ***** end testing *****	
			if (isNaN(ret) || (ret > infinity) || (ret < -infinity) ) {
				if (browserName == "N") {
					alert("undefined");
				}
				else {
					alert("undefined");
				}
			}
			else {
				if (fractionMode) {
					retStr = toFrac(ret,99,.0000001);
				}
				else 
					retStr = roundSigDig(ret, numSigDig-1).toString();
				
				return (retStr);
			}
		} // end with
	} // if oktoroll
}



function verifyunits(){
	if(!document.unitform.radiobutton[0].checked && !document.unitform.radiobutton[1].checked && !document.unitform.radiobutton[2].checked){
		alert("You must select the unit of measurement.");
		return false;
	}else{
	return true;
	}
}

function convertnumbers(number,from,to){
//1 Inches
//2 Centimeters
//3 Feet
	if(from==to)
		return number
	if(from==1 && to==2)
		return number*2.54
	if(from==1 && to==3)
		return number/12
	if(from==2 && to==1)
		return number/2.54
	if(from==2 && to==3)
		return number/2.54/12
	if(from==3 && to==1)
		return number*12
	if(from==3 && to==2)
		return number*12*2.54
}

function roundnumber(number){
	if (number == "N/A") 
		return number;
	if (number.length <= 0) 
		return "N/A";
	return (Math.round(10 * number))/10;
}

function showconversion(unit){
	if(document.distanceform.hiddendistance.value!=0){
		getdistancevalues(document.distanceform.hiddendistance.value,document.distanceform.hiddenunit.value);
		setdistanceform(unit);
	}
	if(document.imageform.hiddenwidth.value!=0 || document.imageform.hiddendiam.value!=0){
		getImageUnit(document.imageform.hiddenwidth.value,document.imageform.hiddendiam.value,document.imageform.hiddenunit.value);
		setImageUnit(unit);
	}
}

function changeDimensions(dimensions) {

	if (dimensions==1)
		useDimensions="4:3";
	else
		useDimensions="16:9";
		
	if(document.distanceform.hiddendistance.value!=0){
		getdistancevalues(document.distanceform.hiddendistance.value,document.distanceform.hiddenunit.value);
		setdistanceform(document.distanceform.hiddenunit.value);
	}
	if(document.imageform.hiddenwidth.value!=0 || document.imageform.hiddendiam.value!=0){
		getImageUnit(document.imageform.hiddenwidth.value,document.imageform.hiddendiam.value,document.imageform.hiddenunit.value);
		setImageUnit(document.imageform.hiddenunit.value);
	}
		
}

function processFunc(theInput, theFunc) {
	if (theFunc == "N/A")
		return "N/A";
	if (theFunc.length <= 0) 
		return "N/A";
	return (calc(theInput,theFunc));
}

function getdistancevalues(distance,unit){
	Distance=convertnumbers(distance,unit,1)

	var fmin = "";
	var fmax = "";
	
	if (!useFormulae) {
		ImageWidthMax=LCDWidth*Distance/FocalMin;
		ImageWidthMin=LCDWidth*Distance/FocalMax;
		
		if (useDimensions=="4:3") {
			ImageDiamMax=ImageWidthMax/0.8;
			ImageDiamMin=ImageWidthMin/0.8;
			if ((fDIVOMN_43.length > 0) && (fDIVOMX_43.length > 0)) {
				fmin = fDIVOMN_43.replace(new RegExp(/b/g), ImageDiamMin);
				fmax = fDIVOMX_43.replace(new RegExp(/b/g), ImageDiamMax);
			}
			VerticalOffsetMin = processFunc(ImageDiamMin, fmin);
			VerticalOffsetMax = processFunc(ImageDiamMax, fmax);
		
		}
		else {
			ImageDiamMax=ImageWidthMax/0.8*0.917877987;
			ImageDiamMin=ImageWidthMin/0.8*0.917877987;

			if ((fDIVOMN_169.length > 0) && (fDIVOMX_169.length > 0)) {
				fmin = fDIVOMN_169.replace(new RegExp(/b/g), ImageDiamMin);
				fmax = fDIVOMX_169.replace(new RegExp(/b/g), ImageDiamMax);
			}
			VerticalOffsetMin = processFunc(ImageDiamMin, fmin);
			VerticalOffsetMax = processFunc(ImageDiamMax, fmax);
		}

		// now process the vertical offset
		// vertical offset is always represented in a function
		// first check to see if we are NOT using formulae, then we need to replace "b" in the function
		// with diagonal values
		
		
		
	}
	else {
		if (useDimensions=="4:3") {
			ImageWidthMin = processFunc(Distance,fDIWMN_43);
			ImageWidthMax = processFunc(Distance,fDIWMX_43);
			ImageDiamMin = processFunc(Distance,fDIGMN_43);
			ImageDiamMax = processFunc(Distance,fDIGMX_43);
			if ((fDIVOMN_43.length > 0) && (fDIVOMX_43.length > 0)) {
				fmin = fDIVOMN_43.replace(new RegExp(/b/g), ImageDiamMin);
				fmax = fDIVOMX_43.replace(new RegExp(/b/g), ImageDiamMax);
			}
			VerticalOffsetMin = processFunc(ImageDiamMin, fmin);
			VerticalOffsetMax = processFunc(ImageDiamMax, fmax);
		}
		else {
			ImageWidthMin = processFunc(Distance,fDIWMN_169);
			ImageWidthMax = processFunc(Distance,fDIWMX_169);
			ImageDiamMin = processFunc(Distance,fDIGMN_169);
			ImageDiamMax = processFunc(Distance,fDIGMX_169);
			if ((fDIVOMN_169.length > 0) && (fDIVOMX_169.length > 0)) {
				fmin = fDIVOMN_169.replace(new RegExp(/b/g), ImageDiamMin);
				fmax = fDIVOMX_169.replace(new RegExp(/b/g), ImageDiamMax);
			}
			VerticalOffsetMin = processFunc(ImageDiamMin, fmin);
			VerticalOffsetMax = processFunc(ImageDiamMax, fmax);
		}

	}
}

function setdistanceform(unit){
	document.distanceform.textfield.value=roundnumber(convertnumbers(document.distanceform.hiddendistance.value,document.distanceform.hiddenunit.value,unit));
	document.distanceform.textfield2.value=roundnumber(convertnumbers(ImageWidthMax,1,unit));
	document.distanceform.textfield3.value=roundnumber(convertnumbers(ImageDiamMax,1,unit));
	document.distanceform.textfield4.value=roundnumber(convertnumbers(ImageWidthMin,1,unit));
	document.distanceform.textfield5.value=roundnumber(convertnumbers(ImageDiamMin,1,unit));
	document.distanceform.textfield10.value=roundnumber(convertnumbers(VerticalOffsetMin,1,unit));
	document.distanceform.textfield11.value=roundnumber(convertnumbers(VerticalOffsetMax,1,unit));
	
}

function processdistance(theForm){
	if (verifyunits()){
		if (!isNaN(theForm.textfield.value) && theForm.textfield.value>=0){
			theForm.hiddendistance.value=theForm.textfield.value;
			if (document.unitform.radiobutton[0].checked){
			//Inches
				getdistancevalues(theForm.textfield.value,1);
				theForm.hiddenunit.value=1;
				setdistanceform(1);
			}else if (document.unitform.radiobutton[1].checked){
			//Centimeters
				getdistancevalues(theForm.textfield.value,2);
				theForm.hiddenunit.value=2;
				setdistanceform(2);			
			}else if (document.unitform.radiobutton[2].checked){
			//Feet
				getdistancevalues(theForm.textfield.value,3);
				theForm.hiddenunit.value=3;
				setdistanceform(3);	
			}
		
		} else
		alert("You must enter a positive number into the Distance field.")
	}
	//var resultFunc = calc(theForm.textfield.value,myFunction);
	//alert(resultFunc);
}

function getImageUnit(width,diam,unit){
	var msg;
    var fmin = "";
    var fmax = "";
    
	
	if (!useFormulae) {
		
		if(theField==0){
			ImageDiam=convertnumbers(diam,unit,1);
			if (useDimensions=="4:3") {
				ImageWidth=ImageDiam*0.8;
				VerticalOffsetMin = processFunc(ImageDiam, fGVOMN_43);
				VerticalOffsetMax = processFunc(ImageDiam, fGVOMX_43);
			}
			else {
				ImageWidth=ImageDiam*0.8/0.917877987;
				VerticalOffsetMin = processFunc(ImageDiam, fGVOMN_169);
				VerticalOffsetMax = processFunc(ImageDiam, fGVOMX_169);
			}
		}
		else if(theField==1){
			ImageWidth=convertnumbers(width,unit,1);
			if (useDimensions=="4:3") {
				ImageDiam=ImageWidth/0.8;
                if ((fWVOMN_43.length > 0) && (fWVOMX_43.length > 0)) {
                    fmin = fWVOMN_43.replace(new RegExp(/b/g), ImageDiam);
                    fmax = fWVOMX_43.replace(new RegExp(/b/g), ImageDiam);
                }
				VerticalOffsetMin = processFunc(ImageDiam, fmin);
				VerticalOffsetMax = processFunc(ImageDiam, fmax);
			}
			else {
				ImageDiam=ImageWidth/0.8*0.917877987;
                if ((fWVOMN_169.length > 0) && (fWVOMX_169.length > 0)) {
                    fmin = fWVOMN_169.replace(new RegExp(/b/g), ImageDiam);
                    fmax = fWVOMX_169.replace(new RegExp(/b/g), ImageDiam);
                }
				VerticalOffsetMin = processFunc(ImageDiam, fmin);
				VerticalOffsetMax = processFunc(ImageDiam, fmax);
			}
		}
		DistanceMax=(FocalMax*ImageWidth)/LCDWidth;
		DistanceMin=(FocalMin*ImageWidth)/LCDWidth;
		
		
	}
	else {
		if(theField==0){
			ImageDiam=convertnumbers(diam,unit,1);
			if (useDimensions=="4:3") {
				ImageWidth=processFunc(ImageDiam,fGIW_43);
				DistanceMax=processFunc(ImageDiam,fGDMX_43);
				DistanceMin=processFunc(ImageDiam,fGDMN_43);
				VerticalOffsetMin = processFunc(ImageDiam, fGVOMN_43);
				VerticalOffsetMax = processFunc(ImageDiam, fGVOMX_43);
				//msg = "" + ImageDiam + ":" + fGDMX_43 + ":" + fGDMN_43;
				//alert (msg);
			}
			else {
				ImageWidth=processFunc(ImageDiam,fGIW_169);
				DistanceMax=processFunc(ImageDiam,fGDMX_169);
				DistanceMin=processFunc(ImageDiam,fGDMN_169);
				VerticalOffsetMin = processFunc(ImageDiam, fGVOMN_169);
				VerticalOffsetMax = processFunc(ImageDiam, fGVOMX_169);
				//msg = "" + ImageDiam + ":" + fGDMX_169 + ":" + fGDMN_169;
				//alert (msg);
			}
		}
		else if(theField==1){
			ImageWidth=convertnumbers(width,unit,1);
			if (useDimensions=="4:3") {
				ImageDiam=processFunc(ImageWidth,fWIG_43);
				DistanceMax=processFunc(ImageWidth,fWDMX_43);
				DistanceMin=processFunc(ImageWidth,fWDMN_43);
                if ((fWVOMN_43.length > 0) && (fWVOMX_43.length > 0)) {
                    fmin = fWVOMN_43.replace(new RegExp(/b/g), ImageDiam);
                    fmax = fWVOMX_43.replace(new RegExp(/b/g), ImageDiam);
                }
				VerticalOffsetMin = processFunc(ImageDiam, fmin);
				VerticalOffsetMax = processFunc(ImageDiam, fmax);
				//msg = "" + ImageWidth + ":" + fWDMX_43 + ":" + fWDMN_43;
				//alert (msg);
			}
			else {
				ImageDiam=processFunc(ImageWidth,fWIG_169);
				DistanceMax=processFunc(ImageWidth,fWDMX_169);
				DistanceMin=processFunc(ImageWidth,fWDMN_169);
                if ((fWVOMN_169.length > 0) && (fWVOMX_169.length > 0)) {
                    fmin = fWVOMN_169.replace(new RegExp(/b/g), ImageDiam);
                    fmax = fWVOMX_169.replace(new RegExp(/b/g), ImageDiam);
                }
				VerticalOffsetMin = processFunc(ImageDiam, fmin);
				VerticalOffsetMax = processFunc(ImageDiam, fmax);
				//msg = "" + ImageWidth + ":" + fWDMX_169 + ":" + fWDMN_169;
				//alert (msg);
			}
		}
	}
}

function setImageUnit(unit){
	if (theField==1){
		document.imageform.textfield8.value=roundnumber(convertnumbers(document.imageform.hiddenwidth.value,document.imageform.hiddenunit.value,unit));
		document.imageform.textfield9.value=roundnumber(convertnumbers(ImageDiam,1,unit));
	}
	else if (theField==0){
		document.imageform.textfield9.value=roundnumber(convertnumbers(document.imageform.hiddendiam.value,document.imageform.hiddenunit.value,unit));
		document.imageform.textfield8.value=roundnumber(convertnumbers(ImageWidth,1,unit));
	}else{
		return false;}
	document.imageform.textfield7.value=roundnumber(convertnumbers(DistanceMax,1,unit));
	document.imageform.textfield6.value=roundnumber(convertnumbers(DistanceMin,1,unit));
	document.imageform.textfield12.value=roundnumber(convertnumbers(VerticalOffsetMin,1,unit));
	document.imageform.textfield13.value=roundnumber(convertnumbers(VerticalOffsetMax,1,unit));
}

function getimagevalues(width,diam,unit,fieldSelected){
	var msg;
	var fmin = "";
	var fmax = "";
	
	if (!useFormulae) {
		if(fieldSelected==0){
			ImageDiam=convertnumbers(diam,unit,1);
			if (useDimensions=="4:3") {
				ImageWidth=ImageDiam*0.8;
                //alert (fGVOMN_43);
                //alert (fGVOMX_43);
				//if ((fGVOMN_43.length > 0) && (fGVOMX_43.length > 0)) {
				//	fmin = fGVOMN_43.replace(new RegExp(/b/g), ImageDiam);
				//	fmax = fGVOMX_43.replace(new RegExp(/b/g), ImageDiam);
				//}
                //alert (fmin);
                //alert (fmax);
				VerticalOffsetMin = processFunc(ImageDiam, fGVOMN_43);
				VerticalOffsetMax = processFunc(ImageDiam, fGVOMX_43);
			}
			else {
				ImageWidth=ImageDiam*0.8/0.917877987;
				//if ((fGVOMN_169.length > 0) && (fGVOMX_169.length > 0)) {
				//	fmin = fGVOMN_169.replace(new RegExp(/b/g), ImageDiam);
				//	fmax = fGVOMX_169.replace(new RegExp(/b/g), ImageDiam);
				//}
				VerticalOffsetMin = processFunc(ImageDiam, fGVOMN_169);
				VerticalOffsetMax = processFunc(ImageDiam, fGVOMX_169);
			}
		}
		else if(fieldSelected==1){
			ImageWidth=convertnumbers(width,unit,1);
			if (useDimensions=="4:3") {
				ImageDiam=ImageWidth/0.8;
				if ((fWVOMN_43.length > 0) && (fWVOMX_43.length > 0)) {
					fmin = fWVOMN_43.replace(new RegExp(/b/g), ImageDiam);
					fmax = fWVOMX_43.replace(new RegExp(/b/g), ImageDiam);
				}
				VerticalOffsetMin = processFunc(ImageDiam, fmin);
				VerticalOffsetMax = processFunc(ImageDiam, fmax);
			}
			else {
				ImageDiam=ImageWidth/0.8*0.917877987;
				if ((fWVOMN_169.length > 0) && (fWVOMX_169.length > 0)) {
					fmin = fWVOMN_169.replace(new RegExp(/b/g), ImageDiam);
					fmax = fWVOMX_169.replace(new RegExp(/b/g), ImageDiam);
				}
				VerticalOffsetMin = processFunc(ImageDiam, fmin);
				VerticalOffsetMax = processFunc(ImageDiam, fmax);
			}
		}
		DistanceMax=(FocalMax*ImageWidth)/LCDWidth;
		DistanceMin=(FocalMin*ImageWidth)/LCDWidth;
	}
	else {
		if(fieldSelected==0){
			ImageDiam=convertnumbers(diam,unit,1);
			if (useDimensions=="4:3") {
				ImageWidth=processFunc(ImageDiam,fGIW_43);
				DistanceMax=processFunc(ImageDiam,fGDMX_43);
				DistanceMin=processFunc(ImageDiam,fGDMN_43);
				//if ((fGVOMN_43.length > 0) && (fGVOMX_43.length > 0)) {
				//	fmin = fGVOMN_43.replace(new RegExp(/b/g), ImageDiam);
				//	fmax = fGVOMX_43.replace(new RegExp(/b/g), ImageDiam);
				//}
				VerticalOffsetMin = processFunc(ImageDiam, fGVOMN_43);
				VerticalOffsetMax = processFunc(ImageDiam, fGVOMX_43);
				//msg = "" + ImageDiam + ":" + fGDMX_43 + ":" + fGDMN_43;
				//alert (msg);
			}
			else {
				ImageWidth=processFunc(ImageDiam,fGIW_169);
				DistanceMax=processFunc(ImageDiam,fGDMX_169);
				DistanceMin=processFunc(ImageDiam,fGDMN_169);
				//if ((fGVOMN_169.length > 0) && (fGVOMX_169.length > 0)) {
				//	fmin = fGVOMN_169.replace(new RegExp(/b/g), ImageDiam);
				//	fmax = fGVOMX_169.replace(new RegExp(/b/g), ImageDiam);
				//}
				VerticalOffsetMin = processFunc(ImageDiam, fGVOMN_169);
				VerticalOffsetMax = processFunc(ImageDiam, fGVOMX_169);
				//msg = "" + ImageDiam + ":" + fGDMX_169 + ":" + fGDMN_169;
				//alert (msg);
			}
		}
		else if(fieldSelected==1){
			ImageWidth=convertnumbers(width,unit,1);
			if (useDimensions=="4:3") {
				ImageDiam=processFunc(ImageWidth,fWIG_43);
				DistanceMax=processFunc(ImageWidth,fWDMX_43);
				DistanceMin=processFunc(ImageWidth,fWDMN_43);
				if ((fWVOMN_43.length > 0) && (fWVOMX_43.length > 0)) {
					fmin = fWVOMN_43.replace(new RegExp(/b/g), ImageDiam);
					fmax = fWVOMX_43.replace(new RegExp(/b/g), ImageDiam);
				}
				VerticalOffsetMin = processFunc(ImageDiam, fmin);
				VerticalOffsetMax = processFunc(ImageDiam, fmax);
				//msg = "" + ImageWidth + ":" + fWDMX_43 + ":" + fWDMN_43;
				//alert (msg);
				
			}
			else {
				ImageDiam=processFunc(ImageWidth,fWIG_169);
				DistanceMax=processFunc(ImageWidth,fWDMX_169);
				DistanceMin=processFunc(ImageWidth,fWDMN_169);
				if ((fWVOMN_169.length > 0) && (fWVOMX_169.length > 0)) {
					fmin = fWVOMN_169.replace(new RegExp(/b/g), ImageDiam);
					fmax = fWVOMX_169.replace(new RegExp(/b/g), ImageDiam);
				}
				VerticalOffsetMin = processFunc(ImageDiam, fmin);
				VerticalOffsetMax = processFunc(ImageDiam, fmax);
				//msg = "" + ImageWidth + ":" + fWDMX_169 + ":" + fWDMN_169;
				//alert (msg);
			}
		}
	}
}

function setimageform(unit,fieldSelected){
	if (fieldSelected==1){
		document.imageform.textfield8.value=roundnumber(convertnumbers(document.imageform.hiddenwidth.value,document.imageform.hiddenunit.value,unit));
		document.imageform.textfield9.value=roundnumber(convertnumbers(ImageDiam,1,unit));
	}
	else if (fieldSelected==0){
		document.imageform.textfield9.value=roundnumber(convertnumbers(document.imageform.hiddendiam.value,document.imageform.hiddenunit.value,unit));
		document.imageform.textfield8.value=roundnumber(convertnumbers(ImageWidth,1,unit));
	}else
		return false;
	document.imageform.textfield7.value=roundnumber(convertnumbers(DistanceMax,1,unit));
	document.imageform.textfield6.value=roundnumber(convertnumbers(DistanceMin,1,unit));
	document.imageform.textfield12.value=roundnumber(convertnumbers(VerticalOffsetMin,1,unit));
	document.imageform.textfield13.value=roundnumber(convertnumbers(VerticalOffsetMax,1,unit));
	
}

function checkValue(fieldSelected){
	if (fieldSelected==0){
		if(document.imageform.textfield9.value<0 || isNaN(document.imageform.textfield9.value)){
			return false;
		} else {return true;}
	} else if (fieldSelected==1){
		if(document.imageform.textfield8.value<0 || isNaN(document.imageform.textfield8.value)){
			return false;
		} else {return true;}
	} 
}


function processimage(theForm,theField){
	if (verifyunits()){
		if(checkValue(theField)){
			this.theField = theField;
			theForm.hiddendiam.value=theForm.textfield9.value;
			theForm.hiddenwidth.value=theForm.textfield8.value;
			if (document.unitform.radiobutton[0].checked){
			//Inches
				getimagevalues(theForm.textfield8.value,theForm.textfield9.value,1,theField);
				theForm.hiddenunit.value=1;
				setimageform(1,theField);
			}else if(document.unitform.radiobutton[1].checked){
			//Centimeters
				getimagevalues(theForm.textfield8.value,theForm.textfield9.value,2,theField);
				theForm.hiddenunit.value=2;
				setimageform(2,theField);		
			}else if(document.unitform.radiobutton[2].checked){
			//Feet
				getimagevalues(theForm.textfield8.value,theForm.textfield9.value,3,theField);
				theForm.hiddenunit.value=3;
				setimageform(3,theField);			
			}
		} else if (theField != 0 && theField!= 1){
		} else {
		alert("You must enter a positive number into this field.")
		}
	}
}

