var siteFeedBack = Class.create(); siteFeedBack.prototype = { initialize: function( feedback_form_id, mandatory_inputs_class, feedback_status_container_id ) { this.wait_alt = 'Sending, please wait...'; this.msg_empty = 'Empty field: '; this.msg_mismatch = 'Wrong value for: '; this.msg_too_short = 'Value is too short in field: '; this.msg_sent = 'Message successfully sent.'; this.msg_not_sent = 'Failed to send message.'; this.mandatory_inputs_class = mandatory_inputs_class; this.min_length = 10; this.use_alert = false; this.max_rows = 15; this.min_rows = 3; this.text_fields = new Array(); this.strict_fields = new Array(); this.feedback_form = $(feedback_form_id); this.feedback_form.onsubmit = function(){ return false; } this.status = $(feedback_status_container_id); Element.hide(this.status); return true; }, init: function() { this.feedback_form.getElements().each( this.filter.bind(this) ); Event.observe ( this.feedback_form, 'submit', this.send.bind(this) ); Event.observe ( this.feedback_form, 'keydown', this.checkPressedButton.bind(this) ); }, filter: function(input) { if( ['text','password','textarea'].indexOf(input.type) != -1 ) { this.text_fields.push(input); if( input.hasClassName(this.mandatory_inputs_class) ) { this.strict_fields.push(input); } Event.observe ( input, 'focus', this.setFocus.bind(this, input ) ); Event.observe ( input, 'focus', this.showMessage.bind(this,'') ); Event.observe ( input, 'blur', this.setBlur.bind(this, input ) ); if( input.type == 'textarea' ) { Event.observe ( input, 'keydown', this.resizeTextArea.bind(this, input ) ); Event.observe ( input, 'change', this.resizeTextArea.bind(this, input ) ); Event.observe ( input, 'focus', this.resizeTextArea.bind(this, input ) ); Event.observe ( input, 'blur', this.resizeTextArea.bind(this, input ) ); } this.setBlur(input); } return true; }, resizeTextArea: function( textarea ) { this.nesessary_rows = Math.round( textarea.value.strip().length / textarea.cols ); if( this.nesessary_rows > this.max_rows ){ this.nesessary_rows = this.max_rows; } if( this.nesessary_rows < this.min_rows ){ this.nesessary_rows = this.min_rows; } if( textarea.rows != this.nesessary_rows) textarea.rows = this.nesessary_rows; return true; }, protectEmail: function( insertion_point_id, input_class, input_title, tabindex, id_suffix ) { this.input_str = '<'+'in'+'put'+' class="'+ input_class +'" tabindex="'+ tabindex +'" type="t'+'ext"'+' value="" title="'+ input_title +'" maxlength="70" name="feed'+'back_e'+'mail" id="feed'+'back_em'+'ail'+id_suffix+'"'+'>'; Element.update(insertion_point_id,this.input_str); }, checkPressedButton: function(event) { if( event.ctrlKey && event.keyCode == Event.KEY_RETURN ) { this.send(); } }, setFocus: function( input ) { input.value = ( input.value.strip() == input.title ) ? '' : input.value.strip(); return true; }, setBlur: function( input) { input.value = ( input.value.strip() == '' ) ? input.title : input.value.strip(); return true; }, checkMail: function( input ) { return /^\w+[A-Za-z0-9\-\_\.]{1,50}\w+@\w+\.?[a-z0-9\-\_]{1,30}\.[a-z]{2,4}$/.test( input.value ) ; }, validate: function( input ) { input.value = input.value.strip(); if( (input.value == input.title) || (input.value == '') ) { this.showMessage( this.msg_empty + input.title ); return false; } if( input.name.indexOf('mail') != -1 ) { if( !this.checkMail(input) ) { this.showMessage( this.msg_mismatch + input.title ); return false; } } if( input.name.indexOf('message') != -1 && input.value.length < this.min_length ) { this.showMessage( this.msg_too_short + input.title ); return false; } return true; }, showMessage: function( message ) { if( message == '' || message == undefined ) { if(this.use_alert){ return false; } if( this.status.innerHTML.length > 0 ) { this.status.innerHTML = ''; Element.hide(this.status); } } else { if(this.use_alert) { alert(message); return true; } this.status.innerHTML = message; Element.show(this.status); } return true; }, send: function() { for( var i=0; i'; this.showMessage(this.wait); this.parameters = Form.serialize(this.feedback_form); this.feedback_form.disable(); this.feedback_request = new Ajax.Request( this.feedback_form.action, { method: "post", parameters: this.parameters+'&ajax', onComplete: this.reply.bind(this) } ); return true; }, reply: function( request ) { this.feedback_form.enable(); if( request.responseText == 'NOT_SENT' ) { this.showMessage(this.msg_not_sent); } else { this.showMessage(this.msg_sent); this.reset(); } return true; }, reset: function() { this.text_fields.each( function(field) { field.value = field.title; } ); } };