function checkDNI(dni) {
    var number, let, letter;
    var regex = /^[XYZ]?\d{5,8}[A-Z]$/;

    dni = dni.toUpperCase();

    if(regex.test(dni) === true){
        number = dni.substr(0,dni.length-1);
        number = number.replace('X', 0);
        number = number.replace('Y', 1);
        number = number.replace('Z', 2);
        let = dni.substr(dni.length-1, 1);
        number = number % 23;
        letter = 'TRWAGMYFPDXBNJZSQVHLCKET';
        letter = letter.substring(number, number+1);
        if (letter != let) {
            return false;
        }else{
            return true;
        }
        return false;
    }
}

function validateString(input){
    if(input == ""){
        return false;
    }
    return true;
}


function validatePatient(){
    var name = document.forms["create_form"]["nombre"];
    var last_name = document.forms["create_form"]["apellido"];
    var dni = document.forms["create_form"]["documento_identificativo"];
    var address = document.forms["create_form"]["direccion"];
    var country = document.forms["create_form"]["pais"];

    if(!checkDNI(dni.value)){
        alert("El DNI/NIE no es correcto");
        return false;
    }

    if(!validateString(name.value)){
        alert("Introduce el nombre");
        return false;
    }

    if(!validateString(last_name.value)){
        alert("Introduce el apellido");
        return false;
    }

    if(!validateString(address.value)){
        alert("Introduce la dirección");
        return false;
    }

    if(!validateString(country.value)){
        alert("Introduce el país");
        return false;
    }
    
    return true;
}
