async function postData(url = '', data = {}) {
document.getElementById("submitButton").setAttribute("disabled", "disabled");
const formData = new FormData();
formData.append('data', JSON.stringify(data));
// Default options are marked with *
const response = await fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Accept': 'application/json; charset=UTF-8',
'Content-Type': 'application/json; charset=UTF-8',
'Content-Length': JSON.stringify(data).length.toString(),
'Authorization': 'Bearer token_guest'
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
return response.json(); // parses JSON response into native JavaScript objects
}
function saveRegistration() {
var passType = document.querySelector('input[name="passType"]:checked').value
const classIds = Array.from(document.querySelectorAll('input[name="class"]:checked')).map((input)=>input.value);
const typeGroup = document.querySelector('input[name="type"]:checked');
let type = null;
if (typeGroup != null) type = typeGroup.value;
const promo = document.getElementById("promo");
const classes = [];
classIds.forEach((id) => {
classes.push({ id: id, person: passType < 2 ? [0] : [0, 1], code: promo != null ? promo.value : "" })
});
postData('/moja/api3/saveregistration.php', {
id: classIds,
passType: passType, //passType.options[passType.selectedIndex].value,
name: document.getElementById("name").value,
surname: document.getElementById("surname").value,
email: document.getElementById("email").value,
partnerName: document.getElementById("partnerName").value,
partnerSurname: document.getElementById("partnerSurname").value,
classes: classes,
lang: document.getElementById("lang").value,
type: type,
web: true
})
.then(msg => {
console.log(msg);
if (msg.status === 'ok') {
if (msg.redirectUrl != null) {
window.location.href = msg.redirectUrl;
fbq('track', 'CompleteRegistration', {content_name: "class" + classIds.join()});
return;
}
const name = document.getElementById("name").value;
if (msg.paymentType == 1) {
document.getElementById("popup").innerHTML = name + ", hvala za prijavo!
Na email naslov smo poslali podatke za plačilo. Če emaila ne najdete, prosim najprej preverite, če je slučajno zataval v mapo z nezaželeno pošto.";
} else {
document.getElementById("popup").innerHTML = name + ", hvala za prijavo na tečaj!
Na email smo poslali potrditev prijave. Če emaila ne najdete, prosim najprej preverite, če je slučajno zataval v mapo z neželeno pošto.";
}
document.getElementById("popupBackground").classList.remove("hide");
fbq('track', 'CompleteRegistration', {content_name: "class" + classIds.join()});
} else if (msg.status === 'fail' && msg.error != null) {
if (msg.error == "already registered") document.getElementById("popup").innerHTML = "Oseba s tem emailom je že prijavljena. Ponovna prijava ni potrebna.";
else if (msg.error == "invalid code") document.getElementById("popup").innerHTML = "Promocijska koda ni veljavna.";
else if (msg.error == "invalid request") document.getElementById("popup").innerHTML = "Vnešeni podatki niso veljavni.";
else if (msg.error == "leaders closed") document.getElementById("popup").innerHTML = "Prijave za plesalce so za ta tečaj trenutno zaprte.";
else if (msg.error == "followers closed") document.getElementById("popup").innerHTML = "Prijave za plesalke so zaradi povečanega števila plesalk za ta tečaj trenutno zaprte. Hvala za razumevanje.";
else if (msg.error == "couples closed") document.getElementById("popup").innerHTML = "Prijave za pare so za ta tečaj trenutno zaprte. Hvala za razumevanje.";
else document.getElementById("popup").innerHTML = "Ne moremo zabeležiti vaše prijave. Prosimo, da nas kontaktirate.";
document.getElementById("popupBackground").classList.remove("hide");
} else {
document.getElementById("popup").innerHTML = "Ne moremo zabeležiti vaše prijave. Prosimo, da nas kontaktirate.";
document.getElementById("popupBackground").classList.remove("hide");
}
document.getElementById("submitButton").removeAttribute("disabled");
})
.catch((error) => {
console.log(error);
document.getElementById("popup").innerHTML = "Težave pri povezovanju na strežnik.";
document.getElementById("popupBackground").classList.remove("hide");
document.getElementById("submitButton").removeAttribute("disabled");
});
}
function selectType() {
const typeGroup = document.querySelector('input[name="passType"]:checked');
let typeId = null;
if (typeGroup != null) typeId = typeGroup.value;
if (Number(typeId) >= 2) {
document.getElementById('coupleForm').classList.remove("hide");
} else {
document.getElementById('coupleForm').classList.add("hide");
}
}
function selectClass() {
if (document.getElementById('class').selectedIndex == 0) {
document.getElementById('passType').disabled = false;
} else {
document.getElementById('passType').value = '1';
document.getElementById('passType').disabled = true;
document.getElementById('coupleForm').classList.add("hide");
document.getElementById('partnerName').value='';
document.getElementById('partnerSurname').value='';
}
}
function closePopup() {
document.getElementById("popupBackground").classList.add("hide");
}
//document.getElementById("class").addEventListener("change", selectClass);
document.getElementById("submitButton").addEventListener("click", saveRegistration);
document.getElementById("popupBackground").addEventListener("click", closePopup);
document.getElementById("popupClose").addEventListener("click", closePopup);
var passTypeRadios = document.querySelectorAll('input[type=radio][name="passType"]');
passTypeRadios.forEach(radio => radio.addEventListener('change', selectType));