// JavaScript Document

var pxmm=1.5;	// pixels per mm
var uni=1;	// unit of length (mm)
var frameSize;
var jgn;
var jgc;
var jgdim=250;
var null_BFD; // CGH back focal distance
var null_width; // width of CGH null aperture
var null_height; // height of CGH null aperture
var null_round;
var null_mode;
var null_axis_dctr=0;
var cyl_radius;
var cyl_width;
var cyl_height;
var cyl_round;
var bfd;		// signed distance CGH face to line focus (real or virtual)
var radius;	// signed distance from cylinder to line focus (= cylinder radius)
var sag;	// cylinder sag at cyl_height
var footprint_height;	// footprint height on cylinder
var pupil_height;	// height of cylinder pupil on CGH
var v_mag;  // ratio of image height to object height
var iscatseye;
submitOK="true";
 
function draw_null() {
	// canvas is jgdim x jgdim
	var xc = jgdim/2;
	var yc = jgdim/2;
	var hw;	// frame half width

  jgn.clear();

  jgn.setColor("#ff0000"); // red aperture
	jgn.setStroke(1);
	if(null_round)
	  jgn.fillEllipse(Math.round(xc-null_width*pxmm/2), Math.round(yc-null_height*pxmm/2), Math.round(null_width*pxmm), Math.round(null_height*pxmm));
	else jgn.fillRect(Math.round(xc-null_width*pxmm/2), Math.round(yc-null_height*pxmm/2), Math.round(null_width*pxmm), Math.round(null_height*pxmm));
	
  jgn.setColor("#aaaaaa");	// gray CGH axis
	jgn.setStroke(Stroke.DOTTED);
  jgn.drawLine(Math.round(xc-null_width*pxmm/2),Math.round(yc-null_axis_dctr*pxmm),Math.round(xc+null_width*pxmm/2),Math.round(yc-null_axis_dctr*pxmm));

	jgn.setStroke(1);
	jgn.setColor("#aaaaaa");	// gray CGH frame
  if(frameSize == 50) {	// 50R frame
	  hw = 32.5;
	  jgn.drawEllipse(Math.round(xc-24*pxmm),Math.round(yc-24*pxmm),Math.round(48*pxmm),Math.round(48*pxmm));
	  jgn.drawRect(Math.round(xc-hw*pxmm),Math.round(yc-47.5*pxmm),Math.round(2*hw*pxmm),Math.round(80*pxmm));
	  jgn.drawEllipse(Math.round(xc-30*pxmm),Math.round(yc-30*pxmm),Math.round(10*pxmm),Math.round(10*pxmm));
	  jgn.drawEllipse(Math.round(xc-30*pxmm),Math.round(yc+20*pxmm),Math.round(10*pxmm),Math.round(10*pxmm));
	  jgn.drawEllipse(Math.round(xc+20*pxmm),Math.round(yc-30*pxmm),Math.round(10*pxmm),Math.round(10*pxmm));
	  jgn.drawEllipse(Math.round(xc+20*pxmm),Math.round(yc+20*pxmm),Math.round(10*pxmm),Math.round(10*pxmm));
	}
	else if(frameSize == 120) {
		hw = 67.5;
	  jgn.drawEllipse(Math.round(xc-52.5*pxmm),Math.round(yc-52.5*pxmm),Math.round(105*pxmm),Math.round(105*pxmm));
	  jgn.drawRect(Math.round(xc-hw*pxmm),Math.round(yc-hw*pxmm),Math.round(2*hw*pxmm),Math.round(2*hw*pxmm));
	  jgn.drawEllipse(Math.round(xc-65*pxmm),Math.round(yc-65*pxmm),Math.round(10*pxmm),Math.round(10*pxmm));
	  jgn.drawEllipse(Math.round(xc-65*pxmm),Math.round(yc+55*pxmm),Math.round(10*pxmm),Math.round(10*pxmm));
	  jgn.drawEllipse(Math.round(xc+55*pxmm),Math.round(yc-65*pxmm),Math.round(10*pxmm),Math.round(10*pxmm));
	  jgn.drawEllipse(Math.round(xc+55*pxmm),Math.round(yc+55*pxmm),Math.round(10*pxmm),Math.round(10*pxmm));
	}
	else if(frameSize == 6025) { // 6025 frame
	  hw = 76.2
		jgn.drawRect(Math.round(xc-89.5*pxmm),Math.round(yc-89.5*pxmm),Math.round(32*pxmm),Math.round(32*pxmm));  // corner
		jgn.drawRect(Math.round(xc-89.5*pxmm),Math.round(yc+57.5*pxmm),Math.round(32*pxmm),Math.round(32*pxmm));  // corner
		jgn.drawRect(Math.round(xc+57.5*pxmm),Math.round(yc-89.5*pxmm),Math.round(32*pxmm),Math.round(32*pxmm));  // corner
		jgn.drawRect(Math.round(xc+57.5*pxmm),Math.round(yc+57.5*pxmm),Math.round(32*pxmm),Math.round(32*pxmm));  // corner
	  jgn.drawEllipse(Math.round(xc-87*pxmm),Math.round(yc-87*pxmm),Math.round(12*pxmm),Math.round(12*pxmm));
	  jgn.drawEllipse(Math.round(xc-87*pxmm),Math.round(yc+75*pxmm),Math.round(12*pxmm),Math.round(12*pxmm));
	  jgn.drawEllipse(Math.round(xc+75*pxmm),Math.round(yc-87*pxmm),Math.round(12*pxmm),Math.round(12*pxmm));
	  jgn.drawEllipse(Math.round(xc+75*pxmm),Math.round(yc+75*pxmm),Math.round(12*pxmm),Math.round(12*pxmm));
		jgn.setColor("#00ff00");
		jgn.drawLine(Math.round(xc-57.5*pxmm),Math.round(yc+hw*pxmm),Math.round(xc+57.5*pxmm),Math.round(yc+hw*pxmm));	// top edge
		jgn.drawLine(Math.round(xc-57.5*pxmm),Math.round(yc-hw*pxmm),Math.round(xc+57.5*pxmm),Math.round(yc-hw*pxmm));  // bottom edge
		jgn.drawLine(Math.round(xc-hw*pxmm),Math.round(yc+57.5*pxmm),Math.round(xc-hw*pxmm),Math.round(yc-57.5*pxmm));  // left edge
		jgn.drawLine(Math.round(xc+hw*pxmm),Math.round(yc+57.5*pxmm),Math.round(xc+hw*pxmm),Math.round(yc-57.5*pxmm));  // right edge
	}
		
	jgn.setColor("#000000");	// black HI/LO labels
	jgn.drawString("HI",Math.round(xc+((bfd>0)? +1: -1)*hw*pxmm-((bfd>0)? 0: 14)),Math.round(yc-4));
	jgn.drawString("LO",Math.round(xc+((bfd>0)? -1: +1)*hw*pxmm-((bfd>0)? 16: 0)),Math.round(yc-4));

  jgn.setColor("#0000ff"); // blue pupil
	jgn.setStroke(1);
	jgn.setStroke(Stroke.DOTTED);
	if(iscatseye) {
	  jgn.drawLine(Math.round(xc-cyl_width*pxmm/2),Math.round(yc-null_height*pxmm),Math.round(xc-cyl_width*pxmm/2),Math.round(yc+null_height*pxmm));
	  jgn.drawLine(Math.round(xc+cyl_width*pxmm/2),Math.round(yc-null_height*pxmm),Math.round(xc+cyl_width*pxmm/2),Math.round(yc+null_height*pxmm));
	}
	else if(isNaN(pupil_height))
	  ; // no pupil
	else if(cyl_round)
	  jgn.drawEllipse(Math.round(xc-cyl_width*pxmm/2), Math.round(yc-Math.abs(pupil_height)*pxmm/2), Math.round(Math.abs(cyl_width*pxmm)), Math.round(Math.abs(pupil_height*pxmm)));
	else jgn.drawRect(Math.round(xc-cyl_width*pxmm/2), Math.round(yc-Math.abs(pupil_height)*pxmm/2), Math.round(Math.abs(cyl_width*pxmm)), Math.round(Math.abs(pupil_height*pxmm)));
  jgn.paint();
	return;
}

