$(function ()
{
  setTimeout("init_budgetmutatie()", 100);
});

var kostensoort_btw = 0;

function init_budgetmutatie()
{
    $("#code").change( change_code );
    //$("#budgetfrom").change( change_budgetfrom );
    $("#budgetto").change( change_budgetto );
    $("#amountto").change( change_amountto );
    $("#vatto").change( change_vatto );
    $("#sequence").change( change_sequence );

    // model_bgt_budgetmutatie hook_pre_edit variabelen:
    var parent_key = budget_key;
    var init_niveau = "";
    if (parent_key == -1)
    {
        var v_budgetdiscipline   = typeof budgetdiscipline_key   == "undefined" ? -1 : budgetdiscipline_key;
        var v_budgetproject      = typeof budgetproject_key      == "undefined" ? -1 : budgetproject_key;
        var v_budgetcostcategory = typeof budgetcostcategory_key == "undefined" ? -1 : budgetcostcategory_key;

        var disc_key = (v_budgetdiscipline   == -1 ? ($("#budgetdiscipline").val()   == "" ? -1 : $("#budgetdiscipline").val() )   : v_budgetdiscipline);
        var proj_key = (v_budgetproject      == -1 ? ($("#budgetproject").val()      == "" ? -1 : $("#budgetproject").val()    )   : v_budgetproject);
        var rubr_key = (v_budgetcostcategory == -1 ? ($("#budgetcostcategory").val() == "" ? -1 : $("#budgetcostcategory").val() ) : v_budgetcostcategory);
        if (disc_key != -1) { init_niveau = "D"; parent_key = disc_key; }
        if (proj_key != -1) { init_niveau = "P"; parent_key = proj_key; }
        if (rubr_key != -1) { init_niveau = "R"; parent_key = rubr_key; }
    }

    change_code();
    change_sequence();
    if (mutatie_key == -1)
    {
        fill_budget_from("MF"+init_niveau, parent_key, parent_key);
        fill_budget_to("MT"+init_niveau, parent_key, parent_key);
    }
    else
    {
        $("#budgetfrom").prop("disabled", true);
        $("#budgetto").prop("disabled", true)
    }
}

function change_code()
{
    if (  ("AW").indexOf($("#code option:selected").val()) > -1)
    {
        $("#sequence").removeClass("required");
    }
    else
    {
        $("#sequence").addClass("required");
    }
}

function change_sequence()
{
    var default_volgnr = nextsequence; // Berekend in model_bgt_budgetmutatie op hook_pre_edit.
    var volgnr = $("#sequence").val();
    volgnr = (volgnr ? volgnr.replace(/\s+/g, "") : null);
    if (!volgnr)
    {
        $("#sequence").val(default_volgnr);
    }

}

function change_budgetto()
{
    var btw_perc = ( $("#budgetto option:selected").attr("perc_btw")
                   ? parseFloat($("#budgetto option:selected").attr("perc_btw").replace(/,/g,"."))
                   : null
                   );
    if ($("#amountto").val() && btw_perc != kostensoort_btw)
    {
        FcltMgr.confirm(L("bgt_confirm_recalc_vat"), function() { change_amountto(); } );
    }
}

function change_amountto()
{
    // bereken btwbedrag (vat) adhv btw-percentage bij kostensoort.
    var btw_incl;
    var btw_perc;
    var amountto = parseFloat($("#amountto").val().replace(/,/g,"."));

    if (mutatie_key == -1)
    {
        btw_incl = $("#budgetto option:selected").attr("incl_btw") == "1";
        btw_perc = ( $("#budgetto option:selected").attr("perc_btw")
                   ? parseFloat($("#budgetto option:selected").attr("perc_btw").replace(/,/g,"."))
                   : null
                   );
    }
    else
    {
        btw_incl = btw_data.btw_inc;
        btw_perc = parseFloat(btw_data.btw_val);
    }

    kostensoort_btw = btw_perc; // Huidige btw-percentage globaal opslaan.
    if (btw_perc && !isNaN(btw_perc))
    {
        var perc = (btw_perc / 100);
        var vat = amountto * perc;
        var incl = amountto + vat;

        $("#vatto").val(num2currEditable(vat));
        $("#vatto").closest("tr").next("tr").find("span").text(num2currEditable(incl));
    }
}

function change_vatto()
{
    // bereken totaalbedrag bij wijziging van het btw-bedrag.
    var amountto = parseFloat($("#amountto").val().replace(/,/g,"."));
    var vatto = parseFloat($("#vatto").val().replace(/,/g,"."));
    var incl = amountto + vatto;
    $("#vatto").closest("tr").next("tr").find("span").text(num2currEditable(incl));
}

function fill_budget_from(niveau, parent_key, init_key)
{
    $.getJSON( "../bgt/get_bgt_info_ajax.asp",
               { req_info: "budgetmutation",
                 niveau: niveau,
                 parent_key: parent_key,
                 init_key: init_key
               },
               process_info);
}

function fill_budget_to(niveau, parent_key, init_key)
{
    $.getJSON( "../bgt/get_bgt_info_ajax.asp",
               { req_info: "budgetmutation",
                 niveau: niveau,
                 parent_key: parent_key,
                 init_key: init_key
               },
               process_info);
}


function process_info(data)
{
    var select;
    switch(data.niveau)
    {
        case "MF": select = $("#budgetfrom"); break;
        case "MT": select = $("#budgetto"); break;
    }

    var sel_key = -1;
    $new_lov = $("<option>").val(-1).html("");
    select.empty().append($new_lov);
    if (data.lov.length > 0)
    {
        // Een bestaande mutatie kan het budget van/naar niet wijzigen.
        // Maar we moeten wel de btw weten.
        for (i=0; i < data.lov.length;i++)
        {
            var opt = data.lov[i];
            $new_lov = $("<option>").val(opt.key).html(opt.oms);
            if (opt.sel)
                $new_lov.attr("selected", "selected");

            if (opt.sel == opt.key)
                sel_key = opt.key;

            $new_lov.attr("incl_btw", opt.atr.btw_inc);
            $new_lov.attr("perc_btw", opt.atr.btw_val);

            select.append($new_lov);
        }
        select.prop("disabled", false);
        select.removeClass("btn_disabled");
    }
    else
    {
        select.prop("disabled", true);
        select.addClass("btn_disabled");
    }

    if (sel_key > -1)
    {
        select.val(sel_key);
    }
}
