
// Function to handle the Submit Profile button on the main page
function submit() {
	let name = document.getElementById("name");
    if (name.value.length == 0) {
        alert("You must enter a name");
    } else {
        req = new XMLHttpRequest();
        req.onreadystatechange = function() {
            if (this.readyState == 4) {
                if (req.status == 200) {
                    alert("Profile Created Successfully!");
                    name.value = "";
                    window.location.href = "/";
                } else {
                    alert("There was an error creating the profile.");
                }
            }
        }
        let person = { name: name.value };
        req.open("PUT", "/newprofile");
        req.send(JSON.stringify(person));
    }
}

// Function to handle the Submit Message button on the profile page
function submitMessage(pid) {
	let msg = document.getElementById("msg");
    if (msg.value.length == 0) {
        alert("You must enter a message");
    } else {
        req = new XMLHttpRequest();
        req.onreadystatechange = function() {
            if (this.readyState == 4) {
                if (req.status == 200) {
                    alert("Message Sent Successfully!");
                    msg.value = "";
                    window.location.reload();
                } else {
                    alert("There was an error sending the message.");
                }
            }
        }
        let messageContent = { message: msg.value, id: pid };
        req.open("POST", "/message");
        req.send(JSON.stringify(messageContent));
    }
}