
	function verificar_numero(numero,formulario)
	{
		if (numero.value=='') {}
		var noes=0;
		var str = numero.value;
		for (var i = 0; i < str.length; i++)
		{	var ch = str.substring(i, i + 1)
		        if ((ch < "0" || "9" < ch) && ch != ',' && ch != '.')
			{  numero.value=''
		           numero.focus()
		           numero.select()
			   noes=1
			}
		}
		if (noes==1)
		{ alert('El valor introducido "' + str + '" no es un n\u00famero.')
		  return 1
		} else { numero.value = str.replace('.', ','); }
		return 0;
	}

	function hay_cambio() {
		if ( document.forms[0].medida_anterior.value != 12.0) { return 1 }
		if ( document.forms[0].tipoVF_anterior.value != 1) { return 1 }
		if ( document.forms[0].importe.value != document.forms[0].importe_anterior.value) { return 1 }
		if ( document.forms[0].plazo.value != document.forms[0].plazo_anterior.value) { return 1 }
		if ( document.forms[0].tipo.value != document.forms[0].tipo_anterior.value) { return 1 }
		if ( document.forms[0].cuota.value != document.forms[0].cuota_anterior.value) { return 1 }
		return 0
	}

	function almacena() {
		document.forms[0].importe_anterior.value = document.forms[0].importe.value
		document.forms[0].plazo_anterior.value = document.forms[0].plazo.value
		document.forms[0].tipo_anterior.value = document.forms[0].tipo.value
		document.forms[0].cuota_anterior.value = document.forms[0].cuota.value
		document.forms[0].medida_anterior.value = 12.0
		document.forms[0].tipoVF_anterior.value = 0
	}

	function calculacuota() {
		// CALCULA LA CUOTA A PAGAR CONOCIENDO:

		if ( hay_cambio() == 0 ) { return }

		imp = parseInt(desformatear_numero(document.forms[0].importe.value))
		plazo_meses = parseInt(desformatear_numero(document.forms[0].plazo.value))
		tipo_mensual = parseFloat(desformatear_numero(document.forms[0].tipo.value))

		medida = 12.0

		if ( isNaN(imp) || imp == 0 ) { alert("Debe rellenar el campo 'Importe a financiar'"); return; }
		if ( isNaN(plazo_meses) || plazo_meses == 0 ) { alert("Debe rellenar el campo 'Plazo de amortizaci\u00f3n'"); return; }
		if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 ) { alert("Debe rellenar el campo 'Tipo de inter\u00e9s'"); return; }
		if ( tipo_mensual > 20.0 ) { alert("El Tipo de inter\u00e9s es superior al 20% y no es aceptable para un pr\u00e9stamo hipotecario."); return; }

		plazo_meses = plazo_meses * medida

		// EL TIPO DE INTERES VIENE EN ANIOS Y LOS PASAMOS A TIPO INTERES MENSUAL
		tipo_mensual = tipo_mensual / 1200.0

		y  = 1.0 + tipo_mensual
		cuota = imp * tipo_mensual * Math.pow(y,plazo_meses) / ( Math.pow(y,plazo_meses) - 1.0 )
		document.forms[0].cuota.value = formatear_numero((Math.round(cuota *100.00) / 100.00).toFixed(2))
		almacena()

	  tipo_mensual_max = 4.00 / 1200.0;
	  yMax=	1.0 + tipo_mensual_max;
	  cuotamax = imp * tipo_mensual_max * Math.pow(yMax,plazo_meses) / ( Math.pow(yMax,plazo_meses) - 1.0 );
	  //document.forms[0].cuotaMax.value = formatear_numero(Math.round(cuotamax *100.00) / 100.00);
	  tipo_mensual_min = 6.00 / 1200.0;
	  yMin= 1.0 + tipo_mensual_min;
	  cuotamin = imp * tipo_mensual_min * Math.pow(yMin,plazo_meses) / ( Math.pow(yMin,plazo_meses) - 1.0 );
	  //document.forms[0].cuotaMin.value = formatear_numero(Math.round(cuotamin *100.00) / 100.00);
	}

	function calculaimporte() {
		// CALCULA EL IMPORTE DEL PRESTAMO A SOLICITAR CONOCIENDO:

		if ( hay_cambio() == 0 ) { return }

		plazo_meses = parseFloat(desformatear_numero(document.forms[0].plazo.value))
		tipo_mensual = parseFloat(desformatear_numero(document.forms[0].tipo.value))
		cuota = parseFloat(desformatear_numero(document.forms[0].cuota.value))

		medida = 12.0

		if ( isNaN(cuota) || cuota == 0 ) { alert("Debe rellenar el campo 'Cuota mensual a pagar'"); return; }
		if ( isNaN(plazo_meses) || plazo_meses == 0 ) { alert("Debe rellenar el campo 'Plazo de amortizaci\u00f3n'"); return; }
		if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 ) { alert("Debe rellenar el campo 'Tipo de inter\u00e9s'"); return; }
		if ( tipo_mensual > 20.0 ) { alert("El Tipo de inter\u00e9s es superior al 20% y no es aceptable para un pr\u00e9stamo hipotecario."); return; }

		plazo_meses = plazo_meses * medida

		// EL TIPO DE INTERES VIENE EN ANIOS Y LOS PASAMOS A TIPO INTERES MENSUAL
		tipo_mensual = tipo_mensual / 1200.0

		y  = 1.0 + tipo_mensual
		imp = cuota / ( tipo_mensual * Math.pow(y,plazo_meses) / ( Math.pow(y,plazo_meses) - 1.0 ) )
		document.forms[0].importe.value = formatear_numero(Math.round(imp *100.00) / 100.00)

		almacena()

	  tipo_mensual_max = 4.00 / 1200.0;
	  yMax=	1.0 + tipo_mensual_max;
	  cuotamax = imp * tipo_mensual_max * Math.pow(yMax,plazo_meses) / ( Math.pow(yMax,plazo_meses) - 1.0 );
	  //document.forms[0].cuotaMax.value = formatear_numero(Math.round(cuotamax *100.00) / 100.00);
	  tipo_mensual_min = 6.00 / 1200.0;
	  yMin= 1.0 + tipo_mensual_min;
	  cuotamin = imp * tipo_mensual_min * Math.pow(yMin,plazo_meses) / ( Math.pow(yMin,plazo_meses) - 1.0 );
	  //document.forms[0].cuotaMin.value = formatear_numero(Math.round(cuotamin *100.00) / 100.00);
	}

	function calculaplazo() {
		// CALCULA EL NUMERO DE PLAZOS NECESARIOS CONOCIENDO:

		if ( hay_cambio() == 0 ) { return }

		imp = parseFloat(desformatear_numero(document.forms[0].importe.value))
		cuota = parseFloat(desformatear_numero(document.forms[0].cuota.value))
		tipo_mensual = parseFloat(desformatear_numero(document.forms[0].tipo.value))

		if ( isNaN(imp) || imp == 0 )
		{ alert("Debe rellenar el campo 'Importe a financiar'")
		  return }
		if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 )
		{ alert("Debe rellenar el campo 'Tipo de inter\u00e9s'")
		  return }
		if ( isNaN(cuota) || cuota == 0 )
		{ alert("Debe rellenar el campo 'Cuota mensual a pagar'")
		  return }

		if ( tipo_mensual > 20.0 )
		{ alert("El Tipo de inter\u00e9s es superior al 20% y no es aceptable para un pr\u00e9stamo hipotecario.")
		  return }


	plazoHipo=-(Math.log(1-((imp*((tipo_mensual/100)/12))/cuota))/Math.log(1+((tipo_mensual/100)/12))) /12;
		document.forms[0].plazo.value = Math.floor( plazoHipo )
		almacena()

		tipo_mensual_max = 4.00 / 1200.0;
	  yMax=	1.0 + tipo_mensual_max;
	  cuotamax = imp * tipo_mensual_max * Math.pow(yMax,plazo_meses) / ( Math.pow(yMax,plazo_meses) - 1.0 );
	  //document.forms[0].cuotaMax.value = formatear_numero(Math.round(cuotamax *100.00) / 100.00);
	  tipo_mensual_min = 6.00 / 1200.0;
	  yMin= 1.0 + tipo_mensual_min;
	  cuotamin = imp * tipo_mensual_min * Math.pow(yMin,plazo_meses) / ( Math.pow(yMin,plazo_meses) - 1.0 );
	  //document.forms[0].cuotaMin.value = formatear_numero(Math.round(cuotamin *100.00) / 100.00);
	}

	function calculatipo() {
		// CALCULA EL TIPO DE INTERES CONOCIENDO:

		if ( hay_cambio() == 0 ) { return }

		imp = parseFloat(desformatear_numero(document.forms[0].importe.value))
		cuota = parseInt(desformatear_numero(document.forms[0].cuota.value))
		plazo_meses = parseInt(desformatear_numero(document.forms[0].plazo.value))

		medida = 12.0

		if ( isNaN(imp) || imp == 0 )
		{ alert("Debe rellenar el campo 'Importe a financiar'")
		  return }
		if ( isNaN(cuota) || cuota == 0 )
		{ alert("Debe rellenar el campo 'Cuota mensual a pagar'")
		  return }
		if ( isNaN(plazo_meses) || plazo_meses == 0 )
		{ alert("Debe rellenar el campo 'Plazo de amortizaci\u00f3n'")
		  return }

		plazo_meses = plazo_meses * medida

		tipo_mensual = 20.0 / 1200.0
		inc_tipo_mensual = 10.0 / 1200.0
		y  = 1.0 + tipo_mensual

		cuota_actual = Math.round(imp * tipo_mensual * Math.pow(y,plazo_meses) / ( Math.pow(y,plazo_meses) - 1.0 ))
		while ( cuota_actual != cuota)
		{	y  = 1.0 + tipo_mensual
			cuota_actual = Math.round(imp * tipo_mensual * Math.pow(y,plazo_meses) / ( Math.pow(y,plazo_meses) - 1.0 ))

			if (cuota_actual > cuota)
			{ // se aplica un interes muy alto, hay que bajarlo
			  tipo_mensual= tipo_mensual - inc_tipo_mensual
			}
			else
			{// se aplica un interes muy bajo, hay que subirlo
			  tipo_mensual= tipo_mensual + inc_tipo_mensual
			}
			if ((tipo_mensual * 1200.0) > 20.0)
			{ alert("El Tipo de inter\u00e9s a aplicar es superior al inter\u00e9s permitido por un banco (20%)")
			  almacena()
			  return
			}
			inc_tipo_mensual = inc_tipo_mensual / 2.0
		}
		tipo_mensual = tipo_mensual * 1200.0
		document.forms[0].tipo.value = formatear_numero(Math.round(tipo_mensual * 100.00) / 100.00)
		almacena()

	  tipo_mensual_max = 4.00 / 1200.0;
	  yMax=	1.0 + tipo_mensual_max;
	  cuotamax = imp * tipo_mensual_max * Math.pow(yMax,plazo_meses) / ( Math.pow(yMax,plazo_meses) - 1.0 );
	  //document.forms[0].cuotaMax.value = formatear_numero(Math.round(cuotamax *100.00) / 100.00);
	  tipo_mensual_min = 6.00 / 1200.0;
	  yMin= 1.0 + tipo_mensual_min;
	  cuotamin = imp * tipo_mensual_min * Math.pow(yMin,plazo_meses) / ( Math.pow(yMin,plazo_meses) - 1.0 );
	  //document.forms[0].cuotaMin.value = formatear_numero(Math.round(cuotamin *100.00) / 100.00);
	}
	function nada () { return }

function formatear_numero(numero)
{
  var auxNumero='';
  var strNumero= numero + '';
  for (var i = 0; i < strNumero.length; i++)
  {
	var ch = strNumero.substring(i, i + 1)
		  if (ch == '.') { auxNumero = auxNumero + ','; }
		  			else { auxNumero = auxNumero + ch; }
  }
  return auxNumero;
}

function desformatear_numero(numero)
{
  var auxNumero='';
  var strNumero= numero + '';
  for (var i = 0; i < strNumero.length; i++)
  {
	var ch = strNumero.substring(i, i + 1)
		  if (ch == ',') { auxNumero = auxNumero + '.'; }
		  			else { auxNumero = auxNumero + ch; }
  }
  return auxNumero;
}