// Copyright 2009. Michael Krekin

// global arrays
var apertureN = new Array("f/1.4", "f/2", "f/2.8", "f/4", "f/5.6", "f/8", "f/11", "f/16");
var aperture = new Array(1.4142135, 2.0, 2.828427, 4.0, 5.656854, 8.0, 11.313708, 16.0);
var distances = new Array(0.25, 0.50, 0.75, 1.0, 1.5, 2.0, 2.5, 3.0, 4.0, 5.0, 7.0, 10.0, 15.0, 20.0, 50.0, 100.0, 200.0, 500.0);

function FillDOFTable() 
{
	var bCalcTable = true;
	var focal = parseFloat(document.getElementById("fr").value);
	
	if (isNaN(focal) || focal == 0)  
		bCalcTable = false;
	if (bCalcTable && focal < 0)
	{
		focal = focal * -1.0;
		document.getElementById("fr").value = focal;
	}

	if (bCalcTable) // focal length is OK
	{
		// get circle of confusion
		var CoC = parseFloat(document.getElementById("camera").value);
		var crop = 0.030 / CoC;
		var efr = Math.round (focal * crop);
		document.getElementById("crop").value = GetDisplayString(crop);
		document.getElementById("efr").value = efr;
	
		var i;
		var j;
		var hyperfocal;
		var textHyperfocal;
		var focusDistance;
		var bDecimal = true;
		var bHighPrecision = false;
		var infinite = 100000000.0;  // set infinity at 10000m
		for (i=0; i<aperture.length; i++)
		{
			hyperFocal = (focal * focal) / (aperture[i] * CoC) + focal;

			for (j=0; j < distances.length; j++)
			{
				focusDistance = distances[j];
				if (0 == i)
					document.getElementById("tableDistance" + j).innerHTML = focusDistance;
				focusDistance = focusDistance * 1000.0;

				// near distance
				dofNear = ((hyperFocal - focal) * focusDistance) / (hyperFocal + focusDistance - (2.0*focal));

				// far distance
				if ( (hyperFocal - focusDistance) <= 0.00001)
					dofFar = infinite;
				else
					dofFar = ((hyperFocal-focal) * focusDistance) / (hyperFocal - focusDistance);

				// Convert depth of field numbers to selected display units
				dofNear = dofNear / 1000.0;
				dofFar  = dofFar / 1000.0;
					
				// write near distance to table
  				document.getElementById("tableNearDistanceC" + i + "R" + j).innerHTML = GetDisplayString(dofNear);

				// write far distance to table
				if ( dofFar < (infinite/1000.0))
 					document.getElementById("tableFarDistanceC" + i + "R" + j).innerHTML = GetDisplayString(dofFar);
				else
	  				document.getElementById("tableFarDistanceC" + i + "R" + j).innerHTML = "&infin;";
			}
			
			hyperFocal = hyperFocal / 1000.0;
			textHyperfocal = GetDisplayString(hyperFocal);
			document.getElementById("tableHyperfocal" + i).innerHTML = textHyperfocal;
			
			// write the line for infinite focus distance
			document.getElementById("tableNearInfinite" + i).innerHTML = textHyperfocal;
			document.getElementById("tableFarInfinite" + i).innerHTML = "&infin;";
		}


	}
	else
	{
		// focal length value is NaN or zero, so just write blanks to the table
		for (i=0; i<aperture.length; i++)
		{
			for (j=0; j < distances.length; j++)
			{
				if (0 == i)
					document.getElementById("tableDistance" + j).innerHTML = "";

				document.getElementById("tableNearDistanceC" + i + "R" + j).innerHTML  = "";
				document.getElementById("tableFarDistanceC" + i + "R" + j).innerHTML = "";
			}
			document.getElementById("tableHyperfocal" + i).innerHTML = "";
			document.getElementById("tableNearInfinite" + i).innerHTML = "";
			document.getElementById("tableFarInfinite" + i).innerHTML = "";
			
		}
		document.getElementById("tableCocUsed").innerHTML = ""
		document.getElementById("tableFocalLengthText").innerHTML = "";
		
		// let the user know why the table was blanked out
		alert("Ошибка при указании фокусного расстояния");
	}
}

