$(document).ready(function()
{
    $('#amebaAccount').change(function()
    {
        var amebaAccount = $(this).val();
        $('#tradeDept').parents('tr').toggle(amebaAccount == 'internalIncome' || amebaAccount == 'internalDeal');
        $('#line').prev().toggle(amebaAccount == 'externalIncome' || amebaAccount == 'internalIncome');
        $('#product').prev().toggle(amebaAccount == 'externalIncome');
        $('#product').val('');
        $('#line').val('').trigger('chosen:updated').change();
        if(amebaAccount == 'externalIncome')
        {
            $('#desc').attr('placeholder', v.desc);
        }
        else
        {
            $('#desc').removeAttr('placeholder');
        }
    });

    $('#line').change(function()
    {
        var product = $('#product').val();
        $('#product').load(createLink('product', 'ajaxGetByCategory', 'status=normal&category=' + $(this).val()), function()
        {
            $('#product').val(product).trigger('chosen:updated');
        });
    });

    $('#year, #product').change(function()
    {
        if($('#amebaAccount').val() != 'externalIncome') return;

        var product = $('#product').val();
        var year    = $('#year').val();
        $.get(createLink('product', 'ajaxGetSubject', 'product=' + product + '&year=' + year + '&type=amebaCategory'), function(category)
        {
            $('#category').val(category).trigger('chosen:updated');
        });
    });

    $('#year, #dept, #amebaAccount, #line, #category').change(function()
    {
        var year         = $('#year').val();
        var dept         = $('#dept').val();
        var amebaAccount = $('#amebaAccount').val();
        var line         = $('#line').val();
        var category     = $('#category').val();

        $.get(createLink('budget', 'ajaxGetMoney', 'year=' + year + '&dept=' + dept + '&amebaAccount=' + amebaAccount + '&line=' + line + '&category=' + category), function(money)
        {
            $('#lastMoney').val(money);
        });
    });
    
    $('#amebaAccount').change();
});