function draw_cyl() {
	// canvas is jgdim x jgdim px with 2px = 1mm
	var xc = jgdim/2;
	var yc = jgdim/2;
	var hw;
	jgc.clear();

  jgc.setColor("#ff0000"); // red footprint
	jgc.setStroke(1);
	if(iscatseye)
	  jgc.drawLine(Math.round(xc-null_width*pxmm/2),Math.round(yc),Math.round(xc+null_width*pxmm/2),Math.round(yc));
	else if(null_round)
	  jgc.fillEllipse(Math.round(xc-null_width*pxmm/2), Math.round(yc-Math.abs(footprint_height)*pxmm/2), Math.round(null_width*pxmm), Math.round(Math.abs(footprint_height*pxmm)));
	else jgc.fillRect(Math.round(xc-null_width*pxmm/2), Math.round(yc-Math.abs(footprint_height)*pxmm/2), Math.round(null_width*pxmm), Math.round(Math.abs(footprint_height*pxmm)));

  jgc.setColor("#0000ff"); // blue aperture
	jgc.setStroke(1);
	if(cyl_round)
	  jgc.drawEllipse(Math.round(xc-cyl_width*pxmm/2), Math.round(yc-cyl_height*pxmm/2), Math.round(cyl_width*pxmm), Math.round(cyl_height*pxmm));
	else {
		jgc.drawRect(Math.round(xc-cyl_width*pxmm/2), Math.round(yc-cyl_height*pxmm/2), Math.round(cyl_width*pxmm), Math.round(cyl_height*pxmm));
	}
  if(2*Math.abs(radius) < cyl_height) {
		jgc.setStroke(1);
		if(cyl_round)
		  hw = (cyl_width/2)*Math.sqrt(1-4*radius*radius/(cyl_height*cyl_height));
	  else hw = cyl_width/2;
		jgc.drawLine(Math.round(xc-hw*pxmm), Math.round(yc+radius*pxmm), Math.round(xc+hw*pxmm), Math.round(yc+radius*pxmm));
		jgc.drawLine(Math.round(xc-hw*pxmm), Math.round(yc-radius*pxmm), Math.round(xc+hw*pxmm), Math.round(yc-radius*pxmm));
	}

  jgc.paint();

	return;
}

