var rules_calculadora_hipotecas = {
	'#submit_frmCalculadoraHipotecas': function(e){
		e.onclick = function(){
			//funcion que se utiliza para calcular la cuota de la hipoteca
			function calcularHipoteca() {
				var importe      = document.getElementById("precio");
				var duracion     = document.getElementById("plazo");
				var tipo_interes = document.getElementById("interes");
				var cuota        = document.getElementById("cuota");
				var error        = false;
				var msg          = "<dt>Error en los siguientes campos:</dt>";
				var temp;
				var resultado;
			
				// quitamos posibles comas y las cambiamos por ceros decimales
				tipo_interes.value = tipo_interes.value.replace(",", ".");
			
				if( isNaN(importe.value) || importe.value <= 0 ){
					importe.value = "";
					importe.focus();
					msg = msg+"<dd><strong>precio de la vivienda</strong> El valor no es válido.</dd>";
					error = true;
				}
				if( isNaN(duracion.value) || duracion.value == 0 ){ 
					msg = msg+"<dd><strong>plazo de la hipoteca</strong> El valor no es válido.</dd>";
					error = true;
				}
				if( isNaN(tipo_interes.value) || tipo_interes.value == 0.0 || tipo_interes.value <= 0 ){ 
					tipo_interes.value = "";
					tipo_interes.focus();
					msg = msg+"<dd><strong>tipo de interés</strong> El valor no es válido.</dd>";
					error = true;
				}
				if(!error){
					document.getElementById("msg_calculadora_hipotecas").innerHTML = "";
					document.getElementById("ctn_msg_calculadora_hipotecas").style.display = "none";
				}else{
					document.getElementById("msg_calculadora_hipotecas").innerHTML = msg;
					document.getElementById("ctn_msg_calculadora_hipotecas").style.display = "block";
					cuota.value = "";
					return false;
				}

				importe      = parseInt(importe.value);
				duracion     = parseInt(duracion.value); 
				tipo_interes = parseFloat(tipo_interes.value);
				duracion     = duracion * 12; //pasamos la duracion en tiempo a meses
				tipo_interes = tipo_interes / 1200.0; //el interes debe ser mensual
				temp         = 1.0 + tipo_interes;
				resultado    = importe * tipo_interes * Math.pow(temp,duracion) / ( Math.pow(temp,duracion) - 1.0 );

				cuota.value= Math.round(resultado*100)/100+" €";
			}//fin funcion calcula

			calcularHipoteca();
			return false;
		}
	}
}
Behaviour.register(rules_calculadora_hipotecas);
