$(document).ready(function() {
    integer_value();
    $("#img_hide").on('ifChanged', function(event){
        $(".toggle").slideToggle();
    });
    
    $.validator.addMethod("already_exist_type_name", function(value, element) {
        var return_val = true;
        var type_name = $('input[name="type"]').val();
        var id = $('input[name="hidden_id"]').val();
        $.ajax({
            async: false,
            url: base_url + 'temple_expenses_type/type_name_exists',
            type: 'POST',
            data: {
                name: type_name,
                id: id
            },
            dataType: "json"
        })
        .done(function(data) {
            if(data == 1) {
                return_val = false;
            }
        })
        .fail(function(jqXHR, status_code, error_thrown) {
            console.log(jqXHR.responseText);
        });
        
        return return_val;
    });
    
    
  
    $('form[name="expenses_type_form"]').validate({
        errorElement: "li",
        errorLabelContainer:'.error_container',
        validClass: 'valid',
        focusInvalid: true,
        rules: {
            type: {
                required: true,
                already_exist_type_name: true
            }
        },
        messages: {
            type: {
                required: 'Please enter Expenses Type!',
                already_exist_type_name: 'Expenses Type already exists!'
            }
        }
    });
	
	$('form[name="expenses_form"]').validate({
        errorElement: "li",
        errorLabelContainer:'.error_container',
        validClass: 'valid',
        focusInvalid: true,
        rules: {
            exp_type: {
                required: true
            },
			exp_for: {
                required: true
            },
			name: {
                required: true
            },
			func_date: {
                required: true
            },
			exp_amount: {
                required: true
            }
        },
        messages: {
            exp_type: {
                required: 'Please select Expenses Type!'
            },
			exp_for: {
                required: 'Please enter Expenses for!'
            },
			name: {
                required: 'Please enter Name!'
            },
			func_date: {
                required: 'Please enter Function Date!'
            },
			exp_amount: {
                required: 'Please enter Expenses Amount!'
            }
        }
    });
});

function goBack() {
    window.history.back();
}