/*
	Some functions called from this file, are in other .js files
	like CostOfInventory.js
*/

/* Global variables */
var a=0
var b=0
var c=0
var d=0
var e=0
var f=0
var g=0

var h=0
var i=0
var j=0
var k=0
var l=0
var m=0
var n=0

var o = 0
var p = 0
var q = 0

var payroll=0
var paytax=0
var payhealth=0
var paybenefits=0




function  CalcGrossSales(frm) {
	var a=0
	var b=0
	var c=0
	b = stringFilter(frm.PharmacySales.value);
	c = stringFilter(frm.FrontEndSales.value);

	if (b != "") {
		a = a + parseFloat(b);
		frm.PharmacySales.value = fmtCurrency(b);
		}
	if (c != "") {
		a = a + parseFloat(c);		
		frm.FrontEndSales.value = fmtCurrency(c);
		}

	
	frm.GrossSales.value = fmtCurrency(a);
	
	/* Calculate Direct Expense to fill a prescription last year.  This is done here
	in case they go through the whole form and then come back to these fields and make
	changes.  We then need to update the calculated fields that use these values	   	
	*/
	DirectExpenseToFillPrescriptonLY(frm);
	
	/* Calculate expected front end sales */
	ExpectedFrontEndSales(frm);
	
}
function fmtCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
			num = num.substring(0,num.length-(4*i+3))+','+
			num.substring(num.length-(4*i+3));
			return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function fmtCurrencyWhole(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		num = Math.floor(num/100).toString();

	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') + '$' + num);

}			
			
function FormatFrontEndGP(gp) {
	var a=0
	a = stringFilter(gp.value);
	a = parseFloat(a);
	gp.value = FormatPercentage(a,2) + "%";

}
function calcBusinessSquareFootage(frm) {
	var a=0
	var b=0
	var c=0
	b = stringFilter(frm.PharmacySquareFootage.value);
	c = stringFilter(frm.FrontEndSquareFootage.value);
	if (b != "") {
		a = a + parseFloat(b);
		frm.PharmacySquareFootage.value = formatnumber(b);
		}
	if (c != "") {
		a = a + parseFloat(c);		
		frm.FrontEndSquareFootage.value = formatnumber(c);
		}
	frm.BusinessSquareFootage.value = formatnumber(a);
	
	/* Calculate Direct Expense to fill a prescription last year.  This is done here
	in case they go through the whole form and then come back to these fields and make
	changes.  We then need to update the calculated fields that use these values	   	
	*/
	DirectExpenseToFillPrescriptonLY(frm);		
}
function fmtTotalOperatingExpenses(frm) {
	var a=0
	//alert("Welcome to fmtTotalOperatingExpeses");
	a = stringFilter(frm.TotalOperatingExpenses.value);
	if (a != "") {
		a = parseFloat(a);
		frm.TotalOperatingExpenses.value = fmtCurrency(a);
		}
	
	/* Calculate Direct Expense to fill a prescription last year.  This is done here
	in case they go through the whole form and then come back to these fields and make
	changes.  We then need to update the calculated fields that use these values	   	
	*/
	DirectExpenseToFillPrescriptonLY(frm);			
}

