﻿function diretto(o,bid,tk) {if (o.keyCode==13) {calcola(bid,tk)}}
function calcola(bid,tk) {
	var p=getFldValue(bid+"_periodo")
	var v=getFldValue(bid+"_importo")
	v=v.replace(/^\s*/,"").replace(/\s*$/,"")
	var msg=""
	if (v=="") {msg="Per effettuare il calcolo inserire l'importo"}
	if (msg=="") {
		var t=v.replace(/^\d{1,}$/,"")
		if (t!="") {msg="Inserire solo numeri"}
	}
	var t=getFldValue(bid+"_tipo")
	var op=getFldValue(bid+"_op")
	var e=getFldValue(bid+"_e")
	var l=getFldValue(bid+"_l")
	if (msg!="") {
		alert(msg)
		var o=$(bid+"_importo")
		if (o) {
			o.select();
			o.focus()
		}
	} else {
		callAction(new Array("G",calcolaDone,"/__get2.php","m="+escape(p)+"&v="+escape(v)+"&k="+escape(bid)+"&tk="+escape(tk)+"&t="+escape(t)+"&op="+escape(op)+"&e="+escape(e)+"&l="+escape(l)))
	}
}

function calcolaDone(mode,xData,url,info) {
	if (mode) {
		var v=$t(xData,"INT")
		if (v.length==0) {
			var xt=xData.firstChild
			if (xt) {
				var o=$("valore_"+xt.getAttribute("rp"))
				if (o) {
					o.innerHTML="Errore. ("+xt.tagName+")"
				}
			}
		} else {
			var k=v[0].getAttribute("rp")
			var op=v[0].getAttribute("o")
			var msg=""
			var o=$("valore_"+k)
			if (v[0].getAttribute("err")=="") {msg=((op=="pre")?"La rata &egrave; di:":"L'importo della rata &egrave;: ")+pretty(v[0].getAttribute("v"))+"\u20ac"}
			if (v[0].getAttribute("err")=="max") {msg=((op=="pre")?"L'importo massimo finanziabile &egrave;:":"La rata massima &egrave;:")+pretty(v[0].getAttribute("ev"))+"\u20ac"}
				if (v[0].getAttribute("err")=="min") {msg=((o=="pre")?"L'importo minimo finanziabile &egrave;:":"La rata minima &egrave;:")+pretty(v[0].getAttribute("ev"))+"\u20ac"}
			if (o) {o.innerHTML=msg}
		}
	} else {
		alert("Errore nella comunicazione")
	}
}
function pretty(v) {
	var v0=Math.round(v*100)/100
	var vv=(v0+"").split(".")
	var d=""
	var m=""
	var c=""
	//if(vv.length==2) {d=","+(vv[1]+"00").substr(0,2)}
	if(vv.length==2) {d=","+(vv[1]+"00").substr(0,2)}
	if (vv[0].length>3) {
		c="."+vv[0].substr(vv[0].length-3,3)
		m=vv[0].substr(0,vv[0].length-3)
		return pretty(m)+c+d
	} else {
		return vv[0]+d
	}
}