$(document).ready(function()
{
    $("div.renten_tool").rentenRechner();
});



(function($) {
    var B4,B5,B6,B9,B10,B12,B15,B16,B17,B18,B20,B22,B33,B34,B36,B37,B38,B40,B41;
    var B9B10;
    $.fn.rentenRechner = function()
    {
        var div = $(this);
        $("input",div).keyup(onKeyUp);
        onKeyUp();
        $("input.percent",div).mask("9,9");
        $("input.age",div).mask("99");
        $("input.money",div).mask("9999");
        $("input.money6",div).keydown(intKeyOnly);
    }

    $.fn.intVal = function()
    {
        var value = parseInt($(this).val());
        if (isNaN(value))
            return "-";
        else
            return value;
    }

    $.fn.floatVal = function()
    {
        var value = parseFloat($(this).val().replace(/\,/,"."));
        if (isNaN(value))
            return "-";
        else
            return value;
    }

    $.fn.intText = function(value,decimals)
    {
        if (decimals == undefined) decimals = 0;
        if (isNaN(value)) 
            $(this).text("-");
        else 
        {
            value = $.format("{0:1."+decimals+"f}",value);
            value = addCommas(value.replace(/\./,","));
            $(this).text(value);
        }
        //alert(".");
    }

    function onKeyUp(e)
    {
        B12 = $("#b12").intVal();
        setB15();
        setB16();
        setB17();
        setB18();
        setB20();
        setB22();
        setB33();
    }

    function intKeyOnly(e)
    {
        var code = e.keyCode;
        if (code == 8 || code == 46 || (code>36 && code<41) || (code>95 && code<106) || (code>46 && code<58))
            return true;
        return false;
    }

    
    function setText(id,decimals,asText)
    {
        var txt = eval(id);
        var label = $("#"+ id.toLowerCase())
        if (!asText)
            label.intText(txt,decimals);
        else
            label.text(txt);
        label.hide().fadeIn("slow");
        
    }

    function addCommas(nStr)
    {
        nStr += '';
        x = nStr.split('.');
        x1 = x[0];
        x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + '.' + '$2');
        }
        return x1 + x2;
    }


    // Lebensstandard in € pro Jahr heute
    function setB15()
    {
        B15 = B12*12;
        setText("B15");
        //$("#b15").intText(B15);
    }

    // Jahre bis zum Renteneintritt
    function setB16()
    {
        B4  = $("#b4").intVal();
        B5  = $("#b5").intVal();
        B16 = B5 - B4;
        setText("B16");
    }

    // Realer Geldentwertungsfaktor bis zum Renteneintritt
    function setB17()
    {
        B10   = $("#b10").floatVal();
        value = B10 / 100.0 + 1.0;
        B17   = Math.pow(value,B16);
        setText("B17",2);
    }

    // "nominaler Lebensstandard in € pro Jahr bei Renteneintritt"
    function setB18()
    {
        B18   = B15 * B17;
        setText("B18");
    }

    // notwendiger Kapitalstock (nachschüssig) bei Renteneintritt mit Kapitalverzehr
    function setB20()
    {
        //=WENN(B9-B10=0;B18*B6;(B18*(POTENZ((1+B9-B10);B6)-1)/(POTENZ((1+B9-B10);B6)*((1+B9-B10)-1)))*(1+B9-B10))
        B6 = $("#b6").intVal();
        B9 = $("#b9").floatVal();
        B9B10 = 1.00 + (B9 - B10)/100.0;
        if (B9 - B10 == 0) 
            B20 = B18 * B6;
        else 
        {
            //x = (B18 * (POTENZ  (value; B6) - 1)/(POTENZ  (value;B6)  * (value - 1))) * value
            B20 = (B18 * (Math.pow(B9B10, B6) - 1)/(Math.pow(B9B10, B6) * (B9B10 - 1))) * B9B10;
        }
        setText("B20");
        //$("#b20").text(value);
    }

    // notwendiger Kapitalstock bei Renteneintritt ohne Kapitalverzehr
    function setB22()
    {
        //=WENN(B9-B10=0;"unendlich";WENN(B9-B10<0;"Berechnung nicht möglich";B18/(B9-B10)))
        if (B9-B10 == 0)
            B22 = "unendlich";
        else if (B9-B10 < 0)
            B22 = "Berechnung nicht möglich";
        else
            B22 = Math.round(B18/(B9-B10)*100);
        setText("B22");
    }
    
    // monatliche Berechnung
    function setB33()
    {
        //B33=POTENZ(1+(B9-B10);B16)*B30
        //B34=B20-B33
        //B36=B9-B10
        //B37=(POTENZ((1+(B9-B10));B16)-1)
        //B38=1/B37
        //B40=B34*B36*B38
        //B41=B40/12
        B30 = $("#b30").intVal();
        B33 = Math.pow(B9B10,B16)*B30;
        B34 = B20-B33;
        B36 = B9B10 - 1;
        B37 = Math.pow(B9B10,B16)-1;
        B38 = 1/B37;
        B40 = B34*B36*B38;
        B41 = B40/12
        
        setText("B33");
        setText("B34");
        setText("B40");
        setText("B41");
        
    }


})(jQuery);
