function computeLoan(){
    const params = new URLSearchParams(document.location.search);
    const interest_ic = params.get("tic");
    const interest_cv = params.get("tcv");
    const amount = document.querySelector('#amount').value;
    const k_ic = document.querySelector('#two').value;
    const k_cv = document.querySelector('#three').value;
    const months = document.querySelector('#months').value;
    const fecha_inicio = document.querySelector('#fecha_inicio').value;
    const periodo = 30;
    const n = months*30/periodo
    const interestic = interest_ic*0.01/12
    const interestcv = interest_cv*0.01/12
    const amountt = 1*amount
    const date = new Date();
    const acciones = amount/1000
    let capital_ic = amountt*k_ic/100
    let capital_cv = amountt*k_cv/100

    let payment_cv = (capital_cv*interestcv)
    let payment = (payment_cv).toFixed(2)
    let capital_c = [capital_ic];
    let capital_g = [capital_cv];
    let capital_t = [amount];
    //let fechas = [0]
    //for (i = 0; i<months; i++){
    //fechas[i+1] = fechas[i] + 1 ;
       // }
    //console.log(fechas)    
    for (i = 0; i<=months; i++){
    capital_c[i+1] = capital_c[i]*(1+interestic);
    capital_g[i+1] = capital_g[i] //+ capital_cv*interestcv
    capital_t[i+1] = capital_c[i] + capital_g[i]
        }
    
    console.log(capital_t)
    
    // Vectores que utilizaremos en el loop que genera la tabla
  var hoy = new Date(fecha_inicio.replace(/-/g, '\/'));
  var fecha = [hoy.toLocaleDateString('es-ES', options)];
  var periodos = [0];
  
  // Creemos una función que nos permita sumar periodos
  var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
  var numOfMonth = periodo/30; // Número de días a sumar, puedes cambiarlo a lo que necesites.
  console.log(numOfMonth)
  //function addDays(numOfDays, date) {
    //date.setDate(date.getDate() + numOfDays);
    //return date.toLocaleDateString('es-ES', options);
  //}
  
  //Function que solo añade meses a la fecha
  function addMonths(originalDate, monthsToAdd) {
  // Make a copy of the original date to avoid modifying it directly
  const newDate = new Date(originalDate);

  // Calculate the new month
  const currentMonth = newDate.getMonth();
  newDate.setMonth(currentMonth + monthsToAdd);

  return newDate.toLocaleDateString('es-ES', options);
}


  // Un loopsillo de periodos y fechas. Lo separamos por obvias razones
  for (var i = 0; i <= n; i++) {
    periodos[i + 1] = periodos[i] + 1;
    fecha[i + 1] = addMonths(hoy, numOfMonth*[i+1]); // Sumamos el total de días acumulados hasta el momento
  }
  fecha[0] = addMonths(hoy, numOfMonth* (0)); // Sumamos el total de días acumulados hasta el momento

    pago_final = (payment_cv + capital_ic*(1+interestic)**(months) +  capital_cv);

    pagos_totales = (payment_cv*months + capital_cv  + capital_ic*(1+interestic)**(months))
    x = pagos_totales - amount
    rentabilidad = (100*((payment_cv*months + capital_cv + capital_ic*(1+interestic)**(months))/amountt-1)).toFixed(2)
    dividendo = 100*rentabilidad/(months*12)
    //regedit to add a comma after every three digits
    payment = payment.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); //\B looks for a word boundary, ? says what to look for, \d looks for 3 digits in a row
    
    c_ic = capital_ic.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); //\B looks for a word boundary, ? says what to look for, \d looks for 3 digits in a row
    capitalcv = capital_cv.toFixed(2)
    dividendo = dividendo.toFixed(2);
    c_cv = capitalcv.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); //\B looks for a word boundary, ? says what to look for, \d looks for 3 digits in a row
    pagofinal = pago_final.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    suma_pagos = pagos_totales.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    ganancia = x.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    rentab = rentabilidad.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    saldoic = (capital_ic*(1+interestic)**(months)).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    accion = acciones.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    inversion = (1000*acciones).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    //let icptg = (k_ic).toFixed(2)
    //icptg = icptg.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    const noproyecto = params.get("proyecto");
    localStorage.setItem( 'proyecto', noproyecto );
    const teso = params.get("tesoreria");
    localStorage.setItem( 'tesoreria', teso );
    document.querySelector('#payment').innerHTML = `$${payment}`
    //document.querySelector('#capital_saldos').innerHTML = `$${c_saldos}`
    document.querySelector('#capital_ic').innerHTML = `$${c_ic}`
    document.querySelector('#capital_cv').innerHTML = `$${c_cv}`
    //document.querySelector('#dividendo').innerHTML = `Dividendo = ${dividendo}`
    document.querySelector('#pago_final').innerHTML = `$${pagofinal}`
    document.querySelector('#suma_pagos').innerHTML = `$${suma_pagos}`
    
    document.querySelector('#rentabilidad').innerHTML = `${rentab}%`
    localStorage.setItem( 'fecha_inicio', fecha_inicio );document.getElementById('fecha_inicio').textContent = fecha_inicio
    localStorage.setItem( 'acciones', accion );
    localStorage.setItem( 'inversion', inversion );
    localStorage.setItem( 'capital_compuesto', c_ic );
    localStorage.setItem( 'capital_v', c_cv );
    localStorage.setItem( 'rentab', rentab );
    localStorage.setItem('dividendo', dividendo);
    console.log(dividendo)
    
    //document.querySelector('#rentabilidad').innerHTML = `Rentabilidad = ${rentab}%`
    //document.querySelector('#saldoic').innerHTML = `Valor futuro del interés compuesto = $${saldoic}`
   // document.querySelector('#icptg').innerHTML = `Rentabilidad = ${icptg}%`
    //let x = (payment_cv*months + capital_cv + payment_s*months + capital_ic*(1+interestic)**(months) + amount*0.1*interestd*months + 0.1*amount-amount).toFixed(2)
    //P*(1+tasa_comision)*interes/(1-(1+interes)**(-n))
    const ctx = document.getElementById('myChart');
    const pagos = document.getElementById('suma_pagos');
    //Ganancia = payment_cv*months + capital_cv + payment_s*months + capital_ic*(1+interestic)**(months) + amount*0.1*interestd*months + 0.1*amount-amount
    Ganancia = payment_cv*months + capital_cv + capital_ic*(1+interestic)**(months)-amount
    g = Ganancia.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    document.querySelector('#ganancia').innerHTML = `$${g}`
    document.querySelector('#ganancia2').innerHTML = `$${g}`
    Highcharts.chart('container', {
      chart: {
        type: 'column'
      },
      title: {
        text: 'Inversión vs. Ganancia'
      },
      subtitle: {
        text: 'A continuación se muestran inversión, ganancia y pago total:'
      },
      xAxis: {
        categories: [
          'Ganancia',
          'Inversión',
          'Pago Total'
        ],
        crosshair: true
      },
      yAxis: {
        title: {
          useHTML: true,
          text: 'Miles de pesos mexicanos, m.n.'
        }
      },
      tooltip: {
        headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
        pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
          '<td style="padding:0"><b>{point.y:.1f}</b></td></tr>',
        footerFormat: '</table>',
        shared: true,
        useHTML: true
      },
      plotOptions: {
        column: {
          pointPadding: 0.2,
          borderWidth: 0
        }
      },
      series: [{
        name: 'Inversión',
        data: [parseFloat(amount)],
        
    
      }, {
        name: 'Ganancia',
        data: [x],
        
      }, {
        name: 'Pago Total',
        data: [pagos_totales],
        
    
      }]
    });
    
    Highcharts.chart('series', {

    title: {
        text: 'Capital y ganancias'
    },

    subtitle: {
        text: 'Comportamiento del capital y ganancias totales en el tiempo'
    },

    yAxis: {
        title: {
            text: 'Miles de pesos, m.n.'
        }
    },

    xAxis: {
        accessibility: {
            rangeDescription: 'Periodos en meses'
        }
    },

    legend: {
        layout: 'vertical',
        align: 'right',
        verticalAlign: 'middle'
    },

    plotOptions: {
        series: {
            label: {
                connectorAllowed: false
            },
            pointStart: -1
        }
    },

    series: [{
        name: 'Capital',
        data: capital_t
    }//, {
     //   name: 'Other',
     //   data: [21908, 5548, 8105, 11248, 8989, 11816, 18274,
     //       17300, 13053, 11906, 10073]
    //}
    ],

    responsive: {
        rules: [{
            condition: {
                maxWidth: 500
            },
            chartOptions: {
                legend: {
                    layout: 'horizontal',
                    align: 'center',
                    verticalAlign: 'bottom'
                }
            }
        }]
    }

});
//var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };  
    //function addMonths(numOfMonths, date = new Date()) {
        //date.setMonth(date.getMonth() + numOfMonths);
        //return date.toLocaleDateString('es-ES', options);
    //}  
        
