/* 
 * Author       : Jai K
 * Purpose      : Script for Expenses
 * Created On   : 2017-01-26 18:31 
 */

function validate(_this){
    
    if( autoValidate($(_this)) ) {
        if( $('[name="exp_type"]').val() == 'add' && $('[name="exp_type_add"]').val() == '') {
            notify('Please enter the New Type!');
            $('[name="exp_type_add"]').focus();
            return false;
        }else {
            if( $('[name="exp_sub"]').val() == 'add' && $('[name="exp_sub_add"]').val() == '') {
                notify('Please enter the New Subject!');
                $('[name="exp_sub_add"]').focus();
                return false;
            }else {
                // Ok
                return true;
            }
        }
    }
    
    return false;
}

$(document).on('ready', function(){
    
    $('[name="exp_type"]').on('change', function(){
        var value = $(this).val() || "";
        
        if($('[name="exp_type"] option:selected').text() == 'Banking Deposits') {
            $('.deposit_mode').show();
        } else {
            $('.deposit_mode').hide();
            $('.cheque_details').hide();
        }
        
        if( value == '' || value != 'add'  ) {
            $('#exp-type-add').slideUp('slow', function(){
                $(this).find('input').val('');
            });
        }else if( value == 'add' ) {
            $('#exp-type-add').slideDown('slow', function(){
                $(this).find('input').focus();
            });
        }

        if( value != '' ) {
            // Get Subjects
            var data = {
                'act'   : 'get_sub',
                'val'   : value == 'add' ? '' : value,  
            };
            $.post(baseURL + controller + '/ajax/', data, function(res){
                res = JSON.parse(res) || "";
                var html = '<option value="">-- Select Expense Subject --</option>';
                    html += '<option value="add" style="color: #f33;">-- Add New --</option>';

                if( res != '' ) {
                    var slug = $('[name="exp_sub"]').attr('data-slug') || "";
                    
                    Object.keys(res).map(function(k, v){
                        html += '<option value="'+ res[k]['exp_sub_slug'] +'" '+ ( slug == res[k]['exp_sub_slug'] ? 'selected  style="color: #1ba;"' : '' ) +'>'+ res[k]['exp_sub'] +'</option>';
                    });

                }

                $('[name="exp_sub"]').html(html);

            });
        }
        
    });
    
    $('[name="mode"]').on('change', function() {
        if($(this).val() != '' && $(this).val() == 2) {
            $('.cheque_details').show();
        } else {
            $('.cheque_details').hide();
        }
    });
    
    $('[name="exp_sub"]').on('change', function(){
        var value = $(this).val() || "";
        
        if( value == '' || value != 'add'  ) {
            $('#exp-sub-add').slideUp('slow', function(){
                $(this).find('input').val('');
            });
        }else if( value == 'add' ) {
            $('#exp-sub-add').slideDown('slow', function(){
                $(this).find('input').focus();
            });
        }
    });
    
    $('[name="exp_type"]').trigger('change');
    
})


