var browser
function getBrowserName()
{
	ver=(navigator.appVersion).substring(0,1)
	return ((navigator.appName).indexOf("Explorer")==-1?"N":"E")
}
function focusPField()
{
	browser=getBrowserName()
	document.interest.p.focus()
}

function round2(nbr,digits)
{
	nbr=nbr.toString()
	if (nbr.indexOf('.')==-1)
	{
		int_part=nbr
		decimal_part=""
	}
	else
	{
		int_part=nbr.substring(0,nbr.indexOf('.'))
		decimal_part=nbr.substring(1+nbr.indexOf('.'),2+nbr.indexOf('.')+digits)
	}
	while(decimal_part.length<digits+1)
		decimal_part+="0"
	if (Number(decimal_part.charAt(decimal_part.length-1)<5))
		decimal_part=decimal_part.substring(0,digits)
	else
	{
		decimal_part=(Number(decimal_part)+10).toString()
		if (decimal_part.length>digits+1)
		{
			int_part=(Number(int_part)+1).toString()
			decimal_part=decimal_part.substring(1,digits+1)
		}
		decimal_part=decimal_part.substring(0,digits)
	}
	if (int_part=="")
		int_part=0;
	if (Number(digits)>0)
		return int_part+'.'+decimal_part
	else
		return int_part
}

function getKeyCode(e)
{
	if (browser=="N")
		chr=e.which
	else chr=e.keyCode
	if ((chr<48 || chr>57) &&chr!=58 && chr!=0 && chr!=46 & chr!=8)
			return false
	return true
}

function isValid2(theForm)
{
	if (theForm.p.value<=0)
	{
		alert("Principal value should be a number >0")
		theForm.p.focus();
		return false;
	}
	if (theForm.r.value<=0)
	{
		alert("Interest rate value should be >0")
		theForm.r.focus();
		return false;
	}
	if (theForm.add.value<0)
	{
		alert("Additional principal should be a number > 0")
		theForm.add.focus();
		return false;
	}
	if (theForm.newWin.checked==true)
	{
	   var str =  compute2(theForm.p.value,theForm.r.value,theForm.t.value,theForm.add.value,theForm.skd.checked);
	   var str1 = "<table width='500px' border=0 cellpadding=0 cellspacing=0>";
	   str1 = str1 + "<tr><td class='topLeftCorner' width='24'>&nbsp;</td>";
	   str1 = str1 + "<td class='top' width='147'>&nbsp;</td>";
	   str1 = str1 + "<td class='topRightCorner' width='24'>&nbsp;</td></tr>";
	   str1 = str1 + "<tr><td colspan='3' align='center' class='center'>";
	   str1 = str1 + "<p><br /></p>";
	   var str2 = "<p><br /></p><p id='center'><a href='javascript:window.close();'>close window</a></p><p><br /></p></td></tr>";
	   str2 = str2 + "<tr><td class='bottomLeftDesign' width='24'>&nbsp;</td>";
	   str2 = str2 + "<td class='bottom' width='147'>&nbsp;</td>";
	   str2 = str2 + "<td class='bottomRightDesign' width='24'>&nbsp;</td></tr></table></body></html>";
	   var str0 = "<html><head><title> « Property-Online International » </title>";
	   str0 = str0 + "<meta http-equiv='content-type' content='text/html; charset=iso-8859-1'>";
	   str0 = str0 + "<link rel='stylesheet' type='text/css' href='../css/main.css'>";
	   str0 = str0 + "<script language='javascript' src='../css/calc1.js'></script>";
	   str0 = str0 + "<script language='javascript' src='../css/script.js'></script>";
	   str0 = str0 + "</head><body id='body' onmousedown='rightclick(event)'>";

	   newWindow = window.open("","newWindow","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=517,height=400");
	   newWindow.document.write(str0);
	   newWindow.document.write(str1);
	   newWindow.document.write(str);
	   newWindow.document.write(str2);	   
	}
	else
	{
		compute(theForm.p.value,theForm.r.value,theForm.t.value,theForm.add.value,theForm.skd.checked);
	}
}

function getYearsMonths(mon)
{
	var months, years
	years=Math.floor(mon/12)
	months=mon-(years*12)
	if (months==0)
		months=null
	else if (months==1)
		months="1 month"
	else months=months+" months"

	if (years==0)
		years =null
	else if (years==1)
		years="1 year"
	else years=years+" years"
	if (years==null)
		return months
	else if (months==null)
		return years
	else return years+" and "+months
}

