
var SendDemocracy = "/wp-content/plugins/democracy/democracy.php?demSend=true";
var GetDemocracy = "/wp-content/plugins/democracy/democracy.php?demGet=true";

function initDemocracy() {
	// initiates the two objects for sending and receiving data
	httpReceiveChat = dem_getHTTPObject();
	httpSendChat = dem_getHTTPObject();
	
	document.getElementById('democracy').onsubmit = function () {
        ReadVote(); return false;
    }
    view_results = document.getElementById('view_results');
    if (document.getElementById('view_results')) {
      document.getElementById('view_results').href = "javascript: SeeResults();";
    }
}

function ReadVote () {
  var the_vote;
  the_poll = document.getElementById("democracy");
  for (x = 0; x < the_poll.poll.length; x++) {
	 if (the_poll.poll[x].checked) {
	   the_vote = the_poll.poll[x].value;
	 }
   }
  if (!the_vote) {
    alert ("You must vote first!"); 
  } else {
    SendVote(the_vote);
    // Let the query run...
    setTimeout("SeeResults()", 400);
  }
}

function SeeResults() {
    poll_id = document.getElementById("poll_id").value;
  	httpReceiveChat.open("GET",GetDemocracy + '&poll_id='+poll_id+'&rand='+Math.floor(Math.random() * 1000000), true);
	httpReceiveChat.onreadystatechange = function () {
		results = httpReceiveChat.responseText;
	    the_poll = document.getElementById("democracy");
    	the_poll.innerHTML = results;
	     }
  	httpReceiveChat.send(null);
}

function jal_getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function SendVote (the_vote) {
    poll_id = document.getElementById("poll_id").value;
    cookie = jal_getCookie('demVoted_'+poll_id);
    if (cookie) {
        alert("Go stuff the ballot box elsewhere!");
        return;
    } else {
      	
    document.cookie = "demVoted_"+poll_id+"="+the_vote+";expires=Tue, 09 Sep 2008 21:14:04 UTC;path=/;";

	param = 'vote='+the_vote+'&poll_id='+poll_id;	
	httpSendChat.open("POST", SendDemocracy, true);
	httpSendChat.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  	httpSendChat.send(param);
  	}
}

// 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', initDemocracy, false);
}
else if(typeof document.addEventListener != 'undefined')
{
	//.. opera 7
	document.addEventListener('load', initDemocracy, false);
}
else if(typeof window.attachEvent != 'undefined')
{
	//.. win/ie
	window.attachEvent('onload', initDemocracy);
}
	//initiates the XMLHttpRequest object
//as found here: http://www.webpasties.com/xmlHttpRequest
function dem_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;
}