<!-- hide code from old browsers...

// COPYRIGHT NOTICE                                                         
// Copyright 2000 Joshua W. Coffield  All Rights Reserved.

// Script may be used and modified free of charge by anyone so long as this 
// copyright notice and the comments above remain intact.  By using this code you agree 
// to indemnify Joshua W. Coffield from any liability that might arise from it's use.   

function analyze() {

// Pull the form variables into the javascript.	
	var Price = (document.rentbuy.price.value);
	var Down = (document.rentbuy.down.value);
	var Apr = (document.rentbuy.rate.value);
	if (Apr>1) {
		Apr = Apr / 100
		}
   	var Apprec = (document.rentbuy.apprec.value);
	if (Apprec>1) {
		Apprec = Apprec / 100
		}
	var Tax = (document.rentbuy.tax.value);
	if (Tax>1) {
		Tax = Tax / 100
		}
	var Rent = (document.rentbuy.rent.value);
	var Irr = (document.rentbuy.invreturn.value);
	if (Irr>1) {
		Irr = Irr / 100
		}
	var Years = (document.rentbuy.years.value);			
	var Principalx = Math.pow((1+Apr/12),(30*12));
	var Principaly = 1-(1/Principalx);
	var Principalz = Principaly/(Apr/12);
	var MonthlyPayment = (Price-Down)/Principalz;
//Calculate the Monthly Fees Such as Taxes, Insurance, and PMI.
//This calculator uses .0125 as the Annual Tax Rate, .002 as the
//Annual Insurance Rate, 20% as the PMI Cutoff, and .004 as the
//Annual PMI rate.
	var MonthlyTax = Price * (.0125/12);
	var LoanAmt = Price - Down;
	var MonthlyInsurance = Price * (.002/12);
	var MonthlyPmi = 0;
	if (Down < (.2 * Price)) {MonthlyPmi = (LoanAmt * (.004/12)) }
	var MonthlyFees = MonthlyTax + MonthlyInsurance + MonthlyPmi;
	var TotalMonthly = MonthlyPayment + MonthlyFees;	
//Calculate the Appreciation Gain and the First Month Tax Deduction.
	var ApprGain = Price * (Apprec/12);	
	var TaxDeduction = LoanAmt * (Apr/12)* Tax;
	var AdjustedMonthly = TotalMonthly - ApprGain - TaxDeduction;
//Calculate the Monthly Cost of Renting.
	var InvestmentLoss = Down * (Irr/12);
	var AdjustedRent = Rent - InvestmentLoss;	
//Calculate the Total Cost of Renting over the life of the comparison.
	var TotalRent = Rent * (Years * 12);
	var TotalInvestmentValue = (Down) * Math.pow((1+Irr),(Years));
	var TotalInvestmentLoss = TotalInvestmentValue - Down;
	var NetRentGain = TotalInvestmentLoss - TotalRent;
//Calculate the Total Cost of Buying over the life of the comparison.
	var n = 0;
	var tinterest = 0;
	var tprinciple = 0;
	var taxdeduct = 0;
	var TotalInterest = 0;
	var TotalPrinciple = 0;
	var PrincipleRemaining = LoanAmt;
	do {
		var tinterest = PrincipleRemaining * Apr / 12;
		var tprinciple = MonthlyPayment - tinterest;
		var TotalInterest = TotalInterest + tinterest;
		var TotalPrinciple = TotalPrinciple - tprinciple;
		var PrincipleRemaining = PrincipleRemaining - TotalPrinciple;
		var taxdeduct = taxdeduct + (tinterest * Tax);
		n = n + 1;
	}
	while (n<(Years*12)) 
	
	var Appreciation = Math.pow((1+(Apprec/12)),(Years*12));
	var AppreciatedValue = Appreciation * Price;
	var TotalAppreciation = AppreciatedValue - Price;
	var TotalPayments = TotalMonthly * Years * 12;
	var NetBuyGain = TotalAppreciation + taxdeduct - TotalPayments;
//Compare the costs of Buying vs the costs of Renting
	var BuyAdvantage = NetBuyGain - NetRentGain;
//Return all variables to the blanks in the form
	document.rentbuy.mortpmt.value = calcRound(MonthlyPayment);
	document.rentbuy.fees.value = calcRound(MonthlyFees);
	document.rentbuy.pmt.value = calcRound(TotalMonthly);
	document.rentbuy.appamt.value = calcRound(TotalAppreciation);
	document.rentbuy.taxamt.value = calcRound(taxdeduct);
	document.rentbuy.invloss.value = calcRound(TotalInvestmentLoss);					
	document.rentbuy.savings.value = calcRound(BuyAdvantage);
	document.rentbuy.rent2.value = calcRound(Rent);

}

function round(x) {
	return Math.round(x*100)/100;
}

function calcRound(num) {
   result="$"+Math.floor(num)+"." 
   n = result.length
   if (num>1000 && num<999999) {  
     result="$"+result.substring(1,n-4)+","+result.substring(n-4,n)
   }
   if (num>1000000) {  
     result = "$"+result.substring(1,n-7)+","+result.substring(n-7,n-4)+","+result.substring(n-4,n)
   }
   var cents=100*(num-Math.floor(num))+0.5
   cents = Math.round(cents)

	if (cents<10) {
		cents = "0" + cents
	}      
	if (cents>99) {
		cents = "99"
	}      

   result = result + cents
   return(result)
}

function help() {
	text = ("<HEAD><TITLE>About This Calculator</TITLE></HEAD>");
	text = (text + "<BODY BGCOLOR = '#FFFFFF'><BR><BR>");
	text = (text + "<CENTER><FONT SIZE=+2><B>About the<BR>Rent vs Buy Analysis Calculator</B></FONT></CENTER>");
	text = (text + "<BR><BR><FONT SIZE=-1><UL><LI>This Calculator calculates the benfits of ");
	text = (text + "renting vs buying.  It uses factors such as tax benefits, potential appreciation of ");
	text = (text + "property, and investment loss from a down payment in calculating this benefit.</LI>");
	text = (text + "<LI>This Calculator is for informational purposes only.  Actual benefits ");
	text = (text + "can not be completely predicted.</LI><LI>This calculator uses a few standard estimates ");
	text = (text + "in its calculations.  It uses 1.25% as the Annual Property Tax Rate, .2% as ");
	text = (text + "the Annual Home Insurance Rate, and .4% as the PMI Rate for anyone putting less ");
	text = (text + "than 20% down on a house.  If your actual rates vary, then these results may be ");
	text = (text + "slightly off.</LI><LI>No rates over 100% may be used in this calculator</LI>");
	text = (text + "<LI>This calculator uses");
	text = (text + " Javascript.  If your browser doesn't support Javascript you will need to upgrade.");
	text = (text + "</LI></UL></FONT></BODY>");
	msgWindow = window.open("","displayWindow","toolbar=no,width=500,height=300,directories=no,status=no,scrollbars=yes,resize=no,menubar=yes")
		msgWindow.document.write(text)
		msgWindow.document.close()
}	
	
	
	
//-->
