var debug = false;

// events ---------------------------
function cboShape_OnChange(new_value) {
	
	if (debug) alert('cboShape_OnChange(' + new_value + ')');
	
	elementVisibilty('tableRectangleInput', 'none');
	elementVisibilty('tableCircleInput', 'none');
	elementVisibilty('tableTriangleInput', 'none');
	
	switch (new_value) {
		case 'rectangle': elementVisibilty('tableRectangleInput', 'block'); break;
		case 'circle': elementVisibilty('tableCircleInput', 'block'); break;
		case 'triangle': elementVisibilty('tableTriangleInput', 'block'); break;
	}
	
}

function btnCalculate_OnClick() {
	
	form = document.frmCalculator;
	shape = form.cboShape.value;
	
	switch (shape) {
		
		case 'rectangle':
			
			if (validateRectangle()) {
				calculateRectangle();
			} else {
				alert('Please enter numbers only.');
			}
			
			break;
		
		case 'circle':
			
			if (validateCircle()) {
				calculateCircle();
			} else {
				alert('Please enter numbers only.');
			}
			
			break;
		
		case 'triangle':
			
			if (validateTriangle()) {
				calculateTriangle();
			} else {
				alert('Please enter numbers only.');
			}
			
			break;
		
	}
	
}

// helper functions -----------------
function calculateRectangle() {
	
	form = document.frmCalculator;
	
	rec_length = form.txtRectangleLength.value;
	rec_width = form.txtRectangleWidth.value;
	rec_depth = form.txtDepth.value;
	
	// convert inches to feet
	rec_depth = rec_depth / 12;
	
	cubic_feet = roundDecimals((rec_length * rec_width) * rec_depth, 2);
	cubic_yards = roundDecimals(cubic_feet / 27, 2);
	
	setResults(cubic_yards);
	
}

function calculateCircle() {
	
	form = document.frmCalculator;
	
	cir_radius = form.txtCircleRadius.value;
	cir_depth = form.txtDepth.value;
	
	// convert inches to feet
	cir_depth = cir_depth / 12;
	
	cubic_feet = roundDecimals((cir_radius * cir_radius * 3.14) * cir_depth, 2);
	cubic_yards = roundDecimals(cubic_feet / 27, 2);
	
	setResults(cubic_yards);
	
	
}

function calculateTriangle() {
	
	form = document.frmCalculator;
	
	tri_length = form.txtTriangleLength.value;
	tri_height = form.txtTriangleHeight.value;
	tri_depth = form.txtDepth.value;
	
	// convert inches to feet
	tri_depth = tri_depth / 12;
	
	cubic_feet = roundDecimals((tri_length * (tri_height / 2)) * tri_depth, 2);
	cubic_yards = roundDecimals(cubic_feet / 27, 2);
	
	setResults(cubic_yards);
	
}

function validateRectangle() {
	
	form = document.frmCalculator;
	
	rec_length = form.txtRectangleLength.value;
	rec_width = form.txtRectangleWidth.value;
	rec_depth = form.txtDepth.value;
	
	if (isInteger(rec_length) && isInteger(rec_width) && isInteger(rec_depth)) {
		return true;
	} else {
		return false;
	}
	
}

function validateCircle() {
	
	form = document.frmCalculator;
	
	rec_radius = form.txtCircleRadius.value;
	rec_depth = form.txtDepth.value;
	
	if (isInteger(rec_radius) && isInteger(rec_depth)) {
		return true;
	} else {
		return false;
	}
	
}

function validateTriangle() {
	
	form = document.frmCalculator;
	
	tri_length = form.txtTriangleLength.value;
	tri_height = form.txtTriangleHeight.value;
	tri_depth = form.txtDepth.value;
	
	if (isInteger(tri_length) && isInteger(tri_height) && isInteger(tri_depth)) {
		return true;
	} else {
		return false;
	}
	
}

function isInteger(s) {
	
	if (s.length == 0) return false;
	
	var i;
	for (i = 0; i < s.length; i++) {   
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

/* This script is Copyright (c) Paul McFedries and 
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as 
this Copyright notice remains in place.*/

function roundDecimals(original_number, decimals) {
	var result1 = original_number * Math.pow(10, decimals)
	var result2 = Math.round(result1)
	var result3 = result2 / Math.pow(10, decimals)
	return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

	// Convert the number to a string
	var value_string = rounded_value.toString()
	
	// Locate the decimal point
	var decimal_location = value_string.indexOf(".")

	// Is there a decimal point?
	if (decimal_location == -1) {
		
		// If no, then all decimal places will be padded with 0s
		decimal_part_length = 0
		
		// If decimal_places is greater than zero, tack on a decimal point
		value_string += decimal_places > 0 ? "." : ""
	}
	else {

		// If yes, then only the extra decimal places will be padded with 0s
		decimal_part_length = value_string.length - decimal_location - 1
	}
	
	// Calculate the number of decimal places that need to be padded with 0s
	var pad_total = decimal_places - decimal_part_length
	
	if (pad_total > 0) {
		
		// Pad the string with 0s
		for (var counter = 1; counter <= pad_total; counter++) 
			value_string += "0"
		}
	return value_string
}

function elementVisibilty(e, visible) {
	
	if (debug) alert('elementVisibilty(' + e + ', ' + visible + ')');
	
	element = document.getElementById(e);
	element.style.display = visible;
	
}

function setResults(s) {
	
	element = document.getElementById('tdResults');
	element.innerHTML = s + '<div style=\'font-size: 12pt; font-weight:bold; color:#38D02B;\'>cubic yards</div>';
	
	
	
}
