// Calculate the total estimate electrical usage (in dollars) using the
// following formula:
//
//	B := BTU/hr.
//	H := hours
//	D := $/kwh
//	E := EER
//
//	B (btu/hr) * H hr * D ($/kwh) = BHD (btu $/kwh)
//	E (eer) * 1000 = 1000 * E kwh
function calculate()
  {
	var b = 0; var b_err = 0;
	var h = 0; var h_err = 0;
	var d = 0; var d_err = 0;
	var e = 0; var e_err = 0;
	var t = 0;
	var error = '';
	var focus = '';	// This'll track where we want to focus the cursor in case 
			// of some user input error.

	b = document.form2.btu.value;
	h = document.form2.hours.value;
	d = document.form2.cents.value;
	e = document.form2.seer.value;

	// Make sure all values are positive real numbers.
	if(!isFloat(e) || e <= 0)
	  {
	  	error += "The SEER value must be a positive number.\n";
		e_err = 1;
		focus = 'e';
	  }
	if(!isFloat(d) || d <= 0)
	  {
	  	error += "The dollar value must be a positive number.\n";
		d_err = 1;
		focus = 'd';
	  }
	if(!isFloat(h) || h <= 0)
	  {
	  	error += "The number of hours must be a positive number.\n";
		h_err = 1;
		focus = 'h';
	  }
	if(!isInteger(b) || b <= 0)
	  {
	  	error += "For the BTU, input whole numbers only, no commas needed.\n";
		b_err = 1;
		focus = 'b'
	  }

	// If there were no errors, calculate the total and display it.
	if(!error)
	  {
		var total = (b * h * (d / 100)) / (e * 1000);

		// Convert total to a string value with two decimal digits
		total = total.toFixed(2) + '';

		document.form2.total.value = total;
	  }
	else	// Otherwise...
	  {
		// Display the error.
		alert(error);

	 	// Reset any invalid fields. 
		if(b_err) { document.form2.btu.value   = ''; }
		if(h_err) { document.form2.hours.value = ''; }
		if(d_err) { document.form2.cents.value = ''; }
		if(e_err) { document.form2.seer.value  = ''; }

		// Move the cursor to the first invalid field
		if(focus == 'b') { document.form2.btu.focus(); }
		if(focus == 'h') { document.form2.hours.focus(); }
		if(focus == 'd') { document.form2.cents.focus(); }
		if(focus == 'e') { document.form2.eer.focus(); }

		// Reset the error message.
		error = '';
	  }
  }