function draw_layout() {
	var axis = parseFloat(200/2)-null_axis_dctr*pxmm;
	var linefocus = Math.round(jgdim)+radius+bfd;
	var vertex = bfd-(iscatseye ? 0: 1)*radius;
	//var vertex = bfd-radius;
	var ycyl = Math.min(footprint_height,cyl_height);
	var ynull;	// rim ray height on null
	var x1;
	var y1;
	var x2;
	var y2;
	var xc = Math.max((20+radius-bfd)*pxmm,650);	// pixel x-coordinate of CGH face
	
  if(footprint_height == 0) // iscatsye=1
    ycyl = 0;
  else if(footprint_height > 0)
	  ycyl = Math.min(footprint_height/2,Math.min(Math.abs(radius),cyl_height/2));
  else ycyl = Math.max(footprint_height/2,-Math.min(Math.abs(radius),cyl_height/2));
  x2 = ((radius>0) ? +1 : -1)*(Math.abs(radius) - Math.sqrt(radius*radius-ycyl*ycyl));

  if(iscatseye)
    ynull = null_height/2;
	else ynull = bfd*Math.tan(Math.asin(ycyl/radius));

	jgl.clear();
	
  jgl.setStroke(1);
  //if(((radius > 0) || (bfd < 0)) && (bfd-radius >= 0)) {
  if(bfd-radius >= 0) {
    jgl.setColor("#ff0000"); // red message
    jgl.drawString("NULL POSITION IS VIRTUAL",200,Math.round(axis)-4);
		jgl.setStroke(Stroke.DOTTED);
	}
  jgl.setColor("#ff0000"); // red rays
	y1 = Math.round(axis+(ynull+null_axis_dctr)*pxmm);
	y2 = Math.round(axis-(ynull-null_axis_dctr)*pxmm);
	if(iscatseye) {
  	jgl.drawLine(Math.round(xc+bfd*pxmm),Math.round(axis),Math.round(xc),y1);
  	jgl.drawLine(Math.round(xc+bfd*pxmm),Math.round(axis),Math.round(xc),y2);
	}
	else {
  	jgl.drawLine(Math.round(xc+(bfd-radius+x2)*pxmm),Math.round(axis+ycyl*pxmm),Math.round(xc),y1);
  	jgl.drawLine(Math.round(xc+(bfd-radius+x2)*pxmm),Math.round(axis-ycyl*pxmm),Math.round(xc),y2);
	}
  jgl.setStroke(1);	// in case was DOTTED
	jgl.drawLine(Math.round(xc),y1,Math.round(xc+25),y1);
	jgl.drawLine(Math.round(xc),y2,Math.round(xc+25),y2);
	
	if((radius > 0) && (bfd < 0)) {	// Line focus?
		jgl.setColor("#00ff00");	// green air slit
	  jgl.drawLine(Math.round(xc+bfd*pxmm),Math.round(axis+2),Math.round(xc+bfd*pxmm),Math.round(axis+5*pxmm));
	  jgl.drawLine(Math.round(xc+bfd*pxmm),Math.round(axis-2),Math.round(xc+bfd*pxmm),Math.round(axis-5*pxmm));
	}
	
	jgl.setStroke(1);
	jgl.setColor("#aaaaaa");	// gray CGH frame
	if(frameSize == 50) { // 50R frame
	  jgl.drawRect(Math.round(xc-pxmm),Math.round(axis-47.5*pxmm),Math.round(4*pxmm),Math.round(80*pxmm));
	  jgl.setColor("#00ff00");	// green CGH
	  jgl.drawRect(Math.round(xc),Math.round(axis-25.4*pxmm),Math.round(3*pxmm),Math.round(50.8*pxmm));
	}
	else if(frameSize == 120) {
	  jgl.drawRect(Math.round(xc-2*pxmm),Math.round(axis-67.5*pxmm),Math.round(8*pxmm),Math.round(130*pxmm));
	  jgl.setColor("#00ff00");	// green CGH
	  jgl.drawRect(Math.round(xc),Math.round(axis-56.25*pxmm),Math.round(6.35*pxmm),Math.round(112.5*pxmm));
	}
	else if(frameSize == 6025) { // 6025 frame
	  jgl.setColor("#00ff00");	// green CGH
	  jgl.drawRect(Math.round(xc),Math.round(axis+(null_axis_dctr-76.2)*pxmm),Math.round(6.35*pxmm),Math.round(152.4*pxmm));
	}
	else alert("Frame size is "+frameSize);
	
	jgl.setColor("#0000ff");	// blue cylinder
	jgl.setStroke(1);
  if(bfd-radius >= 0)
	  jgl.setStroke(Stroke.DOTTED);
	x1 = Math.round(xc+(vertex+((radius>0)?0:sag)-cyl_height/10)*pxmm); // back of test cylinder
	jgl.drawLine(Math.round(xc+(vertex+sag)*pxmm),Math.round(axis+cyl_height*pxmm/2),x1,Math.round(axis+cyl_height*pxmm/2)); // bottom
	jgl.drawLine(Math.round(xc+(vertex+sag)*pxmm),Math.round(axis-cyl_height*pxmm/2),x1,Math.round(axis-cyl_height*pxmm/2)); // top
	jgl.drawLine(x1,Math.round(axis+cyl_height*pxmm/2),x1,Math.round(axis-cyl_height*pxmm/2));	// back
	if((radius > 0) && (2*radius < cyl_height)) {	// draw flats if cylinder height exceeds 2*radius
	  jgl.drawLine(Math.round(xc+(vertex+sag)*pxmm),Math.round(axis+Math.abs(radius)*pxmm),Math.round(xc+(vertex+sag)*pxmm),Math.round(axis+cyl_height*pxmm/2));
	  jgl.drawLine(Math.round(xc+(vertex+sag)*pxmm),Math.round(axis-Math.abs(radius)*pxmm),Math.round(xc+(vertex+sag)*pxmm),Math.round(axis-cyl_height*pxmm/2));
	}
	
	x1 = sag;
	y1 = -Math.min(cyl_height/2,Math.abs(radius));
	do {
	  y2 = y1+2;
		x2 = ((radius>0) ? +1 : -1)*(Math.abs(radius) - Math.sqrt(radius*radius-y2*y2));
	  jgl.drawLine(Math.round(xc+(vertex+x1)*pxmm),Math.round(axis+y1*pxmm),Math.round(xc+(vertex+x2)*pxmm),Math.round(axis+y2*pxmm));
		//alert("(x1,y1)=("+x1+","+y1+")   (x2,y1)=("+x2+","+y2+")");
		x1 = x2;
		y1 = y2;
	} while((y2 < cyl_height/2) && (y2 < Math.abs(radius)));
	
	jgl.setColor("#aaaaaa");  // gray scale legend
	jgl.drawLine(Math.round(xc),Math.round(axis+50*pxmm),Math.round(xc+vertex*pxmm),Math.round(axis+50*pxmm));
	jgl.drawLine(Math.round(xc),Math.round(axis+50*pxmm-3),Math.round(xc),Math.round(axis+50*pxmm+3));
	jgl.drawLine(Math.round(xc+vertex*pxmm),Math.round(axis+50*pxmm-3),Math.round(xc+vertex*pxmm),Math.round(axis+50*pxmm+3));
	jgl.drawString(Math.abs(Math.round(1000*vertex)/1000)+" mm",Math.round(xc+0.5*vertex*pxmm-20),Math.round(axis+50*pxmm));
							 
	
	jgl.paint();
	return;
}

