
function estmortgage()
{
	form=document.form0;
	incm=form.incomepermo.value/12;
	debt=form.debtpermo.value;
	intr=form.interestperyr.value/1200;
	taxi=form.taxinsperyr.value/1200;
	nomo=form.mortgageyrs.value * 12;
	expc=Math.pow((1 + intr),nomo);
	denm=((expc * (intr + taxi)) - taxi);
	form.amt0.value=Math.floor((expc - 1)*((.36 * incm) - debt)/denm);
	form.amt1.value=Math.floor((expc - 1)*((.38 * incm) - debt)/denm);
};

function JSRecalc()
{
	form = document.form1
	LoanAmount= form.LoanAmount.value

	DownPayment = form.DownPayment.value
	if (DownPayment == "")  {
			DownPayment = "0"
	}

	AnnualInterestRate = form.InterestRate.value/100

	Years= form.NumberOfYears.value

	MonthRate=AnnualInterestRate/12
	NumPayments=Years*12
	Prin=LoanAmount-DownPayment

	MonthPayment=Math.floor((Prin*MonthRate)/(1-Math.pow((1+MonthRate),(-1*NumPayments)))*100)/100

	form.NumberOfPayments.value=NumPayments
	form.MonthlyPayment.value=MonthPayment
}



function taxRecalc()
{
        form = document.form1
        LoanAmount= form.LoanAmount.value
	if (isNaN(LoanAmount)) {
		alert("Invalid entry for loan amount. Do not use commas, dollar signs or any other non-numeric characters.")
	}
                
        AnnualInterestRate = form.InterestRate.value/100;
	if (isNaN(AnnualInterestRate)) {
		alert("Invalid entry for interest rate. Do not use commas, dollar signs or any other non-numeric characters.")
	}
	MonthlyInterest = AnnualInterestRate/12;
	numpayments=360;
	currentmonth=1;
	TotalPaidCurrent=0;
	PaidtoPrincipal=0;
	RemainingBalance=0;
	InterestPaid=0;
        TaxPercent= form.TaxPercent.value/100;
	if (isNaN(TaxPercent)) {
		alert("Invalid entry for tax rate. Enter the tax you expect to pay as a percentage of income (ie, 30). Do not use commas, dollar signs or any other non-numeric characters.")
	}
	if (TaxPercent<0.01) {
		alert("Tax rate out of range. Enter the tax you expect to pay as a percentage of income (ie, 30). Do not use commas, dollar signs or any other non-numeric characters.")
	}
//	these are temp variables to simplify the math equations
	monthcalc=0;
	firstcalc=0;
	secondcalc=0;


//	Monthly Payment Calc
	monthcalc=Math.exp(Math.log(MonthlyInterest+1)*(-numpayments));
	MonthlyPayment=(LoanAmount*MonthlyInterest)/(1-monthcalc);
	form.Year5Savings.value=Math.round(MonthlyPayment);

//	Begin Year Calculations!!!!!!!!!!!

//	Year1 savings
	currentmonth=12;	

//	Remaining Balance at month "currentmonth"
	firstcalc=Math.pow((1+MonthlyInterest),numpayments);
	secondcalc=Math.pow((1+MonthlyInterest),currentmonth); 
	RemainingBalance=LoanAmount*((firstcalc-secondcalc)/(firstcalc-1));

//	Interest paid cumulative to currentmonth
	TotalPaidCurrent=MonthlyPayment*currentmonth;
	PaidtoPrincipal=LoanAmount-RemainingBalance;        
	InterestPaid=TotalPaidCurrent-PaidtoPrincipal;
        TaxPercent= form.TaxPercent.value/100;
	SavingsCurrent=InterestPaid*TaxPercent;
	form.Year1Savings.value=Math.round(SavingsCurrent);

//	Year5 savings
	currentmonth=60;	

//	Remaining Balance at month "currentmonth"
	firstcalc=Math.pow((1+MonthlyInterest),numpayments);
	secondcalc=Math.pow((1+MonthlyInterest),currentmonth); 
	RemainingBalance=LoanAmount*((firstcalc-secondcalc)/(firstcalc-1));

//	Interest paid cumulative to currentmonth
	TotalPaidCurrent=MonthlyPayment*currentmonth;
	PaidtoPrincipal=LoanAmount-RemainingBalance;        
	InterestPaid=TotalPaidCurrent-PaidtoPrincipal;
        TaxPercent= form.TaxPercent.value/100;
	SavingsCurrent=InterestPaid*TaxPercent;
	form.Year5Savings.value=Math.round(SavingsCurrent);

//	Year10 savings
	currentmonth=120;	

//	Remaining Balance at month "currentmonth"
	firstcalc=Math.pow((1+MonthlyInterest),numpayments);
	secondcalc=Math.pow((1+MonthlyInterest),currentmonth); 
	RemainingBalance=LoanAmount*((firstcalc-secondcalc)/(firstcalc-1));
//	Interest paid cumulative to currentmonth
	TotalPaidCurrent=MonthlyPayment*currentmonth;
	PaidtoPrincipal=LoanAmount-RemainingBalance;        
	InterestPaid=TotalPaidCurrent-PaidtoPrincipal;
        TaxPercent= form.TaxPercent.value/100;
	SavingsCurrent=InterestPaid*TaxPercent;
	form.Year10Savings.value=Math.round(SavingsCurrent);

//	Year20 savings
	currentmonth=240;	

//	Remaining Balance at month "currentmonth"
	firstcalc=Math.pow((1+MonthlyInterest),numpayments);
	secondcalc=Math.pow((1+MonthlyInterest),currentmonth); 
	RemainingBalance=LoanAmount*((firstcalc-secondcalc)/(firstcalc-1));
//	Interest paid cumulative to currentmonth
	TotalPaidCurrent=MonthlyPayment*currentmonth;
	PaidtoPrincipal=LoanAmount-RemainingBalance;        
	InterestPaid=TotalPaidCurrent-PaidtoPrincipal;
        TaxPercent= form.TaxPercent.value/100;
	SavingsCurrent=InterestPaid*TaxPercent;
	form.Year20Savings.value=Math.round(SavingsCurrent);

//	Year30 savings
	currentmonth=360;	

//	Remaining Balance at month "currentmonth"
	firstcalc=Math.pow((1+MonthlyInterest),numpayments);
	secondcalc=Math.pow((1+MonthlyInterest),currentmonth); 
	RemainingBalance=LoanAmount*((firstcalc-secondcalc)/(firstcalc-1));

//	Interest paid cumulative to currentmonth
	TotalPaidCurrent=MonthlyPayment*currentmonth;
	PaidtoPrincipal=LoanAmount-RemainingBalance;        
	InterestPaid=TotalPaidCurrent-PaidtoPrincipal;
        TaxPercent= form.TaxPercent.value/100;
	SavingsCurrent=InterestPaid*TaxPercent;
	form.Year30Savings.value=Math.round(SavingsCurrent);

}



