

function add_submit(id) {
    document.getElementById("submit").innerHTML = '<input type="submit" value="отдать голос" onclick="do_vote();" class="submit" />';
    answerId = id;
}
function do_vote() {
  
    
var url = "/xml/votes.php";
var params = "voteId=" + voteId + "&answerId=" + answerId;
xmlHttp.open("POST", url, true);
document.getElementById("submit").innerHTML = '<img src="/templates/default/style/img/loader.gif" />';
//Send the proper header information along with the request
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.setRequestHeader("Connection", "close");

    

    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleRequestStateChangeAddVote;
    // make the server request
    xmlHttp.send(params);
	
}
function handleRequestStateChangeAddVote() 
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        
		// do something with the response from the server
        handleServerResponseAddVote();
		
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + 
            xmlHttp.statusText);
    }
  }
}

function handleServerResponseAddVote() {
  var  xmlResponse = xmlHttp.responseXML;
  xmlRoot = xmlResponse.documentElement;  
  
  val = xmlRoot.getElementsByTagName("value");
  per = xmlRoot.getElementsByTagName("percent");
  na = xmlRoot.getElementsByTagName("name");
  
  
  var html = "";  
 
  for (var i = 0; i<val.length; i++) 
   html += '<div class="answer">' + per.item(i).firstChild.data + '%  (' + val.item(i).firstChild.data + ') - ' + na.item(i).firstChild.data + '</div>';
  		 
    document.getElementById("answers").innerHTML = html;
    document.getElementById("submit").innerHTML = 'Спасибо, Ваш голос принят';
}
