function preProcess() {
    var link = document.getElementsByName("link")[0].value;
    var category = document.getElementsByName("category")[0].value;
    var tags = document.getElementsByName("tags")[0].value;
    var notes = document.getElementsByName("notes")[0].value;

    var formData = "saveInfo=set&" +
                   "link=" + link + "&" +
                   "category=" + category + "&" +
                   "tags=" + tags + "&" +
                   "notes=" + notes;

    porcess(formData);
}

function preSearchProcess() {
    var query = "searchInfo=set&query=" + document.getElementsByName("query")[0].value;
    porcess(query);
    clearQuery();
}

// Send Data
function porcess(formData) {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
                updatePage(this);
        }
    };
    xhttp.open("POST", "process.php", true);
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhttp.send(formData);
}

// Insert Returned Data
function updatePage(text) {
    var succ = undefined;
    var err = undefined;
    var succ = undefined;
    document.getElementById("ResultsField").innerHTML = text.responseText;

    try {
        succ = document.getElementsByClassName("success")[0];
        err = document.getElementsByClassName("error")[0];
    } catch (e) { }

    var timeOut = (succ) ? 5000 : 8000;

    sleep(timeOut).then(() => {
        if (timeOut == 5000) {
            succ.style.display = "none";
        } else {
            err.style.display = "none";
        }
    });
}
