
var pi = 3.14159265358979;
var conversionFactor = 28.368466; // feet cubed to liters

function calculateBox(){
	var lengthinch = document.calculatorBoxForm.boxLengthInch.value;
	var lengthfeet = document.calculatorBoxForm.boxLength.value;
	var lengthtot = (lengthinch/12) + (lengthfeet * 1); // force to treat as numbers not strings
	
	var widthinch = document.calculatorBoxForm.boxWidthInch.value;
	var widthfeet = document.calculatorBoxForm.boxWidth.value;
	var width = (widthinch/12) + (widthfeet * 1);
	
	var depthinch = document.calculatorBoxForm.boxDepthInch.value;
	var depthfeet = document.calculatorBoxForm.boxDepth.value;
	var depth = (depthinch/12) + (depthfeet * 1);
	
	var volume = lengthtot * width * depth * conversionFactor; //unrounded number
	var volumeRounded = Math.round(volume*1000)/1000; //rounded value
	document.calculatorBoxForm.boxVolume.value = volumeRounded;
}

function calculateCube(){
	var lengthinch = document.calculatorCubeForm.cubeSideInches.value;
	var lengthfeet = document.calculatorCubeForm.cubeSideFeet.value;
	var lengthtot = (lengthinch/12) + (lengthfeet * 1); // force to treat as numbers not strings
	
	var volume = lengthtot * lengthtot * lengthtot * conversionFactor;
	var volumeRounded = Math.round(volume*1000)/1000; //rounded value
	document.calculatorCubeForm.cubeVolume.value = volumeRounded;
}

function calculateCylinder(){
	var radiusinch = document.calculatorCylForm.cylRadiusInches.value;
	var radiusfeet = document.calculatorCylForm.cylRadiusFeet.value;
	var radius = (radiusinch/12) + (radiusfeet * 1); // force to treat as numbers not strings
	
	var heightinch = document.calculatorCylForm.cylHeightInches.value;
	var heightfeet = document.calculatorCylForm.cylHeightFeet.value;
	var height = (heightinch/12) + (heightfeet * 1); // force to treat as numbers not strings
	
	var volume = pi * radius * radius * height * conversionFactor;
	var volumeRounded = Math.round(volume*1000)/1000; //rounded value
	document.calculatorCylForm.cylinderVolume.value = volumeRounded;
}

function calculateCone(){
	var radiusinch = document.calculatorConeForm.coneRadiusInches.value;
	var radiusfeet = document.calculatorConeForm.coneRadiusFeet.value;
	var radius = (radiusinch/12) + (radiusfeet * 1); // force to treat as numbers not strings
	
	var heightinch = document.calculatorConeForm.coneHeightInches.value;
	var heightfeet = document.calculatorConeForm.coneHeightFeet.value;
	var height = (heightinch/12) + (heightfeet * 1); // force to treat as numbers not strings
	
	var volume = (pi/3) * radius * radius * height * conversionFactor;
	var volumeRounded = Math.round(volume*1000)/1000; //rounded value
	document.calculatorConeForm.coneVolume.value = volumeRounded;
}

function convertVolume(form){
	//number of input boxes
	counter = 6; 
	
	//conversion factors to bring entered value to equivalent litres
	var conversionFactor = new Array(); 
	conversionFactor[0] = 1;
	conversionFactor[1] = 33.8140226;
	conversionFactor[2] = 0.264172051;
	conversionFactor[3] = 0.219969157;
	conversionFactor[4] = 0.0353146667;
	conversionFactor[5] = 0.001;

	var litresValue = 0;
    for (var i = 0; i < counter; i++) {
    	// Find first non-blank entry
    	if (form.elements[i].value != null && form.elements[i].value.length != 0) {
    		if (i == 0 && form.elements[1].value != "") return false;
    		var parsecheck = "" + parseFloat(form.elements[i].value);
    		if (parseFloat(form.elements[i].value) == 0 || parsecheck == "NaN") {
    			window.alert ("Please enter a non-zero number." + '\n' + form.elements[i].value);
    			return false;
    		}
    		
    		//litres value is entered value divided by its conversion factor
    		litresValue = form.elements[i].value / conversionFactor[i];
    		break;
    	}
    }
    
    //if criteria has not been met, reset form
    if (litresValue == 0) {
   	    form.reset();
    	return false;
    }	     

	//show values to 5 decimals
	var unroundedValue;
	var roundedValue;
    for (var i = 0; i < counter; i++) {
		unroundedValue = litresValue * conversionFactor[i];
		roundedValue = Math.round(unroundedValue*10000)/10000;
		form.elements[i].value = roundedValue;
    }
    return true;
}