var xmlHttp = GetXmlHttpObject();
    
function processPoll(boo,boo2) {
    var concatUrl;
    var pollId, barColor;
    var pollChoice;
    var i;
    
    pollId = document.poll.pollid.value;   
    barColor = document.poll.barcolor.value;
    
    if (boo2 == false) {
        for (i=0;i<document.poll.pollchoice.length;i++) {
            if (document.poll.pollchoice[i].checked) {
                pollChoice = document.poll.pollchoice[i].value;
            }
        }
    } else {
        pollChoice = 1;
    }
    
    
    concatUrl = '/en/polls/save.asp?pollid=' + pollId;
    concatUrl = concatUrl + '&pollchoice=' + pollChoice;
    concatUrl = concatUrl + '&barcolor=' + barColor;
    concatUrl = concatUrl + '&process=' + boo;
    concatUrl = concatUrl + '&showpoll=' + boo2;
    
    xmlHttp.open("GET",concatUrl,true);
    xmlHttp.onreadystatechange = populateXML;
    xmlHttp.send(null);

}

function populateXML() {
     if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
		     var xmlDoc = xmlHttp.responseXML;
		     var totalVotes = xmlDoc.getElementsByTagName("totalvotes")[0].firstChild.nodeValue;
		     var barColor = xmlDoc.getElementsByTagName("barcolor")[0].firstChild.nodeValue;
		     var multivote = xmlDoc.getElementsByTagName("allowmulti")[0].firstChild.nodeValue;
		     var showpoll = xmlDoc.getElementsByTagName("showpoll")[0].firstChild.nodeValue;
		     var question = xmlDoc.getElementsByTagName("question")[0].firstChild.nodeValue;
             var i, answer, count;
             var html = '';
	         
	         if (showpoll == 0) {
	             html = '<div class=\"header\">\n';
	             html = html + '<span class=\"headertext\">\n';
	             html = html + 'Poll Result(s):\n';
	             html = html + '</span>\n';
	             html = html + '</div>\n';
                 for (i=0;i<xmlDoc.getElementsByTagName("answer").length;i++) {
                    answer = xmlDoc.getElementsByTagName("answer")[i].firstChild.nodeValue;
                    count = xmlDoc.getElementsByTagName("answercount")[i].firstChild.nodeValue;
                    html = html + displayResults(answer,count,totalVotes,barColor);
                 }
             } else {
                 html = '<div class=\"header\">\n';
	             html = html + '<span class=\"headertext\">\n';
	             html = html + question;
	             html = html + '</span>\n';
	             html = html + '</div>\n';
	             
                 for (i=0;i<xmlDoc.getElementsByTagName("answer").length;i++) {
                    answer = xmlDoc.getElementsByTagName("answer")[i].firstChild.nodeValue;
                    html = html + displayPoll(answer,i+1);
                 }
                 
                 html = html + '<div class=\"submitbutton\">\n';
                 html = html + '\n<input class=\"submit\" type=\"button\" name=\"submit\"';
                 html = html + 'value=\"Vote!\" onclick=\"processPoll(true,false)\">';
                 html = html + '\n<input class=\"submit\" type=\"button\" name=\"submit\"';
                 html = html + 'value=\"Results?\" onclick=\"processPoll(false,false)\">';
                 
                 html = html + '</div>';
             }
             
             if ((multivote == 1) && (showpoll == 0)) {
                html = html + '<div class=\"submitbutton\">\n';
                html = html + '\n<input class=\"submit\" type=\"button\" name=\"submit\"';
                html = html + 'value=\"Vote!\" onclick=\"processPoll(false,true)\">';
                html = html + '</div>';
             }
             
             document.getElementById('line-container').innerHTML = html;
		     

		}
     }
}

function displayPoll(answer,number) {
    var html = '';

    if (answer != '') {
        if (number == 1) {
            html = html + '<div class=\"line\">\n';
            html = html + '<input type=\"radio\" name=\"pollchoice\" value=\"' + number + '\" CHECKED>\n';
            html = html + '<span class=\"linetext\">';     
            html = html + answer;
            html = html + '</span>\n';
            html = html + '</div>\n';
        } else {
            html = html + '<div class=\"line\">\n';
            html = html + '<input type=\"radio\" name=\"pollchoice\" value=\"' + number + '\">\n';
            html = html + '<span class=\"linetext\">';     
            html = html + answer;
            html = html + '</span>\n';
            html = html + '</div>\n';
        }
    } 
    return html;
}

function displayResults(answer,count,totalvotes,barcolor) {
    var html = '';
    var percentage = 0;
    var pixel = 0;
    var total;
 

    if (answer != '') {

        if (!(totalvotes > 0)) {
            totalvotes = 1;
        }
        
       total = count / totalvotes * 100;
       percentage = Math.round(total);
       
       html = '<div class=\"line\">\n<span class=\"linetext\">\n';
       
       html = html + answer;
       html = html + '</span>\n</div>\n<div class=\"line\">\n';
       html = html + '<span class=\"linetext\">\n';  
       if (count == 0) {
        pixel = 1;
       } else {
        pixel = percentage;
       }
       html = html + '<div style=\"height:18px;width:' + (pixel*1.25) + 'px;background:#' + barcolor + ';\">\n';
       html = html + '</div></div>\n</span>\n<div class=\"line\">\n';
       html = html + '<span class=\"linetext\">';
       html = html + ' (' + count + ')' + '  -  <strong>' + percentage + '</strong>%';
       html = html + '</span>\n</div>\n';
       
        
    } 
    return html;
}


function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}