function validate_null_test() {
	var Rnumber;
  cyl_radius = -uni*parseFloat(document.getElementById("cylinder_radius").value);
  if(isNaN(cyl_radius) || (cyl_radius == 0)) {
    alert("Cylinder radius must be a non-zero number.")
		submitOK="false";
	}
	
  cyl_width = uni*parseFloat(document.getElementById("cylinder_width").value);
  if(isNaN(cyl_width) || (cyl_width <= 0)) {
    alert("Cylinder width must be a positive number.");
		submitOK="false";
	}
	
  cyl_height = uni*parseFloat(document.getElementById("cylinder_height").value);
  if(isNaN(cyl_height) || (cyl_height <= 0)) {
    alert("Cylinder height must be a positive number.");
		submitOK="false";
	}
	
	radius = cyl_radius;
	if(Math.abs(radius) > cyl_height/2)
	  sag = ((radius>0) ? +1 : -1)*(Math.abs(radius) - Math.sqrt(radius*radius-cyl_height*cyl_height/4));
  else sag = radius;
	
	Rnumber = Math.max(0.5,Math.abs(radius)/cyl_height);
	document.getElementById("Rnumber").value = Math.round(100*Rnumber)/100;
	
	if(document.getElementById("cylinder_circular").checked == true)
	  cyl_round = true;
	else cyl_round = false;
	
	if(document.getElementById("diverging").checked == true) {
	  bfd = null_BFD;
    document.getElementById("null_BFD").value = -null_BFD;  // display with sign
	}
  else {
    bfd = -null_BFD;
    document.getElementById("null_BFD").value = null_BFD;  // display wi/o sign
  }
	
	iscatseye = (document.getElementById("catseye").checked == true);
	
//  if((radius < 0) && (bfd > 0)) {
//	  alert("Convex cylinders cannot be tested in diverging mode.");
//	}
	
	if(iscatseye)
	  footprint_height = 0;
	else footprint_height = Math.abs(radius*Math.sin(Math.atan(null_height/bfd)));
  document.getElementById("footprint").value=Math.round(1000*footprint_height/uni)/1000;
  document.getElementById("footprint_width").value=Math.round(1000*null_width/uni)/1000;
	
  if(iscatseye || (cyl_height >= 2*Math.abs(radius)))
	  pupil_height = "infinity"; // "&infin;";
  else pupil_height = 2*bfd*Math.tan(Math.asin(Math.min(cyl_height/2,Math.abs(radius))/radius));
	if(isNaN(pupil_height))
    document.getElementById("pupil").value = pupil_height;
  else document.getElementById("pupil").value = Math.round(1000*pupil_height/uni)/1000;
  document.getElementById("pupil_width").value = Math.round(1000*cyl_width/uni)/1000;
  document.getElementById("v_mag").value = Math.round(1000*pupil_height/cyl_height)/1000+" : 1";
	
	draw_null();
	draw_cyl();
	draw_layout();
		
	return;
}

