function CalculaCuota() {
	impor = parseInt(Delete_miles(document.formulario.importeh.value));
	entra = 0
	//if (document.forms['frmDatos'].WG_Entrada.value == "")
	//	entra = 0;
	if (entra == 0)
		imp = (impor * 1.1)
	   else
	   	imp= (impor) - entra
	plazo_meses = parseInt(document.formulario.anos.value)
	tipo_mensual = parseFloat(document.formulario.interes.value)
	if ( isNaN(impor) || impor == 0 )
	{ alert("Debe rellenar el campo 'Precio'")
	  document.formulario.importeh.focus()
	  cuota = "Revisar"
	  return}
	if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 ) 
	{ alert("Debe rellenar el campo 'Interés'")
	  document.formulario.interes.focus()
	  cuota = "Revisar"
	  return}
	if ( isNaN(plazo_meses) || plazo_meses == 0 )
	{ alert("Debe rellenar el campo 'Años'")
	  document.formulario.anos.focus()
	  cuota = "Revisar"
	  return}

	plazo_meses = plazo_meses * 12.0
	tipo_mensual = tipo_mensual / 1200.0
	cuota = imp * tipo_mensual * Math.pow((tipo_mensual + 1.0),plazo_meses) / ( Math.pow((tipo_mensual + 1.0),plazo_meses) - 1.0 )
	document.formulario.cuota.value = Math.round(cuota)
	//fin del calculo de cuota mensual
	//calculo del ahorro fiscal
	var CA;
    var primero;
    var segundo;
    var tercero;
    var suma;
	CA = document.formulario.cuota.value * 12;
	plazo = document.formulario.anos.value;
	if (document.formulario.radio[0].checked){
			segundo = ((CA - 750000) * 0.15) + 187500;
			tercero = ((CA - 750000) * 0.15) + 150000;
			primero = segundo;
			//alert ("primero:.." + primero + " segundo:.." + segundo);
			for (i=3;i<plazo;i++)
			{
			  tercero = tercero + ((CA - 750000) * 0.15) + 150000;
			}
			suma = primero + segundo + tercero;
			document.formulario.indprianio.value = primero;
			document.formulario.dosconprianio.value = primero;
			document.formulario.indtotal.value = suma;
			document.formulario.doscontotal.value = suma;
			CA2 = CA / 2;
			primero = (((CA2 - 750000) * 0.15) + 187500) * 2;
			segundo = (((CA2 - 750000) * 0.15) + 187500) * 2;
			tercero = 0;
			for (i=2;i<plazo;i++)
			{
			  //alert(i);
			  tercero = tercero + (((CA2 - 750000) * 0.15) + 150000) * 2;
			}
			suma2 = primero + segundo + tercero;
			document.formulario.dosindtotal.value = suma2;
			document.formulario.dosindprianio.value = primero;	
	}
	else
	{
			segundo = ((CA - 4507.59) * 0.15) + 1126.8976;
			tercero = ((CA - 4507.59) * 0.15) + 901.5;
			primero = segundo;
			//alert ("primero:.." + primero + " segundo:.." + segundo);
			for (i=3;i<plazo;i++)
			{
			  tercero = tercero + ((CA - 4507.59) * 0.15) + 901.5;
			}
			suma = primero + segundo + tercero;
			document.formulario.indprianio.value = primero;
			document.formulario.dosconprianio.value = primero;
			document.formulario.indtotal.value = suma;
			document.formulario.doscontotal.value = suma;
    		CA2 = CA / 2;
			primero = (((CA2 - 4507.59) * 0.15) + 1126.8976) * 2;
			segundo = (((CA2 - 4507.59) * 0.15) + 1126.8976) * 2;
			for (i=3;i<plazo;i++)
			{
			  tercero = tercero + (((CA2 - 4507.59) * 0.15) + 901.5) * 2;
			}
			suma2 = primero + segundo + tercero;
			document.formulario.dosindtotal.value = suma2;
			document.formulario.dosindprianio.value = primero;
	}	
	//return Math.round(cuota)
}
// Segunda Tanda
function Formato_numero(numero){
	var nombre = "";
	//alert("En Formato_numero, Nombre recibido :" + name);
		var num = numero;
	//alert("Numero recibido :" + num);
	if (num != ""){
	//alert ("Entra en num !=");
		var str = "";
		while (num > 999){
			n1 = Math.floor(num/1000);
			n2 = num - 1000 * n1;

			if (n2 < 10){
			   n2 = "00" + n2;
	        	}
			else{
			   if (n2 < 100)
	       		      n2 = "0" + n2;
	                }
			str = "." + n2 + str;
			num = n1;
		}
		str = num + str;
		//return (str);
	}
	return (str);
}

