{"size": 4908, "ext": "js", "max_stars_repo_path": "public/js/validate_patients_register.js", "max_stars_repo_name": "0horaa/health-first", "max_stars_repo_stars_event_min_datetime": "2021-11-02T13:55:16.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-11T15:34:07.000Z", "max_issues_repo_path": "public/js/validate_patients_register.js", "max_issues_repo_name": "0horaa/health-first", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "public/js/validate_patients_register.js", "max_forks_repo_name": "0horaa/health-first", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 5, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 33.8482758621, "max_line_length": 115, "alphanum_fraction": 0.6442542787}
const form = document.forms["create-patients-form"];
const feedbackFromName = document.querySelector("#feedback-from-name");
const feedbackFromSocialName = document.querySelector("#feedback-from-social-name");
const feedbackFromAvatar = document.querySelector("#feedback-from-avatar");
const feedbackFromAge = document.querySelector("#feedback-from-age");
const feedbackFromCpf = document.querySelector("#feedback-from-cpf");

function handleWithGeneralChecks() {
    let hasEmptyInput = false;

    let inputsFromPatients = {
        name: form.name.value,
        socialName: form.social_name.value,
        avatar: form.avatar.value,
        age: form.age.value,
        cpf: form.cpf.value
    }

    if(someInputIsEmpty(inputsFromPatients.name) || inputNameIsInsufficient(inputsFromPatients.name)) {
        feedbackFromName.style.display = "block";
        feedbackFromName.innerHTML = "Digite o nome completo do paciente."
        feedbackFromName.setAttribute("class", "invalid-feedback");
        hasEmptyInput = true;
    } else {
        feedbackFromName.style.display = "block";
        feedbackFromName.innerHTML = "Tudo certo."
        feedbackFromName.setAttribute("class", "valid-feedback");
    }

    if(someInputIsEmpty(inputsFromPatients.socialName) || inputNameIsInsufficient(inputsFromPatients.socialName)) {
        feedbackFromSocialName.style.display = "block";
        feedbackFromSocialName.innerHTML = "Digite o nome social completo do paciente."
        feedbackFromSocialName.setAttribute("class", "invalid-feedback");
        hasEmptyInput = true;
    } else {
        feedbackFromSocialName.style.display = "block";
        feedbackFromSocialName.innerHTML = "Tudo certo."
        feedbackFromSocialName.setAttribute("class", "valid-feedback");
    }

    if(someInputIsEmpty(inputsFromPatients.avatar)) {
        feedbackFromAvatar.style.display = "block";
        feedbackFromAvatar.innerHTML = "Selecione uma foto para o paciente."
        feedbackFromAvatar.setAttribute("class", "invalid-feedback");
        hasEmptyInput = true;
    } else {
        feedbackFromAvatar.style.display = "block";
        feedbackFromAvatar.innerHTML = "Tudo certo."
        feedbackFromAvatar.setAttribute("class", "valid-feedback");
    }

    if(someInputIsEmpty(inputsFromPatients.age) || inputAgeIsInsufficient(inputsFromPatients.age)) {
        feedbackFromAge.style.display = "block";
        feedbackFromAge.innerHTML = "Informe a idade do paciente."
        feedbackFromAge.setAttribute("class", "invalid-feedback");
        hasEmptyInput = true;
    } else {
        feedbackFromAge.style.display = "block";
        feedbackFromAge.innerHTML = "Tudo certo."
        feedbackFromAge.setAttribute("class", "valid-feedback");
    }

    if(someInputIsEmpty(inputsFromPatients.cpf) || !checkIfCPFIsValid(inputsFromPatients.cpf)) {
        feedbackFromCpf.style.display = "block";
        feedbackFromCpf.innerHTML = "Informe o CPF do paciente corretamente."
        feedbackFromCpf.setAttribute("class", "invalid-feedback");
        hasEmptyInput = true;
    } else {
        feedbackFromCpf.style.display = "block";
        feedbackFromCpf.innerHTML = "Tudo certo."
        feedbackFromCpf.setAttribute("class", "valid-feedback");
    }

    if(hasEmptyInput) {
        return false;
    } else {
        return true;
    }
}


function someInputIsEmpty(input) {
    if(input.length == 0) {
        return true;
    }
}

function inputNameIsInsufficient(name) {
    if(name.length < 6) {
        return true;
    }
}

function inputAgeIsInsufficient(age) {
    if(String(age).length > 3) {
        return true;
    }
}

try{
    document.querySelector("#age").addEventListener("keypress", event => {
        if(!verifyChar(event)){
            event.preventDefault();
        }
    });
    function verifyChar(event){
        var char = String.fromCharCode(event.keyCode);
        var pattern = '[0-9]';
        if(char.match(pattern)){
            return true;
        }
    }
}catch(e){}

function addMaskToCpf(){
    var cpf = form.cpf;
    if(cpf.value.length === 3 || cpf.value.length === 7){
        cpf.value += ".";
    }else if(cpf.value.length === 11){
        cpf.value += "-";
    }
}

function checkIfCPFIsValid(strCPF) {
    strCPF = strCPF.replace(/[^0-9]/g, "");
    var Soma;
    var Resto;
    Soma = 0;
    if (strCPF == "00000000000") return false;

    for (i=1; i<=9; i++) Soma = Soma + parseInt(strCPF.substring(i-1, i)) * (11 - i);
    Resto = (Soma * 10) % 11;

    if ((Resto == 10) || (Resto == 11))  Resto = 0;
    if (Resto != parseInt(strCPF.substring(9, 10)) ) return false;

    Soma = 0;
    for (i = 1; i <= 10; i++) Soma = Soma + parseInt(strCPF.substring(i-1, i)) * (12 - i);
    Resto = (Soma * 10) % 11;

    if ((Resto == 10) || (Resto == 11))  Resto = 0;
    if (Resto != parseInt(strCPF.substring(10, 11) ) ) return false;
    return true;
}