function Select_Null() {
	
	null_axis_dctr = 0;
	
  if(window.document.forms[0].NullModel.value == 'H45F15C') {
		null_BFD=65;
		null_width=45;
		null_height=45;
		null_round=1;
		frameSize=50;
	}
  else if(window.document.forms[0].NullModel.value == 'H45F24C') {
		null_BFD=105;
		null_width=45;
		null_height=45;
		null_round=1;
		frameSize=50;
	}
  else if(window.document.forms[0].NullModel.value == 'H45F45C') {
		null_BFD=201;
		null_width=45;
		null_height=45;
		null_round=1;
		frameSize=50;
	}
  else if(window.document.forms[0].NullModel.value == 'H45F8C') {
		null_BFD=360;
		null_width=45;
		null_height=45;
		null_round=1;
		frameSize=50;
	}
  else if(window.document.forms[0].NullModel.value == 'H95F3C') {
		null_BFD=281;
		null_width=95;
		null_height=95;
		null_round=1;
		frameSize=6025;
	}
  else if(window.document.forms[0].NullModel.value == 'H95F6C') {
		null_BFD=568;
		null_width=95;
		null_height=95;
		null_round=1;
		frameSize=6025;
	}
	else if(window.document.forms[0].NullModel.value == 'H80F2C') {
		null_BFD=155;
		null_width=80;
		null_height=80;
		null_round=0;
		frameSize=6025;
	}
	else if(window.document.forms[0].NullModel.value == 'H80F3C') {
		null_BFD=236;
		null_width=80;
		null_height=80;
		null_round=0;
		frameSize=6025;
	}
	else { // if(window.document.forms[0].NullModel.value == 'Custom') {
		frameSize = 6025;
	  if((null_round) && (null_height <= 45) && (null_width <= 45))
	    frameSize = 50;
	  if(!null_round && (Math.sqrt(null_height*null_height + null_width*null_width) <= 48))
	    frameSize = 50;
	
		validate_null_test();
		null_axis_dctr = parseFloat(document.getElementById("null_axis_dctr").value);
    document.getElementById("null_width").value = null_width;
    document.getElementById("null_height").value = null_height;
	  return;
	}

	if(document.getElementById("diverging").checked == true)
    document.getElementById("null_BFD").value = -null_BFD;
  else document.getElementById("null_BFD").value = null_BFD;
  document.getElementById("null_width").value = null_width;
  document.getElementById("null_height").value = null_height;
  document.getElementById("null_axis_dctr").value = null_axis_dctr;
	if(null_round)
    document.getElementById("nullCircular").checked = true;
  else document.getElementById("nullRectangular").checked = true;
	
	validate_null_test();
	return;
}

