// JavaScript Document
function leaveAlone()
	{
alert("Total Accounts Receivable \n will be automatically calculated when \n you press the  'Calculate Accounts Recv. Aging' button");
	}
function calcMarkup()
	{
	var price  = parseFloat(document.RMath.txtPrice.value);
	var cost   = parseFloat(document.RMath.txtCost.value);
	var markup = parseFloat(((price - cost)/price)*100);
	markup     = (  Math.round(markup*Math.pow(10,2))/Math.pow(10,2)   );	
	document.RMath.txtMarkup.value = markup;
	}
function calcCostGoodsSold()
	{
	var beginInvCost = parseFloat(document.RMath.txtBegInvCost.value);
	var purCost      = parseFloat(document.RMath.txtPurCost.value);
	var endInvCost   = parseFloat(document.RMath.txtEndInvCost.value);
	
	var costGoodSold = parseFloat(beginInvCost + purCost - endInvCost);
	costGoodSold     = (  Math.round(costGoodSold*Math.pow(10,2))/Math.pow(10,2)   );	
	document.RMath.txtCostGoodSold.value = costGoodSold;
	// preset CostGoodSold for next function
	document.RMath.txtCostGoodSoldTwo.value = document.RMath.txtCostGoodSold.value;
	document.RMath.txtCostGoodsSold3.value = document.RMath.txtCostGoodSold.value;
	document.RMath.txtCostGoodsSold4.value = document.RMath.txtCostGoodSold.value;

	}
function calcTurnRate()
	{
	var costGoodSoldTwo = parseFloat(document.RMath.txtCostGoodSoldTwo.value);
	var avrInv        = parseFloat(document.RMath.txtAvrInv.value);
	var turnRate      = parseFloat(costGoodSoldTwo/avrInv);
	turnRate = (Math.round(turnRate*Math.pow(10,2))/Math.pow(10,2));
	document.RMath.txtTurnRate.value = turnRate;

	document.RMath.txtCostGoodsSold3.value = document.RMath.txtCostGoodSoldTwo.value;
	document.RMath.txtCostGoodsSold4.value = document.RMath.txtCostGoodSoldTwo.value;

	}
function calcCurrentRatio()
	{
	var curAsst    = parseFloat(document.RMath.txtCurAsst.value);
	var totalLiab  = parseFloat(document.RMath.txtTotalLiab.value);
	var curRatio   = parseFloat(curAsst/totalLiab);
	curRatio       = (Math.round(curRatio*Math.round(10,2))/Math.pow(10,2));
	document.RMath.txtCurRatio.value = curRatio;
	// preset CurrentAsstest for next functions
	document.RMath.txtCurAsstTwo.value = document.RMath.txtCurAsst.value;
	document.RMath.txtCurAsstThree.value = document.RMath.txtCurAsst.value;

	}
function calcDebtNetRatio()
	{
	var curAsstTwo     = parseFloat(document.RMath.txtCurAsstTwo.value);
	var totalDebt      = parseFloat(document.RMath.txtTotalDebt.value);
	var debtWorthRatio = parseFloat(curAsstTwo/totalDebt);
	debtWorthRatio     = (Math.round(debtWorthRatio*Math.round(10,2))/Math.pow(10,2));
	document.RMath.txtDebtNWorthRatio.value = debtWorthRatio + ':1';
	// preset TotalDebt for next function
	document.RMath.txtTotalDebtTwo.value = document.RMath.txtTotalDebt.value;
	document.RMath.txtCurAsstThree.value = document.RMath.txtCurAsstTwo.value;
	}
function calcAcidRatio()
	{
	var curAsstThree = parseFloat(document.RMath.txtCurAsstThree.value);
	var inv = parseFloat(document.RMath.txtInv.value);
	var curLiab = parseFloat(document.RMath.txtCurLiab.value);
	var acidRatio = parseFloat((curAsstThree - inv)/curLiab);
	acidRatio = (Math.round(acidRatio*Math.round(10,2))/Math.round(10,2));
	document.RMath.txtAcidRatio.value = acidRatio + ':1';
	}
function calcDaysSalesOutstanding()
	{
	var rec         = parseFloat(document.RMath.txtRec.value);
	var netSales    = parseFloat(document.RMath.txtNetSales.value);
	var daysSaleOut =	parseFloat((rec*365)/netSales);
	var daysSaleOut = (Math.round(daysSaleOut*Math.round(10,2))/Math.round(10,2));
	document.RMath.txtDaysSaleOut.value = daysSaleOut;	
	// preset TotalDebt for next function
	document.RMath.txtNetSales2.value = document.RMath.txtNetSales.value;
	document.RMath.txtNetSales3.value = document.RMath.txtNetSales.value;

	}
function calcDebtToEquityRatio()
	{
	var totalDebt      = parseFloat(document.RMath.txtTotalDebtTwo.value);
	var totalEquity    = parseFloat(document.RMath.txtTotalEqt.value);
	var debtToEqtRatio = parseFloat(totalDebt/totalEquity);
	debtToEqtRatio     =(Math.round(debtToEqtRatio*Math.round(10,2))/Math.round(10,2));
	document.RMath.txtDebtEqtRatio.value = debtToEqtRatio;
	// preset TotalEquity for next function
	document.RMath.txtTotalEqtTwo.value = document.RMath.txtTotalEqt.value;
	}