function Delete_miles(numero_puntos){
    		var num_p;
    		num_p = numero_puntos;
    		longitud = 0;
    		longitud = num_p.length;
    		//alert("Longitud de la cifra :" + longitud);
    		n = 0;
    		var num_sp = "";
    		while (n<longitud){
        		//alert ("Dentro del while");
        		var car = ' ';
        		var car = num_p.charAt(n);
        		//alert("Carácter recogido :" + car);
        		if (car != '.')
            			num_sp += num_p.charAt(n);
        		n++;
    		}
    		var sin_puntos = parseInt(num_sp,10);
    		return sin_puntos;
}

function Validacion(valor)
{
	if (document.hipoteca.i1.value=="")
	{
		alert("Es obligatorio rellenar el campo Interés");
	}
	else if(document.hipoteca.anos.value=="")
	{
		alert("Es obligatorio rellenar el campo años");
	}
	else if(document.formulario.importeh.value=="")
	{
		alert("Es obligatorio rellenar el campo dinero");
	}
	else
	{
	 if (valor=="x")
	 {
		if (document.hipoteca.MinApert.value=="")
		{
			document.hipoteca.MinApert.value=0
		}
		if (document.hipoteca.Comision.value=="")
		{
			document.hipoteca.Comision.value=0
		}
		document.hipoteca.result.value="resultados";
	 }
	 document.hipoteca.submit();
	}
}

function isEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}

function CleanThis(Tipo)
{
	if (Tipo=="dinero")
		document.formulario.importeh.value="";
	else if(Tipo=="Apertura")
		document.hipoteca.MinApert.value="";
}

// función que valida según vas tecleando un importe poniendole los puntos
// parámetro de entrada	: obj (string)
//				: sign ("positive"/cualquier otra cosa)
// parámetro de salida 	: no procesa el evento si se teclea incorrectamente

function PonPuntos(obj, sign, imaxlength)
{
   //comprueba que el length no supere el maximo para esa caja de texto
   var snumero;
   snumero = obj.value + 1;
  if (snumero.length > imaxlength)
   {
     event.returnValue = false;
     return false;
   }
  //no dejar que ponga cero al principio de teclear el campo
  if (isEmpty(obj.value) && (event.keyCode == 48)) {
     event.returnValue = false;
  }
  else
  if (sign == "positive")
  {
    if ((event.keyCode < 48) || (event.keyCode > 57))
    {
      event.returnValue = false;
    }
    else
    {
      var s_pre = obj.value;
      var s_inter = "";
      var s_post = "";
      var single_character = "";
      var poner_punto = 1;

      for (var i = 0; i < s_pre.length; i++)
      {
        single_character = s_pre.substring(i,i+1);
        if (single_character != ".")
        {
          s_inter = s_inter + single_character;
        }
      }
      s_inter = s_inter + String.fromCharCode(event.keyCode);
      for (var j = s_inter.length; j > 0; j--, poner_punto++)
      {
        s_post = s_inter.substring(j-1,j) + s_post;

        if ((poner_punto == 3) && (j != 1))
        {
          poner_punto = 0;
          s_post = "." + s_post;
        }
      }
      obj.value = s_post;
      event.returnValue = false;
    }
  }
  else
  {
    if (
         (
           isEmpty(obj.value)
             &&
           ((event.keyCode < 48) || (event.keyCode > 57))
             &&
           (event.keyCode != 43)
             &&
           (event.keyCode != 45)
         )
           ||
         (
           (obj.value.length > 0)
             &&
           ((event.keyCode < 48) || (event.keyCode > 57))
         )
       )
    {
      event.returnValue = false;
    }
    else if (obj.value != "+" && obj.value != "-")
    {
      var s_pre = obj.value;
      var s_inter = "";
      var s_post = "";
      var single_character = "";
      var poner_punto = 1;
      var sign_character = "";

      if (
           (s_pre.substring(0, 1) == "+")
             ||
           (s_pre.substring(0, 1) == "-")
         )
      {
        sign_character = s_pre.substring(0, 1);
        s_pre = s_pre.substring(1, s_pre.length);
      }

      for (var i = 0; i < s_pre.length; i++)
      {
        single_character = s_pre.substring(i,i+1);

        if (single_character != ".")
        {
          s_inter = s_inter + single_character;
        }
      }

      s_inter = s_inter + String.fromCharCode(event.keyCode);

      for (var j = s_inter.length; j > 0; j--, poner_punto++)
      {
        s_post = s_inter.substring(j-1,j) + s_post;
        if ((poner_punto == 3) && (j != 1))
        {
          poner_punto = 0;
          s_post = "." + s_post;
        }
      }

      obj.value = sign_character + s_post;

      event.returnValue = false;
    }
  }
}


