// a set of validation rules for data parsing
var DataValidation = Class.create();
DataValidation.prototype = {
  	initialize: function(elem) {
		this.elem = elem;
		this.value = this.elem.value;
	},
	
	isFilled: function() {
		if (this.elem.type == 'checkbox')
			return this.elem.checked;
		else
			return this.value != '';
	},
	
	isEmail: function() {
		var reEmail = /^([\w-]+\.)*[\w-]+\@([\w-]+\.)+[a-zA-Z]{2,4}$/;
		return reEmail.test(this.value) || !this.value;
	},
	
	isInteger: function() {
		var reInteger = /^\d+$/;
		return reInteger.test(this.value) || !this.value;
	},
	
	isFloat: function() {
		var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
		return reFloat.test(this.value) || !this.value;
	},
	
	isAjaxValid: function(opt) {
		if (opt.url == null || !this.value) return true;
		target = opt.target != null ? $(opt.target) : null;
			
		var ret = true;
		new Ajax.Request(opt.url + this.value, {
			asynchronous: false,
			onComplete: function(t) {
				ret = (t.responseText != 0);
				if (target != null)
					target.value = t.responseText;
			}
		});
		return ret || !this.value;
	},
	
	isAjaxValidAsync: function(opt) {
		if (opt.url == null || !this.value) return true;
		target = opt.target != null ? $(opt.target) : null;
		if (opt.form == null) return true;
		if (opt.elem == null) return true;
		new Ajax.Request(opt.url + this.value, {
			asynchronous: true,
			onComplete: function(t) {
				ret = (t.responseText != 0);
				opt.form.markField(opt.elem, ret);
				if (target != null)
					target.value = t.responseText;
			}
		});
	},
	
	isLengthMin: function(opt) {
		if (opt.length_min == null) return true;
		
		l = parseInt(opt.length_min, 10);
		return (this.value.length >= l) || !this.value;
	},
	
	isEqual: function(opt) {
		if (opt.reference == null) return true;
		if ($(opt.reference) == null) return true;
		value = $(opt.reference).value;
		return (this.value == value) || (!this.value && !value);
	},
	
	isUrl: function() {
		var reUrl = /http\:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?/;
		return reUrl.test(this.value) || !this.value;
	}
}