function ComputeTotal(obj1, obj2, obj3, frm) {
	var col1 = 0
	var col2 = 0
	var col3 = 0

	if (obj1.value != "") {
		col1 = parseFloat(stringFilter(obj1.value));
		obj1.value = fmtCurrencyWhole(col1);
		}
	if (obj2.value != "") {
		col2 = parseFloat(stringFilter(obj2.value));
		obj2.value = fmtCurrencyWhole(col2);
		}		
	col3 = col1 + col2;
	obj3.value = fmtCurrencyWhole(col3);
				
	/* If this is a "wages" col, compute Payroll Taxes */
	if ((obj1.name == "C12") || (obj1.name == "D12")) {
		ComputePayrollTaxes(obj1, obj2, obj3, frm);
	}
	/* Compute Total Operating Expenses */
	TotalOperatingExpenses(frm);
	
	/* Comput the Operating Income */
	OperatingIncome(frm)
	
	/* Compute Net Income Before Taxes*/
	NetIncomeBeforeTaxes(frm)

	/* Compute Net Income */	
	NetIncome(frm);
	
	/* Compute Freight in of Previous Month */
	FreightInOfPreviousMonth(frm);
	
	/* Compute Cash Balance at end of month */
	CashBalanceEndOfMonth(frm)	
}

function ComputeGrossProfitPcnt(obj1, obj2, obj3, CashSales, GPPcnt, CGS1, CGS2, frm){
	var col1 = 0
	var col2 = 0
	var col3 = 0
	var a = 0
	
	if (obj1.value != "") {
		col1 = parseFloat(stringFilter(obj1.value));
		obj1.value = FormatPercentage(col1,2)+ "%";
	}
	if (obj2.value != "") {
		col2 = parseFloat(stringFilter(obj2.value));
		obj2.value = FormatPercentage(col2,2)+ "%";
	}
	col3 = col1 + col2;
	if (col3 != 0) {
		a = col3/2;
		obj3.value = FormatPercentage(a,2)+ "%";

		/* Compute "Cost of Goods Sold" = CashSales * (1 - Gross Profit Percentage) */
		a = parseFloat(stringFilter(CashSales.value)) * (1 - (parseFloat(stringFilter(GPPcnt.value)) / 100));
		CGS1.value = fmtCurrencyWhole(a);
		CGS2.value = fmtCurrencyWhole(a);
		/* Cost of Goods Sold is in two places on the form */
		ComputeCostofGoodsSold(frm.C7, frm.D7, frm.E7);
		ComputeCostofGoodsSold(frm.C53, frm.D53, frm.E53);
		ComputeGrossProfit(frm);		
	}
}

/* Called from function ComputeGrossProfitPcnt() */
function ComputeCostofGoodsSold(obj1, obj2, obj3) {
	var col1 = 0
	var col2 = 0
	var col3 = 0
	var a = 0

	if (obj1.value != "") {
		col1 = parseFloat(stringFilter(obj1.value));
	}	
	if (obj2.value != "") {
		col2 = parseFloat(stringFilter(obj2.value));
	}
	a = col1 + col2
	obj3.value = fmtCurrencyWhole(a);
}

/* Called from function ComputeGrossProfitPcnt() */
function ComputeGrossProfit(frm){
	var col1 = 0
	var col2 = 0
	var col3 = 0
	var a = 0
	if ((frm.C6.value != "" ) && (frm.C7.value != "")){
		a = parseFloat(stringFilter(frm.C6.value)) - parseFloat(stringFilter(frm.C7.value));
		col1 = a;
		frm.C8.value = fmtCurrencyWhole(a);
	}
	if ((frm.D6.value != "" ) && (frm.D7.value != "")){
		a = parseFloat(stringFilter(frm.D6.value)) - parseFloat(stringFilter(frm.D7.value));
		col2 = a;
		frm.D8.value = fmtCurrencyWhole(a);
	}
	col3 = col1 + col2;
	frm.E8.value = fmtCurrencyWhole(col3);
	
}

