// Javascript copyright Sean T. McHugh
//Cambridge in Colour Photography
// http://www.cambridgeincolour.com

//----------
function tscalc0(tsform0) {
coc = 1*document.tsform0.format.options[document.tsform0.format.selectedIndex].value;
aperture = 1*document.tsform0.aperture.options[document.tsform0.aperture.selectedIndex].value;
focal = tsform0.focal0.value; // in mm
tilt = tsform0.tilt0.value; // in degrees
distance = tsform0.distance.value; // in meters

if ( isNaN(focal) || focal <= 0 ) {
alert('Please enter a positive numerical value for the focal length in mm.');
document.tsform0.focal0.focus();
document.tsform0.focal0.select();
}
else {
if ( isNaN(tilt) || tilt < 0.5 ) {
alert('Please enter a numerical value of at least 0.5 degrees for the lens tilt.');
document.tsform0.tilt0.focus();
document.tsform0.tilt0.select();
}
else {
if ( isNaN(distance) || distance < 0.3 ) {
alert('Please enter a numerical value of at least 0.3 meters for the focusing distance.');
document.tsform0.tilt0.focus();
document.tsform0.tilt0.select();
}
else {

distance = 1000*distance; // converts distance from meters to mm's
tilt = (Math.PI/180) * tilt; // converts tilt from degrees into radians

jval = 0.001 * focal / Math.sin( tilt ); // in meters
A = focal / ( 1-focal/distance );

phi = (180/Math.PI) * Math.atan( Math.sin(tilt) / ( Math.cos(tilt) - focal/A ) ); // angle of focal plane relative to unshifted position, in direction of tilt
if ( phi < 0 ) { phi = 180 + phi; } // if focal plane is rotated past ninety degrees
tsform0.fplanerot0.value = Math.round(10 * phi) / 10 + "\u00B0";
tsform0.jval.value = Math.round(10 * jval) / 10 + " m";

//calculated near and far planes of acceptable sharpness
stemp = focal / ( coc * aperture );
dnear = A / (stemp-1); // near depth of focus
dfar = A / (stemp+1); // far depth of focus

neardofangle = (180/Math.PI) * Math.atan( Math.sin(tilt) / ( Math.cos(tilt) - focal/(A+dnear) ) ); // near depth of field angle in degrees
fardofangle = (180/Math.PI) * Math.atan( Math.sin(tilt) / ( Math.cos(tilt) - focal/(A-dfar) ) ); // far depth of field angle in degrees
if ( fardofangle < 0 ) { fardofangle = 180 + fardofangle; }
if ( neardofangle < 0 ) { neardofangle = 180 + neardofangle; }

tsform0.dnear.value = Math.round(10 * neardofangle) / 10 + "\u00B0";
tsform0.dfar.value = Math.round(10 * fardofangle) / 10 + "\u00B0";
tsform0.dtotaov.value = Math.round(10 * (fardofangle-neardofangle) ) / 10 + "\u00B0";

}
}
}

}

//----------
function tscalc1(tsform1) {

focal = tsform1.focal1.value; // in mm
shiftamount = tsform1.shift1.value; // in mm

if ( isNaN(focal) || focal <= 0 ) {
alert('Please enter a positive numerical value for the focal length.');
document.tsform1.focal1.focus();
document.tsform1.focal1.select();
}
else {
if ( isNaN(shiftamount) || shiftamount <= 0 ) {
alert('Please enter a positive numerical value for the shift amount.');
document.tsform1.shift1.focus();
document.tsform1.shift1.select();
}
else {

angle = (180/Math.PI) * Math.atan( shiftamount / focal ); // long dimension angle of view in degrees
tsform1.fplanerot1.value = Math.round(10 * angle) / 10 + "\u00B0";

}
}

}