$(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');
    })

    $('.contractTD select, .purchasecontractID select').empty().load(createLink('crm.contract', 'ajaxGetOptionMenu', 'traderID=' + $('#trader').val()), function()
    {
        $('#order option').each(function()
        {
            if(v.order == $(this).val()) $(this).attr("selected", "selected");
        });
        $('#contract').val(v.contract);
        $('#purchaseContract').val(v.contract).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');
        });
    });
})