function compute(p,r,t,add,skd)
{
	amFactor=getAmFactor(p,r,t)
	monthlyPayment=Number(add)+p/amFactor
	howLong(p,r,t,monthlyPayment,add,skd)
}

function compute2(p,r,t,add,skd)
{
	amFactor=getAmFactor(p,r,t)
	monthlyPayment=Number(add)+p/amFactor
	return howLong2(p,r,t,monthlyPayment,add,skd)
}

function getAmFactor(p,r,t)
{
	numerator=1-Math.pow(1+r/1200,-1*t*12)
	denominator=r/1200
	return numerator/denominator
}

function howLong(p,r,t,monthlyPayment,add,skd)
{
	html2="<table width=90% cols=5 cellpadding=2 border=1 bordercolor=#000099 bgcolor=#ffffff>"+
		"<tr><td colspan=5><h2 id='center'>Schedule of Payments</h2></td></tr>"
	html2+="<tr><td><h3>Month #</h3></td>"+
			"<td><h3>Beginning Principal</h3></td>"+
			"<td><h3>Monthly Payment</h3></td>"+
			"<td><h3>Interest Payment</h3></td>"+
			"<td><h3>Ending Principal</h3></td></tr>"
	var months=0
	monthlyRate=r/1200
	pr=p
	mp=monthlyPayment
	ttlmp=0
	ttlip=0
	while (Number(pr)>0)
	{
		html2+="<tr><td><p id='right'>"+(months+1)+"</td><td><p id='right'>"+round2(pr,2)
		ip=pr*monthlyRate
		pr=Math.abs(pr)+ip
		if (pr>monthlyPayment*1.20)
			pr=pr-mp
		else
		{
			mp=pr
			pr=0
		}
		ttlip+=ip
		ttlmp+=mp
		html2+="</td><td><p id='right'>"+round2(mp,2)+"</td><td><p id='right'>"+
				round2(ip,2)+"</td><td><p id='right'>"+round2(pr,2)+"</td></tr>"
		months++
		}

	html2+="<tr><td></td>" +
		"<td><h3 id='center'>Totals</h3></td>" +
		"<td><p id='right'><b>"+round2(ttlmp,2)+ "</b></td>" +
		"<td><p id='right'><b>"+round2(ttlip,2)+"</b></td></tr></table>"

	html="<p><br /></p><table width='350px' cellpadding=5 cellspacing=0 border=1 bordercolor=#000099 bgcolor=#eeeeff>"+
		"<tr><td colspan='3'><h2 id='center'>Summary of Payments</h2></td></tr>"+
		"<tr><td><p id='right'>Principal</td><td><p id='right'>$"+round2(p,2)
	html+="<tr><td><p id='right'>Interest rate</td><td><p id='right'>"+r+"%"
	html+="<tr><td><p id='right'>Original Term</td><td><p id='right'>"+t+" years ("+t*12+" months)"
	html+="<tr><td><p id='right'>Monthly principal and interest payment</td><td><p id='right'>$"+
		round2((monthlyPayment-add),2)
	html+="<tr><td><p id='right'>Additional monthly payment</td><td><p id='right'>$"+round2(add,2)
	html+="<tr><td><p id='right'>Total monthly payment</td><td><p id='right'>$"+round2(monthlyPayment,2)
	html+="<tr><td><p id='right'>Number of years to pay off</td><td><p id='right'>"+getYearsMonths(months)+
		  "</td></tr></table><p><br /></p><p><br /></p>"
	if (skd)
		html+="<br>"+html2
	unhide()
	write_result("result",html)
}

