/**
 * Class for loading and displaying a survey using XHR.
 * @author Jeff Fohl
 * @copyright Godengo Inc., 2009
 *
 * DEPENDENCIES:
 *  prototype.js and must be loaded prior to this
 **/
 
// function for handling submission of data
// establishh XHR connection to ajaxaserver.php, and send data


var RivistaSurvey = Class.create();

RivistaSurvey.prototype = {

initialize: function(surveyContainer,view) {
			this.view = view;
			this.backendurl = '/core/ajaxserver.php'; // where we get our data
			this.surveyContainer = $(surveyContainer); // the element that contains the entire survey in the markup
			this.surveyID = surveyContainer.replace(/survey_/i,''); // the survey id
			this.loader_img = '/core/media/images/loader-bar-black.gif';
			this.get();
		},
		
		
get: function() {
			var ajax = new Ajax.Updater(this.surveyContainer, this.backendurl, {
				method: 'get',
				parameters: {'req':'getSurvey', 'surveyid': this.surveyID, 'view': this.view },
				asynchronous: true,
				onCreate : function(transport) {
					if(this.surveyContainer)
						this.surveyContainer.innerHTML = '<div style="text-align:center;"><img src="'+this.loader_img+'" /></div>';
				}.bind(this),
				//if the call fails, show an error message
				onFailure : function(transport) {
					if(this.surveyContainer)
						this.surveyContainer.innerHTML = "<p>Sorry. For some reason, the data did not load properly.</p>";
				}.bind(this)
			});
		}
	};
	
var RivistaSurveySubmit = Class.create();
	
RivistaSurveySubmit.prototype = {

initialize: function(surveyid,surveyFormId,surveyContainerId) {
			this.backendurl = '/core/ajaxserver.php'; // where we get our data
			this.surveyContainer = $(surveyContainerId); // the element that contains the entire survey in the markup
			this.surveyForm = $(surveyFormId); 
			this.surveyID = surveyid; // the survey id
			this.loader_img = '/core/media/images/loader-bar-black.gif';
			this.formdata = this.surveyForm.serialize(true);
			this.formdata = $H(this.formdata).toJSON();
			this.get();
		},
		
		
get: function() {
			var ajax = new Ajax.Request('/core/ajaxserver.php', {
				method: 'get',
				parameters: {'req':'submitSurvey', 'formdata':this.formdata, 'surveyid':this.surveyID},
				asynchronous: true,
				onCreate : function() {
					if(this.surveyContainer)
						this.surveyContainer.innerHTML = '<div style="text-align:center;"><img src="'+this.loader_img+'" /></div>';
				}.bind(this),
				onComplete: function(transport) {
					this.surveyContainer.innerHTML = transport.responseText;
				}.bind(this),
				//if the call fails, show an error message
				onFailure : function() {
					if(this.surveyContainer)
						this.surveyContainer.innerHTML = "<p>Sorry. For some reason, the data did not load properly.</p>";
				}.bind(this)
			});
		}
	};