var obj_C, obj_interes, obj_n, obj_cuota ,obj_ingreso,obj_interesanual;
function isNumber(cadena)
{
var anum = /(^\d+$)|(^\d+\.\d+$)/;
if (anum.test(cadena)) { return true; }
else { return false; }
}
function Leer_campo(campo, cambio_comas)
{
var ind1 = 0;
valor = campo.value;
valor2 = valor.toString();
if (valor2 == "") { return "0"; }
if (campo != obj_interes)
{
ind1 = valor2.indexOf(".");
if (ind1 != -1)
{
alert ("Introduce Cantidad sin puntos");
valor2 = "0";
}
}
else
{
ind1 = valor2.indexOf(",");
if (ind1 != -1)
{
valor2 = valor2.replace(/\,/g,".");
}
}
if (!isNumber(valor2)) { alert("El valor introducido debe ser un número entero positivo"); valor2 = "0"; }
return valor2;
}
function Escribir_campo(valor)
{
if (valor == 0) { return ""; }
valor2 = valor.toString();
valor2 = valor2.replace(/\./g, ",");
if (valor2.substr(0, 1) == ",") { valor2 = "0" + valor2; }
pos_coma = valor2.indexOf(",");
if (pos_coma == -1) { pos_coma = valor2.length; }
while (pos_coma > -1 && pos_coma > 3)
{
valor2 = valor2.substr(0, pos_coma-3) + "." + valor2.substr(pos_coma-3);
pos_coma = pos_coma - 3;
}
valor3 = "" + valor2;
valor3 = valor3.replace(/\,/g, ".");
if (parseFloat(valor3) == 0.0) { valor2 = ""; }
return valor2.valueOf();
}
function Redondear(numero)
{
num1 = numero.toString();
ind1 = num1.indexOf(".");
if (ind1 == -1) { return num1; }
else
{
res2 = Math.round(num1);
return(res2);
}
}
function Calcula_cuota_fijo(C, i, m, n, periodo)
{
return ((C*i)/m)/(1.0 - Math.pow(1.0+(i/m), -m*n)) * periodo;
}
var C = 0;
var n = 0;
var interes = 0;
var cuota = 0;
var m = 12.0;
var emp =0;
var mesaumento = 0;
var aumento = 0;
var ingreso = 0;
var interesanual = 0;
var a = new Array(n);
function init()
{
obj_C = document.formulario.textfield3;
obj_interes = document.formulario.textfield1;
obj_n = document.formulario.textfield;
obj_cuota = document.formulario.textfield2;
obj_interesanual = document.formulario.textfield14;
Actualizar_valores("");
calcula();
}
function CalculoInteres()
{
var i,j,ii,k;
var meses = n*12;
a = new Array(meses);
interesanual=0;
for (i=0;i<meses;i++)
{
a[i]=new Array(3);
if (i==0)
{
a[i][1]=C*interes/100/12;
a[i][0]=cuota-a[i][1];
a[i][2]= C-a[i][0];
}
else
{
a[i][1]=a[i-1][2]*interes/100/12;
a[i][0]=cuota-a[i][1];
a[i][2]=a[i-1][2]-a[i][0];
}
}
for (var h = 0;h<meses;h++)
{
interesanual +=a[h][1];
}
}
function calcula(cual)
{
indice = 0;
i = interes / 100.0;
var no = 0;
if (cual == "C") { cuota = 0; }
else if (cual == "n")
{
if ((parseInt(obj_n.value,10))>30)
{
alert ("No admitimos créditos de más de 30 años");
no = 1;
obj_n.value = 0;
obj_cuota.value = 0;
obj_interesanual.value = 0;
obj_n.focus();
}
else
{
cuota = 0;
}
}
else if (cual == "cuota")
{
n = 0;
}
else if (cual == "interes")
{
if (obj_interes.value == 0)
{
alert ("el tipo de interes debe ser superior a cero");
obj_interes.focus();
}
else
cuota = 0;
}
if (obj_C.value == 0 || obj_n.value == 0 || obj_interes.value ==0)
{
obj_cuota.value = 0;
obj_interesanual.value = 0;
}
if (obj_C.value != 0 && obj_n.value != 0 && obj_interes.value !=0 && cuota == 0 && no == 0)
{
cuota = Calcula_cuota_fijo(C, i, m, n, 1);
obj_cuota.value = Escribir_campo(Redondear(cuota));
CalculoInteres();
obj_interesanual.value = Escribir_campo(Redondear(interesanual));
}
else if (C == 0 && n != 0 && cuota != 0 && no == 0)
{
if ((m*cuota) < (C*i))
{
alert("Los datos no son correctos");
}
else
{
C = ((cuota*m)/i)*(1.0-( Math.pow(1.0+(i/m), -m*n)));
obj_C.value = Redondear(C);
CalculoInteres();
obj_interesanual.value = Escribir_campo(Redondear(interesanual));
}
}
else if (C != 0 && n == 0 && cuota != 0 && no == 0)
{
if ((m*cuota) < (C*i))
{
alert("Los datos no son correctos");
}
else
{
n = Math.round(Math.log((m*cuota)/((m*cuota)-(C*i))) / Math.log(Math.pow((1.0+(i/m)), m)));
obj_n.value = parseInt(n);
cuota = Calcula_cuota_fijo(C, i, m, n, 1);
obj_cuota.value = Escribir_campo(Redondear(cuota));
CalculoInteres();
obj_interesanual.value = Escribir_campo(Redondear(interesanual));
}
}
else if (cual == " ")
{
alert("Debes introducir dos de los campos y dejar el tercero en blanco para calcularlo.");
}
}
function Actualizar_valores(variable)
{
switch(variable)
{
case "C": C = Leer_campo(obj_C); break;
case "cuota": cuota = Leer_campo(obj_cuota); break;
case "n": n = Leer_campo(obj_n); break;
case "interes": interes = Leer_campo(obj_interes); break;
}
obj_C.value = Redondear(C);
obj_cuota.value = Escribir_campo(Redondear(cuota));
obj_n.value = parseInt(n,10);
obj_interes.value = interes;
obj_interesanual.value = Escribir_campo(Redondear(interesanual));
}

