var pReq;
var mychart;
var results;
var proxy = '/cgi-bin/miq_tools/miq_poll.pl';
var poll_question;
var Y=1;
var N=1;

function motorcycle_poll() {
	pReq = GetXmlHttpPollObject();
	if (pReq == null) {
		alert ( "Browser does not support AJAX Request" );
		return;
	}
	// Use syncronous http call to get data (e.g set to false)
	pReq.open("GET",proxy,false);
	pReq.send(null);
	document.getElementById("motorcycle_insurance_poll").innerHTML = pReq.responseText;
	Y = document["poll"]["Y"].value;
	N = document["poll"]["N"].value;
	drawPieChart(Y,N);
}

function drawPieChart(y,n) {
	YAHOO.widget.Chart.SWFURL = "http://yui.yahooapis.com/2.8.2r1/build/charts/assets/charts.swf";
	//--- data
	var thedata = [ { response: "Yes", count: y },{ response: "No", count: n } ]
	var ds = new YAHOO.util.DataSource(thedata);
	ds.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
	ds.responseSchema = { fields: [ "response", "count" ] };
	//--- chart
	//mychart = new YAHOO.widget.PieChart("chart", ds, { dataField: "count", categoryField: "response",series: [] });
	mychart = new YAHOO.widget.PieChart("chart", ds, { dataField: "count", categoryField: "response",
	series: [] ,
	style: { 
		legend: { 
		display: "Bottom", 
		padding: 2, 
		spacing: 2, 
		font: { 
				family: "Arial" 
				} 
			} 
		}
	});
}

function vote(form) {
	pReq = GetXmlHttpPollObject();
	if (pReq == null) {
		alert ( "Browser does not support AJAX Request" );
		return;
	}
	var polVal
	for (i=0;i<document.forms["poll"].polling.length;i++) {
		if (document.forms["poll"].polling[i].checked) {
			polVal = document.forms["poll"].polling[i].value;
		}
	}
	var proxyVote=proxy+"?action=1&polling="+polVal;
	pReq.open("GET",proxyVote,false);
	pReq.send(null);
	document.getElementById("motorcycle_insurance_poll").innerHTML = pReq.responseText;
	Y = document["poll"]["Y"].value;
	N = document["poll"]["N"].value;
	drawPieChart(Y,N);
}

function GetXmlHttpPollObject() {
	var PollxmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari, IE 7.0
		PollxmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			PollxmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			PollxmlHttp  =new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return PollxmlHttp;
}

