/* Copyright 2008 Don Fleming. All rights reserved. */

var bLoadingPage = false;

function setCookie(focalText, focalIndex, apertureText, apertureIndex, formatText,
				   formatIndex, unitsText, unitsIndex)
{
	if (true == bLoadingPage)
		return;
		
	document.cookie = "focalText="+focalText;
	document.cookie = "focalIndex="+focalIndex;
	
	document.cookie = "apertureText="+apertureText;
	document.cookie = "apertureIndex="+apertureIndex;
	
	document.cookie = "formatText="+formatText;
	document.cookie = "formatIndex="+formatIndex;
	
	document.cookie = "unitsText="+unitsText;
	document.cookie = "unitsIndex="+unitsIndex;
	
	expireDate = new Date;
	expireDate.setMonth(expireDate.getMonth()+12);
	document.cookie = "expires="+expireDate.toUTCString();
}
function getCookie()
{
	if (document.cookie != "")
	{
		
		var focalIndex, focalText, apertureIndex, apertureText, unitsIndex, unitsText, formatIndex, formatText;
		unitsIndex = apertureIndex = focalIndex = formatIndex = 0;
		focalText = "";
		apertureText = "";
		unitsText = "";
		formatText = "";
		
		var i, cookieName, cookieValue;
		var thisCookie = document.cookie.split("; ");
		for (i = 0; i < thisCookie.length; i++)
		{
			cookieName = thisCookie[i].split("=")[0];
			cookieValue = thisCookie[i].split("=")[1];
			
			switch (cookieName)
			{
				case "focalIndex":
					focalIndex = parseInt(cookieValue);
					break;
				case "focalText":
					focalText = cookieValue;
					break;
				case "apertureIndex":
					apertureIndex = parseInt(cookieValue);
					break;
				case "apertureText":
					apertureText = cookieValue;
					break;
				case "unitsIndex":
					unitsIndex = parseInt(cookieValue);
					break;
				case "unitsText":
					unitsText = cookieValue;
					break;
				case "formatIndex":
					formatIndex = parseInt(cookieValue);
					break;
				case "formatText":
					formatText = cookieValue;
					break;
				default:
					break;
			}
		}			

		
		if (formatText == document.getElementById("formatSelect").options[formatIndex].text)
			document.getElementById("formatSelect").selectedIndex = formatIndex;
		if (apertureText == document.getElementById("apertureSelect").options[apertureIndex].text)
			document.getElementById("apertureSelect").selectedIndex = apertureIndex;
		if (unitsText == document.getElementById("unitsSelect").options[unitsIndex].text)
			document.getElementById("unitsSelect").selectedIndex = unitsIndex;
		if (focalText == document.getElementById("focalSelect").options[focalIndex].text)
			document.getElementById("focalSelect").selectedIndex = focalIndex;
	}
}
			
// hide the url bar after the page loads, and load all the cookies
function onLoadPage()
{
	bLoadingPage = true;
	
	getCookie();
	
	doDepthOfField();	
	
	bLoadingPage = false;

	setTimeout( function(){window.scrollTo(0, 1);} , 100);
}
	
var savedFormat = 0.019;
var savedUnits = 0.001;
var savedFocal = 50.0;
var savedAperture = 16.0;
var savedFormatIndex = 10;
var savedUnitsIndex = 0;
var savedFocalIndex = 45;
var savedApertureIndex = 30;