function Custom_Null() {
  document.getElementById("Custom").selected=true;
	null_BFD = parseFloat(Math.abs(document.getElementById("null_BFD").value));
	if(isNaN(null_BFD)) {
          alert("Cylinder focal length must be a number.");
          return;
        }
        if((null_BFD < -10000) || (null_BFD > 10000)) {
          alert("Cylinder focal length is too large.");
          return;
        }
	null_width = parseFloat(document.getElementById("null_width").value);
  if(isNaN(null_width) || (null_width <= 0) || (null_width > 125)) {
    alert("Null width must be a positive number, <= 125 mm.");
    
		return;
	}
	null_height = parseFloat(document.getElementById("null_height").value);
	null_axis_dctr = parseFloat(document.getElementById("null_axis_dctr").value);
  if(isNaN(null_height) || (null_height <= 0) || (null_height > 125)) {
    alert("Null height must be a positive number, <= 125 mm.");
		return;
	}
	if(document.getElementById("nullCircular").checked == true)
	  null_round = true;
	else null_round = false;

	frameSize = 6025;
	if((null_round) && (null_height <= 45) && (null_width <= 45))
	  frameSize = 50;
	if(!null_round && (Math.sqrt(null_height*null_height + null_width*null_width) <= 50)) // corners a bit beyond 48 mm
	  frameSize = 50;
	
	var gsp = 0.63282/Math.sin(Math.atan((null_height/2)/null_BFD));	// grating spacing in microns
	if(gsp < 1.934) {
	  //alert("Grating spacing of " + Math.round(1000*gsp)/1000 + " micron violates design rules. Reducing null height.");
	  alert("Grating spacing violates design rules. Reducing null height.");
	  null_height = 2*null_BFD*Math.tan(Math.asin(0.63282/1.934)); // reduce null height to satisfy gsp < 1.934 um
	  null_height = Math.floor(10*null_height)/10;
    document.getElementById("null_height").value = null_height;
	}
	
	validate_null_test();
	return;
}

