function newPoll()
{
    var http_string = "update.php";
    var pollStart = document.getElementById('poll_start');
    if (pollStart != null)
    {
        ajax_post(http_string, 
        function(xml) 
        {
            receivePollFromServer(pollStart, xml);
        }, 
        "poll_request=1");
    }
}

// Receive xml from the server, and apply it to syncNode.
function receivePollFromServer(syncNode, xml)
{
    // We have received info from the server, so need to update
    // Expect everything to be wrapped in a "result" element.
    if (xml.nodeName != "result" || xml.childNodes == null)
    {
        alert(xmlToString(xml));
        return;
    }
    
    if (xml.getAttribute('status') != 1)
    {
        newnode = newElement("fieldset", {}, 
            xmlToString(xml.firstChild), 
            [
            ["legend", {}, "Server error"],
            ["button", {onmousedown:'newPoll()'}, "Try again"]
            ]);
        syncNode.parentNode.replaceChild(newnode, syncNode);
        return;
    }
    
    var item1 = xml.firstChild.childNodes[0].getAttribute('name');
    var item2 = xml.firstChild.childNodes[1].getAttribute('name');
    var id1 = xml.firstChild.childNodes[0].getAttribute('id');
    var id2 = xml.firstChild.childNodes[1].getAttribute('id');
    
    var previous_response = null;
    if (xml.firstChild.getAttribute('previous') != null)
    {
        previous_response = "Thankyou! The clear superiority of " + xml.firstChild.getAttribute('previous') + " has been acknowledged.";
    }
    
    var newContent = newElement("fieldset", {"upload":"poll_response",message:"Updating poll"}, previous_response, [
        ["legend", {}, "Which is better?"],
        ["button", {onmousedown:'pollResponse(event, ' + id1 + ', ' + id2 + ')'}, null, 
            [["img", {src:getWizardsUrl(item1), onload:"pollCardLoaded(event)", style:"display:none;"}, null], 
            getCardHTML(xml.firstChild.childNodes[0])]
        ],
        ["text", {}, " "],
        ["button", {onmousedown:'pollResponse(event, ' + id2 + ', ' + id1 + ')'}, null, 
            [["img", {src:getWizardsUrl(item2), onload:"pollCardLoaded(event)", style:"display:none;"}, null], 
            getCardHTML(xml.firstChild.childNodes[1])]
        ]
        ]);
    syncNode.parentNode.replaceChild(newContent, syncNode);
}

function pollCardLoaded(event)
{
    event.target.style.display = "";
    event.target.nextSibling.style.display = "none";
}

function pollResponse(event, winner, loser)
{
    var uploadRoot = getUploadRoot(event.target);
    if (!uploadRoot)
        return;
        
    var http_string = "update.php";
    var params = "poll_response=1&winner=" + winner + "&loser=" + loser;
    
    // Replace the update node with an updating message
    uploadRoot.innerHTML = "Connecting to server...<legend>" + uploadRoot.getAttribute('message') + "</legend>";
    
    ajax_post(http_string, 
    function(xml) 
    {
        receivePollFromServer(uploadRoot, xml);
    }, 
    params);
}


