$(document).ready(function() {
    $.validator.addMethod("already_exist_name", function(value, element) {
        var return_val = true;
        var name = $('.priest_name').val();
        var designation = $('select[name="designation"]').val();
        var id = $('input[name="hidden_id"]').val();
        
        $.ajax({
            async: false,
            url: base_url + 'priest/priest_name_exists',
            type: 'POST',
            data: {
                name: name,
                designation: designation,
                id: id
            },
            dataType: "json"
        })
        .done(function(data) {
            if(data == 1) {
                return_val = false;
                $('.priest_name').focus();
            }
        })
        .fail(function(jqXHR, status_code, error_thrown) {
            console.log(jqXHR.responseText);
        });
        
        return return_val;
    });
    
    
    $('form[name="priest_form"]').validate({
        errorElement: "li",
        errorLabelContainer:'.error_container',
        validClass: 'valid',
        focusInvalid: true,
        rules: {
            name: {
                required: true
            },
            designation: {
                required: true,
                already_exist_name: true
            }
        },
        messages: {
            name: {
                required: 'Please enter Name!'
            },
            designation: {
                required: 'Please select Designation!',
                already_exist_name: 'Name is already exist for this Designation! '
            }
        }
      });
});

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