function httpGetAsync(theUrl, callback, failCallback) {
    const xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState === 4) {
            if (xmlHttp.status === 200) {
                callback(xmlHttp.responseText);
            } else {
                failCallback(xmlHttp.status);
            }
        }
    }
    xmlHttp.open("GET", theUrl, true); // true for asynchronous 
    xmlHttp.send(null);
}


function httpPostAsync(theUrl, data, callback, failCallback) {
    const xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState === 4) {
            if (xmlHttp.status === 200) {
                callback(xmlHttp.responseText);
            } else {
                failCallback(xmlHttp.status);
            }
        }
    }
    xmlHttp.open("POST", theUrl, true); // true for asynchronous 
    xmlHttp.setRequestHeader('Content-type', 'application/json');
    xmlHttp.send(JSON.stringify(data));
}

function setCookie(cname, cvalue, exdays) {
    const d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    let expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}


function getCookie(cname) {
    let name = cname + "=";
    let decodedCookie = decodeURIComponent(document.cookie);
    let ca = decodedCookie.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 "";
}

function writeClass(className, data) {
    if (data === undefined || data == null || data.toString() === 'undefined') {
        data = "";
    }

    let elements = document.getElementsByClassName(className);
    Array.from(elements).forEach(e => {
        e.innerHTML = data;
    });
}

function writeId(idName, data) {
    if (data === undefined || data == null || data.toString() === 'undefined') {
        data = "";
    }

    document.getElementById(idName).innerHTML = data;
}

function writeValue(className, data) {
    if (data === undefined || data == null || data.toString() === 'undefined') {
        data = "";
    }

    let elements = document.getElementsByClassName(className);
    Array.from(elements).forEach(e => {
        e.value = data;
    });
}


function writeSrc(className, data) {
    if (data === undefined || data == null || data.toString() === 'undefined') {
        data = "";
    }

    let elements = document.getElementsByClassName(className);
    Array.from(elements).forEach(e => {
        e.src = data;
    });
}

function timeSince(date) {
    let seconds = Math.floor((new Date() - date) / 1000);

    let interval = seconds / 31536000;

    if (interval > 1) {
        return Math.floor(interval) + " years";
    }
    interval = seconds / 2592000;
    if (interval > 1) {
        return Math.floor(interval) + " months";
    }
    interval = seconds / 86400;
    if (interval > 1) {
        return Math.floor(interval) + " days";
    }
    interval = seconds / 3600;
    if (interval > 1) {
        return Math.floor(interval) + " hours";
    }
    interval = seconds / 60;
    if (interval > 1) {
        return Math.floor(interval) + " minutes";
    }
    return Math.floor(seconds) + " seconds";
}

function timeSinceString(date) {

    date = Date.parse(date + "Z");
    let seconds = Math.floor((new Date() - date) / 1000);

    let interval = seconds / 31536000;

    if (interval > 1) {
        let val = Math.floor(interval);
        let ret = val + " year";
        if (val !== 1) ret += "s";
        return ret;
    }
    interval = seconds / 2592000;
    if (interval > 1) {
        let val = Math.floor(interval);
        let ret = val + " month";
        if (val !== 1) ret += "s";
        return ret;
    }
    interval = seconds / 86400;
    if (interval > 1) {
        let val = Math.floor(interval);
        let ret = val + " day";
        if (val !== 1) ret += "s";
        return ret;
    }
    interval = seconds / 3600;
    if (interval > 1) {
        let val = Math.floor(interval);
        let ret = val + " hour";
        if (val !== 1) ret += "s";
        return ret;
    }
    interval = seconds / 60;
    if (interval > 1) {
        let val = Math.floor(interval);
        let ret = val + " minute";
        if (val !== 1) ret += "s";
        return ret;
    }
    return Math.floor(seconds) + " seconds";
}