aPollDoubleItem = new Array();

function pollVariantList( variant )
{
	var ID = variant.getElementsByTagName('id')[0].firstChild.data;

	//
	var aQuestion = variant.getElementsByTagName('question');
	var container = document.getElementById('dpol_caption_' + ID );
	var newDiv = document.createElement("div");
	newDiv.id = "dpol_caption_text_" + ID;
	newDiv.className = "pollQuestion";

	var newText = document.createTextNode(aQuestion[0].firstChild.data);

	newDiv.appendChild( newText );
	container.appendChild( newDiv );

	//
	var aVariantList = variant.getElementsByTagName('variant');

	container = document.getElementById('dpol_content_' + ID );

	var newInput = document.createElement("input");
	newInput.type = "hidden";
	newInput.id = "current_vote_" + ID;
	newInput.value = '';

	container.appendChild( newInput );
	for ( var i = 0; i < aVariantList.length; i++ ) {
		sText = aVariantList[i].getElementsByTagName('text')[0].firstChild.data;
		newText = document.createTextNode(sText);

		var newDiv = document.createElement("div");
		newDiv.className = "pollTextContainer";

		newInput = createNamedElement( "input", "vote_" + ID );
		newInput.type = "radio";
		newInput.value = i;
		newInput.className = "pollRadioButton";
		newInput.onclick = function(){ setVote( 'current_vote_' + ID, this.value ); };

		newDiv.appendChild( newInput );
		newDiv.appendChild( newText );

		container.appendChild( newDiv );
	}

	return true;
}

function pollResultList( variant, cVote )
{
	var ID = variant.getElementsByTagName('id')[0].firstChild.data;

	//Hide submit-button
	document.getElementById('dpol_actions_' + ID ).style.display = 'none';

	//
	if ( null === document.getElementById('dpol_caption_text_' + ID) ) {
		var aQuestion = variant.getElementsByTagName('question');
		var container = document.getElementById('dpol_caption_' + ID );
		var newDiv = document.createElement("div");
		newDiv.id = "dpol_caption_text_" + ID;
		newDiv.className = "pollQuestion";

		var newText = document.createTextNode(aQuestion[0].firstChild.data);

		newDiv.appendChild( newText );
		container.appendChild( newDiv );
	}

	//
	var aVariantList = variant.getElementsByTagName('variant');

	var iTotalVotes = 0;
	var aVoteList = new Array();
	for ( var i = 0; i < aVariantList.length; i++ ) {
		if ( '' != cVote && i == cVote ) {
			aVariantList[i].getElementsByTagName('votes')[0].firstChild.data++;
		}
		aVoteList[i] = aVariantList[i].getElementsByTagName('votes')[0].firstChild.data;
		iTotalVotes += (+aVoteList[i]);
	}

	var aPercentList = new Array();
	for ( var i = 0; i < aVoteList.length; i++ ) {
		if ( 0 == iTotalVotes ) {
			aPercentList[i] = 0;
		} else {
			aPercentList[i] = Math.round((aVoteList[i]/iTotalVotes*100)*10)/10;
		}
	}

	container = document.getElementById('dpol_content_' + ID );
	container.innerHTML = '';

// =======================================================
// sort result list by votes num desc
/*
				var aSVoteList = new Array();
				var j = 0;
				for(i in aVoteList)
				{
					aSVoteList[j] = new Array(i,parseInt(aVoteList[i]));
					j++;
				}
				aSVoteList.sort(function(a,b){return b[1] - a[1]});
				var i=0;
				for ( var j=0; j < aSVoteList.length; j++ )
	    		{
					i = aSVoteList[j][0];
*/
// =======================================================
	for ( var i = 0; i < aVariantList.length; i++ ) {

		sText = aVariantList[i].getElementsByTagName('text')[0].firstChild.data;

		var newText = document.createTextNode(sText);

		var newDiv3 = document.createElement("div");
		newDiv3.className = "pollTextContainerRes";
		newDiv3.appendChild(newText);

		var newDiv4 = document.createElement("div");
		newDiv4.id='p_' + ID + '_' + i;
		newDiv4.className = "pollProgressBar";

		newText = document.createTextNode('0');
		newDiv4.appendChild( newText );

		container.appendChild( newDiv3 );
		container.appendChild( newDiv4 );

		drawBar(newDiv4.id,aPercentList[i],aVoteList[i]);
	}

	return true;
}

// create element with name ( this is problem with IE )
function createNamedElement( type, name )
{
	var element;
	try {
		element = document.createElement('<'+type+' name="'+name+'">');
	} catch(e) { }
	if (!element || !element.name) {
		element = document.createElement(type)
		element.name = name;
	}

	return element;
}

function setVote( item, val )
{
	document.getElementById( item ).value = val;
}

function drawBar( item, size, votes )
{
	var bar = document.getElementById(item);
	var widthLim = Math.floor( size * (bar.parentNode.offsetWidth / 100) );

	if ( widthLim > bar.offsetWidth ) {
		bar.style.width = bar.offsetWidth + 2 + 'px';
		if ( widthLim < bar.offsetWidth ) {
			bar.style.width = widthLim + 'px';
		}

		var percentStep = Math.round(((size*bar.offsetWidth)/widthLim)*100)/100;
		bar.innerHTML = percentStep;

		setTimeout("drawBar('"+item+"',"+size+","+votes+")", 25 );
	} else {
		if ( votes == 0 ) {
			bar.innerHTML = '&nbsp;';
		} else {
			bar.innerHTML = size + '%';// (' + votes + ' stemmen)
		}
	}
}

/////////////////////////

function serverQuery( callback_function, url, responseType )
{
	var XMLHttpRequestObject = false;
	if( window.XMLHttpRequest ) {
		XMLHttpRequestObject = new XMLHttpRequest();
	} else if( window.ActiveXObject ) {
		XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if( XMLHttpRequestObject ) {
		XMLHttpRequestObject.open( "GET", url );
		XMLHttpRequestObject.onreadystatechange = function()
		{
			if ( XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200 ) {
				if ( '' != callback_function ) {
					if ( 'xml' == responseType )
						answer = XMLHttpRequestObject.responseXML;
					else
						answer = XMLHttpRequestObject.responseText;

					callback_function(answer);
				}

				delete XMLHttpRequestObject;
				XMLHttpRequestObject = null;
			}
		}

		XMLHttpRequestObject.send( null );
	}

	return true;
}
