
// funciones de control
function checkNumeric(o){
	o.value =o.value.replace(/,/g,".");
	o.value =o.value.replace(/[^0-9.]/g,"");	
	if(o.value == "") o.value =0;
}


function calculaHipoteca(){

	var l = document.getElementById("capital").value;
	var i = document.getElementById("interes").value;
	var a = document.getElementById("anios").value;
	
	// Si el interes es cero, simplemente dividimos
	if(i==0) return(l/(a*12));
	
	var p= Math.pow((1+(i/1200)),(a*12));
	var mensualidad = (l*p)*(i/1200)/(p-1);
	
	return(mensualidad);

	
}

function muestraRes(){
	// Mostramos la capa
	


	var res = calculaHipoteca();
	// Conformamos el resultado en pantalla
	var f = document.getElementById("resultado");
	res = res.toFixed(3);
	res = res.replace(".",",");
	f.innerHTML=res + " &euro; /mes";
}


window.onload = function(){
	
	var fc = document.getElementById("capital");
	var fi= document.getElementById("interes");
	var fa = document.getElementById("anios");	
	fc.onchange = fi.onchange = fa.onchange= function(){
		checkNumeric(this);
	}
	
	// Boton de calculo
	
	var boton = document.getElementById("bhipoteca");
	boton.onclick = function(){
		muestraRes();
	}

	
	// Damos valores iniciales
	
	fc.value = 120000;
	fi.value = 4;
	fa.value = 20;
	
	
	// Calculamos
	boton.onclick();
	
}
