                                  

function radio_value(el) {
  for (var i=0;i<el.length; i++) { 
	if (el[i].checked) { return el[i].value; }
  }
}
function on_vote(frm, frmlink) {
	var f0 = document.forms[frm.name];
	var a1=frmlink, a2=radio_value(f0.newchannel_survey), a3=f0.newchannel_own.value;
	window.newchannelfrm.location.href=frmlink+'&newchannel_survey='+a2+'&newchannel_own='+a3+'&surveyvote=1';
	_NCP.UserAnswer=a2; 
	setTimeout( "_NCP.ShowResults()", 2000 );
	return false;
}

function roundTo(inputNum, precision){
var guide = Math.pow(10, precision);
var inputnum = ( Math.round(inputNum * guide) / guide );
var numString = inputNum + "";
if ( numString.search(/\./) < 0 ) {
	numString += ".";	
	for ( var j =0; j<precision; j++ ) numString += "0";
}
var afterDecimal = numString.substring(numString.search(/\./) + 1);
if ( afterDecimal.length > precision ) {
	return numString.substring( 0, numString.search(/\./) + 1 + precision )
}
while (afterDecimal.length < precision) {
	afterDecimal += "0";
	numString += "0";
}
return (numString);
}

NewChannelPoll.prototype.Parent = null;
NewChannelPoll.prototype.Div = null;
NewChannelPoll.prototype.IsActive=false;
NewChannelPoll.prototype.Domain="";
NewChannelPoll.prototype.classQuestion="";
NewChannelPoll.prototype.classAnswer="";
NewChannelPoll.prototype.classOwnAnswer="";
NewChannelPoll.prototype.classVoteButton="";
NewChannelPoll.prototype.classLinks="";
NewChannelPoll.prototype.SurveyID="";
NewChannelPoll.prototype.SurveyVotes="";
NewChannelPoll.prototype.OtherVotes="";
NewChannelPoll.prototype.OtherVotesPct=0;
NewChannelPoll.prototype.UserAnswer=-1;
NewChannelPoll.prototype.Question="";
NewChannelPoll.prototype.AnswerIDs=new Array();
NewChannelPoll.prototype.Answers=new Array();
NewChannelPoll.prototype.AnswerImages=new Array();
NewChannelPoll.prototype.AnswerLinks=new Array();
NewChannelPoll.prototype.AnswerVotes=new Array();
NewChannelPoll.prototype.AnswerPct=new Array();