function toggle_units() {
	if(document.getElementById("Metric").checked) {
		uni = 1.0;
    document.getElementById("footprint_units").innerHTML="mm";
    document.getElementById("cyl_units").innerHTML="mm";
    document.getElementById("radius_units").innerHTML="mm";
	}
	else {
		uni = 25.4;
    document.getElementById("footprint_units").innerHTML="inches";
    document.getElementById("cyl_units").innerHTML="inches";
    document.getElementById("radius_units").innerHTML="inches";
	}
  document.getElementById("cylinder_height").value = Math.round(10000*cyl_height/uni)/10000;
	document.getElementById("cylinder_width").value = Math.round(10000*cyl_width/uni)/10000;
	document.getElementById("cylinder_radius").value = -Math.round(10000*radius/uni)/10000;
  document.getElementById("footprint").value = Math.round(1000*footprint_height/uni)/1000;
  document.getElementById("footprint_width").value = Math.round(1000*null_width/uni)/1000;
	return;
}

function parseURL() {
  var obj = new ArgumentURL();
  null_axis_dctr = 0;
  if((obj.getArgument('NullModel') == '') || (obj.getArgument('NullModel') == null) || document.layers)
    ; // nothing
  else if(obj.getArgument('NullModel') == 'H45F1.5C') {
    window.document.forms[0].NullModel.value = 'H45F1.5C';
  }
  else if(obj.getArgument('NullModel') == 'H45F2.4C') {
    window.document.forms[0].NullModel.value = 'H45F2.4C';
  }
  else if(obj.getArgument('NullModel') == 'H45F4.5C') {
    window.document.forms[0].NullModel.value = 'H45F4.5C';
  }
  else if(obj.getArgument('NullModel') == 'H45F8C') {
    window.document.forms[0].NullModel.value = 'H45F8C';
  }
  else if(obj.getArgument('NullModel') == 'H95F3C') {
    window.document.forms[0].NullModel.value = 'H95F3C';
  }
  else if(obj.getArgument('NullModel') == 'H95F6C') {
    window.document.forms[0].NullModel.value = 'H95F6C';
  }
  else if(obj.getArgument('NullModel') == 'H80F2C') {
    window.document.forms[0].NullModel.value = 'H80F2C';
  }
	else { // if(obj.getArgument('NullModel') == 'Custom') {
		window.document.forms[0].NullModel.value = 'Custom';
    if((obj.getArgument('null_BFD') == '') || (obj.getArgument('null_BFD') == null) || document.layers)
      ; // nothing
		else null_BFD = obj.getArgument('null_BFD'); 
    if((obj.getArgument('null_width') == '') || (obj.getArgument('null_width') == null) || document.layers)
      ; // nothing
		else null_width = obj.getArgument('null_width'); 
    if((obj.getArgument('null_height') == '') || (obj.getArgument('null_height') == null) || document.layers)
      ; // nothing
		else null_height = obj.getArgument('null_height');
	}
  Select_Null();

  if((obj.getArgument('cyl_width') == '') || (obj.getArgument('cyl_width') == null) || document.layers)
    ; // nothing
	else cyl_width = obj.getArgument('cyl_width'); 
  if((obj.getArgument('cyl_height') == '') || (obj.getArgument('cyl_height') == null) || document.layers)
    ; // nothing
	else cyl_height = obj.getArgument('cyl_height'); 
  if((obj.getArgument('cyl_radius') == '') || (obj.getArgument('cyl_radius') == null) || document.layers)
    ; // nothing
	else radius = obj.getArgument('cyl_radius'); 
	toggle_units();
  return;
}

function ZoomIn() {
  if(pxmm >= 3)
	  return;
  pxmm *= 2;
	draw_null();
	draw_cyl();
	draw_layout();
	return;
}

function ZoomOut() {
	if(pxmm <= 1/8)
	  return;
	pxmm /= 2;
	draw_null();
	draw_cyl();
	draw_layout();
	return;
}

function changeTitle() {
	document.getElementById("Title").innerHTML = document.getElementById("cylTitle").value;
	return;
}

function printWindow() {
  if (window.print)
    window.print();
  else alert("Sorry, your browser doesn't support this feature.\nPlease select Print from the File Menu.");
}