/* Called from function ComputeTotal()  */
function ComputePayrollTaxes(obj1, obj2, obj3, frm){
	var col1 = 0
	var col2 = 0
	var col3 = 0
	var a = 0

	if (obj1.value != "") {
		col1 = parseFloat(stringFilter(obj1.value));
		col1 = col1*0.08;
		frm.C13.value = fmtCurrencyWhole(col1);
	}	
	if (obj2.value != "") {
		col2 = parseFloat(stringFilter(obj2.value));
		col2 = col2*0.08;
		frm.D13.value = fmtCurrencyWhole(col2);
	}
	a = col1 + col2
	frm.E13.value = fmtCurrencyWhole(a);
}

/* Called from function ComputeTotal()  */
function TotalOperatingExpenses(frm) {
	var col1 = 0
	var col2 = 0
	var col3 = 0
	var col
	var totalc = 0 
	var totald = 0
	var totale = 0
	var i
	// Dol for col C
	for (i = 12; i < 31; ++i) {
		col = "frm.C" + i + ".value";
		if (eval(col) != "") {
			col1 = parseFloat(stringFilter(eval(col)));
			totalc += col1;
			frm.C32.value = fmtCurrencyWhole(totalc);
		}	
	}
	
	// Do for col D
	for (i = 12; i < 31; ++i) {
		col = "frm.D" + i + ".value";
		if (eval(col) != "") {
			col2 = parseFloat(stringFilter(eval(col)));
			totald += col2;
			frm.D32.value = fmtCurrencyWhole(totald);
		}	
	}	

	// Do for col E
	for (i = 12; i < 31; ++i) {
		col = "frm.E" + i + ".value";
		if (eval(col) != "") {
			col3 = parseFloat(stringFilter(eval(col)));
			totale += col3;
			frm.E32.value = fmtCurrencyWhole(totale);
		}	
	}	
}

/* Called from function ComputeTotal()  */
function OperatingIncome(frm) {
	var col1 = 0
	var col2 = 0
	var col3 = 0
	
	if ((frm.C8.value != "") && (frm.C32.value != "")) {
		col1 = parseFloat(stringFilter(frm.C8.value));
		col2 = parseFloat(stringFilter(frm.C32.value));
		col3 = (col1 - col2);
		frm.C34.value = fmtCurrencyWhole(col3);

	}
	if ((frm.D8.value != "") && (frm.D32.value != "")) {
		col1 = parseFloat(stringFilter(frm.D8.value));
		col2 = parseFloat(stringFilter(frm.D32.value));
		col3 = (col1 - col2);
		frm.D34.value = fmtCurrencyWhole(col3);
	}	
	if ((frm.E8.value != "") && (frm.E32.value != "")) {
		col1 = parseFloat(stringFilter(frm.E8.value));
		col2 = parseFloat(stringFilter(frm.E32.value));
		col3 = (col1 - col2);
		frm.E34.value = fmtCurrencyWhole(col3);
	}	
}

function NetIncomeBeforeTaxes(frm) {
// NetIncomeBeforeTaxes = (Operating Income + Misc. Income + Interest Income) - Interest Espense
	var col1 = 0
	var col2 = 0
	var col3 = 0
	var oper_inc = 0
	var misc_inc = 0
	var int_inc = 0
	var int_exp = 0
// Do for Column "C"	
	// Operating Income
	if (frm.C34.value != "") {
		oper_inc = parseFloat(stringFilter(frm.C34.value));
	}
	
	// Miscellaneous Income
	if (frm.C37.value != "") {
		misc_inc = parseFloat(stringFilter(frm.C37.value));
	}
	
	// Interest Income
	if (frm.C38.value != "") {
		int_inc = parseFloat(stringFilter(frm.C38.value));
	}
		
	// Interest Expense
	if (frm.C39.value != "") {
		int_exp = parseFloat(stringFilter(frm.C39.value));
	}
	
	// Compute Net Income Before Taxes for column "C"
	frm.C41.value = fmtCurrencyWhole((oper_inc + misc_inc + int_inc) - int_exp);
	col1 = (oper_inc + misc_inc + int_inc) - int_exp;
	
// Do for Column "D"
	// Clear the variables
	oper_inc = 0;
	misc_inc = 0;
	int_inc = 0;
	int_exp = 0;	
		
	// Operating Income
	if (frm.D34.value != "") {
		oper_inc = parseFloat(stringFilter(frm.D34.value));
	}
	
	// Miscellaneous Income
	if (frm.D37.value != "") {
		misc_inc = parseFloat(stringFilter(frm.D37.value));
	}
	
	// Interest Income
	if (frm.D38.value != "") {
		int_inc = parseFloat(stringFilter(frm.D38.value));
	}
		
	// Interest Expense
	if (frm.D39.value != "") {
		int_exp = parseFloat(stringFilter(frm.D39.value));
	}
	
	// Compute Net Income Before Taxes for column "D"
	frm.D41.value = fmtCurrencyWhole((oper_inc + misc_inc + int_inc) - int_exp);
	col2 = (oper_inc + misc_inc + int_inc) - int_exp;
	
// Do for column "E"	
	col3 = col1 + col2;
	frm.E41.value = fmtCurrencyWhole(col3);			
}

