/*
The SoftAd Group
Copyright (c) 2000-2004 The SoftAd Group, Inc.  All rights reserved
*/
function getPollChoice(frm)
{
	var channelXml = document.getElementById('Channel' + frm.pagesectionid.value);
	var pollXml = channelXml.selectSingleNode("//pagesection[@id = '" + frm.pagesectionid.value + "']");
	var val = "";
	var displayAs = pollXml.selectSingleNode("fields/field[@name='DisplayAsMenu']").text;

	if (frm.choice == null)
		return val;
		
	if (displayAs == 'Drop down menu' || displayAs == '')
		val = frm.choice.options[frm.choice.selectedIndex].value;
	else
	{
		if (frm.choice.length != null)
		{
			for (var i = 0; i < frm.choice.length; ++i)
			{
				if (frm.choice[i].checked)
				{
					val = frm.choice[i].value;
					break;
				}
			}
		}
		else
		{
			if (frm.choice.checked)
				val = frm.choice.value;
		}
	}
	
	return val;
}

function updateEPollCount(frm)
{
	var channelXml = document.getElementById('Channel' + frm.pagesectionid.value).selectSingleNode("ChannelSiteInfo");
	var newXml = channelXml.selectSingleNode("//CurrentPage//pageinstance");
	var val = getPollChoice(frm);

	if (val == "")
	{
		alert('Please choose for your vote to be tallied.');
		return false;
	}

	var countNode = channelXml.selectSingleNode("//pagesection[@id='" + frm.pagesectionid.value + "']/lists/list/line/fields[field[@id = '" + val + "']]/field[@name='Count']");
	countNode.setAttribute('operation', 'addupdate');

	var countContentNode = countNode.selectSingleNode("content");
	var count = parseInt(countContentNode.text);

	if (isNaN(count))
		count = 0;
		
	countContentNode.text = String(count + 1);
	countContentNode.setAttribute('operation', 'addupdate');
	
	var sitekey = channelXml.selectSingleNode('//ChannelPartnerSite/ChannelPartnerInfo/SiteKey').text;
	//var moduleNode = channelXml.selectSingleNode('//CurrentPage/modules/module');	
	var pageKey = channelXml.selectSingleNode('//CurrentPage/@PageId').text;	
	var pageInstanceKey = channelXml.selectSingleNode('//CurrentPage//pageinstance/@id').text;
	//set the session id for the poll so no new votes can be cast during this session
	var sessionId = document.getElementById('vardata').selectSingleNode("//systemvars/var[@name='sessionid']").text;
	var sessionNode = channelXml.selectSingleNode("//pagesection[@id='" + frm.pagesectionid.value + "']/fields/field[@name='SessionId']");
	var sessionContentNode = sessionNode.selectSingleNode("content");
	sessionNode.setAttribute('operation', 'addupdate');
	sessionContentNode.text = sessionId;

	frm.crt.value = "SiteKey=" + sitekey + "&";
	frm.crt.value += "PageKey=" + pageKey + "&";
	frm.crt.value += "PageInstanceKey=" + pageInstanceKey + "&";
	frm.crt.value += "Mode=WIP_Edit&";
	
	var objDoc = new ActiveXObject("Msxml2.DOMDocument");
	var psElem = objDoc.createElement('pagesections');
	psElem.appendChild(channelXml.selectSingleNode("//pagesection[@id='" + frm.pagesectionid.value + "']"));
	frm.crt.value += "SiteXml=" + escape(psElem.xml);
	
	return true;
}

var polloutput=new Array();
function poll(id, html)
{
	this.html = html;
	this.id = id;
}

function showResults(frm, id)
{
	document.all['polltally' + id].style.display = '';
	document.all['votebutton' + id].style.display = 'none';
	document.all['pollform' + id].style.display = 'none';
	return false;
}

function showCurrentResults(id)
{
	document.all['polltallycopy' + id].style.display = '';	
}

function graphit(g, gwidth, id)
{
	var output = "";
	var total=0;
	for (i=0;i<g.length;i++)
		total+=parseInt(g[i][1]);

	output='<table border="0" cellspacing="0" cellpadding="0">';
	
	for (i=0;i<g.length;i++)
	{
		if (total != 0)
			var calpercentage=Math.round(g[i][1]*100/total);
		else
			calpercentage = 0;
		var calwidth=Math.round(gwidth*(calpercentage/100));
		
		if (calwidth == 0)
			output += '<tr><td class="sidebartext">'+g[i][0]+'&nbsp;</td><td class="sidebartext"> '+calpercentage+'%</td></tr>';
		else
			output+='<tr><td class="sidebartext">'+g[i][0]+'&nbsp;</td><td class="sidebartext"><img src="'+graphimage+'" width="'+calwidth+'" height="10"> '+calpercentage+'%</td></tr>';
	}
	output+='<tr><td><span class="sidebartext"><br>Total participants: <b>'+total+'</b></span></td></tr>';
	
	polloutput[polloutput.length] = new poll(id, output);
}

function writePollResults()
{
	var pollLength = polloutput.length;
	for (var i = 0; i < pollLength; ++i)
	{
		if (document.all['polltally' + polloutput[i].id] != null)
			document.all['polltally' + polloutput[i].id].innerHTML = polloutput[i].html;
		if (document.all['polltallycopy' + polloutput[i].id] != null)
			document.all['polltallycopy' + polloutput[i].id].innerHTML = polloutput[i].html;
	}
}