﻿var _idmPolls = new Array();

function IDMPoll(clientID,unqID,pid,question,opts,data){
_idmPolls[clientID] = this;
this.clientID=clientID;
this.uniqueID=unqID;
this.pid=pid;
this.question=question;
this.options=new Array();
this.total = 0;
this.options=new Array();
var od = eval(data);
for(var i=0; i<opts.length; i++)
{
	this.options[i] = {text:opts[i],votes:od[i]};
	this.total += od[i];
}
this.renderOptions=function()
{
	var p = this;
	var w = new HtmlWriter();
	w.write('<p>' + p.question + '</p>');
	w.write('<table>');
	for(var i=0; i<p.options.length; i++)
	{
		var opt = p.options[i];
		var id = p.clientID+'_'+i;
		w.write('<tr><td>');
		w.write('<input type="radio" name="'+p.clientID+'" id="'+id+'" value="' + i + '" />');
		w.write('</td><td>');
		w.write('<label for="' + id + '">');
		w.write(opt.text);
		w.write('</label></td></tr>');
	}
	w.write('</table>');
	w.write('<input type="button" class="Button" value="Vote Now" onclick="IDMPollVote(\'' + this.clientID + '\');" />');
	document.getElementById(clientID).innerHTML = w.toString();
}
this.renderResults=function()
{
	var p = this;
	var w = new HtmlWriter();
	if(p.total == 0)
		p.total = 1;
	w.write('<p>' + p.question + '</p>');
	var max = 1;
	for(var i=0; i<p.options.length; i++)
		max = (p.options[i].votes > max) ? p.options[i].votes : max;
	for(var i=0; i<p.options.length; i++)
	{
		var opt = p.options[i];
		var pct = 100 * (opt.votes / this.total);
		var dpct = 100 * (opt.votes / max);
		w.write('<div class="PollResult">');
		w.write('<div class="Bar" style="width:'+ dpct.toFixed(0) +'%"></div>');
		w.write('<div class="Text">');
		w.write(opt.text);
		w.write(' ('+pct.toFixed(1)+'%)');
		w.write('</div>');
		w.write('</div>');
	}
	document.getElementById(clientID).innerHTML = w.toString();
}
this.render=function(opt)
{
	var c = ReadCookie('poll_' + this.pid);
	if(opt || (c && c != 'novote'))
		this.renderResults();
	else
		this.renderOptions();
}
this.sendVote=function()
{
	for(var i=0; i<this.options.length;i++)
	{
		var obj = document.getElementById(this.clientID + '_' + i);
		if(obj.checked)
			return WebForm_DoCallback(this.uniqueID,i,OnIDMPollVoteComplete,clientID,OnIDMPollVoteError,true);
	}
	alert('You must pick a poll option before sending your vote.');
}
this.voteComplete=function(result)
{
	var v = eval(result);
	var nt = 0;
	for(var i=0; i<v.length; i++)
	{
		this.options[i].votes = v[i];
		nt += v[i];
	}
	this.total = nt;
	this.renderResults();
}
}

function IDMPollVote(clientID){_idmPolls[clientID].sendVote(); }
function OnIDMPollVoteComplete(result, clientID){_idmPolls[clientID].voteComplete(result);}
function OnIDMPollVoteError(){alert('An error occurred trying to communicate with the server.');}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();