// JavaScript Document
	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("L'importo deve essere >0")
			theForm.p.focus();
			return false;
			}
		if (theForm.r.value<=0){
			alert("Il tasso deve essere >0")
			theForm.r.focus();
			return false;
			}
		
		compute(theForm.p.value,theForm.r.value,theForm.t.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,skd){
		amFactor=getAmFactor(p,r,t)
		monthlyPayment=p/amFactor
		howLong(p,r,t,monthlyPayment,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,skd){
		html2="<table width=95% cols=5 cellpadding=2 border=0 class=news>"+
			"<tr><th colspan=5>Ammortamento<br></th></tr>"
		html2+="<tr><th>Mesi<th>Importo iniziale<th>Rata mensile"+
			"<th>Interessi<th>Rimanenza</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><b>Totale</b></td><td  align='right'>€ "+round2(ttlmp,2)+
			"<td align='right'>€ "+round2(ttlip,2)+"</table>"
		html="<p><table width=200 cellpadding=2 border=0 class=news>"
//		html+="<tr><th colspan='3'>Riepilogo pagamenti<br></th></tr>"+
//				"<tr><td>Importo<td>€ "+round2(p,2)
//		html+="<tr><td>Interessi<td>"+r+"%"
//		html+="<tr><td>Durata<td>"+t+" anni ("+t*12+" mesi)"
		html+="<tr><td><b>Rata mensile:<td>€ "+"<b>"+
			round2((monthlyPayment),2)
		html+="<tr><b>"+""+
			  "</b></table></p>"
		if (skd)
//		html+="<br><br>"+html2
		html+=html2
		
		unhide()
		write_result("result",html)
		}
	
	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
	}


