$(document).ready(function(){
    /**
     * 
     * @returns 
     */
    $.fn.validations = function(){
        return [
            {
                "name":"noEmpty",
                "message":window.translations["create-user"].fieldRequired,
                "run":function (field){
                    if((field.key === "date") || (field.key === "user_id")) 
                        return true;
                    return ((field.value != undefined) && (field.value.trim() !== "")); 
                }
            },
            {
                "name":"noEquals",
                "message":window.translations["create-user"].passwordRepeatValid,
                "run":function (field){
                    if(field.key === "password"){
                        return (field.value === document.querySelector("[name='passwordRepeat']").value);
                    } 
                    return true; 
                }
            }
        ];
    }
    $("#User").on("submit",function(e){
        let form    = document.querySelector("#User");
        let data    = $.fn.getDataForm.call(this,form);
        let valid   = $.fn.runValidations(data,$.fn.validations());
        const selectedRole = document.querySelector('input[name="role"]:checked');
        // Si se selecciona ADM y aún no están en el DOM, los agregamos
        if((selectedRole && selectedRole.value.toUpperCase() === 'DOC' || selectedRole && selectedRole.value.toUpperCase() === 'ADM') && document.querySelector('input[name="gender"]:checked') == null) {
            alert("Seleccione un género");
            e.preventDefault();
        }
        if(!valid.valid)
            e.preventDefault();
    });
})