function poll(TargetDivId)
{
  this.TargetDiv  = document.getElementById(TargetDivId);
  this.CookieName = "PollCookie";
  this.xmlhttp    = null;
  
  if (window.XMLHttpRequest)  { 
    this.xmlhttp = new XMLHttpRequest();
  } else {
    if (window.ActiveXObject) {
      try {
        this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } 
      catch (e) {
        try {
          this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e) {}
      }
    }
  }
  
  var LastVotedPollId = this.readCookie(this.CookieName);

	this.xmlhttp.open('GET', "/poll/poll.php?pollid=" + LastVotedPollId, false);
  this.xmlhttp.send(null);
  
  if ((this.xmlhttp.status == 200) && (this.xmlhttp.readyState == 4)) {
    this.TargetDiv.innerHTML = this.xmlhttp.responseText;
  }
}

poll.prototype.vote = function()
{
	var PollId = document.getElementById('poll_pollid').value;
	var RadioObj = document.getElementsByName('pollchoice');

	for (index=0; index < RadioObj.length; index++) {
  	if (RadioObj[index].checked) {
			var radioValue = RadioObj[index].value;
			break;
		}
	}
	
	this.TargetDiv.innerHTML = "<center><br><img src=\"poll/loader.gif\" border=0/></center>";
	this.createCookie(this.CookieName, PollId);
	
	this.xmlhttp.open('GET', "/poll/poll.php?vote&pollid="+ PollId +"&pollchoice="+ radioValue, false);
  this.xmlhttp.send(null);
  
  if ((this.xmlhttp.status == 200) && (this.xmlhttp.readyState == 4)) {
    this.TargetDiv.innerHTML = this.xmlhttp.responseText;
  }
}

poll.prototype.createCookie = function(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

poll.prototype.readCookie = function(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') {
      c = c.substring(1,c.length);
    }
    if (c.indexOf(nameEQ) == 0) {
      return c.substring(nameEQ.length,c.length);
    }
  }
  return "-1";
}