// Javascript copyright Sean T. McHugh
//Cambridge in Colour Photography
// http://www.cambridgeincolour.com

<!-- Begin Tilt Shift Calculator #1
function tscalc0(tsform0) {
sensor_long = 1*document.tsform0.sensorsize.options[document.tsform0.sensorsize.selectedIndex].value; // long dimension of sensor in mm
sensor_short = (2/3) * sensor_long; // short dimension of sensor in mm, assuming 3:2 aspect ratio
focal = tsform0.focal.value; // in mm
orientation = document.tsform0.orient.options[document.tsform0.orient.selectedIndex].value; // 1 for landsapce, 2 for portrait
shiftamount = tsform0.shift0.value; // in mm
direction = document.tsform0.direction.options[document.tsform0.direction.selectedIndex].value; // 1 for horizontal, 2 for vertical

if ( isNaN(focal) || focal <= 0 ) {
alert('Please enter a positive numerical value for the focal length.');
document.tsform0.focal.focus();
document.tsform0.focal.select();
}
else {
if ( isNaN(shiftamount) || shiftamount <= 0 ) {
alert('Please enter a positive numerical value for the shift amount.');
document.tsform0.shift0.focus();
document.tsform0.shift0.select();
}
else {

if ( orientation == 1 && direction == 1 ) {
shiftedwidth = sensor_long + 2*shiftamount;
shiftedheight = sensor_short;
}
if ( orientation == 2 && direction == 1 ) {
shiftedwidth = sensor_short + 2*shiftamount;
shiftedheight = sensor_long;
}
if ( orientation == 1 && direction == 2 ) {
shiftedwidth = sensor_long;
shiftedheight = sensor_short + 2*shiftamount;
}
if ( orientation == 2 && direction == 2 ) {
shiftedwidth = sensor_short;
shiftedheight = sensor_long + 2*shiftamount;
}

angle_hor = (180/Math.PI) * 2 * Math.atan( shiftedwidth / ( 2*focal ) ); // horizontal angle of view in degrees
angle_ver = (180/Math.PI) * 2 * Math.atan( shiftedheight / ( 2*focal ) ); // vertical angle of view in degrees

tsform0.aov.value = Math.round(10 * angle_hor) / 10 + "\u00B0" + " x " + Math.round(10 * angle_ver) / 10 + "\u00B0"; // Xdeg x Ydeg

dtemp = Math.max(shiftedwidth,shiftedheight);
temp = dtemp / ( 2*focal );
aovtemp = 2 * Math.atan( temp );

focaltemp1 = sensor_long / ( 2 * Math.tan( aovtemp/2 ) );
focaltemp2 = sensor_short / ( 2 * Math.tan( aovtemp/2 ) );
reqfocaltemp = Math.max(focaltemp1,focaltemp2); // max focal length which encompasses both angles of view (if an unshifted single photograph)
tsform0.reqfocal.value = Math.round(10 * reqfocaltemp) / 10 + " mm";
tsform0.mpincrease.value = Math.round(100*(shiftedwidth*shiftedheight)/(sensor_long*sensor_short)-100) + "%";

// dynamically resizes divs to show relative sensor/shift size, where _s denotes rescaled screen dimensions in pixels
maxdivdim = 160;
if ( shiftedwidth >= shiftedheight ) {
	shiftedwidth_s = maxdivdim;
	tsform0.scale0.value = Math.round(100 * shiftedwidth_s/shiftedwidth) / 100;
	shiftedheight_s = Math.round(maxdivdim*(shiftedheight/shiftedwidth));
	} 
else {
	shiftedheight_s = maxdivdim;
	tsform0.scale0.value = Math.round(100 * shiftedheight_s/shiftedheight) / 100;
	shiftedwidth_s = Math.round(maxdivdim*(shiftedwidth/shiftedheight));
	}

if ( orientation == 1 && direction == 1 ) {
sensorheight_s = shiftedheight_s;
sensorwidth_s = (3/2)*sensorheight_s;
}
if ( orientation == 2 && direction == 1 ) {
sensorheight_s = shiftedheight_s;
sensorwidth_s = (2/3)*sensorheight_s;
}
if ( orientation == 1 && direction == 2 ) {
sensorwidth_s = shiftedwidth_s;
sensorheight_s = (2/3)*sensorwidth_s;
}
if ( orientation == 2 && direction == 2 ) {
sensorwidth_s = shiftedwidth_s;
sensorheight_s = (3/2)*sensorwidth_s;
}

sensorheight_topmargin = Math.round((shiftedheight_s-sensorheight_s)/2) + 'px';

shiftedwidth_s = shiftedwidth_s + 'px';
shiftedheight_s = shiftedheight_s + 'px';
sensorwidth_s = sensorwidth_s + 'px';
sensorheight_s = sensorheight_s + 'px';

document.getElementById('s1').style.width=shiftedwidth_s;
document.getElementById('s1').style.height=shiftedheight_s;
//document.getElementById('s1').style.margin='auto';
document.getElementById('s2').style.width=sensorwidth_s;
document.getElementById('s2').style.height=sensorheight_s;

if ( direction == 1 ) { document.getElementById('s2').style.margin='-1px' + ' auto' + ' auto' + ' auto'; }
else { document.getElementById('s2').style.margin=sensorheight_topmargin + ' auto' + ' auto' + ' -1px'; }

}
}

}
//  End Tilt Shift Calculator #1 -->