/* Calculation for total expenses exclusive to pharmacy */
function calcTotalExpensesExclusiveToPharmacy(frm,frmobj){
	//alert("Welcome to totalExpensesExclusiveToPharmacy");

	var total = 0
	//alert(frmobj.value);
	switch(frmobj.name) 
    { 
       case "PharmacyExpense1":
	   {
	   	 //alert("In case: " + frmobj.name);
		 if (frmobj.value != "") {
			a = stringFilter(frmobj.value);
			a = parseFloat(a);
			frmobj.value = fmtCurrency(a);
			total = a+b+c+d+e+f+g;
			frm.TEETP.value = fmtCurrency(total);
		 }//end if
		 break;
	   }//end case
       case "PharmacyExpense2":
	   {
	   	 //alert("In case: " + frmobj.name);
		 if (frmobj.value != "") {
			b = stringFilter(frmobj.value);
			b = parseFloat(b);
			frmobj.value = fmtCurrency(b);
			total = a+b+c+d+e+f+g;
			//alert(frm.name);			
			frm.TEETP.value = fmtCurrency(total);
		 }//end if
		 break;
	   }//end case
       case "PharmacyExpense3":
	   {
	   	 //alert("In case: " + frmobj.name);	   	 
		 if (frmobj.value != "") {
			c = stringFilter(frmobj.value);
			c = parseFloat(c);
			frmobj.value = fmtCurrency(c);
			total = a+b+c+d+e+f+g;
			//alert(frm.name);			
			frm.TEETP.value = fmtCurrency(total);
		 }//end if
		 break;
	   }//end case
       case "PharmacyExpense4":
	   {
	   	 //alert("In case: " + frmobj.name);	   	 
		 if (frmobj.value != "") {
			d = stringFilter(frmobj.value);
			d = parseFloat(d);
			frmobj.value = fmtCurrency(d);
			total = a+b+c+d+e+f+g;
			//alert(frm.name);			
			frm.TEETP.value = fmtCurrency(total);
		 }//end if
		 break;
	   }//end case	   
       case "PharmacyExpense5":
	   {
	   	 //alert("In case: " + frmobj.name);	   	 
		 if (frmobj.value != "") {
			e = stringFilter(frmobj.value);
			e = parseFloat(e);
			frmobj.value = fmtCurrency(e);
			total = a+b+c+d+e+f+g;
			//alert(frm.name);			
			frm.TEETP.value = fmtCurrency(total);
		 }//end if
		 break;
	   }//end case	   
       case "PharmacyExpense6":
	   {
	   	 //alert("In case: " + frmobj.name);	   	 
		 if (frmobj.value != "") {
			f = stringFilter(frmobj.value);
			f = parseFloat(f);
			frmobj.value = fmtCurrency(f);
			total = a+b+c+d+e+f+g;
			//alert(frm.name);			
			frm.TEETP.value = fmtCurrency(total);
		 }//end if
		 break;
	   }//end case	   	   	   
       case "PharmacyExpense7":
	   {
	   	 //alert("In case: " + frmobj.name);	   	 
		 if (frmobj.value != "") {
			g = stringFilter(frmobj.value);
			g = parseFloat(g);
			frmobj.value = fmtCurrency(g);
			total = a+b+c+d+e+f+g;
			//alert(frm.name);			
			frm.TEETP.value = fmtCurrency(total);
		 }//end if
		 break;
	   }//end case	   
	}//switch	
	
	
	/* Calculate Direct Expense to fill a prescription last year.  This is done here
	in case they go through the whole form and then come back to these fields and make
	changes.  We then need to update the calculated fields that use these values	   	
	*/
	DirectExpenseToFillPrescriptonLY(frm);	
	   
}