var _NCP = null;
function NewChannelPoll(){
	this.Domain = "";
	this.SurveyID="20";
	this.SurveyVotes=1833;
	this.OtherVotes=88;
	this.OtherVotesPct=4.8;
	this.Question="Vote for your favorite protein?";
	this.Answers=new Array(7);
	this.AnswerIDs[0]=198; 	this.AnswerVotes[0]=49; 	this.AnswerPct[0]=2.67; 	this.AnswerImages[0]='c26aadf22r3d23e23d23d23d2343244.jpg'; 	this.AnswerLinks[0]='<a href="http://www.invigorate360.com/discount/sni-pro-whey-standard/">view product </a>'; this.Answers[0]="SNI Pro Whey Standard";
	this.AnswerIDs[1]=199; 	this.AnswerVotes[1]=16; 	this.AnswerPct[1]=0.87; 	this.AnswerImages[1]='452708686aasd234234g6y56y5y65dfg.jpg'; 	this.AnswerLinks[1]='<a href="http://www.invigorate360.com/discount/worldwide-sports-nutrition-pure-protein-bar/">view product </a>'; this.Answers[1]="Pure Protein Bar";
	this.AnswerIDs[2]=200; 	this.AnswerVotes[2]=51; 	this.AnswerPct[2]=2.78; 	this.AnswerImages[2]='10063ceeec98798asd987sda9s8da98s.jpg'; 	this.AnswerLinks[2]='<a href="http://www.invigorate360.com/discount/ids-new-whey-liquid-protein-shots/">view product </a>'; this.Answers[2]="New Whey Liquid Protein Shots";
	this.AnswerIDs[3]=201; 	this.AnswerVotes[3]=9; 	this.AnswerPct[3]=0.49; 	this.AnswerImages[3]='54002de06da9s8d7as98d7asa8csfd9f.jpg'; 	this.AnswerLinks[3]='<a href="http://www.invigorate360.com/discount/syntrax-nectar/">view product </a>'; this.Answers[3]="Nectar Whey Protein Isolate";
	this.AnswerIDs[4]=202; 	this.AnswerVotes[4]=7; 	this.AnswerPct[4]=0.38; 	this.AnswerImages[4]='4049c7d0b9sd8f798vb7x98cv7xcv89a.jpg'; 	this.AnswerLinks[4]='<a href="http://www.invigorate360.com/discount/alri-venom-hyperdrive-3-0/">view product </a>'; this.Answers[4]="Hyperdrive 3.0+ 90ct";
	this.AnswerIDs[5]=203; 	this.AnswerVotes[5]=4; 	this.AnswerPct[5]=0.22; 	this.AnswerImages[5]='bg1895a9s8d798c7v89fd7gd9f8g7df9.jpg'; 	this.AnswerLinks[5]='<a href="http://www.invigorate360.com/discount/chef-jay-s-food-products-tri-o-plex-bar/">view product </a>'; this.Answers[5]="Tri-o-plex Bar";
	this.AnswerIDs[6]=204; 	this.AnswerVotes[6]=33; 	this.AnswerPct[6]=1.8; 	this.AnswerImages[6]='540423f5aaas98d76896v7s8df6s97fs.jpg'; 	this.AnswerLinks[6]='<a href="http://www.invigorate360.com/discount/worldwide-sports-nutrition-pure-protein-rtd-shake/">view product </a>'; this.Answers[6]="Pure Protein RTD Shake";
	this.IsActive=true;
	_NCP = this;
}
NewChannelPoll.prototype.gid=function(id){
	var d=document; if(d.getElementById){return d.getElementById(id);}else if(d.all){return d.all[id];}else if(d.layers){return d.layers[id];}
	return null;
}
NewChannelPoll.prototype.cl = function(cls) {
	if(cls=="")return "";return " class="+cls;
}
NewChannelPoll.prototype.tag = function(xml,tag){
	if (document.all){return xml.responseXML.getElementsByTagName(tag)[0].text;}
	else{return xml.responseXML.getElementsByTagName(tag)[0].textContent;}
}
NewChannelPoll.prototype.ShowResults=function(doc) {

	// add 1 vote 
	for ( var i=0; i<this.AnswerIDs.length; i++ ) {
	 	if ( this.AnswerIDs[i] == this.UserAnswer ) {
			this.AnswerVotes[i] ++;	this.SurveyVotes ++; break;
		}
	}
	if ( this.UserAnswer == -1 ) { this.OtherVotes[i] ++; this.SurveyVotes++; }
	

	// recalculate pct array
	for ( var i=0; i<this.AnswerIDs.length; i++ ) {
		this.AnswerPct[i] = roundTo( 100 * this.AnswerVotes[i] / this.SurveyVotes, 2 );
	}
	this.OtherVotesPct = roundTo( 100 * this.OtherVotes / this.SurveyVotes, 2 );

	if ( this.Div != null && this.Parent != null ) this.Parent.removeChild( this.Div );
	this.Div = null; this.DisplayResults( this.Parent );
}
NewChannelPoll.prototype.DisplayResults=function(parent) {
	var div = document.createElement("div");                     
	var o = "";

	o += "<b"+this.cl(this.classQuestion)+">" + this.Question + "</B>";
	for ( var i=0; i<this.AnswerIDs.length; i++ ) {
		var pct = parseInt( this.AnswerPct[i] );

		o += "<div style='margin-top:5px;'"+this.cl(this.classAnswer)+"><B>"+this.Answers[i]+"</B> &nbsp;"
		   + this.AnswerPct[i] + "%, <span style='font-size:9px;color:gray'>("+this.AnswerVotes[i]+" votes)</span>"
		   + "<table width=95% cellspacing=0 cellpadding=0 style='height:6px;border:1px #bcbcbc solid'><TR>";
		if ( pct > 0 ) o += "<TD width=" + parseInt(pct) + "% bgcolor=red style='font-size:4px;'>&nbsp;</TD>";
		if ( pct < 100 ) o += "<TD width=" + parseInt(100-pct) + "% bgcolor=white style='font-size:4px;'>&nbsp;</TD>";
		o += "</TR></TABLE>";
		   + "</div>\n";
	}
	if ( this.OtherVotes > 0 ) {
		pct = this.OtherVotesPct;
		o += "<div style='margin-top:5px;'"+this.cl(this.classAnswer)+"><B>Own Answer</B> &nbsp;"
		   + pct + "%, <span style='font-size:9px;color:gray'>("+this.OtherVotes+" votes)</span>"
		   + "<table width=95% cellspacing=0 cellpadding=0 style='height:6px;border:1px #bcbcbc solid'><TR>";
		if ( pct > 0 ) o += "<TD width=" + parseInt(pct) + "% bgcolor=red style='font-size:4px;'>&nbsp;</TD>";
		if ( pct < 100 ) o += "<TD width=" + parseInt(100-pct) + "% bgcolor=white style='font-size:4px;'>&nbsp;</TD>";
		o += "</TR></TABLE>";
		   + "</div>\n";
	}

	
	o += "<BR /><div align=center style='color:black'><B>Thanks for voting</b></div>";
	div.innerHTML = o; this.Div = div; this.Parent = parent;
	parent.appendChild( div );
}
NewChannelPoll.prototype.Display=function(parent) {

	var div = document.createElement("div");                     
	var o = "";

	o += "<form name='ncpollfrm' onsubmit='on_vote(this,\"http://www.newchannel.com/polls/poll_frm.php?id=20&domain=\"); return false;'>";
	
	o += "<b"+this.cl(this.classQuestion)+">" + this.Question + "</B>";
	for ( var i=0; i<this.AnswerIDs.length; i++ ) {
		lbls = "<label for='answ_"+i+"'>";
		lble = "</label>";
		o += "<div style='margin-top:5px;'"+this.cl(this.classAnswer) +">";
		if( this.AnswerImages[i] )
			o += '<table width=100%><tr><td width="20">';
		o += "<input type=radio name=newchannel_survey id='answ_"+i+"'"
			+" onclick='document.getElementById(\"newchannel_votebtn\").style.display=\"block\"; "
				+" document.getElementById(\"newchannel_ownanswer\").style.display=\"none\"' "
			+" value="+this.AnswerIDs[i]+" />";
		if( this.AnswerImages[i] ) {
			o += '</td><td width="43">' + lbls;
			o += '<img width="40" height="40" align="absmiddle" alt="'+this.Answers[i]+'" title="'+this.Answers[i]+'"'
				+ ' style="cursor:pointer; cursor: hand" '
				+ 'src="http://www.newchannel.com/images/'+ this.AnswerImages[i] + '" />';
			o += lble + '</td><td>';
		}
		o += lbls + this.Answers[i] + lble;
		if( this.AnswerLinks[i] )
			o += '<br /><span class="anslink">' + this.AnswerLinks[i] + '</span>';
		if( this.AnswerImages[i] ) o += '</td></tr></table>';
		o += "</div>";
	}

	o += "<div style='margin-top:5px;'"+this.cl(this.classAnswer)+"><input type=radio id='answ_own' name=newchannel_survey value='-1' "
			+ " onclick='document.getElementById(\"newchannel_votebtn\").style.display=\"block\"; "
				+ " document.getElementById(\"newchannel_ownanswer\").style.display=\"block\"' /><label for='answ_own'> Own answer</label></div>";

	o += "<div id=newchannel_ownanswer style='display:none;' "+this.cl(this.classOwnAnswer)+">"
		+ "<textarea name=newchannel_own rows=4></textarea></div>";	
	o += "<div id=newchannel_votebtn style='display:none;'"+this.cl(this.classVoteButton)+"><input type=submit "
		+ " name='surveyvote' value='Vote!' /></div>";

	o += "<iframe width='100%' name='newchannelfrm' src='http://www.newchannel.com/blank.html' height=0 scrolling=no frameborder=0></iframe>";
	o += "<div"+this.cl(this.classLinks)+">";	
	o += "</div>";

	o += "</form>";
	div.innerHTML = o; this.Div = div; this.Parent = parent;
	parent.appendChild( div );
}
