var tot_units = 0;
var gross_sales = 0;
var gross_profit = 0;

	function roundNumber(numberInput){
		numberInput = (Math.round(numberInput*Math.pow(10,2))/Math.pow(10,2));
		return numberInput;
	}

	function acbCalc(acbForm) {
		with (acbForm) {
			if (!unitnum.value || isNaN(unitnum.value) || !unitprice.value || isNaN(unitprice.value) || unitnum.value <= 0 ) {
				alert("Please re-enter the price and unit amounts again in the correct format. A field must not be blank or contain commas.");
				return;
			}
			else {
				// compute gross margin
				if (!unitcost.value || isNaN(unitcost.value) ) {
					alert("When computing Gross Margin, the field must not be blank or contain commas.");
					return;
				}
				gross_sales = (unitnum.value*1) * (unitprice.value*1);
				grosssales.value = d_places(gross_sales, 2);
				computedgrosssales.value = d_places(gross_sales, 2);
				grossmargin.value = d_places(((unitprice.value - unitcost.value)/unitprice.value*100), 2);
				gross_profit = ((unitnum.value*1) * (unitprice.value*1) - (unitnum.value*1) * (unitcost.value*1));
				grossprofit.value = d_places(gross_profit,2);
			   }
			}
		}
	
	// sets the decimal precision
	function d_places(n,p) {
		var factor = 1;
		var ans  = 0;
		var newgm = 0;
		ans = n.toFixed(p); // BRG testing	
		return ans;
	}
	
	function new_amt(acbForm) {
		var newamt=0;
		var newgpperunit=0;
		var newgp=0;
		newprofitperitem=0;
		var newgpunits=0;
		with (acbForm) {
			if (!newprice.value || isNaN(newprice.value)) {
				alert("Please re-enter the price amount again in the correct format.");
				return;
			}
			else {
				newamt = (computedgrosssales.value*1) / (newprice.value*1);
				mustsellunits.value = roundNumber(newamt);
				newgpperunit = ((newprice.value*1) - (unitcost.value*1));
				newgp = (newgpperunit*1) * (newamt*1);
				newgrossprofit.value = roundNumber(newgp);
				computedgrossmargin.value = "";
				
				 // compute new gross margin 
				newgm = ((newprice.value - unitcost.value)/newprice.value*100);
				computedgrossmargin.value = roundNumber(newgm);
				
				//  Compute # of units to maintain same Gross Profit Dollars
				newprofitperitem = ((newprice.value*1) - (unitcost.value*1));
				newgpunits = (gross_profit) / (newprofitperitem);
				mustsellunits4GPDollars.value = roundNumber(newgpunits);	
			}
	  	}	
	}
	