var inpputs = document.getElementsByTagName("input");
var texrareas = document.getElementsByTagName("textarea");
var firsnames = document.querySelectorAll('input[name="name"]');
var user_name = document.querySelectorAll('input[name="user_name"]');
var surnames = document.querySelectorAll('input[name="surname"]');
var meils = document.querySelectorAll('input[type="email"]');
var submits = document.querySelectorAll('button[type="submit"]')[1]; 
var prog = document.getElementById("progres"); 


var names = Array.from(firsnames).concat(Array.from(surnames));

for (let i = 0; i < inpputs.length; i++) {
    inpputs[i].addEventListener("focusin", function () { inpputs[i].addEventListener("keyup", function () { sprawdzonko(i) })});
    inpputs[i].addEventListener("focusout", function () { inpputs[i].removeEventListener("keyup", function () { sprawdzonko(i) })});
}

for (let i = 0; i < texrareas.length; i++){
    texrareas[i].addEventListener("focusin", function () {texrareas[i].addEventListener("keyup", function () {sprawdzonkotextare(i)})})
    texrareas[i].addEventListener("focusout", function () {texrareas[i].removeEventListener("keyup", function () {sprawdzonkotextare(i)})})
}

for (let i = 0; i < names.length; i++){
    names[i].addEventListener("focusin", function () {names[i].addEventListener("keyup", function () {sprawdzonkoname(i)})});
    names[i].addEventListener("focusin", function () {names[i].removeEventListener("keyup", function () {sprawdzonkoname(i)})});
}

for (let i = 0; i < meils.length; i++){
    meils[i].addEventListener("focusin", function () {meils[i].addEventListener("keyup", function () {sprawdzonkoemail(i)})});
    meils[i].addEventListener("focusin", function () {meils[i].removeEventListener("keyup", function () {sprawdzonkoemail(i)})});
}


function sprawdzonko(xd) {
    if (inpputs[xd].value == null || inpputs[xd].value == "") {
        inpputs[xd].classList.add("error");
    } else {
        inpputs[xd].classList.remove("error");
    }
}

function sprawdzonkoname(xd) {
    let nameRegex = /^[A-Za-z\s]{3,}$/;
    let spr = names[xd].value.trim();
    if (!nameRegex.test(spr)) {
        if (names[xd].classList.contains("progres_check")) {
            subpre(names[xd]);
        }
        names[xd].classList.add('error');
    } else {
        if (names[xd].classList.contains("progres_check")) {
            addpre(names[xd]);
        }
        names[xd].classList.remove('error');
    }
}

function sprawdzonkoemail(xd) {
    let emailRegex = /(?:[a-z0-9+!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/i;
    let spr = meils[xd].value.trim();
    if (!emailRegex.test(spr)) {
        subpre(meils[xd]);
        meils[xd].classList.add("error");
    } else {
        addpre(meils[xd]);
        meils[xd].classList.remove('error');
    }
}

function sprawdzonkotextare(xd) {
    if (texrareas[xd].value == null || texrareas[xd].value == "") {
        subpre(texrareas[xd]);
        texrareas[xd].classList.add("error");
    } else {
        addpre(texrareas[xd]);
        texrareas[xd].classList.remove("error");
    }
}
function addpre(nazwa) {
    if (!nazwa.classList.contains("pre")) {
            prog.value += 25;
        }
    nazwa.classList.add('pre');
    sprawdzankopre();
}

function subpre(nazwa) {
    if (nazwa.classList.contains("pre")) {
            prog.value -= 25;
        }
    nazwa.classList.remove('pre');
    sprawdzankopre();
}

function sprawdzankopre(){
    if (prog.value == 100) {
        submits.classList.remove("no_send");
    } else {
        submits.classList.add("no_send");
    }
}