const interest = params.get("tic")*0.01/12;

//const pago = parseFloat(document.querySelector('#pago').value);
//const monto = parseFloat(document.querySelector('#monto').value);
//const interestic = parseFloat(params.get("tic")/1200);  
//const interestcv = parseFloat(params.get("tcv")/1200);
const m = parseFloat(document.querySelector('#months').value);
//const m = month-1
localStorage.setItem( 'months', m );


//let fechas = [currentDate]
//let periodos = [0]
//let fechas = [new Date().toLocaleDateString('es-ES', options)]
//let fecha = [new Date().toLocaleDateString('es-ES', options)]
monto = amount
let saldo = [capital_ic]
//let periodos = [0]
let pagop = [0]
let interes = [0]
let interes_g =[0]
//for (i = 0; i<m; i++){
//periodos[i+1] = periodos[i] + 1 ;
//fecha[i+1] = addMonths(i +1, new Date());
//}
//console.log(fechas)    
for (i = 0; i<=m; i++){
//capital_c[i+1] = (capital_c[i]*(1+interestic));
saldo[i+1] = saldo[i]*(1+interestic) 
pagop[i+1] = capital_cv*interestcv
interes_g[i+1] = capital_cv*interestcv
interes[i+1] = saldo[i]*interestic 
    }
