$(document).ready(function() {
    
    $.validator.addMethod("already_exist_devotee_name", function(value, element) {
        var return_val = true;
        var devotee_name = $('input[name="devotee_name"]').val();
        var id = $('input[name="hidden_id"]').val();
        $.ajax({
            async: false,
            url: base_url + 'devotee/devotee_name_exists',
            type: 'POST',
            data: {
                id: id,
                name: devotee_name
            },
            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="devotee_form"]').validate({
        errorElement: "li",
        errorLabelContainer:'.error_container',
        validClass: 'valid',
        focusInvalid: true,
        rules: {
            devotee_name: {
                required: true,
                already_exist_devotee_name: true
            }
        },
        messages: {
            devotee_name: {
                required: 'Please enter Devotee Name!',
                already_exist_devotee_name: 'Devotee Name already Exists!'
            }
        }
      });
});

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