function howLong2(p,r,t,monthlyPayment,add,skd)
{
	html2="<table width=90% cols=5 cellpadding=2 border=1 bordercolor=#000099 bgcolor=#ffffff>"+
		"<tr><td colspan=5><h2 id='center'>Schedule of Payments</h2></td></tr>"
	html2+="<tr><td><h3>Month #</h3></td>"+
			"<td><h3>Beginning Principal</h3></td>"+
			"<td><h3>Monthly Payment</h3></td>"+
			"<td><h3>Interest Payment</h3></td>"+
			"<td><h3>Ending Principal</h3></td></tr>"
	var months=0
	monthlyRate=r/1200
	pr=p
	mp=monthlyPayment
	ttlmp=0
	ttlip=0
	while (Number(pr)>0)
	{
		html2+="<tr><td><p id='right'>"+(months+1)+"</td><td><p id='right'>"+round2(pr,2)
		ip=pr*monthlyRate
		pr=Math.abs(pr)+ip
		if (pr>monthlyPayment*1.20)
			pr=pr-mp
		else
		{
			mp=pr
			pr=0
		}
		ttlip+=ip
		ttlmp+=mp
		html2+="</td><td><p id='right'>"+round2(mp,2)+"</td><td><p id='right'>"+
				round2(ip,2)+"</td><td><p id='right'>"+round2(pr,2)+"</td></tr>"
		months++
		}

	html2+="<tr><td></td>" +
		"<td><h3 id='center'>Totals</h3></td>" +
		"<td><p id='right'><b>"+round2(ttlmp,2)+ "</b></td>" +
		"<td><p id='right'><b>"+round2(ttlip,2)+"</b></td></tr></table>"

	html="<p><br /></p><table width='350px' cellpadding=5 cellspacing=0 border=1 bordercolor=#000099 bgcolor=#eeeeff>"+
		"<tr><td colspan='3'><h2 id='center'>Summary of Payments</h2></td></tr>"+
		"<tr><td><p id='right'>Principal</td><td><p id='right'>$"+round2(p,2)
	html+="<tr><td><p id='right'>Interest rate</td><td><p id='right'>"+r+"%"
	html+="<tr><td><p id='right'>Original Term</td><td><p id='right'>"+t+" years ("+t*12+" months)"
	html+="<tr><td><p id='right'>Monthly principal and interest payment</td><td><p id='right'>$"+
		round2((monthlyPayment-add),2)
	html+="<tr><td><p id='right'>Additional monthly payment</td><td><p id='right'>$"+round2(add,2)
	html+="<tr><td><p id='right'>Total monthly payment</td><td><p id='right'>$"+round2(monthlyPayment,2)
	html+="<tr><td><p id='right'>Number of years to pay off</td><td><p id='right'>"+getYearsMonths(months)+
		  "</td></tr></table><p><br /></p><p><br /></p>"
	if (skd)
		html+="<br>"+html2
	unhide()
	//write_result("result",html)
	return html;
}

function changeColor(str)
{
	if (str.style.backgroundColor=="red")
		str.style.backgroundColor="black"
	else str.style.backgroundColor="red"
}

function hide()
{
	//document.getElementById('result').style.visibility="hidden";
	document.getElementById('result').innerHTML="<h3 id=center>Enter details and press Compute.</h3>";
}
function unhide()
{
	document.getElementById('result').style.visibility="visible"
}
function write_result(id,the_result)
{
	d=document;
	re=d.all?d.all[id]:d.getElementById(id);
	re.innerHTML=the_result
}

