﻿// ==========================================
// AJAX Load Poll
// ==========================================
function Poll(id, xsl)
{		
	this._id		= id;
	this._xsl		= xsl;
	this._pollCookieName = "Avusa.Poll.Cookie";
	
	this.initPoll = function(obj)
	{
		this._pollDiv = $(obj);
		
		if ($("ctl00_hdnPageID") != null)
		{
			this._pageid = $("ctl00_hdnPageID").value
			
			if (this._pageid != null)
				this.loadControl();
		}
	};
	
	this.loadControl = function()
	{
		var url = AjaxURL + '/PollGetData';
		var inParameters = Object.toQueryString(
							{
								'iPageID': this._pageid,
								'sControlID': this._id,
								'sXSLTemplate': this._xsl
							});
		
		var ajax = new Ajax(url, 
		{
			update: this._pollDiv,
			method: 'post', 
			data: inParameters,
			onComplete: this.evalResponse
		});
		
		ajax.request();
	};
	
	this.hasVoted = function()
	{	
		var pollID = $('Poll_ID').value;
		var myCookie = new Hash.Cookie(this._pollCookieName, {duration: 3600});
				
		if (myCookie.get('Poll_' + pollID) == null)
		{
			return false;	
		}

		return true;
	};
	
	this.setVoted = function()
	{
		var pollID = $('Poll_ID').value;
		var myCookie = new Hash.Cookie(this._pollCookieName, {duration: 3600, autoSave: false});
		myCookie.set('Poll_' + pollID, pollID);
		
		// check if cookie not too big
		if (!myCookie.save())
		{
			myCookie.erase();
			myCookie.set('Poll_' + pollID, pollID);
			myCookie.save();
		};
	};
	
	this.radioClick = function(e)
	{	
		if (this.hasVoted() == false)
		{
			var obj = eventTrigger (e);	
			
			this.addPollVote(obj.value);
			this.setVoted();
			this.loadControl();
		}
	};
	
	this.addPollVote = function(pollItemID)
	{
		var url = AjaxURL + '/PollAddVote';
		var inParameters = Object.toQueryString(
							{
								'iPollItemID': pollItemID,
								'iPageID': this._pageid,
								'sControlID': this._id,
								'sXSLTemplate': this._xsl
							});
		
		var ajax = new Ajax(url, 
		{
			update: this._pollDiv,
			method: 'post', 
			data: inParameters,
			onComplete: this.evalResponse
		});
		
		ajax.request();
	};
	
	this.initResponse = function()
	{
		if (this.hasVoted() == false)
		{
			$('PollResult').style.display = 'none';
			$('PollOptions').style.display = 'block';
		}
		else
		{
			$('PollResult').style.display = 'block';
			$('PollOptions').style.display = 'none';
		}
	};
}

Poll.prototype._pollCookieName;
Poll.prototype._id;
Poll.prototype._xsl;
Poll.prototype._pageid;
Poll.prototype._pollDiv;


// exec javascript since mootools function is broken
Poll.prototype.evalResponse = function(request)
{
	var script, scripts;
	
	scripts = [];
	var regexp = /<script[^>]*>([\s\S]*?)<\/script>/gi;
	
	while ((script = regexp.exec(request))) 
		scripts.push(script[1]);
	
	scripts = scripts.join('\n');
		
	//alert(scripts);
			
	if (scripts) (window.execScript) ? window.execScript(scripts) : window.setTimeout(scripts, 0);
};