// JavaScript Document

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.Calculadora.medida.options[0].selected)
		{	if ( document.Calculadora.medida_anterior.value != 12.0)
			{ return 1 }
		}
		else
		{	if ( document.Calculadora.medida_anterior.value == 1.0)
		    { return 1 }
		}

		if ( document.Calculadora.importe.value != document.Calculadora.importe_anterior.value)
		{ return 1 }
		if ( document.Calculadora.plazo.value != document.Calculadora.plazo_anterior.value)
		{ return 1 }
		if ( document.Calculadora.tipo.value != document.Calculadora.tipo_anterior.value)
		{ return 1 }
		if ( document.Calculadora.cuota.value != document.Calculadora.cuota_anterior.value)
		{ return 1 }
		return 0
	}

	function almacena() {
		document.Calculadora.importe_anterior.value = document.Calculadora.importe.value
		document.Calculadora.plazo_anterior.value = document.Calculadora.plazo.value
		document.Calculadora.tipo_anterior.value = document.Calculadora.tipo.value
		document.Calculadora.cuota_anterior.value = document.Calculadora.cuota.value
		if (document.Calculadora.medida.options[0].selected)
		{	document.Calculadora.medida_anterior.value = 12.0 }
		else
		{	document.Calculadora.medida_anterior.value = 1.0 }
	}

	function calculacuota() {
		// CALCULA LA CUOTA A PAGAR CONOCIENDO:
		// 	EL IMPORTE DEL PRESTAMO
		//	EL NUMERO DE PLAZOS A PAGAR
		//	EL TIPO DE INTERES QUE SE LE APLICA

		if ( hay_cambio() == 0 ) { return }

		imp = parseInt(document.Calculadora.importe.value)
		plazo_meses = parseInt(document.Calculadora.plazo.value)
		tipo_mensual = parseFloat(document.Calculadora.tipo.value)


		if (document.Calculadora.medida.options[0].selected)
		{ medida = 12.0
		}
		else
		{ medida = 1.0
		}

		if ( isNaN(imp) || imp == 0 )
		{ alert("Debe rellenar el campo 'Importe pr?stamo'")
		  return }
		if ( isNaN(plazo_meses) || plazo_meses == 0 )
		{ alert("Debe rellenar el campo 'Plazo'")
		  return }
		if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 ) 
		{ alert("Debe rellenar el campo 'Tipo Nominal'")
		  return }

		if ( tipo_mensual > 20.0 ) 
		{ alert("El Tipo Nominal 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 A?OS 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.Calculadora.cuota.value = Math.round(cuota *100.00) / 100.00
		almacena()
	}

	function calculaimporte() {
		// CALCULA EL IMPORTE DEL PRESTAMO A SOLICITAR CONOCIENDO:
		//	EL NUMERO DE PLAZOS A PAGAR
		//	EL TIPO DE INTERES QUE SE LE APLICA
		//	LA CUOTA A PAGAR EN CADA PLAZO

		if ( hay_cambio() == 0 ) { return }

		plazo_meses = parseInt(document.Calculadora.plazo.value)
		tipo_mensual = parseFloat(document.Calculadora.tipo.value)
		cuota = parseInt(document.Calculadora.cuota.value)

		if (document.Calculadora.medida.options[0].selected)
		{ medida = 12.0
		}
		else
		{ medida = 1.0
		}

		if ( isNaN(cuota) || cuota == 0 )
		{ alert("Debe rellenar el campo 'Importe cuota'")
		  return }
		if ( isNaN(plazo_meses) || plazo_meses == 0 )
		{ alert("Debe rellenar el campo 'Plazo'")
		  return }
		if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 ) 
		{ alert("Debe rellenar el campo 'Tipo Nominal'")
		  return }

		if ( tipo_mensual > 20.0 ) 
		{ alert("El Tipo Nominal 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 A?OS 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.Calculadora.importe.value = Math.round(imp *100.00) / 100.00

		almacena()
	}

	function calculaplazo() {
		// CALCULA EL NUMERO DE PLAZOS NECESARIOS CONOCIENDO:
		// 	EL IMPORTE DEL PRESTAMO
		//	EL TIPO DE INTERES QUE SE LE APLICA
		//	LA CUOTA A PAGAR EN CADA PLAZO

		if ( hay_cambio() == 0 ) { return }

		imp = parseInt(document.Calculadora.importe.value)
		cuota = parseInt(document.Calculadora.cuota.value)
		tipo_mensual = parseFloat(document.Calculadora.tipo.value)

		if ( isNaN(imp) || imp == 0 )
		{ alert("Debe rellenar el campo 'Importe pr?stamo'")
		  return }
		if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 ) 
		{ alert("Debe rellenar el campo 'Tipo Nominal'")
		  return }
		if ( isNaN(cuota) || cuota == 0 )
		{ alert("Debe rellenar el campo 'Importe cuota'")
		  return }

		if ( tipo_mensual > 20.0 ) 
		{ alert("El Tipo Nominal es superior al 20% y no es aceptable para un pr?stamo hipotecario.")
		  return }

		tipo_mensual = tipo_mensual / 1200.0

		y  = 1.0 + tipo_mensual

		plazo_meses = 1;
		cuota_actual = Math.round(imp * tipo_mensual * Math.pow(y,plazo_meses) / ( Math.pow(y,plazo_meses) - 1.0 ))
		while (cuota_actual > cuota)
		{	ultima_cuota = cuota_actual;
			cuota_actual = Math.round(imp * tipo_mensual * Math.pow(y,plazo_meses) / ( Math.pow(y,plazo_meses) - 1.0 ))
			plazo_meses++
			if (plazo_meses > 360)
			{	alert("El n?mero de plazos necesarios excede el limite admitido por un banco (30 a?os)")
				almacena()
				return;
			}
		}
		document.Calculadora.plazo.value = Math.round(plazo_meses - 1) 
		document.Calculadora.medida.options[1].selected = true
		// document.Calculadora.medida.selectedindex = 1
		almacena()
	}

	function calculatipo() {
		// CALCULA EL TIPO DE INTERES CONOCIENDO:
		// 	EL IMPORTE DEL PRESTAMO
		//	EL NUMERO DE PLAZOS A PAGAR
		//	LA CUOTA A PAGAR EN CADA PLAZO

		if ( hay_cambio() == 0 ) { return }

		imp = parseInt(document.Calculadora.importe.value)
		cuota = parseInt(document.Calculadora.cuota.value)
		plazo_meses = parseInt(document.Calculadora.plazo.value)

		if (document.Calculadora.medida.options[0].selected)
		{ medida = 12.0
		}
		else
		{ medida = 1.0
		}

		if ( isNaN(imp) || imp == 0 )
		{ alert("Debe rellenar el campo 'Importe pr?stamo'")
		  return }
		if ( isNaN(cuota) || cuota == 0 )
		{ alert("Debe rellenar el campo 'Importe cuota'")
		  return }
		if ( isNaN(plazo_meses) || plazo_meses == 0 )
		{ alert("Debe rellenar el campo 'Plazo'")
		  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 interes a aplicar es superior al interes permitido por un banco (20%)")
			  almacena()
			  return
			}
			inc_tipo_mensual = inc_tipo_mensual / 2.0
		}
		tipo_mensual = tipo_mensual * 1200.0
		document.Calculadora.tipo.value = Math.round(tipo_mensual * 100.00) / 100.00
		almacena()
	}
	function nada ()
	{
	  return
	}



