isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
NS4 = (document.layers) ? true : false;
IEmac = ((document.all)&&(isMac)) ? true : false;
IE4plus = (document.all) ? true : false;
IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
ver4 = (NS4 || IE4plus) ? true : false;
NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;



function roundPen(n)
{
if(n > 0){
pennies = n*100;
pennies = Math.round(pennies);
strPennies = "" + pennies;
len = strPennies.length;

return strPennies.substring(0, len - 2) + "." + strPennies.substring(len -2, len);
}
else return 0;
}



function CalcCmp(){

var formObj = GetFormObj();

var error=0;

var htmlStr = '';

if(document.rentcalc.rate.value == null || document.rentcalc.rate.value.length == 0)
{
var RATE = 0;
}else{
var RATE = document.rentcalc.rate.value/100;
}

if(document.rentcalc.downp.value == null || document.rentcalc.downp.value.length == 0)
{
var downp = 0;
}else{
var downp = document.rentcalc.downp.value*1.0;
}

if(document.rentcalc.protax.value == null || document.rentcalc.protax.value.length == 0)
{
var tax = 0;
}else{
var tax = document.rentcalc.protax.value;
}

if(document.rentcalc.incr.selectedIndex == 0)
{
var incr = 0;
}else{
var incr = document.rentcalc.incr[document.rentcalc.incr.selectedIndex].value;
}

if(document.rentcalc.monpay.value == null || document.rentcalc.monpay.value.length == 0)
{
var monpay = 0;
}else{
var monpay = document.rentcalc.monpay.value;
}

if(document.rentcalc.rent.value == null || document.rentcalc.rent.value.length == 0)
{
var rent = 0;
}else{
var rent = document.rentcalc.rent.value;
}

if(document.rentcalc.compare.selectedIndex == 0)
{
var compare = 0;
}else{
var compare = document.rentcalc.compare[document.rentcalc.compare.selectedIndex].value;
}

var compound = 2/12;
var monTime = 25 * 12;
var yrRate = RATE/2;
var rdefine    = Math.pow((1.0 + yrRate),compound)-1.0;
var purchcompound = Math.pow((1.0 + rdefine),monTime);

var a1 = monpay - (tax/12);
var b1 = (0+((a1*(purchcompound-1.0))/rdefine))/purchcompound;
var c1 = downp+b1;
var res = (b1*(Math.pow((1.0 + rdefine),(12*compare)))) -  ((a1 * ((Math.pow((1.0 + rdefine),(12*compare))) - 1.0))/rdefine);
var d1 = b1-res;
var e1 = c1*((Math.pow((1+incr),compare))-1);
var f1 = monpay - rent;
var g1 = (downp+(f1*12))*(Math.pow((1.04),compare))-downp;
var h1 = d1+e1;

var decide =  (h1>g1) ? 'BUY' : 'CONTINUE TO RENT';
var words =  (h1>g1) ? 'greater' : 'less';




htmlStr += ('<form name=rentcalc>');
htmlStr += ('<table border="1" cellpadding=0 width="450" bgcolor="#CCCCCC" bordercolor="#FFFFFF">');

htmlStr += ('<td width="50%" align="right" class="normaltext">Monthly mortgage:</td>');
htmlStr += ('<td width="50%" align="left" class="normaltext"><input type="text" size="10" name="monpay" value=' + monpay + '> $</td>');
	     
htmlStr += ('<TR>');
htmlStr += ('<td width="50%" align="right" class="normaltext">Downpayment:</td>');
htmlStr += ('<td width="50%" align="left" class="normaltext"><input type="text" size="10" name="downp" value=' + downp + '> $</td>');
	     
htmlStr += ('<TR>');
htmlStr += ('<td width="50%" align="right" class="normaltext">Annual property taxes:</td>');
htmlStr += ('<td width="50%" align="left" class="normaltext"><input type="text" size="10" name="protax" value=' + tax + '> $</td>');
	     
htmlStr += ('<TR>');
htmlStr += ('<td width="50%" align="right" class="normaltext">Interest rate:</td>');
htmlStr += ('<td width="50%" align="left" class="normaltext"><input type="text" size="10" name="rate" value=' + RATE*100 + '> %</td>');

htmlStr += ('<TR>');
htmlStr += ('<td width="50%" align="right" class="normaltext">Annual home value increase:</td>');
htmlStr += ('<td width="50%" align="left" class="normaltext"><select name="incr">');
htmlStr += ('<option value=.00 selected>0%');
htmlStr += ('<option value=.01>1%');
htmlStr += ('<option value=.02>2%');
htmlStr += ('<option value=.03>3%');
htmlStr += ('<option value=.04>4%');
htmlStr += ('<option value=.05>5%');
htmlStr += ('<option value=.06>6%');
htmlStr += ('</select></td>');
htmlStr += ('<TR>');

htmlStr += ('<td width="50%" align="right" class="normaltext">Monthly rent:</td>');
htmlStr += ('<td width="50%" align="left" class="normaltext"><input type="text" size="10" name="rent" value=' + rent + '> $</td>');
htmlStr += ('<TR>');

htmlStr += ('<td width="50%" align="right" class="normaltext">Years to compare:</td>');
htmlStr += ('<td width="50%" align="left" class="normaltext"><select name="compare">');
htmlStr += ('<option value=1>1 Year');
htmlStr += ('<option value=2>2 Years');
htmlStr += ('<option value=3>3 Years');
htmlStr += ('<option value=4>4 Years');
htmlStr += ('<option value=5>5 Years');
htmlStr += ('<option value=6>6 Years');
htmlStr += ('<option value=7>7 Years');
htmlStr += ('<option value=8>8 Years');
htmlStr += ('<option value=9>9 Years');
htmlStr += ('<option value=10 selected>10 Years');
htmlStr += ('</select></td>');
htmlStr += ('<TR>');

htmlStr += ('<td width="100%" colspan="2" align="center" class="normaltext"><input type="button" value="Compare Rent vs Buy" name="B1"onclick="CalcCmp();"></a></td>');
htmlStr += ('</table>');

htmlStr += ('</form>');


htmlStr += ('<center>');
htmlStr += ('<table border=0 width=600><tr><td class=redtext>');

htmlStr += ('You would probably earn more if you decide to '+decide+'</div>');

htmlStr += ('<div class=normaltext>If you buy for $'+roundPen(c1)+'(the maximum you would qualify for) you will pay down your mortgage ');
htmlStr += ('of '+roundPen(b1));
htmlStr += (' by '+roundPen(d1)+' over '+compare+' year(s) with your Principal and Interest payments of $'+roundPen(a1)+' per month, plus your property ');
htmlStr += ('will increase in value by $' +roundPen(e1)+' for a total investment growth of ');
htmlStr += ('$'+roundPen(h1)+' This total is '+words+'than your total investment growth ');
htmlStr += ('from renting, which is approximately $'+roundPen(g1));
htmlStr += (' after '+compare+' year(s). This was calculated by growing the monthly savings from renting ($'+roundPen(f1)+') plus your current downpayment');
htmlStr += (' of $'+roundPen(downp)+' at a standard after-tax rate of 4% per annum.');
htmlStr += ('<hr>');
htmlStr += ('</td><tr></table></center>');


if (IE4plus){
		document.all.formlayer.innerHTML = htmlStr;
	}
	else if (NS4){
		document.formlayer.document.open();
		document.formlayer.document.write(htmlStr);
		document.formlayer.document.close();
	}
	else if (NS6){
		document.getElementById("formlayer").innerHTML = htmlStr;
	}
}





function GetFormObj()
{
	var returnObj = null;
	if (IE4plus){
		returnObj =  document.calc;
	}
		else if (NS4){
			returnObj =  document.formlayer.document.windows_form;
		}
			else if (NS6){
				returnObj =  document.getElementById("windows_form");
			}
	return returnObj;
}

