﻿function	castvote(survey_area_string, survey_id, survey_rb)	{

	var		sl	=	new	tcSurveyLoader();
	var	aa	=	$(survey_area_string).getElements('input[name='+survey_rb+']') ;

	sl.survey_area_string	=	survey_area_string;
	
	for( var i=0; i<aa.length; i++) {
	   if (aa[i].checked)	{
		  var _sopt = aa[i].value;
	   }
	}
	sl.castvote(survey_id, _sopt);
}



function	tcSurveyLoader()	{
	this.survey_area_string	=	"";
	this.isLoading		=	0;
	this.onloadusercallback	=	null;
}
/* =========================================================== */


tcSurveyLoader.prototype.init	=	function()	{
	this.isLoading	=	false;
}

tcSurveyLoader.prototype.castvote		=	function( _sid, _sopt )	{
	this._castVote(_sid, _sopt);
}

tcSurveyLoader.prototype.setOnLoadCallback				=	function( cb )			{	this.onloadusercallback	=	cb;		}
tcSurveyLoader.prototype.showLoading					=	function()				{	
	this.isLoading		=	true;		
	createMessageWindow( this.survey_area_string, 'Voting ...',0,'pr');

}
tcSurveyLoader.prototype.hideLoading					=	function()				{	
	this.isLoading		=	false;		
}

/* =========================================================== */

tcSurveyLoader.prototype._castVote	=	function(sid, sopt)	{

	this.showLoading();

	var	aurl	=	'/code/ajaxcall.php?module=survey&action=castvote&survey_id=' + sid + '&survey_option=' + sopt;

	var myloader = function( jsonObj ) {
		this._onloadcallback( jsonObj );
	}.create({bind : this});

	var request = new Request.JSON({
		url: aurl,
		onComplete: function( jsonObj ) {
			myloader( jsonObj );
		}
	}).send();
}

tcSurveyLoader.prototype._onloadcallback	=	function (jsonObj)	{

	if( this.onloadusercallback )	{
		this.onloadusercallback( jsonObj );
	}

	var	fwuri	=	new	URI().clearData().set('fragment','').toString();
	createMessageWindow( this.survey_area_string, 'Thanks for your vote<br/><a href="'+fwuri+'">Click to see the results</a>',0,'pr');
	this.hideLoading();
}


function	forwardToSurvey( sid, sr )	{
	l	=	new	URI().clearData().set('fragment','');
	l.set('data', {'showresults': sr});

	document.location	=	l;
}