function CMSPublicPluginVoting(allowedAnswers){var $this=this;var id;var allowedAnswers=allowedAnswers;var request=new Request();var recaptcha;this.bind=function(){$('.questions').bind('change',function(){var checked=$('.questions:checked').length;if(checked>=allowedAnswers){$('.questions').each(function(index){if($(this).prop('checked')===false){$(this).attr('disabled','disabled');}});}else{$('.questions').removeAttr('disabled');}});$('#vote_form').bind('submit',function(event){event.preventDefault();$this.voteAnswer();});$(document).on('submit','#activate_form',function(event){event.preventDefault();$this.activateAnswer();});$(document).on('submit','#userdata_form',function(event){event.preventDefault();$this.userForm();});$(document).on('change','select#district',{action:'GET_COMMUNES',target:'select#commune'},this.updateSelect);$(document).on('change','select#commune',{action:'GET_CITIES',target:'select#city'},this.updateSelect);$(document).on('change','select#city',{action:'GET_STREETS',target:'select#street'},this.updateSelect);};this.updateSelect=function(event){var data={};var selected=$(this).find(':selected');switch(event.data.action){case'GET_COMMUNES':data={"districtId":selected.data('id')};break;case'GET_CITIES':data={"communeId":selected.data('id')};break;case'GET_STREETS':data={"cityId":selected.data('id')};break;} request.postPlugin('voting',event.data.action,data,function(data){var select=$(event.data.target);var options=select.find('option');if(select.length>0){if(options.length>1){for(var i=0;i0){options[i].remove();}} select.trigger('change');select.attr("disabled",true);} if(data.options!==null){for(var i=0;i0){request.postPlugin('voting','VOTE_ANSWER',data,this.addVoteResponse);}else{$('#error_box').html('').show();$(window).scrollTop(0);}};this.activateAnswer=function(){var data=$('#activate_form').serialize();request.postPlugin('voting','ACTIVATE_ANSWER',data,this.activateVoteResponse);};this.addVoteResponse=function(data){$("#vote_container").html(data.displaydata);$("select#district").select2();$("select#commune").select2();$("select#city").select2();$("select#street").select2();recaptcha=grecaptcha.render(document.getElementById('g-recaptcha'),{'sitekey':'6LfHKgoTAAAAAFbSGMH2UrqpYw-TKyH9LoRJtgRm'});if(typeof scroll_y!=="undefined"){$('html,body').animate({scrollTop:scroll_y()},'slow');}else{$(window).scrollTop(0);}};this.activateVoteResponse=function(data){if(data.displaydata!==''){$("#vote_container").html(data.displaydata);} var error;if(data.notice.length>0){error='';} error+='';$('#error_box').html(error).show();$(window).scrollTop(0);}else{$('#error_box').html('').hide();}};this.userForm=function(){var data=$('#userdata_form').serialize();request.postPlugin('voting','USER_DATA',data,this.userFormResponse);};this.userFormResponse=function(data){if(data.displaydata!==''){$("#vote_container").html(data.displaydata);} var error;if(data.notice.length>0){error='';} error+='';$('#error_box').html(error).show();grecaptcha.reset(recaptcha);$(window).scrollTop(0);}else{$('#error_box').html('').hide();}};this.create=function(){this.bind();};}