function thickness(form) {
A=Math.round(eval(form.A.value));
Dbl=Math.round(eval(form.Dbl.value));
PD=Math.round(eval(form.PD.value)*10)/10;
Sph=eval(form.Sph.value);
Cyl=eval(form.Cyl.value);
Axis=Math.round(eval(form.Axis.value));

//validation of form fields

if (A<30 || A>80) {alert('The A size must be between 30mm and 80mm'); document.getElementById('A').focus();return false;}
if (Dbl<10 || Dbl>25) {alert('The Dbl size must be between 10mm and 25mm'); document.getElementById('Dbl').focus();return false;}
if (PD<40 || PD>75) {alert('The patients PD must be between 40mm and 75mm'); document.getElementById('PD').focus();return false;}
if (Sph<-20 || Sph>15) {alert('The spherical power cannot exceed +15.00D or -20.00D'); document.getElementById('Sph').focus();return false;}
if (Cyl<-8 || Cyl>8) {alert('The cylinder power cannot exceed 8.00D'); document.getElementById('Cyl').focus();return false;}
if (A<1 || A>180) {alert('The axis must be between 1 and 180 degrees'); document.getElementById('Axis').focus();return false;}

//helper functions

function toRad(deg) {
angle=deg*(Math.PI/180);
return angle;
}

function toDeg(rad) {
angle=rad*(180/Math.PI);
return angle;
}

//Transpose the Rx to minus cylinder

if (Cyl>0) {
Sph=Sph+Cyl;
Cyl=(-1)*Cyl;
if (Axis>90) {Axis=Axis-90;} else {Axis=Axis+90;}
}
document.getElementById('Sph').value=Sph.toFixed(2);
document.getElementById('Cyl').value=Cyl.toFixed(2);
document.getElementById('Axis').value=Axis;

//Lens array with index and minimum thickness

var Lens = new Array();
Lens[0]=new Array('CR-39','1.498','2.0');
Lens[1]=new Array('Triv','1.527','1.0');
Lens[2]=new Array('Poly','1.586','1.0');
Lens[3]=new Array('Hi-1.60','1.592','1.5');
Lens[4]=new Array('Hi-1.67','1.661','1.5');
Lens[5]=new Array('Su-1.70','1.700','1.5');
Lens[6]=new Array('Su-1.74','1.732','1.5');

//Step 1 - determine minimum blank size

MBS=Math.abs((A+Dbl)-PD)+A;
MBS=Math.round(MBS);

//Step 2 - determine power along the 180

Power=Sph+Cyl*Math.pow(Math.sin(toRad(Axis)),2);
Power=Math.round(Power*100)/100;


//Start looping through materials

var Thick=new Array();
var AsphThick=new Array();

var counter=0;
while (counter<=6) {

//Step 3 - determine the back curve of the lens using 3rd order theory for a point focal lens design

Index=eval(Lens[counter][1]);
minthick=eval(Lens[counter][2]);

ap=Index+2;
bp=2*37*(Math.pow(Index,2)-1)-Power*(Index+2);
cp=-2*Power*37*(Index-1)+Index*Math.pow(Power,2)+Index*Math.pow(37,2)*Math.pow(Index-1,2);
discrim=Math.pow(bp,2)-4*ap*cp;

if (discrim>=0) {Back=(-bp+Math.sqrt(discrim))/(2*ap);} else {Back=(Power/2)-((37*(Math.pow(Index,2)-1))/(Index+2));}

//Step 4 - determine the average thickness

averagethick=(Math.abs(Power)*Math.pow((MBS/2),2))/(2000*(Index-1))+minthick;
averagethick=Math.round(averagethick*100)/100;

//Step 5 - determine the front curve of the lens

Front=Math.round(Power-Back);

//Step 6 - determine the real back curve of the lens

difference=1;

while (Math.abs(difference)>0.1) {

Back2=Power-Front/(1-averagethick*Front/(Index*1000));

RadFront=((Index-1)/Front)*1000;
RadBack=Math.abs(((Index-1)/Back2)*1000);

sagFront=RadFront-Math.sqrt(Math.pow(RadFront,2)-Math.pow((MBS/2),2));
sagBack=RadBack-Math.sqrt(Math.pow(RadBack,2)-Math.pow((MBS/2),2));


if (Power>0) {thickness=sagFront+minthick-sagBack;}
if (Power<0) {thickness=sagBack+minthick-sagFront;}
if (Power==0) {thickness=minthick;}


difference=averagethick-thickness;
averagethick=thickness+(difference/2);
}

thickness=averagethick;


//Step 7 - factor in aspheric curvature

//if (Aspheric==1) {
//if (Power>0) {thickness=thickness*0.75};
//if (Power<0) {thickness=thickness*0.8};
//}

//Step 8 - display the data

Thick[counter]=Math.round(thickness*10)/10;
if (Power>0) {AsphThick[counter]=Math.round(thickness*0.75*10)/10;}
if (Power<0) {AsphThick[counter]=Math.round(thickness*0.80*10)/10;}
if (Power==0) {AsphThick[counter]=Math.round(thickness*10)/10;}

counter++;
}

//write to the screen all the thicknesses and data

document.writeln('<head><style>td {text-align:center\;} \.header td {text-decoration: underline\;}</style></head>');

if (Power>=0) {
document.writeln('<body style="background-color: black; color: white;">');
document.writeln('For a: <b>\+' + Sph.toFixed(2) + ' \-' + Cyl.toFixed(2) + ' ' + ' x ' + Axis + '</b> Lens<br />');
document.writeln('with a minimum blank size of: <b>' + MBS + 'mm</b><br /><br />');
document.writeln('<table style="color: white; padding: 5px;"><tr class="header"><td>Material</td><td>Index</td><td>Edge (min)</td><td>Center</td><td>Asph</td></tr>');
counter=0;
while (counter<=6) {
if (isNaN(Thick[counter])) {displayThick='Len';displayAsphThick='Len';} else {displayThick=Thick[counter].toFixed(1)+'mm';displayAsphThick=AsphThick[counter].toFixed(1)+'mm';}
document.writeln('<tr><td>'+ Lens[counter][0] +'</td><td>'+ Lens[counter][1] +'</td><td>'+ Lens[counter][2] +'mm</td><td>' + displayThick + '</td><td>' + displayAsphThick + '</td></tr>');
counter++;
}
}

if (Power<0) {
document.writeln('<body style="background-color: black; color: white;">');
document.writeln('For a: <b>' + Sph.toFixed(2) + ' \-' + Cyl.toFixed(2) + ' ' + ' x ' + Axis + '</b> Lens<br />');
document.writeln('with a minimum blank size of: <b>' + MBS + 'mm</b><br /><br />');
document.writeln('<table style="color: white; padding: 5px;"><tr class="header"><td>Material</td><td>Index</td><td>Edge</td><td>Asph</td><td>Center (min)</td></tr>');
counter=0;
while (counter<=6) {
if (isNaN(Thick[counter])) {displayThick='Len';displayAsphThick='Len';} else {displayThick=Thick[counter].toFixed(1)+'mm';displayAsphThick=AsphThick[counter].toFixed(1)+'mm';}
document.writeln('<tr><td>'+ Lens[counter][0] +'</td><td>'+ Lens[counter][1] +'</td><td>'+ displayThick +'</td><td>' + displayAsphThick + '</td><td>' + Lens[counter][2] + 'mm</td></tr>');
counter++;
}
}
document.writeln('</table><br /><br />');
document.writeln('<input type="button" value="Calculate Another Example" onClick="location.replace(\'thick.html\')" />');
document.writeln('<br /><a href="http://www.luzerneoptical.com">Luzerne Optical Laboratories</a>');
document.writeln('<br />(800)233-9637');
document.writeln('</body>');


return;
}

//Repopulate with default value
function replaceDefault(el) {if (el.value=="") el.value=eval(el.defaultValue);}

//Clear default values from input boxes
function clearDefault(el) {if (el.defaultValue==el.value) el.value = "";}

//Transpose
function transpose() {
Sph=eval(document.getElementById('Sph').value);
Cyl=eval(document.getElementById('Cyl').value);
Axis=eval(document.getElementById('Axis').value);

if (Cyl>0) {Sph=Sph+Cyl; Cyl=(-1)*Cyl; if (Axis>90) {Axis=Axis-90;} else {Axis=Axis+90;} }

document.getElementById('Sph').value=Sph.toFixed(2);
document.getElementById('Cyl').value=Cyl.toFixed(2);
document.getElementById('Axis').value=Math.round(Axis);
}

//Redirect to pages in app

function redir(r) {location.replace(r);}




