
function trim(strText)
{
    while (strText.substring(0,1) == ' ') // Quita los espacios del lado izquierdo
	    strText = strText.substring(1, strText.length);
    while (strText.substring(strText.length-1,strText.length) == ' ') // Quita los espacios del lado derecho
	    strText = strText.substring(0, strText.length-1);
	return strText;
}

function ValidaCampo(Objeto,Nombre,TipoValidacion,Requerido,Min)
{
	ValidaMensaje = 0
	var ObjetoValor = trim(Objeto.value);

	if (parseInt(ObjetoValor.length,10) < parseInt(Min,10) && parseInt(ObjetoValor.length,10) > 0) // Valida longitud del texto
	{ 
		alert("El numero minimo de caracteres permitido para el dato " + Nombre + " es de " + Min);
		Objeto.focus();
		return (false);
	}

	if(TipoValidacion.toUpperCase() == "TEXTO"){ // Valida solo Texto
		TipoValidacion = "Texto";
		ControlString="ÄËÏÖÜäëïöüÑñáéíóúÁÉÍÓÚABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,;:<>°¬+-# ()\t\r\n\f/|!#$%&/()=?¡¿*[]{}@¨~^";
	}

	else if(TipoValidacion.toUpperCase() == "NUMEROS"){ // Valida solo Números
		TipoValidacion = "Numeros enteros";ControlString="0123456789";
	}
	
	else if(TipoValidacion.toUpperCase() == "DECIMAL"){ // Valida solo Decimales
		TipoValidacion = "Decimales";ControlString="0123456789.";
	}
	
	else if(TipoValidacion.toUpperCase() == "ALFA"){ // Valida Alfanumérico
		TipoValidacion = "Texto y Numeros";ControlString="ÄËÏÖÜäëïöüÑñáéíóúÁÉÍÓÚABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,;:<>°¬+-_# ()\t\r\n\f/|!#$%&/()=?¡¿*[]{}@¨~^";
	}
	
	else if(TipoValidacion.toUpperCase() == "SERIE"){ // Valida Número de Serie
		TipoValidacion = "Texto y Numeros";ControlString="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	}
	
	else if(TipoValidacion.toUpperCase() == "EMAIL"){ // Valida EMAIL
		  ControlString = /^(([a0-zA9-Z]{1,5}.+(\[?)|[0-9]{1,3}.+(\[]))+\@[a-zA-Z0-9]+\.([a-zA-Z]{1,5}.+(\[?)|[0-9]{1,3}.+(\[]))(\]?))*$/;ValidaMensaje = 1;
	}
	
	else if(TipoValidacion.toUpperCase() == "DINERO"){ // Valida Cantidad Monetaria
		ControlString="0123456789.,$";
	}
	
	if(Requerido == 1){
		if(Objeto.options)
		{	
			if (Objeto.options[Objeto.selectedIndex].value == -1)
			{
				alert("Por favor seleccione una de las opciones del dato "+ Nombre + ".");
				Objeto.focus();return (false);
			}
		}
		else
		{	
			if (trim(Objeto.value) == "")
			{ 
				alert("Por favor el dato " + Nombre + " es requerido.");
					if (Objeto.type!='hidden')
					{	
						Objeto.focus();
						Objeto.select();
					}
				return (false);
			}
		}
		//return true;
	}
	
	if (ValidaMensaje == 1)
	{
		if (ControlString.test(Objeto.value)) 
			return true;
		alert("El valor capturado para el dato " + Nombre + " es invaido.");
	}
	else
	{
		var CadenaValida = ObjetoValor, CaracterValido = true;
		for(i=0;i<parseInt(CadenaValida.length);i++)
		{
			CaracterValido = false;
			for (j=0;j<ControlString.length;j++)
			{
				if (CadenaValida.charAt(i) ==  ControlString.charAt(j))
					CaracterValido = true;
			}
			if (!CaracterValido)
				break;
		}
		if (!CaracterValido)
			alert("Favor de teclear solo " + TipoValidacion + " en el campo " + Nombre);
		else
			return true;
	}
	if (Objeto.type!='hidden')
	{	
		Objeto.focus();
		Objeto.select();
	}
	
	return false;
}