function calcReturnOnEqt()
	{
	var netIncome   = parseFloat(document.RMath.txtNetIncome.value);
	var totalEqtTwo = parseFloat(document.RMath.txtTotalEqtTwo.value);
	var returnOnEqt = parseFloat(netIncome/totalEqtTwo);
	returnOnEqt =(Math.round(returnOnEqt*Math.round(10,2))/Math.round(10,2));
	document.RMath.txtReturnOnEqt.value = returnOnEqt;
	// preset NetIncome for next function
	document.RMath.txtNetIncomeTwo.value = document.RMath.txtNetIncome.value;
	}
function calcReturnOnAssets()
	{
	var netIncomeTwo = parseFloat(document.RMath.txtNetIncomeTwo.value);
	var totalAsst = parseFloat(document.RMath.txtTotalAsst.value);
	var returnOnAsst = parseFloat(netIncomeTwo/totalAsst);
	returnOnAsst =(Math.round(returnOnAsst*Math.round(10,2))/Math.round(10,2));
	document.RMath.txtRetOnAsst.value = returnOnAsst;
	}
function ActRecTurnover()
	{
	var netCreditSales = parseFloat(document.RMath.txtNetCreditSales.value);
	var avrAcctRec = parseFloat(document.RMath.txtAvrAcctRec.value);
	var actRecTurnover = netCreditSales/avrAcctRec; 
	actRecTurnover = (Math.round(actRecTurnover*Math.round(10,2))/Math.round(10,2));
	document.RMath.txtActRecTurnover.value = actRecTurnover;
	}
	
function ActRecAging()
	{
	var under30ActRec      = parseFloat(document.RMath.txt30ActRec.value);
	var under3060ActRec    = parseFloat(document.RMath.txt3060ActRec.value);
	var under6090ActRec    = parseFloat(document.RMath.txt6090ActRec.value);
	var under9012ActRec    = parseFloat(document.RMath.txt9012ActRec.value);
	var over120PlusActRec = parseFloat(document.RMath.txt120PlusActRec.value);
	var totalPlusActRec = under30ActRec + under3060ActRec + under6090ActRec + under9012ActRec + over120PlusActRec;
	document.RMath.txtTotalPlusActRec.value = totalPlusActRec;
	
	var under30Perc = under30ActRec/totalPlusActRec;
	under30Perc = (Math.round(under30Perc*Math.round(10,2))/Math.round(10,2));
	document.RMath.txtUnder30.value = (under30Perc*100) + "%";
	
	var under3060Perc = under3060ActRec/totalPlusActRec;
	under3060Perc = (Math.round(under3060Perc*Math.round(10,2))/Math.round(10,2));
	document.RMath.txtUnder3060.value = (under3060Perc*100) + "%";
	
	var under6090Perc = under6090ActRec/totalPlusActRec;
	under6090Perc = (Math.round(under6090Perc*Math.round(10,2))/Math.round(10,2));
	document.RMath.txtUnder6090.value = (under6090Perc*100) + "%";
	
	var under90120Perc = under9012ActRec/totalPlusActRec;
	under90120Perc = (Math.round(under90120Perc*Math.round(10,2))/Math.round(10,2));
	document.RMath.txtUnder90120.value = (under90120Perc*100) + "%";
	
	var over120Perc = over120PlusActRec/totalPlusActRec;
	over120Perc = (Math.round(over120Perc*Math.round(10,2))/Math.round(10,2));
	document.RMath.txtOver120.value = (over120Perc*100) + "%";
	}
	

function daysInvOnHand()
	{
	var Inv2 = parseFloat(document.RMath.txtInv2.value);
	var costGoodsSold3 = parseFloat(document.RMath.txtCostGoodsSold3.value);
	var daysInvHand = Inv2/(costGoodsSold3/360);

	daysInvHand = (Math.round(daysInvHand*Math.round(10,2))/Math.round(10,2));
	document.RMath.txtDaysInvHand.value = daysInvHand;

	document.RMath.txtCostGoodsSold4.value = document.RMath.txtCostGoodsSold3.value;
	}


function grossProfitPerc()
	{
	var netSales2= parseFloat(document.RMath.txtNetSales2.value);
	var costGoodsSold4= parseFloat(document.RMath.txtCostGoodsSold4.value);
	var grossProfit = (netSales2-costGoodsSold4)/netSales2;
	grossProfit = (Math.round(grossProfit*Math.round(10,2))/Math.round(10,2));

	document.RMath.txtGrossProfit.value = (grossProfit*100)+"%";

	document.RMath.txtNetSales3.value = document.RMath.txtNetSales2.value;
	}


function operExpPerc()
	{
	var operExpenses= parseFloat(document.RMath.txtOperExpenses.value);
	var netSales3= parseFloat(document.RMath.txtNetSales3.value);

	var operExp = operExpenses/netSales3;
	
	operExp = (Math.round(operExp*Math.round(10,2))/Math.round(10,2));

	document.RMath.txtOperExpPerc.value = (operExp*100) + "%";
	}


