function createLoginForm() {
const formId = 'loginForm';
closeForm( formId );
const body = document.body;
const container = document.createElement('div');
container.id = 'loginForm';
container.style = `
position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
background: white; padding: 25px; border: 1px solid #ccc;
box-shadow: 0 4px 8px rgba(0,0,0,0.4); z-index: 1010;
text-align: left; max-width: 520px; min-width: 320px;
`;
const form = document.createElement('form');
form.method = 'post';
form.action = 'https://io.trebeste.no/form';
form.setAttribute('data-hs-ignore', 'true');
const storedAddress = getCookie('feedbackPassword');
const storedSrcV = getCookie('_tbv_Src');
const storedFbclidV = getCookie('_tbv_fbclid');
const storedGclidV = getCookie('_tbv_gclid');
const storedSrcT = getCookie('_tbt_Src');
const storedFbclidT = getCookie('_tbt_fbclid');
const storedGclidT = getCookie('_tbt_gclid');
form.innerHTML = `
Innlogging
`;
const sendBtn = document.createElement('button');
sendBtn.type = 'submit';
sendBtn.innerHTML = 'Logg inn';
sendBtn.style = 'padding: 10px 15px; background: #2E94D2; color: #FFFFFF; border: none; border-radius: 3px; cursor: pointer;';
const closeBtn = document.createElement('button');
closeBtn.type = 'button';
closeBtn.textContent = 'Lukk';
closeBtn.style = 'padding: 10px 15px; margin-left: 10px; background: #6C757D; color: #FFFFFF; border: none; border-radius: 3px; cursor: pointer;';
closeBtn.addEventListener('click', function () {
closeForm(formId);
});
form.appendChild(sendBtn);
form.appendChild(closeBtn);
form.onsubmit = async function (e) {
e.preventDefault();
const formData = new FormData(form);
const email = formData.get('Email');
const password = formData.get('Password');
const stayLoggedin = document.getElementById('stay').checked;
try {
const response = await fetch(form.action, {
method: 'POST',
body: formData
});
if (response.ok) {
const data = await response.json();
if ( data.status == 'OK' ){
showResponseMessage('Takk! Vi har sendt din forespørsel til de tre beste.', 'success');
} else {
showResponseMessage('Feil e-postadresse eller passord.', 'error');
}
closeForm( formId );
} else {
showResponseMessage('Det oppsto en feil ved sending av skjema.', 'error');
}
} catch {
showResponseMessage('Nettverksfeil. Prøv igjen senere.', 'error');
}
};
container.appendChild(form);
body.appendChild(container);
body.style.overflow = 'hidden';
// Autofill poststed if zip is valid
setTimeout(() => {
const zipField = document.getElementById('zipInput');
const cityField = document.getElementById('cityInput');
function updateCity() {
const zip = zipField.value.trim();
if (/^\d{4}$/.test(zip)) {
fetch(`https://io.trebeste.no/zipcode2place?zip=${zip}`)
.then(res => {
if (!res.ok) throw new Error('Ikke funnet');
return res.json();
})
.then(data => {
const city = data?.place || '';
cityField.value = city;
cityField.setAttribute('value', city);
})
.catch(() => {
cityField.value = '';
});
} else {
cityField.value = '';
}
}
if (zipField && cityField) {
zipField.addEventListener('input', updateCity);
updateCity();
}
}, 10);
}