var Validador = function()
{
	var Validacion = function()
	{
		var nombreCampo;
		var funcionValidacion;
		var parametros;
		var mensajeError;
	};
	
	var formulario = arguments[0];
	var listaValidaciones = [];
	
	this.AgregarValidacion = function()
	{
		var nuevaValidacion = new Validacion();
		nuevaValidacion.nombreCampo = arguments[0];
		nuevaValidacion.funcionValidacion = arguments[1];
		nuevaValidacion.mensajeError = arguments[arguments.length - 1];
		
		var parametros = [];
		parametros.push(formulario[nuevaValidacion.nombreCampo]);
		
		if(arguments.length != 3)
		{
			for(var i=0;i<arguments.length - 3;i++)
			{
				parametros.push(arguments[2+i]);
			}
		}
		
		nuevaValidacion.parametros = parametros;
		
		listaValidaciones.push(nuevaValidacion);
	}
	
	this.Add = this.AgregarValidacion;
	
	this.Comprobar = function()
	{
		var validacion,funcion;
		for(var i=0;i<listaValidaciones.length;i++)
		{
			validacion = listaValidaciones[i];
					
			if(!validacion.funcionValidacion.apply(null,validacion.parametros))
			{
				alert(validacion.mensajeError);
				formulario[validacion.nombreCampo].select();
				formulario[validacion.nombreCampo].focus();
				return(false);
			}
		}
		
		return(true);
	}
	
	this.Existe = function(campo)
	{
		if(campo.value == "")
			return(false);
		else
			return(true);	
	}
	
	this.Entero = function(campo)
	{
		if(campo.value.match(/^\d+$/))	
			return(true);
		else
			return(false);
			
	}
	
	this.Coincide = function(campo,cadenaRegExp)
	{
		var expresionRegular = new RegExp(cadenaRegExp);
		
		if(campo.value.match(expresionRegular))
			return(true);
		else
			return(false);	
	}
	
	this.DNI = function(campo)
	{
		if(campo.value.match(/^\d{8}-{0,1}[A-Za-z]{1}$/))
			return(true);
		else
			return(false);
	}
	
	this.CIF = function(campo)
	{
		if(campo.value.match(/^.{10}$/))
			return(true);
		else
			return(false);
	}
	
	this.CodigoPostal = function(campo)
	{
		if(campo.value.match(/^\d{5}$/))
			return(true);
		else
			return(false);
	}
	
	this.Fecha = function(campo)
	{
		if(campo.value.match(/^\d{1,2}\/\d{1,2}\/\d{4}$/))
			return(true);
		else
			return(false);
	}
	
	this.Telefono = function(campo)
	{
		if(campo.value.match(/^\d{8,9}$/))
			return(true);
		else
			return(false);
	}
	
	this.Email = function(campo)
	{
		if(campo.value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/))
			return(true);
		else
			return(false);
	}
	
};