$(document).ready(function()
{
    $('#menu .nav li').removeClass('active').find('a[href*=' + v.type + ']').parent().addClass('active');    

    $('a.reset').click(function()
    {
        if(confirm(v.confirmReset))
        {
            window.location = $(this).attr('href');
        }
        return false;
    });

    /* Add a trade detail item. */
    $(document).on('click', '.addItem', function()
    {
        if($(this).parents('.saleCommission').length) $(this).parents('.itemDiv').after(v.fixedItemRow.replace(/key/g, v.key));
        if($(this).parents('.multistepCommission').length) $(this).parents('.itemDiv').after(v.multistepItemRow.replace(/key/g, v.key));
        if($(this).parents('.fixedMultistepCommission').length) $(this).parents('.itemDiv').after(v.fixedMultistepItemRow.replace(/key/g, v.key));
        $(this).parents('.itemDiv').next().find('select').chosen();
        $(this).parents('.itemDiv').next().fixInputGroup();
        v.key ++;
    });

    $(document).on('click', '.delItem', function()
    {
        if($(this).parents('.panel-body').find('.delItem').size() > 1)
        {
            $(this).parents('.itemDiv').remove();
        }
        
        $(this).parents('.itemDiv').find('[name^=contribution], [name^=rate], [name^=amount]').val(0);

        $('[name^=contribution]').change();

        computeRate();
        computeAmount();
    });

    $('.editItem').click(function()
    {
        $(this).parents('.itemDiv').find('.saleCommission').removeAttr('readonly');
    });

    $(document).on('change', '#commission', function()
    {
        $('[name^=rate]').change();

        computeLineCommission();
    });

    $(document).on('change', '[name^=contribution]', function()
    {
        var contribution = 0;
        $(this).parents('.panel-body').find('[name^=contribution]').each(function()
        {
            if($.isNumeric($(this).val())) contribution += parseFloat($(this).val());
        });

        contribution = Math.round(contribution * 100) / 100;
        $(this).parents('.panel').find('.panel-heading span.contribution').html(contribution);
    });

    $(document).on('change', '[name^=rate]', function()
    {
        var rate       = $(this).val() == '' ? 0 : $(this).val();
        var commission = $('#commission').val();
        if(!$.isNumeric(rate) || !$.isNumeric(commission)) return;

        var amount = parseFloat(rate) * parseFloat(commission) / 100;
        amount = Math.round(amount * 100) / 100; 
        amount = amount == 0 ? '' : amount;
        $(this).parents('.itemDiv').find('[name^=amount]').val(amount);

        computeRate();
        computeAmount();
    });

    $(document).on('change', '[name^=amount]', function()
    {
        var amount     = $(this).val();
        var commission = $('#commission').val();
        if(!$.isNumeric(amount) || !$.isNumeric(commission)) return;

        var rate = parseFloat(amount) / parseFloat(commission) * 100;
        rate   = Math.round(rate * 100) / 100; 
        rate   = rate == 0 ? '' : rate;
        $(this).parents('.itemDiv').find('[name^=rate]').val(rate);
        
        computeRate();
        computeAmount();
    });

    $('[name^=rate], [name^=contribution]').change();

    computeLineCommission();
});

function computeRate()
{
    var rate = 0;
    $('div.col-md-3.saleCommission').find('[name^=rate]').each(function()
    {
        if($.isNumeric($(this).val())) rate += parseFloat($(this).val());
    });

    rate = Math.round(rate * 100) / 100; 
    $('div.col-md-3.saleCommission > .panel > .panel-heading span.rate').html(rate + '%');
}

function computeAmount()
{
    var amount = 0;
    $('div.col-md-3.saleCommission').find('[name^=amount]').each(function()
    {
        if($.isNumeric($(this).val())) amount += parseFloat($(this).val());
    });

    amount = Math.round(amount * 100) / 100; 
    $('div.col-md-3.saleCommission > .panel > .panel-heading span.amount').html(amount);
}

function computeLineCommission()
{
    var amount = 0;
    var rate   = 0;
    $('div.col-md-3.lineCommission').find('[name^=amount]').each(function()
    {
        if($.isNumeric($(this).val())) amount += parseFloat($(this).val());
    });

    $('div.col-md-3.lineCommission').find('[name^=rate]').each(function()
    {
        if($.isNumeric($(this).val())) rate += parseFloat($(this).val());
    });

    amount = Math.round(amount * 100) / 100; 
    rate   = Math.round(rate * 100) / 100; 
    $('div.col-md-3.lineCommission > .panel > .panel-heading span.rate').html(rate + '%');
    $('div.col-md-3.lineCommission > .panel > .panel-heading span.amount').html(amount);
}
