function addCommas2obj(obj) {
	var num = obj.value;
	num = "" + num;			// convert to string
	num = num.replace(/,/g, '');
	num = parseInt(num);
	if ( isNaN(num) )
		num = 0;
	nStr = '' + num;
	if ( nStr == '' )
		nStr = '0';
	var rgx = /(\d+)(\d{3})/;
	while ( rgx.test(nStr) ) {
		nStr = nStr.replace(rgx, '$1' + ',' + '$2');
	}
	obj.value = nStr;

	if ( num < 50 || num > 1000 ) {		// check for 50 - 1000 range
		alert ('Please enter only a value that is between 50 and 1000');
      }
}

function removeCommas(num) {
	num = num.replace(/,/g, "");		//remove any commas
	num = num.replace(/\s/g, "");		//remove any spaces
	return num;
}

function checkNum(num) {
// throw an error if the number entered has more than one decimal, or any non-numeric characters
	var num = num + "";
	// if the number has one or zero decimal points, lastIndexOf and indexOf will give the same answer
	if ( num.lastIndexOf(".") != num.indexOf(".") ) {
		alert ('Please enter no more than 1 decimal point for Projected Tons per Day of Waste');
		return;
	}
	if ( num.match(/[^0-9.]/) ) {		// check for ONLY numbers and decimals
		alert ('Please enter only a simple numeric value for Projected Tons per Day of Waste');
	}
}

function addCommas2num(num) {
	// add any needed commas for proper formatting
	num = parseFloat(num);
	nStr = "" + num;			// convert to string
	if( nStr == '' )
	var x= new Array(2);
	x = nStr.split('.');
	var x1 = x[0];
	var x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return (x1 + x2);
}

function DoCalc() {
	// constants
	var OneBoilerHorsePower = 33520;		// (approx) BTU/hr
	var PoundsOfSteamPerHourFromOneBHP = 34.5;		// lb steam/hr
	var BoilerEntryTempInDegreesF = 1600;		// degrees F
	var BoilerExitTempInDegreesF = 500;		// degrees F
	var PoundsPerSquareInchBoilerPressure = 650;		// psi
	var PoundsPerCubicFootDensityOfAir = 0.075;		// lb / cubic ft
	var BtuPerPoundSpecificHeatOfAir = 0.25;		// BTU/lb
	var PoundsOfSteamGeneratedPerHourPerBHP = 34.5;		// lb of steam/hr
	var EfficiencyFactor = 10;
	var PercentAssumedLossToCarbonChar = 0.25;
	var PercentNotLostToCarbonChar = 1 - PercentAssumedLossToCarbonChar;

	// get input
	var TonsPerDayOfWaste = document.getElementById("TonsPerDayOfWaste").value;
	TonsPerDayOfWaste = removeCommas(TonsPerDayOfWaste);
	checkNum(TonsPerDayOfWaste);
	var MaterialBTUPerPound = document.getElementById("FeedMaterial").value;

	// do the math
	var BtuPerHour = MaterialBTUPerPound * TonsPerDayOfWaste * 2000 / 24;		// 2000 pounds/ton and 24 hours/day
	var BtuToThermalOxidizer = BtuPerHour * PercentNotLostToCarbonChar;
	var AirFlowToThermalOxidizerSCFM = BtuToThermalOxidizer / ( 60 * 1600 * PoundsPerCubicFootDensityOfAir * BtuPerPoundSpecificHeatOfAir );
	var BtuTransferredToBoiler = AirFlowToThermalOxidizerSCFM * 60 * 1375 * PoundsPerCubicFootDensityOfAir * BtuPerPoundSpecificHeatOfAir;
	var GeneratedBoilerHp = BtuTransferredToBoiler / OneBoilerHorsePower;
	var GeneratedSteam = GeneratedBoilerHp * PoundsOfSteamGeneratedPerHourPerBHP;
	var KwhOfPowerProducedFromSteam = GeneratedSteam / 10;

	// show the result, rounded to 2 decimal places
	KwhOfPowerProducedFromSteam = KwhOfPowerProducedFromSteam * 100;
	KwhOfPowerProducedFromSteam = Math.round(KwhOfPowerProducedFromSteam);
	KwhOfPowerProducedFromSteam = KwhOfPowerProducedFromSteam / 100;
      BtuPerHour = BtuPerHour * 100;
      BtuPerHour = Math.round(BtuPerHour);
      BtuPerHour = BtuPerHour / 100;
	document.getElementById('KwhOfPowerProducedFromSteam').value = addCommas2num(KwhOfPowerProducedFromSteam);
	document.getElementById('BtusPerHour').value = addCommas2num(BtuPerHour);
}