$(document).ready(function() {
    
    $.validator.addMethod("already_exist_supplier_name", function(value, element) {
        var return_val = true;
        var name = $('input[name="name"]').val();
        var id = $('input[name="hidden_id"]').val();
        $.ajax({
            async: false,
            url: base_url + 'supplier/supplier_name_exists',
            type: 'POST',
            data: {
                name: 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="supplier_form"]').validate({
        errorElement: "li",
        errorLabelContainer:'.error_container',
        validClass: 'valid',
        focusInvalid: true,
        rules: {
            name: {
                required: true,
                already_exist_supplier_name: true
            },
            phone_no: {
                required: true
            },
            email: {
                //required: true,
                email: true
            }
        },
        messages: {
            name: {
                required: 'Please enter Supplier Name!',
                already_exist_supplier_name: 'Supplier Name already Exists!'
            },
            phone_no: {
                required: 'Please enter Contact No!'
            },
            email: {
                //required: 'Please enter Email!',
                email: 'Please enter Valid Email!'
            }
        }
      });
});

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