vFunctions = {
	//el campo no este vacio
	required: function(val, t){
		return (val != '')
	},
	//sea un email valido
	email: function(val, t){
		var r = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		return val.match(r);
	},
	//longitud minima de l
	minLength: function(val, l){
		return (val.length >= l);
	},
	//igual al campo #id2
	equalTo: function(val, id2){
		return (val == $(id2).val());
	},
	//diferente a x
	different: function(val, x){
		return (val != x);
	}
}


//(function($){

	//opciones por defecto
	$.fn.vDefaultOptions = {
		errorClass: 'form-error',
		errorMessage: 'Por favor rellene este campo.',
		fields: {},
		messages: {}	
	}
	
	//objeto para ir guardando las opciones de los formularios
	$.fn.vOptions = {};

	//funcion main
	$.fn.vForm = function(vOptions){
	
		return this.each(function(){
		
			//cogemos todas las opciones
			$.fn.vOptions[this.id] = $.extend($.fn.vDefaultOptions, vOptions);		
			
			//añadimos required a los required
			$('div.required input', this).each(function(){
				var id_form = $(this).parents('form').attr('id');
				if(!$.fn.vOptions[id_form].fields[this.id]){
					//alert('lo a�adimos');
					$.fn.vOptions[id_form].fields[this.id] = {};
				}
				$.extend($.fn.vOptions[id_form].fields[this.id], {required: true});				
			});
			
			//bindeamos el formulario 
			$(this)
			.submit(function(e){ return $(this).vSubmit(); })			
			.one('submit', function(e){
				//e.preventDefault();
				//y focus y blur despues del submit
				for(i in $.fn.vOptions[this.id].fields){
					//alert(i);
					$('#'+i)
					.blur(function(){
						$(this).removeVError().vField();
					})
					.focus(function(){
						$(this).removeVError();
					});
				}
				//return true;
			});		
		});
		
	}	

	//funciones auxiliares
	$.fn.addVError = function(msg){
		return this.each(function(){
			$(this)
				.addClass('form-error')
				.after('<p class="error-message">'+msg+'</p>');
		});
	};
	$.fn.removeVError = function(){
		return this.each(function(){
			$this = $(this);
			$this.removeClass('form-error')
			$('.error-message', $this.parent().get()).remove(); // �?
		});
	};
	
	//para cada campo
	$.fn.vField = function(){
		var ok = true;
		var $this = $(this.get());		
		var id_field = $this.attr('id');
		var id_form = $this.parents('form').attr('id');
		//alert(id_form+' - '+id_field);
		fOptions = $.fn.vOptions[id_form];
		for(rule in fOptions.fields[id_field]){
			var value = fOptions.fields[id_field][rule];
			//alert(id_field+' - '+rule+' - '+value);
			if(vFunctions[rule]($this.val(), value)){
				//todo guay
				//$('#'+id_field).removeVError()
			}else{
				ok = false;
				try{
					msg = fOptions.messages[id_field][rule];
				}catch(e){
					msg = fOptions.errorMessage;
				}
				if(msg == undefined) msg = fOptions.errorMessage;		
				$('#'+id_field).addVError(msg);
			}
		}
		return ok;
	}

	//el main
	$.fn.vSubmit = function(){
	
		var ok = true;
		var $this = $(this.get());	
		var id_form = $this.attr('id');
		
		//miramos los de options
		var fields = $.fn.vOptions[id_form].fields;
		if(fields){
			for(i in fields) {
				//alert('validamos: '+i);
				ok &= $('#'+i).removeVError().vField();
			}		
		}else{
			alert('no hay validation!!');
		}
		//alert(ok);
		return (ok == true);		
	}	

//})(jQuery);