/* Calculation for total expenses exclusive to front end */
function calcTotalExpensesExclusiveToFrontEnd(frm,frmobj){
	//alert("Welcome to totalExpensesExclusiveToFrontEnd");

	var total = 0
	//alert(frmobj.value);
	switch(frmobj.name) 
    { 
       case "FrontEndExpense1":
	   {
	   	 //alert("In case: " + frmobj.name);
		 if (frmobj.value != "") {
			h = stringFilter(frmobj.value);
			h = parseFloat(h);
			frmobj.value = fmtCurrency(h);
			total = h+i+j+k+l+m+n;
			frm.TotalExpensesExclusiveToFrontEnd.value = fmtCurrency(total);
		 }//end if
		 break;
	   }//end case
       case "FrontEndExpense2":
	   {
	   	 //alert("In case: " + frmobj.name);
		 if (frmobj.value != "") {
			i = stringFilter(frmobj.value);
			i = parseFloat(i);
			frmobj.value = fmtCurrency(i);
			total = h+i+j+k+l+m+n;
			//alert(frm.name);			
			frm.TotalExpensesExclusiveToFrontEnd.value = fmtCurrency(total);
		 }//end if
		 break;
	   }//end case
       case "FrontEndExpense3":
	   {
	   	 //alert("In case: " + frmobj.name);	   	 
		 if (frmobj.value != "") {
			j = stringFilter(frmobj.value);
			j = parseFloat(j);
			frmobj.value = fmtCurrency(j);
			total = h+i+j+k+l+m+n;
			//alert(frm.name);			
			frm.TotalExpensesExclusiveToFrontEnd.value = fmtCurrency(total);
		 }//end if
		 break;
	   }//end case
       case "FrontEndExpense4":
	   {
	   	 //alert("In case: " + frmobj.name);	   	 
		 if (frmobj.value != "") {
			k = stringFilter(frmobj.value);
			k = parseFloat(k);
			frmobj.value = fmtCurrency(k);
			total = h+i+j+k+l+m+n;
			//alert(frm.name);			
			frm.TotalExpensesExclusiveToFrontEnd.value = fmtCurrency(total);
		 }//end if
		 break;
	   }//end case	   
       case "FrontEndExpense5":
	   {
	   	 //alert("In case: " + frmobj.name);	   	 
		 if (frmobj.value != "") {
			l = stringFilter(frmobj.value);
			l = parseFloat(l);
			frmobj.value = fmtCurrency(l);
			total = h+i+j+k+l+m+n;
			//alert(frm.name);			
			frm.TotalExpensesExclusiveToFrontEnd.value = fmtCurrency(total);
		 }//end if
		 break;
	   }//end case	   
       case "FrontEndExpense6":
	   {
	   	 //alert("In case: " + frmobj.name);	   	 
		 if (frmobj.value != "") {
			m = stringFilter(frmobj.value);
			m = parseFloat(m);
			frmobj.value = fmtCurrency(m);
			total = h+i+j+k+l+m+n;
			//alert(frm.name);			
			frm.TotalExpensesExclusiveToFrontEnd.value = fmtCurrency(total);
		 }//end if
		 break;
	   }//end case	   	   	   
       case "FrontEndExpense7":
	   {
	   	 //alert("In case: " + frmobj.name);	   	 
		 if (frmobj.value != "") {
			n = stringFilter(frmobj.value);
			n = parseFloat(n);
			frmobj.value = fmtCurrency(n);
			total = h+i+j+k+l+m+n;
			//alert(frm.name);			
			frm.TotalExpensesExclusiveToFrontEnd.value = fmtCurrency(total);
		 }//end if
		 break;
	   }//end case	   
	}//switch	   
}

function calcRoomForAdditionalPrescriptions(frm,frmobj){
	//alert("Welcome to calcRoomForAdditionalPrescriptions");
	var n = 0
	n = stringFilter(frmobj.value);
	if (frm.NumberOfPrescriptionsFilledLastYear.value != "") {
		o = stringFilter(frm.NumberOfPrescriptionsFilledLastYear.value);
	}
	if (frm.NumberOfPrescriptionsAnticipatedWithThisPlan.value != "") {
		p = stringFilter(frm.NumberOfPrescriptionsAnticipatedWithThisPlan.value);
	}	
	if (frm.EstimatedMaximumNbrOfPrescriptionsYourBusinessCanFill.value != "") {
		q = stringFilter(frm.EstimatedMaximumNbrOfPrescriptionsYourBusinessCanFill.value);	
	}		
		
	n = parseFloat(n);
	o = parseFloat(o);
	p = parseFloat(p);
	q = parseFloat(q);
	frmobj.value = formatnumber(n);
			
	if (o != "") {
		o = parseFloat(o);
		frm.RoomForAdditionalPrscriptions.value = formatnumber((q - (o + p)));
	}//end if
	if (p != "") {
		p = parseFloat(p);
		frm.RoomForAdditionalPrscriptions.value = formatnumber((q - (o + p)));
	}//end if
	if (q != "") {
		q = parseFloat(q);
		frm.RoomForAdditionalPrscriptions.value = formatnumber((q - (o + p)));
	}//end if
	
	
	/* Calculate Direct Expense to fill a prescription last year.  This is done here
	in case they go through the whole form and then come back to these fields and make
	changes.  We then need to update the calculated fields that use these values	   	
	*/
	DirectExpenseToFillPrescriptonLY(frm);
	
	
	/* Calculate your cost to fill a prescription after acccepting new plan.  This is done here
	in case they go through the whole form and then come back to these fields and make
	changes.  We then need to update the calculated fields that use these values	   	
	*/
	CostToFillPrescriptionAfterAcceptingNewPlan(frm)		
		
}


