$(document).ready(function() {
    
    $.validator.addMethod("already_exist_prasadam_name", function(value, element) {
        var return_val = true;
        var prasadam_name = $('input[name="prasadam_name"]').val();
        var id = $('input[name="hidden_madappalli_id"]').val();
        $.ajax({
            async: false,
            url: base_url + 'madappalli/prasadam_name_exists',
            type: 'POST',
            data: {
                id: id,
                name: prasadam_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="madappalli_form"]').validate({
        errorElement: "li",
        errorLabelContainer:'.error_container',
        validClass: 'valid',
        focusInvalid: true,
        rules: {
            prasadam_name: {
                required: true,
                already_exist_prasadam_name: true
            },
            unit: {
                required: true
            },
            price: {
                required: true
            }
        },
        messages: {
            prasadam_name: {
                required: 'Please enter Prasadam Name!',
                already_exist_prasadam_name: 'Prasadam Name already exists!'
            },
            unit: {
                required: 'Please select Unit!'
            },
            price: {
                required: 'Please enter Price!'
            }
        }
      });
});

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