/**
 * Post comment
 */
var PostComment = new Class({

	form: {},
	author: {},
	email: {},
	url: {},
	comment: {},

	initialize: function() {

		this.form = $('commentform');

		if (!this.form) return;

		this.url = this.form.getElement('input#url');
		this.author = this.form.getElement('input#author');
		this.email = this.form.getElement('input#email');
		this.comment = this.form.getElement('textarea#comment');

		this.form.addEvent('submit', function(e) {
			if (!this.validate()) {
				e.stop();
				return false;
			}
			return true;
		}.bind(this));

		this.form.getElements('input[type=text], textarea').each(function(item, index) {
			item.addEvents({
				'focus': function() {
					if (this.hasClass('highlight-error')) return;
					this.addClass('highlight-focus');
				},
				'blur': function() {
					if (this.hasClass('highlight-error')) return;
					this.removeClass('highlight-focus');
				}
			});
		});
	},

	notifyField: function(el, msg) {
		el.addClass('highlight-error');
		el.focus();
		alert(msg);
	},

	validate: function() {
		if (this.author && this.author.value.trim().length == 0) {
			this.notifyField(this.author, _("Name must be filled"));
			return false;
		}
		if (this.email && (this.email.value.length == 0 || !this.email.value.test(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/))) {
			this.notifyField(this.email, _("Email must be filled and be a valid email address"));
			return false;
		}
		if (this.comment.value.trim().length == 0) {
			this.notifyField(this.comment, _("Comment must be filled"));
			return false;
		}
		return true;
	}
});

var postComment = {};
window.addEvent('domready', function() {
	postComment = new PostComment();
});
