var siteVote = Class.create(); siteVote.prototype = { initialize: function( container_id ) { this.container = $(container_id); if( !this.container ) { return false; } this.init(); return true; }, init: function() { this.form = this.container.getElementsByTagName('form')[0]; if( !this.form ) { return false; } this.selected_option = -1; this.msg_select_option = 'Please, select any option.'; this.form.onsubmit = function(){ return false; } Event.observe( this.form, 'submit', this.send.bind(this) ); this.radios = this.form.getInputs('radio'); this.radios.each( this.observeRadio.bind(this) ); this.radios.clear(); return true; }, observeRadio: function(radio) { radio.checked = false; Event.observe( radio, 'click', this.selectOption.bind(this,radio) ); Event.observe( radio, 'focus', this.selectOption.bind(this,radio) ); }, selectOption: function( radio ) { if(this.selected_option != radio.value){ this.selected_option = radio.value; } }, send: function() { if( this.selected_option < 0 ) { alert(this.msg_select_option ); return false; } this.parameters = this.form.serialize()+'&ajax=true'; this.form.disable(); this.request = new Ajax.Request( this.form.action, { method: this.form.method, parameters: this.parameters, onComplete: this.reply.bind(this) } ); return false; }, reply: function( request ) { Element.update( this.container, request.responseText ); this.init(); return true; } }