saldo[m] = saldo[m-1]*(1+interestic)  + capital_g[i]   
//pagop[m] = saldo[m]    
//pagop[m] = saldo[m] 
//saldonuevo = [saldo]
//for (i = 0; i<=m; i++){ 
//saldonuevo[i] = saldonuevo[i].toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
//pagop[i] = pagop[i].toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

pagop[m] = saldo[m] + pagop[m-1]
for (i=1; i<=m; i++){
  saldo[i] = saldo[i].toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  interes[i] = interes[i].toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  pagop[i] = pagop[i].toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  capital_g[i] = capital_g[i].toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  interes_g[i] = interes_g[i].toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
      }
  capital_g[0] = capital_g[0].toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");  
  saldo[0] = saldo[0].toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
//fecha[i] = fechas[i].getUTCDay() + "/" + fechas[i].getUTCMonth()+1+"/"  + fechas[i].getUTCFullYear();
//fecha[i]= fechas[i];
    
var headers = ["Periodo",
"Rendimiento Ordinario", "Saldo Global" , "Rendimiento Global",  "Pago", "Saldo Total", 
"Fecha"];
var table = document.createElement("TABLE");  //makes a table element for the page
    
for(var i = 0; i <= m; i++) {
    var row = table.insertRow(i);                       
    row.insertCell(0).innerHTML = periodos[i];
    row.insertCell(1).innerHTML = `$${interes[i]}`;
    row.insertCell(2).innerHTML = `$${capital_g[i]}`;
    row.insertCell(3).innerHTML = `$${interes_g[i]}`;
    row.insertCell(4).innerHTML = `$${pagop[i]}`; 
    row.insertCell(5).innerHTML = `$${saldo[i]}`;
    row.insertCell(6).innerHTML = fecha[i];
    
    
}

var header = table.createTHead();
var headerRow = header.insertRow(0);
for(var i = 0; i < headers.length; i++) {
    headerRow.insertCell(i).innerHTML = headers[i];
}

//document.body.append(table);
//var myData = [header.innerHTML, row.innerHTML]
const myData = table
console.log(myData.innerHTML)
localStorage.setItem( 'tabla', myData.innerHTML );document.getElementById('tabla').innerHTML = myData.innerHTML;

}


