//	The following scripts (doSelectFormat() and doDepthOfField() were modified from source found 
//	on The JavaScript Source!! http://javascript.internet.com -->
//	Original script:  alistair@silverlight.co.uk -->
//	Web Site:  http://www.silverlight.co.uk -->
//	Modified script: Copyright 2005 Don Fleming. All Rights Reserved. http://www.dofmaster.com -->

//Without limiting the rights under copyright reserved above,
//no part of this script may be reproduced, stored in or
//introduced into a retrieval system, or transmitted, in any form
//or by any means (electronic, mechanical, photocopying,
//recording or otherwise), without the prior permission of the
//copyright owner.
//
//The uploading and distribution of this script via the Internet or
//via any other means without the permission of the copyright
//owner is illegal and punishable by law. Please do not
//participate in or encourage electronic piracy of copyrighted
//materials. Your support of the author’s rights is appreciated.

// function to calculate depth of field numbers
// when selections change





function doSelectFormat()
{
	
	// calcilating airy disk value
	var apertureAd = document.getElementById("aperture").value;
	//var ADisk = 2.43932*0.000555*apertureAd
	var ADisk=Math.round((2.43932*0.000555*apertureAd)*1000)/1000
	//document.getElementById("airydisk").value = ADisk;
	
	// put the coc on the form
	CoC = document.getElementById("format").value;
	CoC = CoC*1.0;
	document.getElementById("cocused").value = CoC;
	
	
	// checking to see if airy disk value is greater that Coc value
	if (ADisk < CoC)
		CoC = CoC;
	else
		CoC = ADisk;
	
	// calculate
	doDepthOfField();
}

// Calculate all depth of field numbers
function doDepthOfField() 
{
	// get input data from the form
	
	// calcilating airy disk value
	var apertureAd = document.getElementById("aperture").value;
	var ADisk=Math.round((2.43932*0.000555*apertureAd)*1000)/1000
	var distance = document.getElementById("distance").value;
	var CoCT = document.getElementById("format").value;
	
	// checking to see if airy disk value is greater that Coc value
	if (ADisk < CoCT)
		CoC = CoCT;
	else
		CoC = ADisk;
	
	var aperture = document.getElementById("aperture").value;
	var focal = document.getElementById("focal").value;
	var units = document.getElementById("units").value;


	// ensure that we have numbers in the variables
	focal = focal*1.0; 
	aperture = aperture*1.0;
	CoC = CoC*1.0;
	units = units*1.0;

	// check if distance input is OK
	var bCalcDistance = true;
	if (isNaN(distance) || distance <= 0)  
		bCalcDistance = false;
	
	// calculate hyperfocal and near distance
	var hyperFocal = (focal * focal) / (aperture * CoC) + focal;
		
	var dofNear = 0.0;
	var dofFar = 0.0;
	var dofTotal = 0.0;
	var dofNearPercent = 0.0
	var dofFarPercent = 0.0
	if (bCalcDistance)
	{
		distance = distance*1000*units; // convert to millimeters
		dofNear = ((hyperFocal - focal) * distance) / (hyperFocal + distance - (2*focal));
	
		// Prevent 'divide by zero' when calculating far distance.
		if ( (hyperFocal - distance) <= 0.00001)
			dofFar = 10000000.0; // set infinity at 10000m
		else
			dofFar = ((hyperFocal-focal) * distance) / (hyperFocal - distance);

		// Calculate percentage of DOF in front and behind the subject.
		dofNearPercent = (distance - dofNear)/(dofFar-dofNear) * 100.0;
		dofFarPercent = (dofFar - distance)/(dofFar-dofNear) * 100.0;

		// Convert depth of field numbers to proper units
		dofNear = dofNear / 1000.0 / units;
		dofFar  = dofFar / 1000.0 / units;
		dofTotal = dofFar - dofNear;
		distance = distance / 1000.0 / units;
	}		

	// convert hyperfocal distance to proper units
	hyperFocal = hyperFocal / 1000.0 / units;
		
	// set the units string
	if (units > 0.4)
		unitsString = " m";
	else if (units < 0.02)
		unitsString = " cm";
	else if (units < 0.1)
		unitsString = " in";
	else
	  	unitsString = " ft";
		
	// transfer values to the form
	if (hyperFocal < 10.0)
		document.getElementById("hyperFocal").value  = Math.round(hyperFocal*100)/100 + unitsString;
	else
		document.getElementById("hyperFocal").value  = Math.round(hyperFocal*10)/10 + unitsString;
		
	if (bCalcDistance)
	{
		if (dofNear < 10.0)
			document.getElementById("dofNear").value = Math.round(dofNear*100)/100 + unitsString;
		else
  			document.getElementById("dofNear").value = Math.round(dofNear*10)/10 + unitsString;
  
		if ( dofFar < 10000.0)
		{	
			if (dofFar < 10.0)
		      	document.getElementById("dofFar").value  = Math.round(dofFar*100)/100 + unitsString;
     		else
      			document.getElementById("dofFar").value  = Math.round(dofFar*10)/10 + unitsString;
				
			if (dofTotal < 10.0)
			{
      			document.getElementById("dofTotal").value  = Math.round(dofTotal*100)/100 + unitsString;
				document.getElementById("dofFront").value = Math.round((distance-dofNear)*100)/100 + unitsString +
											"\t(" + Math.round(dofNearPercent) + "%)";
				document.getElementById("dofRear").value = Math.round((dofFar - distance)*100)/100 + unitsString +
											"\t(" + Math.round(dofFarPercent)  +  "%)";
			}
     		else
			{
      			document.getElementById("dofTotal").value  = Math.round(dofTotal*10)/10 + unitsString;
				document.getElementById("dofFront").value = Math.round((distance-dofNear)*10)/10 + unitsString +
				              "\t(" + Math.round(dofNearPercent) + "%)";
				document.getElementById("dofRear").value = Math.round((dofFar - distance)*10)/10 + unitsString +
											"\t(" + Math.round(dofFarPercent)  +  "%)";
			}
		}
		else
		{
    		document.getElementById("dofFar").value  = "Infinity";
    		document.getElementById("dofTotal").value  = "Infinite";
			document.getElementById("dofFront").value = Math.round((distance-dofNear)*10)/10 + unitsString;
			document.getElementById("dofRear").value = "Infinite";
		}
	}
	else
	{
		document.getElementById("dofFar").value = " ";
  		document.getElementById("dofNear").value = " ";
  		document.getElementById("dofTotal").value = " ";
		document.getElementById("dofFront").value = " ";
		document.getElementById("dofRear").value = " ";
	}
	
	//modification to calculate diffraction
	airGreen = Math.round((2.43932*0.000555*apertureAd)*1000)/1000
	//airRed = 0.0017075*aperture;
	airRed = Math.round((2.43932*0.000700*apertureAd)*1000)/1000
	airBlue = Math.round((2.43932*0.000400*apertureAd)*1000)/1000
	
	if (airGreen > CoCT)
	   	document.getElementById("difGreen").value  = "YES";
    else
    	document.getElementById("difGreen").value  = "NO";
		
	if (airRed > CoCT)
	   	document.getElementById("difRed").value  = "YES";
    else
    	document.getElementById("difRed").value  = "NO";
		
	if (airBlue > CoCT)
	   	document.getElementById("difBlue").value  = "YES";
    else
    	document.getElementById("difBlue").value  = "NO";

	// set the CoC value on the form	
	//document.getElementById("airydisk").value = ADisk + " mm"
	document.getElementById("cocused").value = CoCT + " mm"
}