function NetIncome(frm) {
// Net Income = (Net Income Before Taxes - Federal Income Taxes)
	var col1 = 0;
	var col2 = 0;
	var col3 = 0;
	var net_inc_before_taxes = 0;
	var fed_inc_tax = 0;

// Do for column "C"	
	if (frm.C41.value != "") {
		net_inc_before_taxes = parseFloat(stringFilter(frm.C41.value));
	}
	if (frm.C43.value != "") {
		fed_inc_tax = parseFloat(stringFilter(frm.C43.value));
	}
	col1 = (net_inc_before_taxes - fed_inc_tax);
	frm.C45.value = fmtCurrencyWhole(col1);
	frm.C50.value = fmtCurrencyWhole(col1);
	
// Do for column "D"

	// Clear variables
	net_inc_before_taxes = 0;
	fed_inc_tax = 0;
		
	if (frm.D41.value != "") {
		net_inc_before_taxes = parseFloat(stringFilter(frm.D41.value));
	}
	if (frm.D43.value != "") {
		fed_inc_tax = parseFloat(stringFilter(frm.D43.value));
	}
	col2 = (net_inc_before_taxes - fed_inc_tax);
	frm.D45.value = fmtCurrencyWhole(col2);
	frm.D50.value = fmtCurrencyWhole(col2);	
	
// Do for column "E"
	col3 = col1 + col2;
	frm.E45.value = fmtCurrencyWhole(col3);
	frm.E50.value = fmtCurrencyWhole(col3);			
}

function FreightInOfPreviousMonth(frm){
//Freight in of Previous Month = (Inventory Purchases of Previous Month * 0.02)
	var col1 = 0;
	var col2 = 0;
	var col3 = 0;
	
	if (frm.C56.value != "") {
		col1 = (parseFloat(stringFilter(frm.C56.value))*0.02);
	}
	if (frm.D56.value != "") {
		col2 = (parseFloat(stringFilter(frm.D56.value))*0.02);
	}
	col3 = col1 + col2;
	frm.C57.value = fmtCurrencyWhole(col1);
	frm.D57.value = fmtCurrencyWhole(col2);
	frm.E57.value = fmtCurrencyWhole(col3);		
}

