


function poll_array_to_string (buttons) {
	var arr = new Array ();

	for (var i in buttons) {
		if (buttons[i].checked) {
			arr.push (buttons[i].value);
		}
	}

	return arr.join ();
}


function drawPollXml(xml) {
	var type = 'single';
	var question = '';
	var voters = 0;
	var id = -1;

	// draw:
	var poll = document.createElement('div');
	poll.setAttribute ('id', 'poll');

	var myerr = false;

	var err = $(xml).find("error");
	err.each(function() {
		if ($('#pollerror')[0] == null) {
			$('#poll').prepend ('<div style="color: RED;" id="pollerror">Es ist ein Fehler aufgetreten!</div>');
		}

		myerr = true;
	});

	if (myerr) {
		return ;
	}

	$(xml).find("poll").each(function() {
		id = $(this).attr ("id");
		voters = $(this).attr ("voters");
		type = $(this).attr ("type");
	});

	$(xml).find("question").each(function() {
		poll.appendChild (document.createTextNode($(this).text ()));
	});

	poll.appendChild (document.createElement('br'));
	poll.appendChild (document.createElement('br'));

	var table = document.createElement('table');
	table.setAttribute ('cellpadding', '0');
	table.setAttribute ('cellspacing', '0');
	poll.appendChild (table);

	var archivdiv = document.createElement('div')
	poll.appendChild (archivdiv);

	archivdiv.appendChild (document.createElement('br'));

	var link = document.createElement('a');
	link.appendChild (document.createTextNode('Archiv'));
	link.setAttribute ('href', '/poll-archive.html');
	archivdiv.appendChild (link);
	archivdiv.appendChild (document.createTextNode(' | ' + voters + ' Wähler'));


	archivdiv.appendChild (document.createElement('br'));
	archivdiv.appendChild (document.createElement('br'));


	$(xml).find("option").each(function() {
		var opercent = $(this).attr("percent");
		var ovoters = $(this).attr("voters");
		var ooption = $(this).text ();


		var tr = document.createElement('tr');
		table.appendChild (tr);

		// Zeile 1
		var td = document.createElement('td');
		tr.appendChild (td);



		var polldiv = document.createElement('div');
		polldiv.setAttribute ('class', 'pollbalkenbg');

		var pollbalken = document.createElement('div');
		pollbalken.setAttribute ('class', 'pollbalken');
		pollbalken.setAttribute ('style', 'width: '+Math.round (opercent)+'%;');
		polldiv.appendChild (pollbalken);

		var img = document.createElement('img');
		img.setAttribute ('src', '/images/rpg2/poll_left.gif');
		img.setAttribute ('class', 'pollende_l');
		img.setAttribute ('alt', '');
		pollbalken.appendChild (img);

		img = document.createElement('img');
		img.setAttribute ('src', '/images/rpg2/poll_right.gif');
		img.setAttribute ('class', 'pollende_r');
		img.setAttribute ('alt', '');
		pollbalken.appendChild (img);

		td.appendChild (polldiv);


		td = document.createElement('td');
		td.appendChild (document.createTextNode(opercent+'%'));
		td.setAttribute ('style', 'text-align: right;');
		td.setAttribute ('class', 'prozentwert');
		tr.appendChild (td);

		// Zeile 2
		tr = document.createElement('tr');
		table.appendChild (tr);

		td = document.createElement('td');
		td.setAttribute ('colspan', '2');
		td.appendChild (document.createTextNode(ooption));
		tr.appendChild (td);

	});


	$("#poll").replaceWith (poll);
}




$(document).ready(
	function() {
		$('#poll-submit').click (
			function () {
				var votes = $("input[name=pollanswer]");
				var urlstr = '/data/poll.xml';

				if (votes != null) {
					urlstr = urlstr + '?votes=' + poll_array_to_string (votes) + '&poll=' + $("#poll-id")[0].value;
				}

				$.ajax({
					cache: false,
					type: "GET",
					url: urlstr,
					dataType: "xml",
					success: drawPollXml
				});
			}
		);
	}
);