/** -----------------  start 5 NEW FORMULAS ----------------------------- */



/* validate field input: */

function validateInput(data, warningMsg){	
	var input = data;
	var show = warningMsg;
		
	if(isNaN(input) == true){
		alert(show);
		return false;
	}
}
 

function salesToInventoryRatio()
	{	
		var sales = parseFloat(document.RMath.txtSales.value);		
		
		if(validateInput(sales, "check sales") == false){
			return false;
		}		

		var inventory = parseFloat(document.RMath.txtInventory.value);

		if(validateInput(inventory, "check inventory") == false){
			return false;
		}

		var salesToInventory = (sales/inventory);
		
		// round the number off:
		// Math.round(_VARIABLE_*Math.round(10,2))/Math.round(10,2);
		salesToInventory = Math.round(salesToInventory*Math.round(10,2))/Math.round(10,2);
		document.RMath.txtSalesToInventoryRatio.value = salesToInventory;		
	}

	
function grossMarginReturnOnInventory()
	{
	var grossMargin = parseFloat(document.RMath.txtGrossMargin.value);

		if(validateInput(grossMargin, "check Gross Marging") == false){
			return false;
		}

	
	/* get salesByInventory from salesToInventoryRatio() function
		 make sure field is not empty: 
	*/		
		if (document.RMath.txtSalesToInventoryRatio.value == ""){
		alert("perform Sales to Inventory Ratio calculation first");
		return false;
		}

	var salesByInventory = parseFloat(document.RMath.txtSalesToInventoryRatio.value);
		
		if (isNaN(salesByInventory) == true)
		{
		alert("check and correct entries for \"Sales To Inventory Ratio\" function above");
		return false;
		}
	
	document.RMath.txtSalesByInventory.value = salesByInventory;
	
	var grossMarginReturnOnInv = grossMargin*salesByInventory;
	
	grossMarginReturnOnInv = Math.round(grossMarginReturnOnInv*Math.round(10,2))/Math.round(10,2);
	document.RMath.txtGrossMarginRetOnInv.value = grossMarginReturnOnInv;
	}


function salesPerEmployee()
	{
	
	var netSales4 = parseFloat(document.RMath.txtNetSales4.value);
	var totalHours = parseFloat(document.RMath.txtTotalHours.value);	
	var totalEmployees = parseFloat(document.RMath.txtTotalEmployees.value);
	
	var salesPerEmployee;
	
		if(validateInput(netSales4, "check Net Sales") == false){
			return false;
		}


		/* check HOURS and/or EMPLOYEES are entered */
		if( (isNaN(totalHours) == true) && (isNaN(totalEmployees) == true) ){
			alert("Enter HOURS and or EMPLOYEES");
			return false;		
		}	
		
		/* HOURS are OK, but NO EMPLOYEES */
		else if( (isNaN(totalHours) == false) && (isNaN(totalEmployees) == true)) {
			totalEmployees = totalHours/40;
			document.RMath.txtTotalEmployees.value = totalEmployees;
			
			salesPerEmployee = (netSales4/totalEmployees)
			
			salesPerEmployee = Math.round(salesPerEmployee*Math.round(10,2))/Math.round(10,2);
			document.RMath.txtSalesPerEmployee.value = salesPerEmployee;		
			
			return true;
		}
		
		/* EMPLOYEES are OK, but no HOURS */
		else if( isNaN(totalEmployees) == false ){

			salesPerEmployee = (netSales4/totalEmployees)			
			salesPerEmployee = Math.round(salesPerEmployee*Math.round(10,2))/Math.round(10,2);
			document.RMath.txtSalesPerEmployee.value = salesPerEmployee;	

		return true;
		}
	}


function spaceProductivity()
	{
	var netSales5 = parseFloat(document.RMath.txtNetSales5.value);

		if(validateInput(netSales5, "check Net Sales") == false){
			return false;
		}

	var totalSqFt = parseFloat(document.RMath.txtTotalSqFt.value);

		if(validateInput(totalSqFt, "check Total Square Feet") == false){
			return false;
		}

	var spaceProd = netSales5/totalSqFt;

	// Math.round(Math.pow(10, X)/Math.pow(10, X)) rounds number to X places
	spaceProd = Math.round(spaceProd*Math.pow(10,2))/Math.pow(10,2);
	document.RMath.txtSpaceProductivity.value = spaceProd;	
}


function personalProductivityRatio()
	{
	var totalPayrol = parseFloat(document.RMath.txtTotalPayrol.value);

		if(validateInput(totalPayrol, "check Total Payrol") == false){
			return false;
		}


	var grossProfit2 = parseFloat(document.RMath.txtGrossProfit2.value);	

		if(validateInput(grossProfit2, "check Profit") == false){
			return false;
		}

	var personalProductivityRatio = (totalPayrol/grossProfit2)*100;
	
	personalProductivityRatio = Math.round(personalProductivityRatio*Math.round(10,2))/Math.round(10,2);
	document.RMath.txtPersonalProductivityRatio.value = personalProductivityRatio;
}	
