$(document).ready(function()
{
    $('#depositor').change(function()
    {
        $.get(createLink('trade', 'ajaxGetCurrency', 'depositorID=' + $(this).val()), function(currency)
        {
            if(!currency) return false;

            var exchangeRate = currency == v.mainCurrency ? 1 : '';
            $('#currency').val(currency);
            $('#currencyLabel').val(currency);
            $('.exchangeRate').val(exchangeRate).toggle(currency != v.mainCurrency);
        });
    });

    $('#order, #contract').change(function()
    {
        $('#money').val($(this).find('option:selected').attr('data-amount'));
        $('#customer').val($(this).find('option:selected').attr('data-customer'));
        $('#customer').trigger('chosen:updated');
    })

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

    $('#product').change(function()
    {
        if(v.modeType != 'in') return false;

        var product = $(this).val();
        if(product == '0') return false;
        $.get(createLink('product', 'ajaxGetSubject', 'product=' + product), function(category)
        {
            if(category && category != '0') $('#category').val(category).trigger('chosen:updated');
        });
    });

    $('.exchangeRate').hide();
    $('[name*=createTrader]').change();
    $('#traderName').blur();
})

function ajaxGetCategories()
{
    $.ajax(
    {
        type: "GET",   
        url: createLink('trade', 'ajaxGetCategories', "type=" + v.modeType),
        success : function(response)
        {
            $('#category').remove();
            $('#category_chosen').remove();

            $('.categoryBox').prepend(response);
            $('#category').chosen();
        }
    });
}
