s.messages = function(selector){
	this.forms = $$(selector);
}

s.messages.prototype.append = function() {
	if (this.forms.length <= 0 ) return false;
	this.forms.forEach(function(form){
		y.util.Event.addListener(form, "submit", function(event){
			y.util.Event.preventDefault(event);
			y.util.Connect.setForm(form);
			y.util.Connect.asyncRequest('POST', form.action, this.callback(this,form));
		},this,true);
	},this,true);
}

s.messages.prototype.callback = function(obj,form){
	return {
		'customevents':{
			'onStart': function(eventType, args) {
				s.notifications.show(dict['sending'],'wait');
			},
			'onSuccess': function(eventType, args) {
				try {
					response = y.lang.JSON.parse(args[0].responseText);
					if (response.status == 1) {
						obj.clear_form(form);
					}
					s.notifications.show(response.message,'info');
				}
				catch (error) {
					s.notifications.show(dict['ajax_error'] + ': '+error,'error');
				}
			},
			'onFailure': function(eventType, args) {
				s.notifications.show(dict['ajax_error'] + ': '+error,'error');
			}
		}
	}
}

s.messages.prototype.clear_form = function(form){
	$$('select, input, textarea',form).forEach(function(el){
		el.value = '';
	});
}