function verificar_numero2(numero,formulario2)
 {
  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_cambio2() {
 
  if (document.Calculadora2.medida2.options[0].selected)
  { if ( document.Calculadora2.medida_anterior2.value != 12.0)
   { return 1 }
  }
  else
  { if ( document.Calculadora2.medida_anterior2.value == 1.0)
      { return 1 }
  }
 
  if ( document.Calculadora2.importe2.value != document.Calculadora2.importe_anterior2.value)
  { return 1 }
  if ( document.Calculadora2.plazo2.value != document.Calculadora2.plazo_anterior2.value)
  { return 1 }
  if ( document.Calculadora2.tipo2.value != document.Calculadora2.tipo_anterior2.value)
  { return 1 }
  if ( document.Calculadora2.cuota2.value != document.Calculadora2.cuota_anterior2.value)
  { return 1 }
  return 0
 }
 
 function almacena2() {
  document.Calculadora2.importe_anterior2.value = document.Calculadora2.importe2.value
  document.Calculadora2.plazo_anterior2.value = document.Calculadora2.plazo2.value
  document.Calculadora2.tipo_anterior2.value = document.Calculadora2.tipo2.value
  document.Calculadora2.cuota_anterior2.value = document.Calculadora2.cuota2.value
  if (document.Calculadora2.medida2.options[0].selected)
  { document.Calculadora2.medida_anterior2.value = 12.0 }
  else
  { document.Calculadora2.medida_anterior2.value = 1.0 }
 }
 
 function calculacuota2() {
  // CALCULA LA CUOTA A PAGAR CONOCIENDO:
  //  EL IMPORTE DEL PRESTAMO
  // EL NUMERO DE PLAZOS A PAGAR
  // EL TIPO DE INTERES QUE SE LE APLICA
 
  if ( hay_cambio2() == 0 ) { return }
 
  imp = parseInt(document.Calculadora2.importe2.value)
  plazo_meses = parseInt(document.Calculadora2.plazo2.value)
  tipo_mensual = parseFloat(document.Calculadora2.tipo2.value)
 

  if (document.Calculadora2.medida2.options[0].selected)
  { medida = 12.0
  }
  else
  { medida = 1.0
  }
 
  if ( isNaN(imp) || imp == 0 )
  { alert("Debe rellenar el campo 'Importe pr?stamo'")
    return }
  if ( isNaN(plazo_meses) || plazo_meses == 0 )
  { alert("Debe rellenar el campo 'Plazo'")
    return }
  if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 ) 
  { alert("Debe rellenar el campo 'Tipo Nominal'")
    return }
 
  if ( tipo_mensual > 20.0 ) 
  { alert("El Tipo Nominal 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 A?OS 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.Calculadora2.cuota2.value = Math.round(cuota *100.00) / 100.00
  almacena2()
 }
 
 function calculaimporte2() {
  // CALCULA EL IMPORTE DEL PRESTAMO A SOLICITAR CONOCIENDO:
  // EL NUMERO DE PLAZOS A PAGAR
  // EL TIPO DE INTERES QUE SE LE APLICA
  // LA CUOTA A PAGAR EN CADA PLAZO
 
  if ( hay_cambio2() == 0 ) { return }
 
  plazo_meses = parseInt(document.Calculadora2.plazo2.value)
  tipo_mensual = parseFloat(document.Calculadora2.tipo2.value)
  cuota = parseInt(document.Calculadora2.cuota2.value)
 
  if (document.Calculadora2.medida2.options[0].selected)
  { medida = 12.0
  }
  else
  { medida = 1.0
  }
 
  if ( isNaN(cuota) || cuota == 0 )
  { alert("Debe rellenar el campo 'Importe cuota'")
    return }
  if ( isNaN(plazo_meses) || plazo_meses == 0 )
  { alert("Debe rellenar el campo 'Plazo'")
    return }
  if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 ) 
  { alert("Debe rellenar el campo 'Tipo Nominal'")
    return }
 
  if ( tipo_mensual > 20.0 ) 
  { alert("El Tipo Nominal 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 A?OS 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.Calculadora2.importe2.value = Math.round(imp *100.00) / 100.00
 
  almacena2()
 }
 
 function calculaplazo2() {
  // CALCULA EL NUMERO DE PLAZOS NECESARIOS CONOCIENDO:
  //  EL IMPORTE DEL PRESTAMO
  // EL TIPO DE INTERES QUE SE LE APLICA
  // LA CUOTA A PAGAR EN CADA PLAZO
 
  if ( hay_cambio2() == 0 ) { return }
 
  imp = parseInt(document.Calculadora.importe2.value)
  cuota = parseInt(document.Calculadora.cuota2.value)
  tipo_mensual = parseFloat(document.Calculadora.tipo2.value)
 
  if ( isNaN(imp) || imp == 0 )
  { alert("Debe rellenar el campo 'Importe pr?stamo'")
    return }
  if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 ) 
  { alert("Debe rellenar el campo 'Tipo Nominal'")
    return }
  if ( isNaN(cuota) || cuota == 0 )
  { alert("Debe rellenar el campo 'Importe cuota'")
    return }
 
  if ( tipo_mensual > 20.0 ) 
  { alert("El Tipo Nominal es superior al 20% y no es aceptable para un pr?stamo hipotecario.")
    return }
 
  tipo_mensual = tipo_mensual / 1200.0
 
  y  = 1.0 + tipo_mensual
 
  plazo_meses = 1;
  cuota_actual = Math.round(imp * tipo_mensual * Math.pow(y,plazo_meses) / ( Math.pow(y,plazo_meses) - 1.0 ))
  while (cuota_actual > cuota)
  { ultima_cuota = cuota_actual;
   cuota_actual = Math.round(imp * tipo_mensual * Math.pow(y,plazo_meses) / ( Math.pow(y,plazo_meses) - 1.0 ))
   plazo_meses++
   if (plazo_meses > 360)
   { alert("El n?mero de plazos necesarios excede el limite admitido por un banco (30 a?os)")
    almacena2()
    return;
   }
  }
  document.Calculadora2.plazo2.value = Math.round(plazo_meses - 1) 
  document.Calculadora2.medida2.options[1].selected = true
  // document.Calculadora.medida.selectedindex = 1
  almacena2()
 }
 
 function calculatipo2() {
  // CALCULA EL TIPO DE INTERES CONOCIENDO:
  //  EL IMPORTE DEL PRESTAMO
  // EL NUMERO DE PLAZOS A PAGAR
  // LA CUOTA A PAGAR EN CADA PLAZO
 
  if ( hay_cambio2() == 0 ) { return }
 
  imp = parseInt(document.Calculadora2.importe2.value)
  cuota = parseInt(document.Calculadora2.cuota2.value)
  plazo_meses = parseInt(document.Calculadora2.plazo2.value)
 
  if (document.Calculadora2.medida2.options[0].selected)
  { medida = 12.0
  }
  else
  { medida = 1.0
  }
 
  if ( isNaN(imp) || imp == 0 )
  { alert("Debe rellenar el campo 'Importe pr?stamo'")
    return }
  if ( isNaN(cuota) || cuota == 0 )
  { alert("Debe rellenar el campo 'Importe cuota'")
    return }
  if ( isNaN(plazo_meses) || plazo_meses == 0 )
  { alert("Debe rellenar el campo 'Plazo'")
    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 interes a aplicar es superior al interes permitido por un banco (20%)")
     almacena2()
     return
   }
   inc_tipo_mensual = inc_tipo_mensual / 2.0
  }
  tipo_mensual = tipo_mensual * 1200.0
  document.Calculadora2.tipo2.value = Math.round(tipo_mensual * 100.00) / 100.00
  almacena2()
 }
 
 function nada2() {
   return
 }
