// a set of validation rules for data parsing
var DataValidator = new Class({

	re: {
		email: /^[a-zA-Z0-9\._-]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/,
		int:   /^\d+$/,
		float: /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/,
		url:   /http\:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?/,
		date:  /^\d{2}\.\d{2}\.\d{4}$/
	},

	check: function(elem, rule, options) {
		this.elem = elem;
		this.options = options;
		
		var isOk = false;
		try { eval('var isOk = this.is' + rule + '();'); }
		catch(e) {}
		return isOk;
	},
	
	isFilled: function() {
		return this.elem.type == 'checkbox' ? this.elem.checked : this.elem.value != '';
	},
	
	isEmail: function() {
		return !this.elem.value || this.re.email.test(this.elem.value);
	},
	
	isInteger: function() {
		return !this.elem.value || this.re.int.test(this.elem.value);
	},
	
	isFloat: function() {
		return !this.elem.value || this.re.float.test(this.elem.value);
	},
	
	isLengthMin: function(opt) {
		return !this.elem.value || this.elem.value.length >= this.options.toInt();
	},
	
	isLengthMax: function(opt) {
		return !this.elem.value || this.elem.value.length <= this.options.toInt();
	},
	
	isEqual: function(opt) {
		if (this.options == null || $(this.options) == null) return true;
		return this.elem.value == $(this.options).value;
	},
	
	isUrl: function() {
		return !this.elem.value || this.re.url.test(this.elem.value);
	},
	
	isBigger: function(opt) {
		if (this.options == null) return false;
		return !this.elem.value || this.elem.value.toFloat() > this.options.toFloat();
	},
	
	isMin: function(opt) {
		if (this.options == null) return false;
		return !this.elem.value || this.elem.value.toInt() >= this.options.toInt();
	},
	
	isMax: function(opt) {
		if (this.options == null) return false;
		return !this.elem.value || this.elem.value.toInt() <= this.options.toInt();
	},
	
	isChecked: function() {
		if (typeof(this.elem.form.elements[this.elem.name].length) == 'undefined')
			return this.elem.checked;
		else {
			var inputs = this.elem.form.elements[this.elem.name];
			var no = 0;
			for (var i = 0; i <= inputs.length; i++)
				if (typeof(inputs[i]) != 'undefined' && inputs[i].checked) no++;
			return no == this.options;
		}
	},
	
	isDate: function() {
		return !this.elem.value || this.re.date.test(this.elem.value);
	}
	
})