var caltable = new Array(4);

function colObj(obj1, obj2, obj3, val, iobj1, iobj2, iobj3, units) {
	// output field
	this.d2 = obj1;
	this.distance = obj2;
	this.d1 = obj3;

	// const value
	this.constant = val;

	// input field
	this.ifno = iobj1;
	this.idistance = iobj2;
	this.ifocallength = iobj3;

	return this;
}

colObj.prototype.calc = function(inUnits, outUnits) {
	// Front Depth of Field "D1" ----------------------------------------------
	var F = this.ifno.value;
	var l = this.idistance.value;
	var f = this.ifocallength.value;
	var c = this.constant;

	if (inUnits == "feet") {
		l = convertFeetToM(l);
	}

	var d = (c * F * Math.pow(l, 2) * 1000) / (Math.pow(f, 2) + c * F * l * 1000);
	if (outUnits == "feet") {
		d = convertMToFeet(d);
	}
	d *= 100;
	str = new String(Math.round(d) / 100);
	this.d1.value = this.format(str);

	// Object Distance "l" ----------------------------------------------------
	//d = 100 * l;
	d = 100 * this.idistance.value;
	str = String(Math.round(d) / 100);
	this.distance.value = this.format(str);

	// Rear Depth of Field "D2" -----------------------------------------------
	d = Math.pow(f, 2) - c * F * l * 1000;
	if (d > 0) {
		d = (c * F * Math.pow(l, 2) * 1000) / d;

		if (outUnits == "feet") {
			d = convertMToFeet(d);
		}

		d *= 100;
		str = String(Math.round(d) / 100);
		this.d2.value = this.format(str);
	} else {
		//this.d2.value = String.fromCharCode(216);
		this.d2.value = "infinity";
	}
}

colObj.prototype.format = function(str) {
	var len = str.length;
	var pos = str.indexOf(".");
	switch (pos) {
	case -1:
		str += ".00";
		break;
	default:
		for (var i = 0; i < 3 - len + pos; i++) {
			str += "0";
		} 
	}
	return str;
}

function init() {
	caltable[0] = new colObj(document.calcform.sdtvd2, document.calcform.sdtvl, document.calcform.sdtvd1, 0.021, document.calcform.fno, document.calcform.distance, document.calcform.focallength, "m");
	caltable[1] = new colObj(document.calcform.hdtvd2, document.calcform.hdtvl, document.calcform.hdtvd1, 0.01, document.calcform.fno, document.calcform.distance, document.calcform.focallength, "m");
	caltable[2] = new colObj(document.calcform.sdtvhd2, document.calcform.sdtvhl, document.calcform.sdtvhd1, 0.016, document.calcform.fno, document.calcform.distance, document.calcform.focallength, "m");
	caltable[3] = new colObj(document.calcform.hdtvhd2, document.calcform.hdtvhl, document.calcform.hdtvhd1, 0.008, document.calcform.fno, document.calcform.distance, document.calcform.focallength, "m");
	caltable[4] = new colObj(document.calcform.sdtvtd2, document.calcform.sdtvtl, document.calcform.sdtvtd1, 0.012, document.calcform.fno, document.calcform.distance, document.calcform.focallength, "m");
	//caltable[5] = new colObj(document.calcform.hdtvtd2, document.calcform.hdtvtl, document.calcform.hdtvtd1, 0.006, document.calcform.fno, document.calcform.distance, document.calcform.focallength, "m");
}

function checkform(form) {
	if (form.fno.value== null || form.fno.value.length==0 || form.distance.value== null || form.distance.value.length==0 || form.focallength.value== null || form.focallength.value.length==0) {
		return false;
	}

	if (isNaN(form.fno.value)==true || isNaN(form.distance.value)==true || isNaN(form.focallength.value)==true) {
		return false;
	}

	return true;
}

function calculate(form) {
	setUnits(form.units.value, "unitdisplay");
	if (checkform(form)) {
		for (var i=0; i < caltable.length; i++) {
			caltable[i].calc(form.units.value, form.units.value);
		}
	}
}