function CashBalanceEndOfMonth(frm) {
// Cash Balance End of Month = Cash Balance Begining of Month + SUM(Net Income + cost of goods sold) - SUM(Inventory Purchases of Previous Month + Freight in + Principal Payments on loans)
	var col1 = 0;
	var col2 = 0;
	var col3 = 0;
	var cash_bal_beg_of_month = 0;
	var net_income = 0;
	var cost_of_goods_sold = 0;
	var inv_purchases_of_previous_month = 0;
	var freight_in = 0;
	var principal_payments_on_loans = 0;
	
	if (frm.C48.value != "") {
		cash_bal_beg_of_month = parseFloat(stringFilter(frm.C48.value));
	}
	if (frm.C50.value != "") {
		net_income = parseFloat(stringFilter(frm.C50.value));
	}
	if (frm.C53.value != "") {
		cost_of_goods_sold = parseFloat(stringFilter(frm.C53.value));
	}	
	if (frm.C56.value != "") {
		inv_purchases_of_previous_month = parseFloat(stringFilter(frm.C56.value));
	}
	if (frm.C57.value != "") {
		freight_in = parseFloat(stringFilter(frm.C57.value));
	}		
	if (frm.C58.value != "") {
		principal_payments_on_loans = parseFloat(stringFilter(frm.C58.value));
	}	
	col1 = (cash_bal_beg_of_month + ((net_income + cost_of_goods_sold) - (inv_purchases_of_previous_month + freight_in + principal_payments_on_loans)));
	frm.C60.value = fmtCurrencyWhole(col1);	
	frm.D48.value = fmtCurrencyWhole(col1);
	
// Compute for column "D"
	cash_bal_beg_of_month = 0;
	net_income = 0;
	cost_of_goods_sold = 0;
	inv_purchases_of_previous_month = 0;
	freight_in = 0;
	principal_payments_on_loans = 0;
	if (frm.D48.value != "") {
		cash_bal_beg_of_month = parseFloat(stringFilter(frm.D48.value));
	}
	if (frm.D50.value != "") {
		net_income = parseFloat(stringFilter(frm.D50.value));
	}
	if (frm.D53.value != "") {
		cost_of_goods_sold = parseFloat(stringFilter(frm.D53.value));
	}	
	if (frm.D56.value != "") {
		inv_purchases_of_previous_month = parseFloat(stringFilter(frm.D56.value));
	}
	if (frm.D57.value != "") {
		freight_in = parseFloat(stringFilter(frm.D57.value));
	}		
	if (frm.D58.value != "") {
		principal_payments_on_loans = parseFloat(stringFilter(frm.D58.value));
	}	
	col2 = (cash_bal_beg_of_month + ((net_income + cost_of_goods_sold) - (inv_purchases_of_previous_month + freight_in + principal_payments_on_loans)));
	frm.D60.value = fmtCurrencyWhole(col2);
	
// Compute for column "E"
	cash_bal_beg_of_month = 0;
	net_income = 0;
	cost_of_goods_sold = 0;
	inv_purchases_of_previous_month = 0;
	freight_in = 0;
	principal_payments_on_loans = 0;
	if (frm.E48.value != "") {
		cash_bal_beg_of_month = parseFloat(stringFilter(frm.E48.value));
	}
	if (frm.E50.value != "") {
		net_income = parseFloat(stringFilter(frm.E50.value));
	}
	if (frm.E53.value != "") {
		cost_of_goods_sold = parseFloat(stringFilter(frm.E53.value));
	}	
	if (frm.E56.value != "") {
		inv_purchases_of_previous_month = parseFloat(stringFilter(frm.E56.value));
	}
	if (frm.E57.value != "") {
		freight_in = parseFloat(stringFilter(frm.E57.value));
	}		
	if (frm.E58.value != "") {
		principal_payments_on_loans = parseFloat(stringFilter(frm.E58.value));
	}	
	col3 = (cash_bal_beg_of_month + ((net_income + cost_of_goods_sold) - (inv_purchases_of_previous_month + freight_in + principal_payments_on_loans)));
	frm.E60.value = fmtCurrencyWhole(col3);					
}