function clearAll()
{
	//d=document;
	//re=d.all?d.all["result"]:d.getElementById("result");
	//re.style.visibility="visible";
	//re1=d.all?d.all["temp"]:d.getElementById("temp");
	//re1.style.visibility="hidden";
	hide()
}
function showAll(transfer)
{
	str0 = "<html>";
	str0 = str0 + "<head>";
	str0 = str0 + 	"<title> « Property-Online International » </title>";
	str0 = str0 + 	"<meta http-equiv='content-type' content='text/html; charset=iso-8859-1'>";
	str0 = str0 + 	"<link rel='stylesheet' type='text/css' href='../css/main.css'>";
	str0 = str0 + 	"<script language='javascript' src='../css/script.js'></script>";
	str0 = str0 + "<body id='body' onmousedown='rightclick(event)'>";
	str0 = str0 + "<table width='500px' border=0 cellpadding=0 cellspacing=0>";
	str0 = str0 + "<tr><td class='topLeftCorner' width='24'>&nbsp;</td>";
	str0 = str0 + "<td class='top' width='147'>&nbsp;</td>";
	str0 = str0 + "<td class='topRightCorner' width='24'>&nbsp;</td></tr>";
	str0 = str0 + "<tr>";
	str0 = str0 + 	"<td colspan='3' align='center' class='center'>";
	str0 = str0 + 	"<p><br /></p>";
	str = 		"<h2 id=center>Mortgage Bond & Transfer Cost</h2>";
	str = str + 			"<table width='90%' border=1 cellpadding=3 cellspacing=2 bordercolor='#000099' bgcolor='#ffffff'>";
	str = str + 			"<tr>";
	str = str + 				"<td colspan=2><h3>Bond Costs</h3></td>";
	str = str + 				"<td colspan=2><h3>Transfer Costs</h3></td>";
	str = str + 			"</tr>";
	str = str + 			"<tr>";
	str = str + 				"<td><p id=left>Bond Registration Costs (+ VAT)</p></td>";
	str = str + 				"<td><p id=right><span id=brc>0.00</span></td>";
	str = str + 				"<td><p id=left>Transfer Costs (+ VAT)</p></td>";
	str = str + 				"<td><p id=right><span id=tc>0.00</span></td>";
	str = str + 			"</tr>";
	str = str + 			"<tr>";
	str = str + 				"<td><p id=left>Stamp Duty on Bond</p></td>";
	str = str + 				"<td><p id=right><span id=sdb>0.00</span></td>";
	str = str + 				"<td><p id=left>Transfer Duty</p></td>";
	str = str + 				"<td><p id=right><span id=td>0.00</span></td>";
	str = str + 			"</tr>";
	str = str + 			"<tr>";
	str = str + 				"<td><p id=left>Deeds Office Levy</p></td>";
	str = str + 				"<td><p id=right><span id=dol>0.00</span></td>";
	str = str + 				"<td><p id=left>Deeds Office Levy</p></td>";
	str = str + 				"<td><p id=right><span id=dol1>0.00</span></td>";
	str = str + 			"</tr>";
	str = str + 			"<tr>";
	str = str + 				"<td><p id=left>Sectional Title Insurance Cert.</p></td>";
	str = str + 				"<td><p id=right><span id=stic>0.00</span></td>";
	str = str + 				"<td><p id=left>Municipal Valuation Cert.</p></td>";
	str = str + 				"<td><p id=right><span id=mvc>0.00</span></td>";
	str = str + 			"</tr>";
	str = str + 			"<tr>";
	str = str + 				"<td><p id=left>Post & Petties (+ VAT)</p></td>";
	str = str + 				"<td><p id=right><span id=pp>0.00</span></td>";
	str = str + 				"<td><p id=left>Levy Clearance Cert.</p></td>";
	str = str + 				"<td><p id=right><span id=lcc>0.00</span></td>";
	str = str + 			"</tr>";
	str = str + 			"<tr>";
	str = str + 				"<td><p id=left>Assessment Fee</p></td>";
	str = str + 				"<td><p id=right><span id=af>0.00</span></td>";
	str = str + 				"<td><p id=left>Post & Petties</p></td>";
	str = str + 				"<td><p id=right><span id=pp1>0.00</span></td>";
	str = str + 			"</tr>";
	str = str + 			"<tr>";
	str = str + 				"<td><p id=left>Initiation Fee (+ VAT)</p></td>";
	str = str + 				"<td><p id=right><span id=if>0.00</span></td>";
	str = str + 				"<td></td><td></td>";
	str = str + 			"</tr>";
	str = str + 			"<tr>";
	str = str + 				"<td><h3>Total Bond Costs</h3></td>";
	str = str + 				"<td><p id=right><span id=tbc style='font-weight: bold;'>0.00</span></td>";
	str = str + 				"<td><h3>Total Transfer Costs</h3></td>";
	str = str + 				"<td><p id=right><span id=ttc style='font-weight: bold;'>0.00</span></td>";
	str = str + 			"</tr>";
	str = str + 			"<tr>";
	str = str + 				"<td></td><td></td>";
	str = str + 				"<td><h3>Grand Total</h3></td>";
	str = str + 				"<td><p id=right><span id=gt style='font-weight: bold;'>0.00</span></td>";
	str = str + 			"</tr>";
	str = str + 			"</table>";
	str1 = "<p><br /></p>";
	str1 = str1 + 	"</td>";
	str1 = str1 + "</tr>";
	str1 = str1 + "<tr><td class=bottomLeftDesign width=24>&nbsp;</td>";
	str1 = str1 + "<td class=bottom width=147>&nbsp;</td>";
	str1 = str1 + "<td class=bottomRightDesign width=24>&nbsp;</td></tr>";
	str1 = str1 + "</table>";
	str1 = str1 + "</body>";
	str1 = str1 + "</html>";
	if (transfer.chk.checked==true)
	{
		newWin1 = window.open("","newWin1","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=517,height=315");
		newWin1.document.write(str0);
		newWin1.document.write(str);
		newWin1.document.write(str1);
		//newWin1.document.close();
	}
	else
	{
		write_result("result",str)
	}
}
