var IE = false;
var NS = false;

//document.write('<scr'+'ipt type="text/javascript" language="javascript" src="http://www.civicscience.com/widget/js/index/steelers" id="steelers" ></scr'+'ipt>');


if (document.all) IE = true;
if (document.layers) NS = true;
function setCookie(name,value){document.cookie  = name + "=" + escape(value);}

function readCookie(name)
{
	if (document.cookie == '') return false;
	else
	{
		var c0, c1;
		var cookie = document.cookie;
		c0 = cookie.indexOf(name);
		if(c0 != -1)
		{
			c0 += name.length + 1;
			c1 = cookie.indexOf(';', c0);
			if(c1 == -1) c1 = cookie.length;
			return unescape(cookie.substring(c0,c1));
		}
		else return false;
    }
}
function Poll(_name,_application,_id,_question,_answers,_answerIDs,_votes,_sumvotes,_action,_folder,_pollSize,_voteBtn,_resultsBtn)
{
	this.name = _name;
	this.application= _application;
	this.id		= _id;
	this.question	= _question;
	this.answers	= _answers;
	this.answerIDs	= _answerIDs;
	this.votes	= _votes;
	this.sumvotes	= _sumvotes;
	this.action	= _action;
	this.pollSize	= (_pollSize == "")?"100%":_pollSize;
	this.folder	= _folder;
	this.voteBtn	= _voteBtn;
	this.resultsBtn	= _resultsBtn;
}

function submitPoll(form,p)
{
	s = -1;
	for (i=0;i<form.elements["a"].length;i++) if (form.elements["a"][i].checked) s = i;
	if (s==-1) return true;
	vote = form.elements["a"][s].value;
	cgi = "P="+p.id+"&a="+vote;
	img = new Image();
	src = p.action + p.folder + "/collectVote.io?"+cgi;
	img.src = src;

	document.images["result"+p.id].src=img.src;
	p.votes[s]++;
	p.sumvotes++;
	p.div.innerHTML = displayPoll(p,vote);
//	setCookie(p.application.toUpperCase()+".PA"+p.id,vote)
	return false;
}

function displayPoll(p,vote)
{
	s="";
	if (NS) var size=10; else var size=12;
	if ((readCookie(p.application.toUpperCase()+".PA"+p.id)) || (vote > 0))
	{
		if (vote == 0 && vote < 99) vote = readCookie(p.application.toUpperCase()+".PA"+p.id);
		for (i=0;i<p.answers.length;i++)
			{
				if (vote == p.answerIDs[i])
					className='pollAnswerBarVoted';
				else 	className='pollAnswerBar';
				if (p.sumvotes == 0)
					percent=0
				else percent = Math.floor(p.votes[i]/p.sumvotes*100);
				width = percent+1;
				s+="		<Table cellpadding=0 cellspacing=2 width=100%>";
				s+="		<tr>";
				s+="			<td valign=top><img src=/images/spacer.gif width="+width+" height=8 class='"+className+"' align=absmiddle><span class='pollAnswerText'> "+percent+"% -";
				s+="				"+p.answers[i]+"</span></td>";
				s+="		</tr>";
				s+="		</table>";
			}
		s+="	<center><span class='pollAnswerText'>"+p.sumvotes+" Total Votes</span></center>";
	}
	else
	{
		for (i=0;i<p.answers.length;i++)
		{
			s+="		<span><input type=radio class='Radio' name=a value="+p.answerIDs[i]+"></span>";
			s+="			<span class='pollAnswerText'>"+p.answers[i]+"</span>";
			s+="			<br>";
		}
		s+="<br><center><input type=image src='"+p.voteBtn+"' border=0>";
		if (p.resultsBtn > '')
			s+="<span onclick='"+p.name+".div.innerHTML = displayPoll("+p.name+",1);' style='margin-left:10px;cursor:hand;'><img src='"+p.resultsBtn+"' border=0></span>";
		s+="</center>";
	}
	return s;
}
function writePoll(p)
{
	var s = '';
	if (p.action == null) action='';
	s+="<table cellspacing=0 cellpadding=2 width="+p.pollSize+">";

	s+="<form name='Poll_"+p.id+"' action='"+p.action+p.folder+"' method='post' target='_top' onsubmit='return submitPoll(this,p_"+p.id+");'>";
	s+="<input type=hidden name=P"+p.id+" value="+p.id+">";
	s+="<input type=hidden name=Display.objectsAvailable value="+p.id+">";
	s+="<tr>";
	s+="	<td colspan=2 align=center>";
	s+="		<img src='/images/spacer.gif' id=result"+p.id+">";
	s+="		<span class='PollQuestion'><b>"+p.question+"</b></span>";
	s+="	</td>";
	s+="</tr>";
	s+="<tr>";
	s+="	<td colspan=2>";
	s+="		<div id='poll"+p.id+"' style='display:block;'>";
	s+=displayPoll(p,0);
	s+="		</div>";
	s+="	</td>";
	s+="</tr>";
	s+="</form>";
	s+="</table>";
	document.write(s);
	p.div = document.getElementById("poll"+p.id);
}

