
function NullToZero(x){
	if(x==null || x=="")
		x=0;
}

function CalculateFOV(){

	var CCDHeight, CCDWidth, FOVHeight, FOVWidth, FOVAngle, Distance, LensSize;

	LensSize = document.calcfov.lens.value;
	Distance = document.calcfov.distance.value;

	// Check that the required values have been supplied. Display message if not
	if ((LensSize <= 0) || (Distance <= 0)){
		document.calcfov.angle.value = 0;
		document.calcfov.height.value = 0;
		document.calcfov.width.value = 0;				
		window.alert("Please enter a valid Lens Size and Distance.")
	}
	else{
	//got valid input values
		switch (document.calcfov.ccdvalue.value){
			case "half":
				CCDWidth = 6.4;
				CCDHeight = 4.8;
				break;
			case "third":
				CCDWidth = 4.8;
				CCDHeight = 3.6;
				break;
			case "quarter":
				CCDWidth = 3.6;
				CCDHeight = 2.7;
				break;
		}

		FOVHeight = Math.round(((CCDHeight * Distance)/LensSize)*10)/10;
		FOVWidth = Math.round(((CCDWidth * Distance)/LensSize)*10)/10;				
		FOVAngle = Math.atan(FOVWidth/Distance) * 100;
		document.calcfov.height.value = FOVHeight;
		document.calcfov.width.value = FOVWidth;	
		//window.alert("FOVAngle:" + FOVAngle);				
		//window.alert("FOVAngle Rounded:" + Math.round(FOVAngle));	
		document.calcfov.angle.value = Math.round(FOVAngle);
		//debug
		//window.alert("LensSize:" + LensSize);
		//window.alert("Distance:" + Distance);		
	}

}

function CalculateLens(){
	var CCDHeight, CCDWidth, FOVHeight, FOVWidth, FOVAngle, Distance, LensSize;

	Distance = document.calclens.distance.value;
	FOVWidth = document.calclens.width.value;
	FOVHeight = document.calclens.height.value;

	// Check that the required values have been supplied. Display message if not
	if ((Distance <= 0) || ((FOVWidth <= 0) && (FOVHeight <= 0))){
		document.calclens.lens.value = 0;
		window.alert("Please enter a valid Distance, and either the minimum width or height of the FOV.")
	}
	else{
	//got valid input values
		switch (document.calclens.ccdvalue.value){
			case "half":
				CCDWidth = 6.4;
				CCDHeight = 4.8;
				break;
			case "third":
				CCDWidth = 4.8;
				CCDHeight = 3.6;
				break;
			case "quarter":
				CCDWidth = 3.6;
				CCDHeight = 2.7;
				break;
		}

		if(FOVWidth != 0){
		
				// Calculate lens type
				// from field of view width and distance to object
				
				LensSize = (CCDWidth * (Distance/FOVWidth));
				FOVHeight = Math.round(((CCDHeight * Distance)/LensSize)*10)/10;	
				document.calclens.height.value = FOVHeight;	

		} else {
			
				// Calculate lens type
				// from field of view height and distance to object
				
				LensSize = (CCDHeight * (Distance/FOVHeight));
				FOVWidth = Math.round(((CCDWidth * Distance)/LensSize)*10)/10;
				document.calclens.width.value = FOVWidth;
		}
					
		document.calclens.lens.value = Math.round(LensSize*10)/10;
	}

}


