// the final result of the calculator task
var calcResult

// displaying the result value in the web form
function dspResults() {
	document.formOpenArea.CalcResult.value = calcResult;
}

// rounds number to X decimal places, defaults to 2
function numRound(number,x) {
    x = (!x ? 2 : x);
    return Math.round(number * Math.pow(10,x)) / Math.pow(10,x);
}

function calcOpenArea() {
	// object holder
	var calc
	// these variables will hold the values of the selected form-elements
	var _holePattern
	var _holeDiameter
	var _holeCenters
	// misc variables for use in function
	var radioSomethingChecked = 0
	var execCalc = 1

	calcResult = 0;
	calc = eval("document.formOpenArea");

	// extracting the values out of the selected form elements
	for (i = 0; i < 2; i++) {
		if (calc.HolePattern[i].checked == true) {
			radioSomethingChecked = 1
			_holePattern = calc.HolePattern[i].value;
		}
	}
	_holeDiameter = (calc.HoleDiameter.options[calc.HoleDiameter.selectedIndex].value / 32);
	_holeCenters = (calc.HoleCenters.options[calc.HoleCenters.selectedIndex].value / 32);

	// handle data-entry validation
	if (radioSomethingChecked == 0) {
		execCalc = 0;
		window.alert("You must select a Hole Pattern");
	}
	if (_holeDiameter >= _holeCenters) {
		execCalc = 0;
		window.alert("Warning: The Hole Centers must be larger than the Hole Diameter");
	}

	if (execCalc == 1) {
		// make the calculation
		calcResult = (Math.pow(_holeDiameter, 2) / Math.pow(_holeCenters, 2)) * _holePattern;
		calcResult = Math.round(calcResult);
		calcResult = calcResult + '%';
	}
	dspResults();
}
