var Validation = Class.create();

Validation.prototype = {

	initialize: function() {},
	
	present: function(elementId, opt_args) {
	
		var options = Object.extend({
			displayNotPresent:null
		}, opt_args || {});
		
		if ($F(elementId) == '') {
			if (options.displayNotPresent != null) Element.show(options.displayNotPresent);
			Element.addClassName(elementId, 'error');
			return false;
		}
		else {
			if (options.displayNotPresent != null) Element.hide(options.displayNotPresent);
			Element.removeClassName(elementId, 'error');
			return true;
		}
		return true;
	},
	
	equals: function(elementId, element2Id, opt_args) {
		
		var options = Object.extend({
			displayNotPresent:null,
			displayNotValid:null
		}, opt_args || {});
				
		var err = 0;
		
		if (! this.present(elementId, opt_args)) err += 1;
		
		if ($F(elementId) != $F(element2ID)) {
			if (options.displayNotValid != null) Element.show(options.displayNotValid);
			Element.addClassName(elementId, 'error');
			err += 1;
		}
		else {
			if (options.displayNotValid != null) Element.hide(options.displayNotValid);
			Element.removeClassName(elementId, 'error');
		}
		
		return (err == 0);
	
	},
	
	withFilter: function (elementId, filter, opt_args) {
		
		var options = Object.extend({
			displayNotPresent:null,
			displayNotValid:null, 
			validatePresent:true
		}, opt_args || {});
				
		var err = 0;
		
		if (filter == "") return false;
		
		if (options.validatePresent) if (!this.present(elementId, opt_args)) err += 1;
		
		if (!options.validatePresent && $F(elementId)=="") return true;
		
		if (!filter.test($F(elementId))) {
			if (options.displayNotValid != null) Element.show(options.displayNotValid);
			Element.addClassName(elementId, 'error');
			err += 1;
		}
		else {
			if (options.displayNotValid != null) Element.hide(options.displayNotValid);
			Element.removeClassName(elementId, 'error');
		}
		
		return (err == 0);
	},
	
	email: function (elementId, opt_args) {
		
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
		return this.withFilter(elementId, filter, opt_args);
	},
	
	alphanum: function (elementId, opt_args) {
		
		var filter = /^([a-zA-Z0-9_])+$/;	
		
		return this.withFilter(elementId, filter, opt_args);
	},
	
	num: function (elementId, opt_args) {
		
		var filter = /^([0-9])+$/;	
		
		return this.withFilter(elementId, filter, opt_args);
	},
	
	alpha: function (elementId, opt_args) {
		
		var filter = /^([a-zA-Z_])+$/;	
		
		return this.withFilter(elementId, filter, opt_args);
	},
	
	url: function (elementId, opt_args) {
		
		var filter = /^(?:(?:ftp|https?):\/\/)?(?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)+(?:com|edu|biz|org|gov|int|info|mil|net|name|museum|coop|aero|[a-z][a-z])\b(?:\d+)?(?:\/[^;"'<>()\[\]{}\s\x7f-\xff]*(?:[.,?]+[^;"'<>()\[\]{}\s\x7f-\xff]+)*)?/;
	
		return this.withFilter(elementId, filter, opt_args);
	},
	
	date: function (elementId, opt_args) {
		
		var filter = /^(0[1-9]|[12][0-9]|3[01])[-](0[1-9]|1[012])[-](19|20)\d\d+/;
		
		return this.withFilter(elementId, filter, opt_args);	
	
	},

	optionPresent: function (elementId, opt_args) {
		
		var options = Object.extend({
			displayNotPresent:null
		}, opt_args || {});
		
		if ($(elementId).length == 0) {
			if (options.displayNotPresent != null) Element.show(options.displayNotPresent);
			Element.addClassName(elementId, 'error');
			return false;
		}
		else {
			if (options.displayNotPresent != null) Element.hide(options.displayNotPresent);
			Element.removeClassName(elementId, 'error');
			return true;
		}
		return true;
	},
	
	optionSelected: function (elementId, opt_args) {
		
		var options = Object.extend({
			displayNotPresent:null
		}, opt_args || {});
		
		if ($(elementId).selectedIndex == -1) {
			if (options.displayNotPresent != null) Element.show(options.displayNotPresent);
			Element.addClassName(elementId, 'error');
			return false;
		}
		else {
			if (options.displayNotPresent != null) Element.hide(options.displayNotPresent);
			Element.removeClassName(elementId, 'error');
			return true;
		}
		return true;
	}
	
};