/*	Poll class	-------------------------------------------------	Copyright by Semaphor.	If you want to use this library, you are very welcome.	But please notice:	*/var POLL_BAD_INSTANCE_CREATION = "Could not create XMLHTTP instance";var POLL_EXCEPTION_TITLE = "An error occurred:";var POLL_ERR_NO_ANSWERS = "Found no answer options.";var POLL_MSXML_OBJECT1 = "Microsoft.XMLHTTP";var POLL_MSXML_OBJECT2 = "Msxml2.XMLHTTP";var AJAX_POLL_REQUEST_ID = "POLL";/*	PollOption  constructor method.*/function PollOption() {	this.percent = 0;	this.count = 0;	this.text = "";	this.value = "";	this.checked = false;	//Added by jc@semaphor 11022008}/*	Poll  constructor method.*/function Poll() {	this.id = "";	this.subject ="";	this.options = new Array();	this.labelVotesTotal = "";	this.labelButtonVote = "";		this.imageURL = "";	this.content = "";	this.defaultOption = ""; //Added by jc@semaphor 11022008		this.width= 190;}var pollValue = null;var pollId = null;/*	PollManager  constructor method.*/function PollManager(ownerId, language, elem, rootURL) { 	this.ownerId = ownerId;	this.language = language; 	this.elem = elem; 	this.rootURL = rootURL;}/*	Trims a string.*/function trim(str){	var i=0,p = str.length-1;	while(str.charAt(i)==' ') i++;	while(str.charAt(p)==' ') p--;	if(i>p) return '';	return str.substring(i,p+1);}/*	Handles parsing and formatting the xml result.*/function parsePollXML(xmlReply) {	var INDEX_ID = 0;	var INDEX_SUBJECT = 1;	var INDEX_CONTENT = 2;		var INDEX_IMAGE_URL = 3;			var INDEX_LABEL_VOTES_TOTAL = 4;		var INDEX_LABEL_BUTTON_VOTE = 5;				// The xml document	var xmldoc = xmlReply;	var root = xmldoc.documentElement;	var poll = new Poll();	poll.id = root.childNodes[INDEX_ID].childNodes[0].nodeValue;		poll.subject = root.childNodes[INDEX_SUBJECT].childNodes[0].nodeValue;		poll.content  = root.childNodes[INDEX_CONTENT].childNodes[0].nodeValue;	poll.imageURL  = root.childNodes[INDEX_IMAGE_URL].childNodes[0].nodeValue;	poll.labelVotesTotal = root.childNodes[INDEX_LABEL_VOTES_TOTAL].childNodes[0].nodeValue;	poll.labelButtonVote = root.childNodes[INDEX_LABEL_BUTTON_VOTE].childNodes[0].nodeValue;		var defaultOption = root.getElementsByTagName("option-default"); //Added by jc@semaphor 11022008	if (defaultOption != null) {		poll.defaultOption = defaultOption[0].childNodes[0].nodeValue;	}	var options = root.getElementsByTagName("option");		// Are there any items?	if(options.length == 0 ) {		//alert(POLL_ERR_NO_ANSWERS);		return;	}		for (var index = 0; index < options.length; index++) {		var po = new PollOption();			po.percent = parseFloat(options[index].getAttribute("percent"));		po.count = parseInt(options[index].getAttribute("count"));				var split = options[index].childNodes[0].nodeValue.split("|");		po.text = trim(split[0]);		po.value = trim(split[1]);				if (po.value === poll.defaultOption) {	//Added by jc@semaphor 11022008			po.checked = true;		}		poll.options[poll.options.length] = po;		}	return poll;}/*	Handles getting the current poll from the server, via AJAX..*/PollManager.prototype.getCurrentPoll = function (callbackFunction) {   var pollElem= document.getElementById(this.elem); var manager = this;		var request = ajaxHelper.createRequest(AJAX_POLL_REQUEST_ID);		if (request == null) {			alert(POLL_BAD_INSTANCE_CREATION);			return false;		}		request.onreadystatechange = function() {			try {  				var request = ajaxHelper.getRequest(AJAX_POLL_REQUEST_ID);				if (request.readyState != 4) return;				if (request.status == 200) {					var p = parsePollXML(request.responseXML);					pollId = p.id					callbackFunction( manager, p, pollElem);				} else {					// fall on our sword				}							} catch(exception)	{			//			var errMsg = POLL_EXCEPTION_TITLE;//			alert(errMsg + exception.description );		}		}		var url = this.rootURL + "/handlepollaction?openagent&action=get&ownerId=" +this.ownerId + "&languageId=" + this.language + "&ts=" + new Date().valueOf();		request.open('GET', url , true);		request.send(null);  }/*	Handles sending the poll selection to the server, via AJAX..*/PollManager.prototype.registerPoll = function (callbackFunction) {		 var manager = this;		if(!ajaxHelper.browserSupportsCookies()) {			alert("Du skal have cookies sl\u00E5et til for at stemme.")			return;		}		var request = ajaxHelper.createRequest(AJAX_POLL_REQUEST_ID);		if (request == null) {			alert(POLL_BAD_INSTANCE_CREATION);			return false;		}		request.onreadystatechange = function() {						try {      					var request = ajaxHelper.getRequest(AJAX_POLL_REQUEST_ID);				if (request.readyState != 4) return;				if (request.status == 200) {					callbackFunction( manager, true);				} else {					// fall on our sword				}							} catch(exception)	{			var errMsg = POLL_EXCEPTION_TITLE;			//alert(errMsg + exception.description );		}		}		var url = this.rootURL + "/handlepollaction?openagent&action=set&ownerId=" +this.ownerId + "&pollId=" + pollId + "&pollValue=" + pollValue + "&ts=" + new Date().valueOf();		request.open('GET', url , true);		request.send(null);    }/*	Handles showing the pollvoting panel.*/function showPollPanel( poll, element) {	var nl = "<br>";	var htmlData = "";	if( poll.content !="") {		htmlData += unescape(poll.content).replace(/\+/g, " ");	}	if( poll.imageURL !="") {		htmlData +="<img src='" + poll.imageURL+"'>";	}	htmlData += "<div class='poll-info'><table cellspacing='0' cellpadding='0' width='100%' border='0'>";	htmlData += "<tr><td valign='top' class='poll-question' colspan='2'>" + poll.subject+"</td></tr>";	var checkedState = "";	for(var index=0; index < poll.options.length; index++) {		var po = poll.options[index];				if (po.checked) {			checkedState = "checked";		}		htmlData += "<tr><td colspan='2' style='height:5px;'></td></tr><tr><td valign='top' class='poll-radio-td'><input class='poll-radio' type='radio' id='pollOption" + index+"' name='pollOptions' value='" + po.value+"' onchange='pollValue=this.value' " + checkedState+"></td><td class='poll-answer' onclick='selectPoll("+index+")'>" + po.text + "</td></tr>";		checkedState = "";	}	htmlData += "<tr><td colspan='0' valign='top' align='right' class='poll-bottom' colspan='2'><input class='poll-button' type='button' value='" + poll.labelButtonVote+"' onclick='pm.submitPoll()'></td></tr>"	htmlData += "</table></div>"		element.innerHTML = htmlData;		//selectPoll(0);}/*	Handles showing the poll result.*/function showPollResult( poll, element) {	var totalVotes = 0;	var nl = "<br>";	var htmlData = "";		if( poll.content !="") {		htmlData += unescape(poll.content).replace(/\+/g, " ");	}	if( poll.imageURL !="") {		htmlData +="<img src='" + poll.imageURL+"'>";	}		htmlData += "<div class='poll-info'><table cellspacing='0' cellpadding='0' width='100%' border='0'>";	htmlData += "<tr><td valign='top' class='poll-question' width='100%'>" + poll.subject+"</td></tr>";	var totals = 0;	htmlData +="<tr><td><table cellspacing='0' cellpadding='0' width='100%' border='0'>";	var totalPercent = 0;	for(var index=0; index < poll.options.length; index++) {		var po = poll.options[index];		totalVotes += po.count;		var title = "";		var pctValue = Math.round(po.percent);/*			if(index+1==poll.options.length) {		pctValue = 100 -totalPercent;			}*/			totalPercent += pctValue;		var pctText =  pctValue;			htmlData += "<tr><td valign='top' class='poll-result-text'>" + po.text+"</td><td class='poll-result-percent'>" + pctText  +"%</td></tr>";		htmlData += "<tr><td colspan='2' valign='top' class='poll-bar-td'  title='" + title+"' >";		if(po.percent > 0.0) htmlData +="<div class='poll-bar' style='width:" + Math.round((poll.width-50) * (po.percent /100))+"px;'></div>";		htmlData +="</td></tr>";} 	htmlData += "</table></td></tr>"	htmlData += "<tr><td valign='top' class='poll-label-votes-total' colspan='2'>" + poll.labelVotesTotal+": " + totalVotes + "</td></tr>";	htmlData += "</table></div>"	element.innerHTML = htmlData;}/*	Retrieves the current poll.*/PollManager.prototype.showPoll = function () {	this.getCurrentPoll( callbackGetPoll);}/*	Handles resitering the selected poll.*/PollManager.prototype.submitPoll = function () {	if(pollValue == null) {		alert("V\u00E6lg venligst et svar f\u00F8rst")		return;	}	this.registerPoll( callbackRegisterPoll);	}/*	Callback method for getting the right poll gui.*/function callbackGetPoll(manager, poll, element) { 	if(!hasVoted(poll.id)) {		showPollPanel(poll, element);	} else {		showPollResult(poll, element);		}	if(placeMenuSpot!=null) placeMenuSpot();}/*	Callback method for registering the selected poll answer.*/function callbackRegisterPoll(manager, success) { 	setPollCookie(pollId);	manager.showPoll();}/*	Handles setting the selected poll radfiobutton, if the user clicks beside it.*/function selectPoll(index) {	var elem = document.getElementById("pollOption"+ index);	elem.checked=true;	pollValue=elem.value;}/* Cookie methods. *//*	Checks to see if the user has answer this before.*/function hasVoted(pollId) {	var cookieName = "poll" +pollId;	var cookieValue = getPollCookie( pollId);	if( cookieValue == '1') {		return true;	}	return false}/*	Set a cookie that registers that the user has answer this poll.*/	function setPollCookie( pollId) {			var cookieName = "poll" +pollId;		var cookieExpireDate = new Date;		cookieExpireDate.setMonth( cookieExpireDate.getMonth()+2);				var cookieValue = "1";		var cookiePath = "/";		var cookieStr = cookieName + '=' + cookieValue+'; path=' + cookiePath+'; expires=' + cookieExpireDate.toGMTString() ;		document.cookie = cookieStr;	}/*	Retrieves the poll cookie, if any.*/	function getPollCookie( pollId) {		var name = "poll" +pollId;		var index = -1;		if(document.cookie) {			index = document.cookie.indexOf( name);		}		if (index != -1) {			var key = name + '=';			var countbegin = (document.cookie.indexOf( key, index) + key.length);			var countend = document.cookie.indexOf(';', index);			if (countend == -1) {				countend = document.cookie.length;			}			return document.cookie.substring(countbegin, countend);		}		return '';	}