function  CalcTotalPharmacyExpenses(frm) {

	if (frm.PharmacyPayroll.value != "") {
		payroll = stringFilter(frm.PharmacyPayroll.value);
		payroll = parseFloat(payroll);
		frm.PharmacyPayroll.value = fmtCurrency(payroll);
		}
	if (frm.PharmacyPayrollTaxes.value != "") {
		paytax = stringFilter(frm.PharmacyPayrollTaxes.value);
		paytax = parseFloat(paytax);		
		frm.PharmacyPayrollTaxes.value = fmtCurrency(paytax);
		}
	if (frm.PharmacyHealthLifeInsurances.value != "") {
		payhealth = stringFilter(frm.PharmacyHealthLifeInsurances.value);
		payhealth = parseFloat(payhealth);
		frm.PharmacyHealthLifeInsurances.value = fmtCurrency(payhealth);
		}
	if (frm.PharmacyBenefits.value != "") {
		paybenefits = stringFilter(frm.PharmacyBenefits.value);
		paybenefits = parseFloat(paybenefits);		
		frm.PharmacyBenefits.value = fmtCurrency(paybenefits);
		}		
	frm.TotalPharmacyExpenses.value = fmtCurrency(payroll+paytax+payhealth+paybenefits);
	
	/* Calculate your cost to fill a prescription after acccepting new plan.  This is done here
	in case they go through the whole form and then come back to these fields and make
	changes.  We then need to update the calculated fields that use these values	   	
	*/
	CostToFillPrescriptionAfterAcceptingNewPlan(frm)
	
}
function BasicFormat(frmObj) {
	var str=""
	str = frmObj.value.toString();
	frmObj.value = formatnumber(str);
}

function FormatBankInterestRate(intRate) {
	var a=0
	a = stringFilter(intRate.value);
	a = parseFloat(a);
	intRate.value = FormatPercentage(a,2) + "%";

}

function FormatAveragePrescription(frm,frmObj) {
	//var str=""
	//str = frmObj.value.toString();
	frmObj.value = fmtCurrency(frmObj.value);
	
	/* Compute the expected front end sales */
	ExpectedFrontEndSales(frm);
}

function FormatAWPPercent(intRate) {
	var a=0
	a = stringFilter(intRate.value);
	a = parseFloat(a);
	intRate.value = FormatPercentage(a,2) + "%";

}

function BasicCurrencyFormat(frmObj) {
	frmObj.value = fmtCurrency(frmObj.value);
}

/* Compute pharmacy sales as a percentage of gross sales */
function Pharmacy_Percentage_of_Sales(frm) {
	var pcntofsales = 0;
	
	pcntofsales = (parseFloat(stringFilter(frm.PharmacySales.value)) / parseFloat(stringFilter(frm.GrossSales.value)))*100;
	
	//alert("Pharmacy Sales: " + frm.PharmacySales.value);
	//alert("Pharmacy % of Sales: " + pcntofsales);
	return pcntofsales;
}

/* Compute front end sales as a percentage of gross sales */
function Front_End_Percentage_of_Sales(frm) {
	var pcntofsales = 0;

	pcntofsales = (parseFloat(stringFilter(frm.FrontEndSales.value)) / parseFloat(stringFilter(frm.GrossSales.value)))*100;
	
	//alert("Front End Sales: " + frm.FrontEndSales.value);
	//alert("Front End % of Sales: " + pcntofsales);	
	return pcntofsales;
}