function SetValues() {
	frm = document.myForm;
	frm.C6.value = fmtCurrencyWhole(62000);
	frm.C9.value = FormatPercentage(36.5,2)+ "%";
	frm.C12.value = fmtCurrencyWhole(11000);
	frm.C15.value = fmtCurrencyWhole(8000);
	frm.C16.value = fmtCurrencyWhole(2400);
	frm.C17.value = fmtCurrencyWhole(150);
	frm.C18.value = fmtCurrencyWhole(1575);
	frm.C19.value = fmtCurrencyWhole(200);
	frm.C20.value = fmtCurrencyWhole(140);
	frm.C21.value = fmtCurrencyWhole(210);
	frm.C22.value = fmtCurrencyWhole(175);
	frm.C23.value = fmtCurrencyWhole(300);
	frm.C24.value = fmtCurrencyWhole(800);
	frm.C25.value = fmtCurrencyWhole(100);
	frm.C26.value = fmtCurrencyWhole(300);
	frm.C27.value = fmtCurrencyWhole(250);
	frm.C28.value = fmtCurrencyWhole(1000);
	frm.C38.value = fmtCurrencyWhole(265);
	frm.C48.value = fmtCurrencyWhole(17500);
	frm.C56.value = fmtCurrencyWhole(42000);

	frm.D6.value = fmtCurrencyWhole(65000);
	frm.D9.value = FormatPercentage(36.5,2)+ "%";
	frm.D12.value = fmtCurrencyWhole(17500);
	frm.D15.value = fmtCurrencyWhole(8000);
	frm.D16.value = fmtCurrencyWhole(2400);
	frm.D17.value = fmtCurrencyWhole(150);
	frm.D18.value = fmtCurrencyWhole(1600);
	frm.D19.value = fmtCurrencyWhole(200);
	frm.D20.value = fmtCurrencyWhole(140);
	frm.D21.value = fmtCurrencyWhole(210);
	frm.D22.value = fmtCurrencyWhole(175);
	frm.D23.value = fmtCurrencyWhole(0);
	frm.D24.value = fmtCurrencyWhole(800);
	frm.D25.value = fmtCurrencyWhole(0);
	frm.D26.value = fmtCurrencyWhole(300);
	frm.D27.value = fmtCurrencyWhole(250);
	frm.D28.value = fmtCurrencyWhole(0);
	frm.D38.value = fmtCurrencyWhole(0);
	frm.D56.value = fmtCurrencyWhole(54850);
	
	ComputeTotal(frm.C6, frm.D6, frm.E6, frm);
	ComputeGrossProfitPcnt(frm.C9, frm.D9, frm.E9, frm.C6, frm.C9, frm.C7, frm.C53, frm)
	ComputeGrossProfitPcnt(frm.C9, frm.D9, frm.E9, frm.D6, frm.D9, frm.D7, frm.D53, frm)
	ComputeTotal(frm.C12, frm.D12, frm.E12, frm);
	ComputeTotal(frm.C15, frm.D15, frm.E15, frm);
	ComputeTotal(frm.C16, frm.D16, frm.E16, frm);
	ComputeTotal(frm.C17, frm.D17, frm.E17, frm);
	ComputeTotal(frm.C18, frm.D18, frm.E18, frm);
	ComputeTotal(frm.C19, frm.D19, frm.E19, frm);
	ComputeTotal(frm.C20, frm.D20, frm.E20, frm);
	ComputeTotal(frm.C21, frm.D21, frm.E21, frm);
	ComputeTotal(frm.C22, frm.D22, frm.E22, frm);
	ComputeTotal(frm.C23, frm.D23, frm.E23, frm);	
	ComputeTotal(frm.C24, frm.D24, frm.E24, frm);
	ComputeTotal(frm.C25, frm.D25, frm.E25, frm);	
	ComputeTotal(frm.C26, frm.D26, frm.E26, frm);
	ComputeTotal(frm.C27, frm.D27, frm.E27, frm);
	ComputeTotal(frm.C28, frm.D28, frm.E28, frm);
	ComputeTotal(frm.C38, frm.D38, frm.E38, frm);
	ComputeTotal(frm.C48, frm.D48, frm.E48, frm);
	ComputeTotal(frm.C56, frm.D56, frm.E56, frm);	
	
}

// Code below is for "What_is_a_customer_worth.htm"
var co = new Object;

function recalc_onclick(ctl) {

  //if (document.myForm.automatic_recalc.checked || ctl=='') {
	co.pA1B=eeparseFloatTh(document.myForm.pA1B.value);
	co.pA2B=eeparseFloat(document.myForm.pA2B.value);
	co.pA4B=eeparseFloat(document.myForm.pA4B.value);
	co.pA6B=eeparsePercent(document.myForm.pA6B.value);
	calc(co);
	document.myForm.pA3B.value=eedisplayFloatNDTh(co.pA3B,2);
	document.myForm.pA5B.value=eedisplayFloatNDTh(co.pA5B,2);
	document.myForm.pA7B.value=eedisplayFloatNDTh(co.pA7B,2);

//};
};



