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()
		}

//for testing purposes only
function setInitialValues(){
	/*theForm=document.forms.interest
	theForm.p.value=13000
	theForm.r.value=5.75
	theForm.t.value=30
	theForm.skd.checked="checked"*/
	}

	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 isValid(theForm){
		if (theForm.p.value<=0){
			alert("Pricipal 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;
			}
		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 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=65% cols=5 cellpadding=2 border=1>"+
			"<tr><th colspan=5>Schedule of Payments</th></tr>"
		html2+="<tr><th>Month #<th>Beginning Principal<th>Monthly Payment"+
			"<th>Interest Payment<th>Ending Principal</tr>"
		var months=0
		monthlyRate=r/1200
		pr=p
		mp=monthlyPayment
		ttlmp=0
		ttlip=0
		while (Number(pr)>0){
			html2+="<tr><td align='right'>"+(months+1)+"<td align='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 align='right'>"+round2(mp,2)+"<td align='right'>"+
					round2(ip,2)+"<td align='right'>"+round2(pr,2)+"</tr>"
			months++
			}
		html2+="<tr><td><b><td>Totals</td><td  align='right'>"+round2(ttlmp,2)+
			"<td align='right'>"+round2(ttlip,2)+"</table>"
		html="<p><table width=65% cellpadding=2 border=1>"+
			  "<tr><th colspan='3'>Summary of Payments</th></tr>"+
				"<tr><td>Principal<td>$"+round2(p,2)
		html+="<tr><td>Interest rate<td>"+r+"%"
		html+="<tr><td>Original Term<td>"+t+" years ("+t*12+" months)"
		html+="<tr><td>Monthly principal and interest payment <td>$"+
			round2((monthlyPayment-add),2)
		html+="<tr><td>Additional monthly payment<td>$"+round2(add,2)


		html+="<tr><td>Total monthly payment<td>$"+round2(monthlyPayment,2)
		html+="<tr><td>Number of years to pay off <td>"+getYearsMonths(months)+
			  "</table></p>"
		if (skd)
			html+="<br>"+html2
		unhide()
		write_result("result",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"
		}
	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
	}
