/*
 * Calculadora de Comprueba tu ahorro
 *
 * 2009/09/03
 * Cristian Acuña
 * cacuna@4sale.cl
 *
 * Depende de jQuery 1.3+
 * http://jquery.com
 *
 * Depende de NumberFormat154.js +
 *
 */

var _supHi = Array(5.19, 5.19, 5.19);
var _empHi = Array(_supHi[0]-(_supHi[0]*0.28), _supHi[1]-(_supHi[1]*0.4), _supHi[1]-(_supHi[1]*0.57));

var _supTo = Array(1.87, 1.87, 1.87);
var _empTo = Array(_supTo[0]-(_supTo[0]*0.3), _supTo[1]-(_supTo[1]*0.4), _supTo[1]-(_supTo[1]*0.36));

$().ready(function(){
    $("form.jqCalcularAhorro").submit(function(){
        var _inEmp = $("input.valorCalcular").val();
        if(isNaN(_inEmp) || _inEmp.length <1){
            alert("Debe ingresar un número válido.");
            $("input.valorCalcular").val("");
            return false;
        }
        var _totalIns = 0;
        var _totalSup = 0;

        var _resEmpHiIns = ((_inEmp<=50)? _empHi[0] * _inEmp:0) +
                            ((_inEmp>50 && _inEmp<=100)? _empHi[1] * _inEmp:0) +
                            ((_inEmp>100)? _empHi[2] * _inEmp:0);
        _totalIns+=_resEmpHiIns;
        _resEmpHiIns = new NumberFormat(_resEmpHiIns);
        _resEmpHiIns = _resEmpHiIns.toFormatted();

        var _resEmpHiSup = ((_inEmp<=50)? _supHi[0] * _inEmp:0) +
                            ((_inEmp>50 && _inEmp<=100)? _supHi[1] * _inEmp:0) +
                            ((_inEmp>100)? _supHi[2] * _inEmp:0);
        _totalSup+=_resEmpHiSup;
        _resEmpHiSup = new NumberFormat(_resEmpHiSup);
        _resEmpHiSup = _resEmpHiSup.toFormatted();

        var _resEmpToIns = ((_inEmp<=50)? _empTo[0] * _inEmp:0) +
                            ((_inEmp>50 && _inEmp<=100)? _empTo[1] * _inEmp:0) +
                            ((_inEmp>100)? _empTo[2] * _inEmp:0);
        _totalIns+=_resEmpToIns;
        _resEmpToIns = new NumberFormat(_resEmpToIns);
        _resEmpToIns = _resEmpToIns.toFormatted();

        var _resEmpToSup = ((_inEmp<=50)? _supTo[0] * _inEmp:0) +
                            ((_inEmp>50 && _inEmp<=100)? _supTo[1] * _inEmp:0) +
                            ((_inEmp>100)? _supTo[2] * _inEmp:0);
        _totalSup+=_resEmpToSup;
        _resEmpToSup = new NumberFormat(_resEmpToSup);
        _resEmpToSup = _resEmpToSup.toFormatted();

        var _diferencia = new NumberFormat((_totalSup - _totalIns));
        _diferencia.setPlaces(0);
        _diferencia = _diferencia.toFormatted();

        var _diferenciaP = new NumberFormat((1-(_totalIns/_totalSup))*100);
        _diferenciaP.setPlaces(0);
        _diferenciaP = _diferenciaP.toFormatted();

        _totalIns = new NumberFormat(_totalIns);
        _totalIns = _totalIns.toFormatted();

        _totalSup = new NumberFormat(_totalSup);
        _totalSup = _totalSup.toFormatted();

        $("#td_h_h_e").text(_resEmpHiIns);
        $("#td_h_h_r").text(_resEmpHiSup);
        $("#td_h_t_e").text(_resEmpToIns);
        $("#td_h_t_r").text(_resEmpToSup);
        $("#td_m_t_e3").text(_totalIns);
        $("#td_m_t_r3").text(_totalSup);
        $("#result_metros > span").text(_diferencia);
        $("#result_percent > span > b").text(_diferenciaP);

        return false;
    });
});
