$(document).ready(function()
{
    oldAmount = $('#totalMoney').val();
    $.setAjaxForm('#quotationForm', function(response)
    {
        if(response.result == 'success')
        {   
            if(response.locate == '' || response.locate == 'reload')
            {   
                location.reload(); 
            }   
            else if(response.locate == 'parent.reload')
            {   
                parent.location.reload();
            }   
            else
            {   
                location = response.locate;
            }   
        }
        else
        {
            if(response.productChanged == true && $('#prompt').val() == '1')
            {
                 var updateProduct = confirm(response.tips);
                 if(updateProduct)
                 {
                     $('#prompt').val('0');
                     $('#quotationForm').submit();
                 }
            }

            if(response.error && response.error.length)
            {
                $('#duplicateError').html($('.errorMessage').html());
                $('#duplicateError .alert').prepend(response.error).show();

                $(document).on('click', '#duplicateError #continueSubmit', function()
                {
                    $('#duplicateError').append("<input value='1' name='continue' class='hide'>");
                    $('#submit').attr('type', 'button');
                })
            }
        }
    }, function()
    {
        if($('#prompt').val() == '1')
        {
            var currentAmount = parseFloat($('#totalMoney').val());
            currentAmount = currentAmount.toFixed(2);
            if(oldAmount != currentAmount)
            {
                var updateOrder = false;
                if(v.isOrderQuote == 'yes') var updateOrder = confirm(v.confirmUpdateOrder);

                var updateContract = confirm(v.confirmUpdateContract);

                var update = 1;
                if(updateOrder  && !updateContract) update = 2;
                if(!updateOrder && updateContract)  update = 3;
                if(updateOrder  && updateContract)  update = 4;

                $('#update').val(update);
            }
        }
    });

    /* Change product. */
    $(document).on('change', '.select-product', function()
    {
        var unit = $(this).find('option:selected').attr('data-unit');
        if(unit == 0) return false;
        
        $(this).parents('tr').find('[name*=unit]').val(unit);
    });
    
    PRECISION = 100;

    /* Change product amount and compute product money. */
    $(document).on('change', '.product-amount', function()
    {
        var price  = $(this).parents('tr').find('[name*=price]').val();
        var amount = $(this).val();
        if(price == '' || amount == '') return false;

        var money = Math.round(price * amount * PRECISION) / PRECISION;

        $(this).parents('tr').find('[name*=money]').val(money);
        $(this).parents('tr').find('[name*=money]').change();
    });
    
    /* Change product price and compute product money. */
    $(document).on('change', '.product-price', function()
    {
        var amount = $(this).parents('tr').find('[name*=amount]').val();
        var price  = $(this).val();
        if(price == '' || amount == '') return false;

        var money = Math.round(price * amount * PRECISION) / PRECISION;

        $(this).parents('tr').find('[name*=money]').val(money);
        $(this).parents('tr').find('[name*=money]').change();
    });
    
    /* Compute order money. */
    $(document).on('change', '.product-money', function()
    {
        var total     = 0;
        var moneyList = $('.product-money');
        for(i = 0; i < moneyList.size(); i++)
        {
            if($.isNumeric(moneyList[i].value))
            {
                total += parseFloat(moneyList[i].value);
            }
        }

        total = Math.round(total * PRECISION ) / PRECISION;

        $('#totalMoney').val(total);
    });
});