// Calculate all depth of field numbers
function doDepthOfField() 
{
	// get input data from the form
	var distance = parseFloat(document.getElementById("zip_distance").value);
	var CoC = parseFloat(document.getElementById("formatSelect").value);
	var units = parseFloat(document.getElementById("unitsSelect").value);
	var focal = parseFloat(document.getElementById("focalSelect").value);
	var aperture = parseFloat(document.getElementById("apertureSelect").value);
	
	// input error checking
	if (CoC < 0.0001)
	{
		document.getElementById("formatSelect").selectedIndex = savedFormatIndex ;
		CoC = savedFormat ;
	}
	if (units < 0.001)
	{
		document.getElementById("unitsSelect").selectedIndex = savedUnitsIndex ;
		units = savedUnits ;
		
	}
	if (isNaN(focal) || focal < 0.0001)
	{
		document.getElementById("focalSelect").selectedIndex = savedFocalIndex ;
		focal = savedFocal ;
	}
	if (isNaN(aperture) || aperture < 0.0001)
	{
		document.getElementById("apertureSelect").selectedIndex = savedApertureIndex;
		aperture = savedAperture ;
	}
	var bCalcDistance = true;
	if (isNaN(distance) || distance <= 0.0001)  
		bCalcDistance = false;


	// save current selections
	savedApertureIndex = document.getElementById("apertureSelect").selectedIndex;
	savedFocalIndex = document.getElementById("focalSelect").selectedIndex; 
	savedUnitsIndex = document.getElementById("unitsSelect").selectedIndex;
	savedFormatIndex= document.getElementById("formatSelect").selectedIndex;
	savedAperture = aperture;
	savedCoC = CoC;
	savedFocal = focal;
	savedUnits = units;	
	setCookie(document.getElementById("focalSelect").options[savedFocalIndex].text,
				savedFocalIndex,
				document.getElementById("apertureSelect").options[savedApertureIndex].text,
				savedApertureIndex,
				document.getElementById("formatSelect").options[savedFormatIndex].text,
				savedFormatIndex,
				document.getElementById("unitsSelect").options[savedUnitsIndex].text,
				savedUnitsIndex);
	
	// 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
	var unitsString;
	if (units > 0.4)
		unitsString = "&nbsp;m";
	else if (units < 0.02)
		unitsString = "&nbsp;cm";
	else if (units < 0.1)
		unitsString = "&nbsp;in";
	else
	  	unitsString = "&nbsp;ft";
		
	// transfer values to the form
	
	if (hyperFocal < 10.0)
	{
		hyperfocalDistance = Math.round(hyperFocal*100)/100;
		document.getElementById("hyperFocal").innerHTML  = "Hyperfocal distance:&nbsp;" + hyperfocalDistance + unitsString;
	}
	else
	{
		hyperfocalDistance = Math.round(hyperFocal*10)/10;
		document.getElementById("hyperFocal").innerHTML  = "Hyperfocal distance:&nbsp;" + hyperfocalDistance + unitsString;
	}
		
	if (bCalcDistance)
	{
		if (distance < 10.0)
			document.getElementById("dofFocus").innerHTML =  Math.round(distance*100)/100 + unitsString;
		else if (distance < 100.0)
			document.getElementById("dofFocus").innerHTML =  Math.round(distance*10)/10 + unitsString;
		else
			document.getElementById("dofFocus").innerHTML =  Math.round(distance) + unitsString;

		if (dofNear < 10.0)
		{
	 		document.getElementById("dofNear").innerHTML = Math.round(dofNear*100)/100 + unitsString;
		}
		else if (dofNear < 100.0)
		{
	 		document.getElementById("dofNear").innerHTML = Math.round(dofNear*10)/10 + unitsString;
		}
		else
		{
 			document.getElementById("dofNear").innerHTML = Math.round(dofNear) + unitsString;
		}
  
		if ( dofFar < 10000.0)
		{	
			if (dofFar < 10.0)
			{
		      	document.getElementById("dofFar").innerHTML  = Math.round(dofFar*100)/100 + unitsString;
			}
			else if (dofFar < 100.0)
			{
		      	document.getElementById("dofFar").innerHTML  = Math.round(dofFar*10)/10 + unitsString;
			}
     		else
			{
	      		document.getElementById("dofFar").innerHTML  = Math.round(dofFar) + unitsString;
			}
				
			if (dofTotal < 10.0)
			{
	      		document.getElementById("dofTotal").innerHTML  = Math.round(dofTotal*100)/100 + unitsString;
			}
			else if (dofTotal < 100.0)
			{
	      		document.getElementById("dofTotal").innerHTML  = Math.round(dofTotal*10)/10 + unitsString;
			}
     		else
			{
      			document.getElementById("dofTotal").innerHTML  = Math.round(dofTotal) + unitsString;
			}
		}
		else
		{
    		document.getElementById("dofFar").innerHTML  = "Infinity";
    		document.getElementById("dofTotal").innerHTML  = "Infinite";
		}
	}
	else
	{
		document.getElementById("dofFocus").innerHTML = " ";
		document.getElementById("dofFar").innerHTML = " ";
  		document.getElementById("dofNear").innerHTML = " ";
  		document.getElementById("dofTotal").innerHTML = " ";
	}

}
	
var hyperfocalDistance = 27.1;

function doHyperfocal()
{
	document.getElementById("zip_distance").value = hyperfocalDistance;
	doDepthOfField();
}


