//document get ready function
    const username = getCookie('email');
    const loggedIn = getCookie('loggedIn');
    
$(document).ready(function () {
        const usernameHeader = document.getElementById("usernameHeader");
        const salutation = document.getElementById("salutation");
        const firstname = document.getElementById("firstname");
        const lastname = document.getElementById("lastname");
        const street = document.getElementById("street");
        const postalcode = document.getElementById("postalcode");
        const city = document.getElementById("city");
        const email = document.getElementById("email");
        const phone = document.getElementById("phone");
        let hashedPassword = '';
        //make json from username
        const sendData = {
            "username": username
        };
        stringData = JSON.stringify(sendData);
    if (loggedIn == 'true') {
        $.ajax({
            url: '../logic/getUserData.php',
            type: 'POST',
            cache: false,
            datatype: 'json',
            data: stringData,
            success: function (response) {
                const data = JSON.parse(response);
                //set data to fields
                usernameHeader.innerHTML = data.username;
                salutation.value = data.salutation;
                firstname.value = data.firstname;
                lastname.value = data.lastname;
                street.value = data.address;
                postalcode.value = data.plz;
                city.value = data.city;
                email.value = data.email;
                phone.value = data.phone;
                hashedPassword = data.password;
            }
        });
    } else {
        window.location.href = '../index.html';
    }
    const userForm = document.getElementById("userProfile");
    userForm.addEventListener('submit', function (e) {
        e.preventDefault();
        updateUser(hashedPassword);
    });
});

function updateUser(pwd){

    const sendData = {
        "username": username,
        "street": street.value,
        "postalcode": postalcode.value,
        "city": city.value,
        "phone": phone.value,
        "password": password.value,
        "hashedPassword": pwd
    };
    stringData = JSON.stringify(sendData);
    console.log(stringData);
    $.ajax({
        url: '../logic/updateUserData.php',
        type: 'POST',
        cache: false,
        datatype: 'text',
        data: stringData,
        success: function (response) {
            console.log(response);
            if (response == 'success') {
                alert('Data updated');
            } else {
                alert('Data not updated - please try again later or enter correct password.');
            }
        }
    });
}

function getCookie(cname) {
    let name = cname + "=";
    let decodeCookie = decodeURIComponent(document.cookie);
    let ca = decodeCookie.split(';');
    for (let i = 0; i < ca.length; i++) {
        let c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}