function DirectExpenseToFillPrescriptonLY(frm) {
	//alert("Welcome to DirectExpenseToFillPrescriptonLY");
	/*
	Direct expenses to fill a prescription last year - (Not an easy 
	calculation) Take the expenses total, subtract the pharmacy exclusive 
	expenses, take the remainder and multiply by the percentage of the 
	building occupied by the pharmacy. Then take that number, add 
	expenses exclusive to the pharmacy and you will have expenses 
	directly related to fill a prescription last year.
	*/

	/* Get the expense total */	
	var totOperExp = stringFilter(frm.TotalOperatingExpenses.value);
	
	/* Get the pharmacy exclusive expense total */	
	var pharExclusiveExp = stringFilter(frm.TEETP.value);

	/* Get the square footage of the pharmacy */
	var pharSqFt = stringFilter(frm.PharmacySquareFootage.value);
	
	/* Get the total square footage of the business */
	var busSqFt = stringFilter(frm.BusinessSquareFootage.value);
	
	var total=0;	
	var pharOccupationPercent=0;
	var nbrOfPrescriptions = 0;

	/* Subtract out pharmacy exclusive expenses */
	if ((totOperExp != "") && (pharExclusiveExp != "")){
		total = (parseFloat(totOperExp) - parseFloat(pharExclusiveExp));
	}//end if
	
	/* find the percentage space occupied by pharmacy */
	if ((pharSqFt != "" ) && (busSqFt != "")) {
		pharOccupationPercent = (parseFloat(pharSqFt) / parseFloat(stringFilter(busSqFt)));
	}//end if
		
	if (frm.NumberOfPrescriptionsFilledLastYear.value != "") {
		nbrOfPrescriptions = parseFloat(stringFilter(frm.NumberOfPrescriptionsFilledLastYear.value));
	}else{
		return;
	}//end if	
	
	/* Operating expenses associated to pharmacy as a % of building occupied by pharmacy */ 
	total = total * (pharOccupationPercent * 100);
	
	/* Divide by the number of prescriptions filled */
	total = total / nbrOfPrescriptions;
	frm.DirectExpenseToFillAPrescriptionLastYear.value = fmtCurrency(total);
	
}

function CostToFillPrescriptionAfterAcceptingNewPlan(frm) {
	/*
	Your cost to fill a prescription after accepting new plan - After going 
	through the math of adding a new pharmacist, we are dividing the new 
	total cost and dividing it by the number of prescriptions you expect to 
	now fill.
	*/
	var totNewPharmacyExpenses = stringFilter(frm.TotalPharmacyExpenses.value);
	var nbrAnticipatePresciptions = stringFilter(frm.NumberOfPrescriptionsAnticipatedWithThisPlan.value);
	var YourCost = 0;
	
	if ((totNewPharmacyExpenses != "") && (nbrAnticipatePresciptions != "")) {
		YourCost = (parseFloat(totNewPharmacyExpenses) / parseFloat(nbrAnticipatePresciptions));
		frm.YourCostToFillAPrescriptionAfterAcceptingNewPlan.value = fmtCurrency(YourCost);	
	}
}

function ExpectedFrontEndSales(frm) {
	/*
	In addition to the prescription, you should expect front end sales of - 
	We have already determined the percentage of sales that are in the 
	front. This step should tell us with the average prescription they are 
	already filling, how much in front end sales they should expect. 
	*/
	var ExpectedFrontEndSales = 0;
	var PharmacyPcntOfSales = Pharmacy_Percentage_of_Sales(frm);
	//var FrontPcntOfSales = Front_End_Percentage_of_Sales(frm);
	
	var AvgPrescriptionPrice = parseFloat(stringFilter(frm.DollarAmountOfTheAveragePrescriptionYourPharmacyFills.value));
	if (AvgPrescriptionPrice != 0) {
		ExpectedFrontEndSales = (AvgPrescriptionPrice / PharmacyPcntOfSales) * 100;
		ExpectedFrontEndSales = (ExpectedFrontEndSales - AvgPrescriptionPrice);
		frm.YourExpectedFrontEndSales.value = fmtCurrency(ExpectedFrontEndSales);
	}//end if
	
	/* gross profit on the front end sales */
	ExpectedGrossProfitOnFrontEndSales(frm);
}

function ExpectedGrossProfitOnFrontEndSales(frm) {
	/*
	You should expect a gross profit on the front end sales of - When we 
	have the gross margin from the front end, we should multiply this 
	percentage number by the average front end sale.
 	*/
	/* Convert the percentage to two decimal places*/
	var FrontEndGrossProfit = (parseFloat(stringFilter(frm.FrontEndGrossProfit.value))/100);
	var ExpectedFrontEndSales = parseFloat(stringFilter(frm.YourExpectedFrontEndSales.value));
	var GPFromFrontEndSales = FrontEndGrossProfit * ExpectedFrontEndSales;
	if ((FrontEndGrossProfit != 0) && (ExpectedFrontEndSales !=0)) {
		frm.YourExpectedgrossProfitFromFrontEndSales.value = fmtCurrency(GPFromFrontEndSales);
	}

}