//
// estimator.js
//


// Service rates
var bw_rate = 19.50; // boulder wall
var wbw_rate = 27.00; // weathered block wall
var nsw_rate = 34.50; // natural stone wall
var pp_rate = 16.00; // paver patio
var nsp_rate = 16.50; // natural stone patio
var wp_rate = 43.00; // watefall/pond
var pd_rate = 13.00; // paver driveway


// ---------------------------------------------------------------------------
// float calculate_area( float length, float width )
// ---------------------------------------------------------------------------
function calculate_area( length, width )
{
	var area = length * width;
	return area;
}

// ---------------------------------------------------------------------------
// float calculate_cost( float area, float rate )
// ---------------------------------------------------------------------------
function calculate_cost( area, rate )
{
	var cost = area * rate;
	return cost;
}

// ---------------------------------------------------------------------------
// string format_cost( float cost )
// ---------------------------------------------------------------------------
function format_cost( cost )
{
	// Convert to string so that string methods can be used.
	var temp_cost = cost.toString();

	// Check for decimal point in string.
	var decimal_position = temp_cost.indexOf(".");
	if( decimal_position == -1 ) // Integer
	{
		// Insert commas.
		temp_cost = insert_commas(temp_cost);
		// Add trailing zeros.
		temp_cost = "$" + temp_cost + ".00";
	}
	else // Float
	{	
		// Split the string at the decimal point.
		var string_parts = temp_cost.split(".");
		var integer = string_parts[0];
		var decimal = string_parts[1];
		// Add commas to integer part.
		integer = insert_commas(integer);
		// Work with decimal string.
		var num_decimal_places = decimal.length;
		if( num_decimal_places == 1 )
		{
			decimal += "0";
		}
		else if( num_decimal_places > 2 )
		{
			// Round to two decimal places (converts to float).
			var rounded_decimal = Math.round((decimal/100));
			// Convert to string.
			decimal = rounded_decimal.toString();
			// Limit string to two chars.
			decimal = decimal.substring(0, 2);
		}
		// Reassemble cost string.
		temp_cost = "$" + integer + "." + decimal;
	}
	return temp_cost;
}

// ---------------------------------------------------------------------------
// void estimate_service( string form_id )
// ---------------------------------------------------------------------------
function estimate_service( form_id )
{
	//
	var service = document.getElementById( "ServiceType" + form_id ).value;
	var length = document.getElementById( "Length" + form_id ).value;
	var width = document.getElementById( "Width" + form_id ).value;
	
	// Clean input
	var errorMsg = "";
	var displayValue = "none";
	if( length == "" ) { length = 0 };
	if( width == "" ) { width = 0 };
	if( isNaN( length ) || isNaN( width ) )
	{
		errorMsg = "<p>Please enter only integer or decimal numbers may be entered.</p>";
		displayValue = "block";
	}
	document.getElementById( "Msg" + form_id ).innerHTML = errorMsg;
	document.getElementById( "Msg" + form_id ).style.display = displayValue;

	// AREA
	var area = calculate_area( length, width );

	// COST
	var rate = 0;
	// Select the rate to use.
	var rate = eval( service + '_rate' );
	var cost = calculate_cost( area, rate);
	// Prepare cost for display.
	var formatted_cost = format_cost( cost );
	// Display total price
	//document.getElementById( "Total" + form_id ).value = formatted_cost;
	document.getElementById( "Total" + form_id ).innerHTML = formatted_cost;
}

// ---------------------------------------------------------------------------
// float insert_commas( string str_number )
// ---------------------------------------------------------------------------
function insert_commas( str_number )
{
	var num_digits = str_number.length;
	var str_reversed = str_number.reverse();
	for( var i = 0; i < num_digits; i++ )
	{
		if( i==3 || i==6 || i==9 || i==12 || i==15 || i==18 || i==21 )
		{
			var offset = i / 3 - 1;
			var left = str_reversed.substring( 0, i + offset );
			var right = str_reversed.substring( i + offset );
			str_reversed = left + "," + right;
		}
	}
	var str_formatted = str_reversed.reverse();
	return str_formatted;
}

String.prototype.reverse = function() {return this.split('').reverse().join('')};

// EOF