var oButton;
var bEnabled;

function activateSurvey(sButtonParam) {
	var arRadios = document.getElementById("blokOdpowedziSonda").getElementsByTagName("input");
	for(var i = 0; i < arRadios.length; i++) {
		addEventHandler(arRadios[i], "click", enableSubmit);
		arRadios[i].checked = false;
	}
	oButton = document.getElementById(sButtonParam);
	oButton.style.cursor = "default";
 	oButton.style.color = "gray";
}
	      
function enableSubmit(oEvent) {
	if(!bEnabled) {
	 	oButton.style.cursor = "pointer";
	 	oButton.style.color = "";
		addEventHandler(oButton, "click", vote);
		bEnabled = true;
	}
}



// wysyÅ‚a asynchroniczne Å¼Ä…danie protokoÅ‚em HTTP korzystajÄ…c z obiektu XMLHttpRequest
function vote()
{
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    var arInputs = document.getElementById("blokOdpowedziSonda").getElementsByTagName("input");
    var id;
    for(var i = 0; i < arInputs.length; i++) {
		if(arInputs[i].checked)
			id = arInputs[i].value;
    }

    xmlHttp.open("GET", "surveyVote.php?aid=" + id, true);  
    xmlHttp.onreadystatechange = getVotingResults;
    xmlHttp.send(null);
  }
  else
    setTimeout('vote()', 1000);
}

// wykonywana automatycznie po otrzymaniu odpowiedzi z serwera
function getVotingResults() 
{
  // kontynuuje jedynie jeÅ›li transakcja zostaÅ‚a zakoÅ„czona
  if (xmlHttp.readyState == 4) 
  {
    if (xmlHttp.status == 200) 
    {
      sResponse = xmlHttp.responseText;
      var oSurveyDiv = document.getElementById("bloczekSonda");
	  var oResultDiv = document.createElement("div");
	  oResultDiv.innerHTML = sResponse; 
	  oSurveyDiv.parentNode.replaceChild(oResultDiv.firstChild, oSurveyDiv);
    } 
    // dla statusu protokoÅ‚u HTTP innego niÅ¼ 200 zgÅ‚asza bÅ‚Ä…d
    else 
    {
      alert("Wyst¹pi³ b³¹d podczas uzyskiwania dostêpu do serwera: " + xmlHttp.statusText);
    }
  }
}
