/* create entry of +1 score in the thread_score table */
function upVoteThread(id)
{
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function()
    {
        /* make sure request has finished succesfully */
        if (this.readyState == 4 && this.status == 200)
        {
            refreshThreadKarma(id);
        }
    }
    xmlhttp.open("GET","givethreadkarma.php?id=" + id+"&value=1",true);
    xmlhttp.send();
}

/* create entry of -1 score in the thread_score table */
function downVoteThread(id) {
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function()
    {
        /* make sure request has finished succesfully */
        if (this.readyState == 4 && this.status == 200)
        {
            refreshThreadKarma(id);
        }
    }
    xmlhttp.open("GET","givethreadkarma.php?id=" + id+"&value=-1",true);
    xmlhttp.send();
}

/* refresh the karma counter on the thread */
function refreshThreadKarma(id)
{
    $.getJSON("threadkarma.php?id=" + id, function(data)
    {
        var points = parseInt(data.points);
        var uservote = parseInt(data.vote);

        var upButton = document.getElementById("upButtonThread"+id);
        var downButton = document.getElementById("downButtonThread"+id);

        document.getElementById("threadKarma"+id).innerHTML = points;

        if(upButton && downButton)
        {
            switch (uservote)
            {
                /* user gave upvote */
                case 1:
                    upButton.style.backgroundColor = "#85bb5c";
                    upButton.style.color = "white";
                    downButton.style.background = "none";
                    downButton.style.color = "#255d00";
                    downButton.style.border = "#255d00 solid 1px";
                    break;
                /* user gave downvote */
                case -1:
                    upButton.style.background = "none";
                    upButton.style.color = "#255d00";
                    downButton.style.backgroundColor = "#951717";
                    downButton.style.color = "white";
                    downButton.style.border = "#8b0000 solid 1px"
                    break;
                /* user did not vote */
                default:
                    upButton.style.background = "none";
                    upButton.style.color = "inherit";
                    downButton.style.color = "inherit";
                    downButton.style.border = "#255d00 solid 1px";
                    downButton.style.background = "none";
                    break;
            }
        }
    });
}

/* refresh the karma on the comment with the given ID */
function refreshCommentKarma(id)
{
    $.getJSON("commentkarma.php?id=" + id, function(data)
    {
        var points = parseInt(data.points);
        var uservote = parseInt(data.vote);

        var upButton = document.getElementById("upButtonComment"+id);
        var downButton = document.getElementById("downButtonComment"+id);

        document.getElementById("commentKarma"+id).innerHTML = points;

        /* set the buttons according to previous given vote by user */
        if(upButton && downButton)
        {
            switch (uservote)
            {
                /* user gave upvote */
                case 1:
                    upButton.style.backgroundColor = "#85bb5c";
                    upButton.style.color = "white";
                    downButton.style.background = "none";
                    downButton.style.color = "#255d00";
                    downButton.style.border = "#255d00 solid 1px";
                    break;
                /* user gave downvote */
                case -1:
                    upButton.style.background = "none";
                    upButton.style.color = "#255d00";
                    downButton.style.backgroundColor = "#951717";
                    downButton.style.color = "white";
                    downButton.style.border = "#8b0000 solid 1px"
                    break;
                /* user did not vote */
                default:
                    upButton.style.background = "none";
                    upButton.style.color = "inherit";
                    downButton.style.color = "inherit";
                    downButton.style.border = "#255d00 solid 1px";
                    downButton.style.background = "none";
                    break;
            }
        }
    });
}

/* create entry of +1 score in the reactie_score table */
function upVoteComment(id)
{
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function()
    {
        /* make sure request has finished succesfully */
        if (this.readyState == 4 && this.status == 200)
        {
            refreshCommentKarma(id);
        }
    }
    xmlhttp.open("GET","givecommentkarma.php?id=" + id+"&value=1",true);
    xmlhttp.send();
}

/* create entry of -1 score in the reactie_score table */
function downVoteComment(id)
{
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function()
    {
        /* make sure request has finished succesfully */
        if (this.readyState == 4 && this.status == 200)
        {
            refreshCommentKarma(id);
        }
    }
    xmlhttp.open("GET","givecommentkarma.php?id=" + id+"&value=-1",true);
    xmlhttp.send();
}
