$(function()
{
    var $table    = $('#receivableTable');
    var $origin   = $table.find('#origin');
    var $contract = $table.find('#contract');
    var $order    = $table.find('#order');
    var $batch    = $table.find('#batch');
    var $deserved = $table.find('#deserved');
    var $actual   = $table.find('#actual');
    var $balance  = $table.find('#balance');

    $origin.change(function()
    {
        var origin      = $(this).val();
        var $contractTR = $contract.parents('tr');
        var $orderTR    = $order.parents('tr');
        var $batchTR    = $batch.parents('tr');
        var $required   = $table.find('#desc').parent().find('.required-wrapper');

        if(origin == 'other')
        {
            $contractTR.hide();
            $orderTR.hide();
            $batchTR.hide();
            $required.show();
        }
        else if(origin == 'early')
        {
            $contractTR.hide();
            $orderTR.hide();
            $batchTR.hide();
            $required.hide();
        }
        else if(origin == 'contract')
        {
            $contractTR.show();
            $orderTR.hide();
            $batchTR.hide();
            $required.hide();

            loadContracts();
        }
        else if(origin == 'sale' || origin == 'purchase')
        {
            $contractTR.hide();
            $orderTR.show();
            $batchTR.show();
            $required.hide();

            loadOrders();
            loadBatches();
        }
    });

    $contract.change(function()
    {
        console.log('contract change');
        if($(this).val() != 0) $deserved.val($(this).find('option:selected').data('money'));
    });

    $order.change(function()
    {
        if($(this).val() != 0)
        {
            $deserved.val($(this).find('option:selected').data('money'));

            loadBatches();
        }
    });

    $batch.change(function()
    {
        if($(this).val() != 0) $deserved.val($(this).find('option:selected').data('money'));
    });

    $deserved.change(function()
    {
        var deserved = $.isNumeric($deserved.val()) ? parseFloat($deserved.val()) : 0;
        var actual   = $.isNumeric($actual.val())   ? parseFloat($actual.val())   : 0;

        $balance.val(Math.round((deserved - actual) * 100) / 100);
    });

    $actual.change(function()
    {
        console.log('actual change');
        var deserved = $.isNumeric($deserved.val()) ? parseFloat($deserved.val()) : 0;
        var actual   = $.isNumeric($actual.val())   ? parseFloat($actual.val())   : 0;

        $balance.val(Math.round((deserved - actual) * 100) / 100);
    });
})

var $contract = $('#receivableTable #contract');
var $order    = $('#receivableTable #order');
var $batch    = $('#receivableTable #batch');
var $origin   = $('#receivableTable #origin');
var $trader   = $('#receivableTable #trader');

function loadContracts()
{
    var contract = $contract.val();
    var trader   = $trader.val();
    var url      = createLink('receivable', 'ajaxGetContracts', 'trader=' + trader);
    $contract.load(url, function()
    {
        if(contract) $contract.val(contract);
        $contract.trigger('chosen:updated');
    });
}

function loadOrders()
{
    var order  = $order.val();
    var origin = $origin.val();
    var trader = $trader.val();
    var url    = createLink('receivable', 'ajaxGetOrders', 'type=' + origin + '&trader=' + trader);
    $order.load(url, function()
    {
        if(order) $order.val(order);
        $order.trigger('chosen:updated');
    });
}

function loadBatches()
{
    var batch  = $batch.val();
    var origin = $origin.val();
    var trader = $trader.val();
    var type   = origin == 'sale' ? 'out' : 'in';
    var url    = createLink('receivable', 'ajaxGetBatches', 'type=' + type + '&trader=' + trader);
    $batch.load(url, function()
    {
        if(batch) $batch.val(batch);
        $batch.trigger('chosen:updated');
    });
}