function Formato(numero){

	var nombre = "";

	//alert("En Formato_numero, Nombre recibido :" + name);
		var num = numero;
	//alert("Numero recibido :" + num);

	if (num != ""){

	//alert ("Entra en num !=");

		var str = "";
		while (num > 999){

			n1 = Math.floor(num/1000);
			n2 = num - 1000 * n1;

			if (n2 < 10){
			   n2 = "00" + n2;
	        	}
			else{
			   if (n2 < 100)
	       		      n2 = "0" + n2;
	                }
			str = "." + n2 + str;
			num = n1;

		}
		str = num + str;
		//return (str);
	}
	return (str);
}



function euro() {
	if (!(document.formulario.importeh.value == "")){
	var valor_actual;
	var res;

		valor1=Delete_miles(document.formulario.importeh.value);

	valor_actual = valor1 / 166.386;
	document.formulario.importeh.value = Math.round(valor_actual);
	document.formulario.importeh.value = Formato_numero(document.formulario.importeh.value);
	}
}

function pts() {
	if (!(document.formulario.importeh.value == "")){
		var valor_actual;
		var res;
		valor1=Delete_miles(document.formulario.importeh.value);
		valor_actual = valor1 * 166.386;
		document.formulario.importeh.value = Math.round(valor_actual);
		document.formulario.importeh.value = Formato_numero(document.formulario.importeh.value);
		alert ("El valor en pesetas se reajusto!");
		valor_actual = 0;
	}
}

function borrar(obj) {

obj.value = "";

}

function calcular_onclick() {
	//alert ("holaaaaaaaa");

		valor1=Delete_miles(document.formulario.importeh.value);

		if (!EsNumerico(document.formulario.anos.value)){
		alert ("EL valor introducido no es un número");
		document.formulario.anos.focus();
		return;}

		if (!EsNumerico(document.formulario.interes.value)){
		alert ("EL valor introducido no es un número");
		document.formulario.interes.focus();
		return;}

		if (document.formulario.anos.value > 35)
		{
			alert("El Banco de España no autoriza amortizaciones de mas de 35 años");
			document.formulario.anos.focus();
		}
		if (document.formulario.interes.value < 0){
				alert ("No introduzca intereses negativos");
				document.formulario.interes.focus();
				return; }
		if (document.formulario.anos.value < 0){
				alert ("No introduzca años negativos");
				document.formulario.anos.focus();
				return; }
		if (document.formulario.interes.value > 20)
		{
		alert ("¡Interés máximo 20%!");
		document.formulario.interes.value = "";
		document.formulario.interes.focus();
		return;
		}
		var suma;
		var totalsuma;

	if (document.formulario.importeh.value == "" || document.formulario.importeh.value == 0 )
	{ alert("Debe rellenar el campo -Importe-")
	  document.formulario.importeh.focus()
	  return}
	if ( isNaN(document.formulario.anos.value) || document.formulario.anos.value == 0 )
	{ alert("Debe rellenar el campo -Plazo-")
	  document.formulario.anos.focus()
	  return}
	if ( isNaN(document.formulario.interes.value) || document.formulario.interes.value == 0.0 )
	{ alert("Debe rellenar el campo -Interés-")
	  document.formulario.interes.focus()
	  return}


		CalculaCuota();
		document.formulario.indprianio.value = Formato_numero(Math.round(document.formulario.indprianio.value));
		document.formulario.indtotal.value = Formato_numero(Math.round(document.formulario.indtotal.value));
		document.formulario.dosconprianio.value = Formato_numero(Math.round(document.formulario.dosconprianio.value));
		document.formulario.doscontotal.value = Formato_numero(Math.round(document.formulario.doscontotal.value));
		document.formulario.dosindprianio.value = Formato_numero(Math.round(document.formulario.dosindprianio.value));
		document.formulario.dosindtotal.value = Formato_numero(Math.round(document.formulario.dosindtotal.value));


}

function EsNumerico(dato)
{
	var i;
	var longitud;
	var caracter;
	var sumo = 0;
	longitud = dato.length;
	valido = "+-0123456789.";
	for ( i=0; i <= longitud; i++)
	{
		caracter = dato.substring(i,i+1)
		if (valido.indexOf(caracter) == -1)
				   return false;
	}
	return true;
}



