//

function init()
{
  calcular_calculo();
  calcular_presupuesto();
}

function calcular_presupuesto()
{
  // Obtener valores iniciales
  var alim     = parseFloat(jQuery("#alim").val());
  var vivienda = parseFloat(jQuery("#vivienda").val());
  var servs    = parseFloat(jQuery("#servs").val());
  var educ     = parseFloat(jQuery("#educ").val());
  var trans    = parseFloat(jQuery("#trans").val());
  var prestamo = parseFloat(jQuery("#prestamo").val());
  var otros1   = parseFloat(jQuery("#otros1").val());
  var ahorro   = parseFloat(jQuery("#ahorro2").val());
  var salario  = parseFloat(jQuery("#salario").val());
  var otros2   = parseFloat(jQuery("#otros2").val());


  // Sumar totales
  var gastos   = alim + vivienda + servs + educ + trans + prestamo + otros1 + ahorro;
  var entradas = salario + otros2;

  var perc       = 0.30;
  var disponible = entradas - gastos;
  var sal_perc   = perc * salario;
  var advert     = '<p>ENDEUDAMIENTO M&Aacute;XIMO (30% de su total de ingresos):      &cent;'+sal_perc+'</p>';
  var perc2		 = 0.10;
  var ahosure	 = perc2 * disponible

  if (sal_perc < prestamo)
  {
	advert += '<img src="imagenes/cuidado.gif" />';
	//advert += '<p>ADVERTENCIA: blablablabla!</p>';
  }
  else
  {
	advert += '<img src="imagenes/felicidades.gif" />';
	//advert += '<p>Felicidades!</p>';
  }

  var disp_tag   = (0.0001 <= disponible) ? ' superavit' : ' deficit';
  var disp_title = (0.0001 <= disponible) ? ' Superavit' : ' Deficit';
  
  var res1 = '<div id="entradas"> &cent;'+(entradas.toFixed(2))+'</div>';
  var res2 = '<div id="gastos"> &cent;'+(gastos.toFixed(2))+'</div>';
  var res3 = '<div id="disp" class="'+disp_tag+'">'+disp_title
           +': &cent;'+(disponible.toFixed(2))+'</div>';
  var res4 = '<div id="ahosure" class="'+disp_tag+'"> &cent;'+(ahosure.toFixed(2))+'</div>';

  jQuery("#entradas_res").html(res1);
  jQuery("#gastos_res").html(res2);
  jQuery("#presu_res").html(res3);
  jQuery("#advertencia").html(advert);
  jQuery("#ahosure_res").html(res4);
}

function calcular_calculo()
{
  // Obtener valores iniciales
  var tasa     = parseFloat(jQuery("#tasa-anual").val());
  var capi     = parseInt(jQuery("#capitalizacion").val());
  var ahorro   = parseFloat(jQuery("#ahorro").val());
  var agnosmax = parseInt(jQuery("#agnos").val());

  // validaciones:
  if (0 >= capi)
  {
    err = "<b>Error: La capitalizaci&oacute;n no puede ser menor"
        + " o igual a cero.</b>";
    jQuery("#acumulado").html(err);
    return;
  }

  var aporte = ahorro * capi;

  jQuery("#aporte").html('El aporte es de &cent;'+ (aporte.toFixed(2)) + '' );

  // Calculamos Acumulado por mes
  i = (tasa / 100) / parseFloat(capi);

  var tabla = '<table><thead><tr>'
  			+ '<th>Mes</th>'
  			+ '<th>Aporte Mensual</th>'
  			+ '<th>Aporte Acumulado</th>'
  			+ '<th>Intereses Acumulados</th>'
  			+ '<th>Total Acumulado</th>'
  			+ '</tr><thead><tbody>';

  var acumulado = 0.0;
  var interes   = 0;
  for (var n = 1; n <= capi; n++) // Por cada mes
  {
	vfmes = vf(i, n, ahorro);

    acumulado +=ahorro;
    interes   = vfmes - acumulado;

    tabla += '<tr>'
              +  '<td>'+n+'</td>'								// Numero de mes
              +  '<td>&cent;'+(ahorro.toFixed(2))+'</td>'		// Aporte p/mes
              +  '<td>&cent;'+(acumulado.toFixed(2))+'</td>'	// Aporte acumulado
              +  '<td>&cent;'+(interes.toFixed(2))+'</td>'		// Interes acumulado
              +  '<td>&cent;'+(vfmes.toFixed(2))+'</td>'		// Total Acumulado
              +  '</tr>';
  }
  tabla += '</tbody></table><br />';

  var pagno = '<table><thead><tr>'
  			+ '<th>A&ntilde;o</th>'
  			+ '<th>Acumulado por a&ntilde;o</th>'
  			+ '<th>Aporte Acumulado</th>'
  			+ '<th>Intereses Acumulados</th>'
  			+ '</tr><thead><tbody>';
  var acumulado = 0.0;
  var interes   = 0;
  for (var agno = 1; agno <= agnosmax; agno++) // Por cada aņo
  {
	// i ya lo tenemos
	var nper         = agno * capi;
	var vfagno       = vf(i,nper,ahorro);
	var aporte_acum  = ahorro * capi * agno;
	var interes_acum = vfagno - aporte_acum;

    pagno += '<tr>'
              +  '<td>'+agno+'</td>'							// Numero de aņo
              +  '<td>&cent;'+(vfagno.toFixed(2))+'</td>'		// Acumulado por aņo
              +  '<td>&cent;'+(aporte_acum.toFixed(2))+'</td>'	// Aporte acumulado
              +  '<td>&cent;'+(interes_acum.toFixed(2))+'</td>'	// Interes acumulado
              +  '</tr>';
  }
  pagno += '</tbody></table>';

  jQuery("#acumulado").html(tabla);
  jQuery("#poragnos").html(pagno);

  // Calculamos Intereses
  var intereses = vfmes - aporte;
  jQuery("#intereses").html('Los intereses son de &cent;'+(intereses.toFixed(2)) );
}

/*
** Calcula el VF (una funcion financiera)
** @param i		Tasa
** @param n		NPER
** @param c		Pago
*/
function vf(i, n, c)
{
  var tmp = 1 + i;
  tmp = Math.pow(tmp, n);
  tmp = tmp - 1;
  tmp = tmp / i;
  tmp = tmp * c;
  return tmp;
}


