/* template from http://www.root.cz/clanky/ajax/ */
function send_xmlhttprequest(obsluha, method, url, content, headers){
    var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
    if (!xmlhttp) {return false;}
    xmlhttp.open(method, url);
    xmlhttp.onreadystatechange = function() {obsluha(xmlhttp);};
    if (headers) {for (var key in headers) {xmlhttp.setRequestHeader(key, headers[key]);}}
    xmlhttp.send(content);
    return true;
}

function poll_vote(hlas, prvek){
    if (!send_xmlhttprequest(poll_handler, 'GET', '?hlasxml=' + hlas)) {return false;}
      for (var i=0; i < 10; i++) {
         if (document.getElementById(prvek + i)){
            document.getElementById(prvek + i).innerHTML = document.getElementById(prvek + i).innerHTML.replace(/<a .*>(.*)<\/a>/gi, "$1");
         }
      }

    document.getElementById('pollsummary').innerHTML = 'Váš hlas se ukládá.';
    return true;
}

function poll_handler(xmlhttp){
    if (xmlhttp.readyState == 4) {
        var odpovedi = xmlhttp.responseXML.getElementsByTagName('option');

        max=0; total=0;
        for (var i=0; i < 10; i++) {
            if (max<parseInt(odpovedi[i].firstChild.data)){
               max=parseInt(odpovedi[i].firstChild.data);
            }
        }

        for (var i=0; i < 10; i++) {
            if (document.getElementById('ph' + i)){
               redraw_votes(i, parseInt(odpovedi[i].firstChild.data), max);
               total+=parseInt(odpovedi[i].firstChild.data);
               document.getElementById('ph' + i).innerHTML = odpovedi[i].firstChild.data;
            }
        }
        document.getElementById('pollsummary').innerHTML = 'Celkem hlasů: <b>' + total + '</b>. Váš hlas byl uložen.';
    }
}

