
function pollProcess(data) {
    var poll = document.getElementById("poll");
    if (! poll) return false;

    if (data && data.status == "ok") {
        poll.innerHTML = "<p class=\"ok\">Thank you, your vote has been recorded.</p>";
    }
    else if (data && data.status == "dupe") {
        poll.innerHTML = "<p class=\"ok\">Sorry, you have already voted on this poll.</p>";
    }
    else {
        poll.innerHTML = "<p class=\"error\">Sorry, there was an error while processing your vote.</p>";
    }

    return true;
}

function pollHasVoted(target, poll) {
    if (! target) return;
    if (! poll || poll.length == 0) return;

    var val; 
    if ((val = readCookie("poll_voted"))) {
        var a = val.split(".");
        for (var i = 0; i < a.length; i++) {
            if (a[i] == poll) {
                target.innerHTML = "<p class=\"ok\">You have already voted on this poll.</p>";
            }
        }
    }
}

function pollClick(script, target, poll, answer) {
    if (AJAXAvailable()) {
        var ajax = new AJAX();
        var data = {
            "query"     : "poll",
            "poll"      : poll,
            "answer"    : answer
        };
        ajax.sendjson(pollProcess, script, data);
        return false; /* do not follow the link */
    }
    else {
        alert("Sorry, this poll requires an AJAX compatible browser.");
        return false; /* do not reload the page */
    }
}


