function Round(value, precision) {
    var tmp = Math.pow( 10, precision );
    return Math.round( value * tmp ) / tmp;
}

/* Lens calculation functions */
function widthChanged()
{
	w = window.document.formcalc.width.value;
	kvot = eval(w * 3 / 4);
	window.document.formcalc.height.value = Round(kvot, 2);
}

function heightChanged()
{
	h = window.document.formcalc.height.value;
	kvot = eval(h * 4 / 3);
	window.document.formcalc.width.value = Round(kvot, 2);
}

function calcFocal()
{
	var d = window.document.formcalc.distance.value;
	var w = window.document.formcalc.width.value;
	var h = CCDHeight();
	if ((d != "") && (w != ""))
	{
		var ratio = eval (d / w);
		result = eval (ratio * h );
		window.document.formcalc.focal.value = Round(result, 2);
	}
}

function calcDistance()
{
	var f = window.document.formcalc.focal.value;
	var w = window.document.formcalc.width.value;
	var h = CCDHeight();
	if ((f != "") && (w != ""))
	{
		var ratio = eval (f * w);
		result = eval (ratio / h );
		window.document.formcalc.distance.value = Round(result, 2);
	}
}

function calcFocalAndDistance()
{
	calcFocal();
	calcDistance();
}

function calcDim()
{
	var f = window.document.formcalc.focal.value;
	var d = window.document.formcalc.distance.value;
	var h = CCDHeight();
	if ((f != "") && (d != ""))
	{
		var ratio = eval (d / f);
		result = eval (ratio * h );
		window.document.formcalc.width.value = Round(result, 2);
		widthChanged();
	}
}

function CCDHeight()
{
	// CCDw = CCD Width Variable
	// CCDh = CCD Height Variable
	var CCDv, CCDh;
	var sensor = window.document.formcalc.sensor[ window.document.formcalc.sensor.selectedIndex ].value;

	switch (sensor)
	{
		case "1_3":     // For 1/3-inch CCDs
			CCDv = 3.6; // mm
			CCDh = 4.8;
			break;
		case "1_4":
			CCDv = 2.7;
			CCDh = 3.6;
			break;
		case "1_2":
			CCDv = 4.65;
			CCDh = 6.2;
//			CCDv = 4.8;
//			CCDh = 6.4;
			break;
		case "2_3":
			CCDv = 6.6;
			CCDh = 8.8;
			break;
		case "1":
			CCDv = 9.5;
			CCDh = 12.7;
			break;
		default:
			alert("Unknown sensor type: " + sensor);
	}
//	alert("FormatValue: " + sensor + "  CCDv: " + CCDv);
	return CCDv;
}


/* Storage calculation functions */
function calcStorage()
{
	var size = window.document.formstorage.size.value;
	var fps  = window.document.formstorage.fps.value;
	var time = window.document.formstorage.time.value;
	var nrofcams = window.document.formstorage.cameras.value;

	if (size != "" && fps != "" && time != "")
	{
		var result = eval(size * fps * time * nrofcams * 60 * 60 * 24 / 1024 / 1024); // GB
		window.document.formstorage.space.value = Round(result,2);
	}
}

function calcTime()
{
	var size  = window.document.formstorage.size.value;
	var fps   = window.document.formstorage.fps.value;
	var space = window.document.formstorage.space.value;
	var nrofcams = window.document.formstorage.cameras.value;

	if (size != "" && fps != "" && space != "")
	{
		var result = eval(space * 1024 * 1024 / (size * fps * nrofcams * 60 * 60 * 24));
		window.document.formstorage.time.value = Round(result,2);
	}
}

function calcStorageAndTime()
{
	calcStorage();
	calcTime();
}


/* Bandwidth calculation functions */
function calcFPS()
{
	var size = window.document.formbandwidth.size.value;
	var bandwidth= window.document.formbandwidth.bandwidth.value;
	var nrofcams = window.document.formbandwidth.cameras.value;

	if (size != "" && nrofcams != "" && bandwidth != "")
	{
		var result = eval(bandwidth * 1024 / (size * nrofcams * 10));
		window.document.formbandwidth.fps.value = Round( result, 2 );
	}
}

function calcBandwidth()
{
	var size = window.document.formbandwidth.size.value;
	var fps  = window.document.formbandwidth.fps.value;
	var nrofcams = window.document.formbandwidth.cameras.value;

	if (size != "" && nrofcams != "" && fps != "")
	{
		var result = eval(size * fps * nrofcams / 1024 * 10);
		window.document.formbandwidth.bandwidth.value = Round( result, 2 );
	}
}

function calcFPSAndBandwidth()
{
	calcFPS();
	calcBandwidth();
//	bandwidthChanged();
}

function bandwidthChanged()
{
	var bandwidth = window.document.formbandwidth.bandwidth.value;
	kvot = eval(bandwidth * 8);
	window.document.formbandwidth.bandwidth_kb.value = Round( kvot, 2 );
}


