var urlSendVote = "/polls/vote/";
var urlGetResults = "/polls/results/";
var urlGetForm = "/polls/votingform/";

var httpGetResults;
var httpSendVote;

function ReadVote(poll_id) 
{
  var choice_id = GetChoiceId(poll_id)

  if (choice_id == null)
    alert ("Выберете вариант ответа!");
  else
  	SendVote(poll_id, choice_id);
 
  return false;
}

function GetChoiceId(poll_id) 
{
  var choice_id;
  var pollForm = document.getElementById("pollForm_" + poll_id);
  for (i = 0; i < pollForm.poll_choice.length; i++) 
	 if (pollForm.poll_choice[i].checked) 
	   return pollForm.poll_choice[i].value;
  
  return null;
}

function SendVote(poll_id, choice_id) 
{
	var cookie = getCookie('poll_'+poll_id);
	if (cookie)
	{
		alert("Извините, Вы уже голосовали по даному опросу.");
        return;
	}
	
	if (!testCookie())
	{
		//alert("Для голосования нужно включить cookies.");
        return;
	}
	
	if (httpSendVote.readyState == 4 || httpSendVote.readyState == 0) 
	{
		httpSendVote.open("GET", urlSendVote + choice_id + '/?rand='+ Math.floor(Math.random() * 1000000, true));
		httpSendVote.send(null);
		
		date = new Date();
		date.setTime(date.getTime()+(90*24*60*60*1000));
		var expires = "expires="+date.toGMTString();		
		document.cookie = "poll_"+poll_id+"="+choice_id+";"+expires+";path=/;";
		
		clearTestCookie();
		
		// Let the query take some time to run..
  		setTimeout('SeeResults(' + poll_id + ')', 400);  	
	} 
	else 
	{
		setTimeout('SendVote(' + poll_id + ',' + choice_id + ')', 400)
  	}
}

function SeeResults(poll_id) 
{
    if (httpGetResults.readyState == 4 || httpGetResults.readyState == 0) {
  		httpGetResults.open("GET",urlGetResults + poll_id + '/?rand='+Math.floor(Math.random() * 1000000), true);
		httpGetResults.onreadystatechange = function () {
			if (httpGetResults.readyState == 4) {
		    	var pollDiv = document.getElementById("pollDiv" + poll_id);
		    	pollDiv.innerHTML = httpGetResults.responseText;
    		}
		}
  		httpGetResults.send(null);
  	} 
	else 
	{
	  	setTimeout('SeeResults(' + poll_id + ')', 500);
  	}
}

function SeeVotingForm(poll_id)
{
    if (httpGetResults.readyState == 4 || httpGetResults.readyState == 0) {
  		httpGetResults.open("GET", urlGetForm + poll_id + '/?rand='+Math.floor(Math.random() * 1000000), true);
		httpGetResults.onreadystatechange = function () {
			if (httpGetResults.readyState == 4) {
		    	var pollDiv = document.getElementById("pollDiv" + poll_id);
		    	pollDiv.innerHTML = httpGetResults.responseText;
    		}
		}
  		httpGetResults.send(null);
  	} 
	else 
	{
	  	setTimeout('SeeVotingForm(' + poll_id + ')', 500);
  	}
}
  
// ============= Initialization ============

function initPolls() {
	httpGetResults = getHTTPObject();
	httpSendVote = getHTTPObject();
}

// brothercake's generic onload
// http://www.brothercake.com/site/resources/scripts/onload/

if(typeof window.addEventListener != 'undefined')
{
	//.. gecko, safari, konqueror and standard
	window.addEventListener('load', initPolls, false);
}
else if(typeof document.addEventListener != 'undefined')
{
	//.. opera 7
	document.addEventListener('load', initPolls, false);
}
else if(typeof window.attachEvent != 'undefined')
{
	//.. win/ie
	window.attachEvent('onload', initPolls);
}  

//initiates the XMLHttpRequest object
//as found here: http://www.webpasties.com/xmlHttpRequest
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}  
