$(document).ready(function() {
    $.validator.addMethod("already_exist_category_name", function(value, element) {
        var return_val = true;
        var category_name = $('input[name="category_name"]').val();
        var id = $('input[name="hidden_id"]').val();
        $.ajax({
            async: false,
            url: base_url + 'inventory_category_master/category_name_exists',
            type: 'POST',
            data: {
                category_name: category_name,
                category_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="inventory_category_form"]').validate({
        errorElement: "li",
        errorLabelContainer:'.error_container',
        validClass: 'valid',
        focusInvalid: true,
        rules: {
            code: {
                required: true
            },
            category_name: {
                required: true,
                already_exist_category_name: true
            },
			status: {
                required: true
            }
        },
        messages: {
            code: {
                required: 'Please enter Category Code!'
            },
            category_name: {
                required: 'Please enter Category Name!',
                already_exist_category_name: 'Category Name already Exists!'
            },
			status: {
                required: 'Please select Status!'
            }
        }
      });
});

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