function OpenImage(src, alt)
{
  image = window.open('','image','left=50, top=50, width=400, height=400, toolbar=no, menubar=no, status=no, resizable=no, scrollbars=no');
  image.focus();
  image.document.write('<html><head><title>' + alt + '</title>');
  image.document.write('<script language="javascript"> var NS = (navigator.appName=="Netscape") ? true:false; function FitPic() { iWidth = (NS)?window.innerWidth:document.body.clientWidth; iHeight = (NS)?window.innerHeight:document.body.clientHeight; iWidth = document.images[0].width - iWidth; iHeight = document.images[0].height - iHeight; window.resizeBy(iWidth, iHeight); self.focus(); } </script>');
  image.document.write('</head><body onload="FitPic();" topmargin="0" leftmargin="0" marginwidth="0" marginheight="0" align="center"><a href="javascript: window.close();"><img src="'+src+'" alt="'+alt+'" border="0" /></a></body></html>');
  image.document.close();
}


// AJAX - anketa

var inquiryID = 0;

function send_xmlhttprequest(handler, protocol, url, content, headers)
{
    var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
    if (!xmlhttp) {
        return false;
    }
    xmlhttp.open(protocol, url);
    xmlhttp.onreadystatechange = function() {
        handler(xmlhttp);
    };
    if (headers) {
        for (var key in headers) {
            xmlhttp.setRequestHeader(key, headers[key]);
        }
    }
    xmlhttp.send(content);
    return true;
}

function inquiry_vote(answer, lang)
{
    if (!send_xmlhttprequest(inquiry_handler, 'GET', '/inquiry.php?inquiry=' + inquiryID + '&answer=' + answer + '&lang=' + lang))
    {
        return false;
    }

    // informace o ukladani hlasovani
    return true;
}

function inquiry_handler(xmlhttp)
{
    if (xmlhttp.readyState == 4)
    {
        var inquiry = xmlhttp.responseXML.getElementsByTagName('inquiry');
        inquiryID = inquiry[0].getAttribute('id');

        var question = xmlhttp.responseXML.getElementsByTagName('question');
        
        var content = '<h3>' + question[0].firstChild.data + '</h3>';
        
        var answers = xmlhttp.responseXML.getElementsByTagName('answer');
        for (var i=0; i < answers.length; i++)
        {
            content += '<a href="javascript:inquiry_vote(' + answers[i].getAttribute('id') + ', \'cs\'); void(0);">' + answers[i].firstChild.data + ' - ' + answers[i].getAttribute('count') + ' hlasů</a> ' +
                       '<img src="/images/enquiry-bar.png" alt="" style="width: ' + (Math.round(190 * answers[i].getAttribute('perc') / 100)) + 'px;" />';
        }
        
        document.getElementById('enquiry').innerHTML = content;
    }
}

