	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 != ',') 
			{  numero.value=''
		           numero.focus()
		           numero.select()
			   noes=1
			}               
		}	  
		if (noes==1)
		{ alert('El valor introducido "' + str + '" no es un número.')
		  return 1
		}
		return 0;
	}

	function hay_cambio() {

		if ( document.forms[1].medida_anterior.value != 12.0)
			{ return 1 }
		
		if ( document.forms[1].tipoVF_anterior.value != 1)
			{ return 1 }
		
		if ( document.forms[1].importe.value != document.forms[1].importe_anterior.value)
		{ return 1 }
		if ( document.forms[1].plazo.value != document.forms[1].plazo_anterior.value)
		{ return 1 }
		if ( document.forms[1].tipo.value != document.forms[1].tipo_anterior.value)
		{ return 1 }
		if ( document.forms[1].cuota.value != document.forms[1].cuota_anterior.value)
		{ return 1 }
		return 0
	}

	function almacena() {
		document.forms[1].importe_anterior.value = document.forms[1].importe.value
		document.forms[1].plazo_anterior.value = document.forms[1].plazo.value
		document.forms[1].tipo_anterior.value = document.forms[1].tipo.value
		document.forms[1].cuota_anterior.value = document.forms[1].cuota.value
		document.forms[1].medida_anterior.value = 12.0
		document.forms[1].tipoVF_anterior.value = 0
	}

	function calculacuota() {
		// CALCULA LA CUOTA A PAGAR CONOCIENDO:

		if ( hay_cambio() == 0 ) { return }

		imp = parseInt(desformatear_numero(document.forms[1].importe.value))
		plazo_meses = parseInt(desformatear_numero(document.forms[1].plazo.value))
		tipo_mensual = parseFloat(desformatear_numero(document.forms[1].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ón'")
		  return }
		if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 ) 
		{ alert("Debe rellenar el campo 'Tipo de interés'")
		  return }

		if ( tipo_mensual > 20.0 ) 
		{ alert("El Tipo de interés es superior al 20% y no es aceptable para un préstamo 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[1].cuota.value = formatear_numero(Math.round(cuota *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[1].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[1].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[1].plazo.value))
		tipo_mensual = parseFloat(desformatear_numero(document.forms[1].tipo.value))
		cuota = parseFloat(desformatear_numero(document.forms[1].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ón'")
		  return }
		if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 ) 
		{ alert("Debe rellenar el campo 'Tipo de interés'")
		  return }

		if ( tipo_mensual > 20.0 ) 
		{ alert("El Tipo de interés es superior al 20% y no es aceptable para un préstamo 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[1].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[1].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[1].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[1].importe.value))

		cuota = parseFloat(desformatear_numero(document.forms[1].cuota.value))
	
		tipo_mensual = parseFloat(desformatear_numero(document.forms[1].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és'")
		  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és es superior al 20% y no es aceptable para un préstamo hipotecario.")
		  return }

		
    plazoHipo=-(Math.log(1-((imp*((tipo_mensual/100)/12))/cuota))/Math.log(1+((tipo_mensual/100)/12))) /12;		
		document.forms[1].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[1].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[1].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[1].importe.value))
		cuota = parseInt(desformatear_numero(document.forms[1].cuota.value))
		plazo_meses = parseInt(desformatear_numero(document.forms[1].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ón'")
		  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és a aplicar es superior al interés permitido por un banco (20%)")
			  almacena()
			  return
			}
			inc_tipo_mensual = inc_tipo_mensual / 2.0
		}
		tipo_mensual = tipo_mensual * 1200.0
		document.forms[1].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[1].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[1].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; 
}


