$(document).ready(function()
{
    $('.main-actions a.psiBtn, .panel-history a').click(function()
    {
        var href = $(this).prop('href');
        var app  = '';
        if(href.indexOf('/cash/') != -1) app = 'cash';
        if(href.indexOf('/crm/')  != -1) app = 'crm';
        if(href.indexOf('/psi/')  != -1) app = 'psi';

        if(app != '' && $(this).data('toggle') == undefined)
        {
            $.openEntry(app, href);
            return false;
        }
    });

    /* Add a product of quotation. */
    $(document).on('click', '.plus', function()
    {   
        var tr = $(this).parents('tr');
        tr.after($('#quotationProductTpl').html().replace(/key/g, v.key));
        v.key++;
        return false;
    }); 

    /* Remove a trade detail item. */
    $(document).on('click', '.minus', function()
    {
        if($('tr.quotationProduct').size() > 1)
        {
            $(this).parents('tr').remove();
        }
        else
        {
            $(this).parents('tr').find('input,select').val('');
        }
        return false;
    });

    /* Add a row of product. */
    $(document).on('click', '.btn-plus', function()
    {
        $(this).parents('tr').after('<tr>' + $(this).parents('tr').html() + '</tr>');
        var rowspan = $('#product_title').attr('rowspan');
        $('#product_title').attr('rowspan', parseInt(rowspan) + 1);
        /* Set properties of new row elements about product. */
        var newRow = $(this).parents('tr').next('tr');
        newRow.find('th').remove();
        newRow.find('input').val('');
        newRow.find('select').val('');
        newRow.find('.product-newProduct').hide();
        newRow.find('.chosen-container').remove();
        newRow.find('.chosen').chosen();
        newRow.find('.product-batchEdit').each(function()
        {
            var inputName = $(this).attr('name');
            var end = inputName.indexOf('[');
            if(end >= 0)
            {
                var preName = inputName.substring(0, end);
                $(this).attr('name', preName + '[' + v.key + ']');
                $(this).attr('id', preName + v.key);
            }
        })
        v.key ++;
    });
    
    /* Remove a row of product. */
    $(document).on('click', '.btn-remove', function()
    {
        if($('.btn-remove').length == 1)
        {
            $(this).parents('tr').find('input').not('input[type=hidden]').val('');
            return false;
        }
        var rowspan = $('#product_title').attr('rowspan');
        var title   = $('#product_title').html();
        $(this).parents('tr').remove();

        /* Update the order money. */
        var controls = $('.btn-remove');
        $(controls[0]).parents('tr').find('.product-money').change();

        /* Juage if the product_title was removed. */
        if($('#product_title').length > 0)
        {
            $('#product_title').attr('rowspan', parseInt(rowspan) - 1);
        }
        else
        {
            $(controls[0]).parents('tr').find('td:first').before("<th id='product_title' rowspan='" + (rowspan - 1) + "'>" + title + "</th>")       
        }
    });
})