var eeisus=1;
var eetrue="TRUE";
var eefalse="FALSE";
var eedec=".";
var eeth=",";
var eedecreg=new RegExp("[.]","g");
var eethreg=new RegExp(",","g");


function calc(data){
	var cA1B=data.pA1B;
	var cA2B=data.pA2B;
	var cA4B=data.pA4B;
	var cA6B=data.pA6B;
	var cA3B=(((cA1B)*(cA2B)));
	var cA5B=(((cA3B)*(cA4B)));
	var cA7B=(((cA5B)*(cA6B)));
	data.pA3B=cA3B;
	data.pA5B=cA5B;
	data.pA7B=cA7B;
};


function myIsNaN(x){
	return(isNaN(x)||(typeof x=='number'&&!isFinite(x)));};

function round(n,nd){
	if(isFinite(n)&&isFinite(nd)){
		var sign_n=(n<0)?-1:1;
		var abs_n=Math.abs(n);
		var factor=Math.pow(10,nd);
		return sign_n*Math.round(abs_n*factor)/factor;
	}else{
		return NaN;}};

function eeparseFloat(str){
	str=String(str).replace(eedecreg,".");
	var res=parseFloat(str);
	if(isNaN(res)){
		return 0;
	}else{return res;}};

function eeparsePercent(str){
	var parts=String(str).split('%');
	var tmp=String(parts[0]).replace(eedecreg,".");
	var res=parseFloat(tmp)/100;
	if(isNaN(res)){return 0;}else{return res;}};

function eedisplayFloat(x){
	if(myIsNaN(x)){
		return Number.NaN;
		}else{
		return String(x).replace(/\./g,eedec);}};

function eedisplayFloatND(x,nd){
	if(myIsNaN(x)){return Number.NaN;
	}else{
		var res=round(x,nd);
		if(nd>0){
			var str=String(res);
			if(str.indexOf('e')!=-1)return str;
				if(str.indexOf('E')!=-1)return str;
					var parts=str.split('.');
					if(parts.length<2){
						var decimals=('00000000000000').substring(0,nd);
						return(parts[0]).toString()+eedec+decimals;
						}else{
							var decimals=((parts[1]).toString()+'00000000000000').substring(0,nd);
							return(parts[0]).toString()+eedec+decimals;}
							}else{
							return res;}}};

function eedisplayPercentND(x,nd){
	if(myIsNaN(x)){
		return Number.NaN;
		}else{
		return eedisplayFloatND(x*100,nd)+'%';}}

function eeparseFloatTh(str){
	str=String(str).replace(eethreg,"");
	str=String(str).replace(eedecreg,".");
	var res=parseFloat(str);
	if(isNaN(res)){return 0;
	}else{
	return res;}};

function eedisplayFloatNDTh(x,nd){if(myIsNaN(x)){return Number.NaN;}else{var res=round(x,nd);if(nd>0){var str=String(res);if(str.indexOf('e')!=-1)return str;if(str.indexOf('E')!=-1)return str;var parts=str.split('.');var res2=eeinsertThousand(parts[0].toString());if(parts.length<2){var decimals=('00000000000000').substring(0,nd);return(res2+eedec+decimals);}else{var decimals=((parts[1]).toString()+'00000000000000').substring(0,nd);return(res2+eedec+decimals);}}else{return(eeinsertThousand(res.toString()));}}};

function eeinsertThousand(whole){if(whole==""||whole.indexOf("e")>=0){return whole;}else{var minus_sign="";if(whole.charAt(0)=="-"){minus_sign="-";whole=whole.substring(1);};var res="";var str_length=whole.length-1;for(var ii=0;ii<=str_length;ii++){if(ii>0&&ii%3==0){res=eeth+res;};res=whole.charAt(str_length-ii)+res;};return minus_sign+res;}};	