var fieldValidator = new Class({
	
	Implements: [Options, Events],
	
	options : {},
	
	initialize : function(form, options) {
		this.setOptions(options);
		this.form = $(form);
		this.fields = [];
		
		this.form.getElements("*[class*=input]").each(function(el) {
			this.register(el);
		}, this);
	},
	
	register : function(el){
		el.validation = [];
		el.getProperty("class").split(' ').each(function(classX) {
			if(classX.match(/^input(\[.+\])$/)) {
				var validators = eval(classX.match(/^input(\[.+\])$/)[1]);
				validators.each(function(validate){
					el.validation.push(validate);
					//addEvents
					//	integer
					if(validate == 'integer')
					{
						el.addEvent('keypress', function(event){
							var key = event.code;
							/*if(event.control)
							{
								//	Allow paste
								if(event.code == 118)
									return true;
							}*/
							// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57	
							return (key <= 13 || (key >= 48 && key <= 57));
						});
					}
					else 
					{
						if(validate == 'float')
						{
							
							el.addEvent('keypress', function(evt){
								var key = evt.key;
								
								var keyCode = evt.code;
								if(keyCode == 46)
								{
									if(countChar(el.value, '.') > 0)
										return false;
								}
								return (keyCode <= 13 || (keyCode >= 48 && keyCode <= 57) || (keyCode == 46));
							});
							
						}
					}
					
				}.bind(fieldValidator));
				//validators.each(function(value){alert(value);});
			}
		});
	}
});

function validateEmail()
{
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	str = $('email').value;
	if(str.match(emailRegEx))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function countChar(text, character)
{
	var counter = 0
	for(var i = 0; i < text.length; i++)
	{
		if(text.substr(i, 1) == character)
		{
			counter++;
		}
	}
	return counter;
}