function CreateDOFTable()
{

	var htmlText;
	var selectFormatText;
	var textClasses = new Array("textClass0", "textClass1", "textClass2");
	var classText;

	htmlText = "<div style=\"text-align:center;\">";
	htmlText += "<table width=\"100%\" border=\"1\" rules=\"rows\" cellspacing=\"0\" cellpadding=\"3\">";
	classText = textClasses[2];
	htmlText += "<tr><td colspan=\"" + (apertureN.length*2+1) + "\" class=\"" + classText + "\" align=\"center\">Диафрагма</td></tr>";
 
	// write f-stop row
	classText = textClasses[2];
	htmlText += "<tr>"
	htmlText += "<td colspan=\"1\" ID=\"tableFstop\" class=\"" + classText + "\" align=\"center\"> </td>";
	var i;
	for (i = 0; i < apertureN.length; i++)
	{
		classText = textClasses[i%2];
		htmlText += "<td colspan=\"2\" ID=\"tableFstop" + i + "\" align=\"center\" class=\"" + classText + "\" >" + apertureN[i] + "</td>";
	}
	htmlText += "</tr>";

	// write near/far distance header row
	classText = textClasses[2];
	htmlText += "<tr><td ID=\"tableNearFar\" class=\"" + classText + "\" align=\"center\">Расст.,<br />м</td>";
	for (i = 0; i < apertureN.length; i++)
	{
		classText = textClasses[i%2];
		htmlText += "<td ID=\"tableNear" + i + "\" align=\"center\" class=\"" + classText + "\">От</td>";
		htmlText += "<td ID=\"tableFar" + i + "\" align=\"center\" class=\"" + classText + "\">До</td>";
	}
	htmlText += "</tr>";

	// write focus distance rows
	for (var j = 0; j < distances.length; j++)
	{
		classText = textClasses[2];
		htmlText += "<tr><td align=\"center\" ID=\"tableDistance" + j + "\" class=\"" + classText + "\">" + distances[j] + "</td>";
		for (i = 0; i < apertureN.length; i++)
		{
			classText = textClasses[i%2];
			htmlText += "<td ID=\"tableNearDistanceC" + i + "R" + j + "\" align=\"center\" class=\"" + classText + "\">0</td>";
			htmlText += "<td ID=\"tableFarDistanceC" + i + "R" + j + "\" align=\"center\" class=\"" + classText + "\">0</td>";
		}
		htmlText += "</tr>";
	}

	// write infinity focus distance row
	classText = textClasses[2];
	htmlText += "<tr><td colspan=\"1\" ID=\"tableInfinite\" align=\"center\" class=\"" + classText + "\">&infin;</td>";
	for (var i = 0; i < apertureN.length; i++)
	{
		classText = textClasses[i%2];
		htmlText += "<td ID=\"tableNearInfinite" + i + "\" align=\"center\" class=\"" + classText + "\">0</td>";
		htmlText += "<td ID=\"tableFarInfinite" + i + "\" align=\"center\" class=\"" + classText + "\">0</td>";
	}
	htmlText += "</tr>";

	// write hyperfocal distance row
	classText = textClasses[2];
	htmlText += "<tr style=\"border-bottom: 1px solid #ddd;\"><td colspan=\"1\" ID=\"tableHyperfocal\" align=\"center\" class=\"" + classText + "\">Гипер-<br />фокаль-<br />ное</td>";
	for (var i = 0; i < apertureN.length; i++)
	{
		classText = textClasses[i%2];
		htmlText += "<td colspan=\"2\" ID=\"tableHyperfocal" + i + "\" align=\"center\" valign=\"center\" class=\"" + classText + "\">0</td>";
	}
	htmlText += "</tr>";


	// close out the table
	htmlText += "</table>";
	htmlText += "</div>";

	// write the depth of field table to the web page
	document.getElementById("doftable").innerHTML = htmlText;
	
	// fill the table with the calculated distances
	FillDOFTable();
	
}

// get display string for distance
function GetDisplayString(theValue)
{
	var theText;
	var v;
	var dec;
	
	if (theValue <= 10.0)
	{
		v = Math.round(theValue*100.0);
		dec = v % 100.0;
		theText = Math.floor(v/100.0).toString();
		if (dec < 10)
			dec = "0" + dec;
		theText += ".";
		theText += dec;
	}
	else if (theValue <= 100.0)
	{
		v = Math.round(theValue*10.0);
		dec = v % 10.0;
		theText = Math.floor(v/10.0).toString();
		theText += ".";
		theText += dec;
	}
	else
	  	theText = Math.round(theValue);

	return theText;
}
