currency = {
    vip: false,
    
    get_course: function(dir, mode) {
        if (!mode || mode === undefined) mode = '';
        $('.error').hide();
        var from_val = $('select[name=from_currency]').val();
        var to_val = $('select[name=to_currency]').val();
        
        if (from_val > 0 && to_val > 0) {
            $.getJSON('/money/get_course/'+from_val+'/'+to_val+'/'+mode+'/' , function(data) {
                $('#from_cur').html(data.from);
                $('#to_cur').html(data.to).parent().show();
                $('#reserve').html(data.reserve).parent().show();
                $('#sys_comission').html(data.comission);
				currency.recalc(dir);
            });
        }
    },
        
    recalc: function(dir, mode) {
        if (!mode || mode === undefined) mode = '';

        $('#to_big_sum_error').hide();
        var from_val = $('input[name=from_sum]').val();
        var from_com_val = $('input[name=from_comission_sum]').val();
        var to_val = $('input[name=to_sum]').val().replace(',', '.').replace(' ', '');
		if (to_val.charAt(to_val.length-1) == '.') return true;
		
		var from = $('select[name=from_currency]').val();
		var to = $('select[name=to_currency]').val();
        
        var reserve = parseFloat($('#reserve').text().replace(',', '.').replace(' ', ''));
        to_val = parseFloat(to_val);
        if (to_val > reserve && !this.vip) {
            $('#to_big_sum_error').show();
        }
        
        if (from_val == 0 && to_val == 0) {
            $('input[name=from_sum]').val(0);
            $('input[name=from_comission_sum]').val(0);
            $('input[name=to_sum]').val(0);
        } else {
            if (dir == 'from') var val = from_val;
            else if (dir == 'to') var val = to_val;
            else var val = from_com_val;
            
            $.getJSON('/money/recalc/'+dir+'/'+val+'/'+from+'/'+to+'/'+mode+'/', function(data) {
                if (dir == 'from_comission') {
                    $('input[name=from_sum]').val(data.val_from);
                    $('input[name=to_sum]').val(data.val_to);
                } else {
                    $('input[name='+(dir=='from'?'to':'from')+'_sum]').val(data.val);
                    $('input[name=from_comission_sum]').val(data.val_c);
                }
            });
        }
    }
}

io_type = {
    check: function(t) {
        if (!t || t === undefined) t = 'phisic';
        var a_t = t == 'phisic'?'legal':'phisic';
        $('#'+t+'_data').show();
        $('#'+a_t+'_data').hide();
    },
    recalc: function(d) {
        var f = $('select[name=from]').val();
        var t = $('select[name=to]').val();
        var s = $('#'+d).val();
        $.getJSON('/money/recalc_io/'+d+'/'+s+'/'+f+'/'+t+'/', function(v) {
            if (d == 'send_comission') {
                $('#send').val(v.val_from);
                $('#get').val(v.val_to);
            } else {
                var ad = d == 'send'?'get':'send';
                $('#'+ad).val(v.val);
                $('#comission_send').val(v.val_c);
            }
        });
    }
}
