
// Funçoes genéricas de validação em JScript

function ValidaTexto(valor) 
	{
	var i;
	
			// Testa se string vazia
	if (valor=='')
		{
		return false;
		}
	else 
		{
			// Testa se string só tem espaços
		for (i=0; i < valor.length; i++) 
			{
			if (valor.substring(i,i+1)!=' ') 	
				return true;
			}
		return false;
		}
	}

function ValidaTamanho(valor, tam)
	{
	if(valor.length > tam)
		{
		return false;
		}
	else
		{
		return true;
		}
	}

function ValidaNumero(valor)
	{
	for(i=0;i<valor.length;i++)
		{
		if(valor.charAt(i)== ',')
			return false;
		}
	return true;
	}
	
function ValidaDigitos (valor)
{	//retorna true se a string so possuir digitos, senao retorna falso
	for(i=0; i<valor.length; i++)
		if(valor.charAt(i) < '0' || valor.charAt(i) > '9') //Testa se e diferente de um digito
			return false;
	return true;
}

function ValidaLetrasMaiusculas (valor)
{	//retorna true se a string so possuir letras, senao retorna falso
	for(i=0; i<valor.length; i++)
		if(valor.charAt(i) < 'A' || valor.charAt(i) > 'Z') //Testa se e diferente de uma letra
			return false;
	return true;
}

function ValidaLetrasMinusculas (valor)
{	//retorna true se a string so possuir letras, senao retorna falso
	for(i=0; i<valor.length; i++)
		if(valor.charAt(i) < 'a' || valor.charAt(i) > 'z') //Testa se e diferente de uma letra
			return false;
	return true;
}

function ValidaData (valor)
{
	var dia, mes, ano, str;
	
	if(valor.length != 10)
		return false;
	else
	{
		if(valor.charAt(2) != '/' || valor.charAt(5) != '/')
			return false;
		else
		{
			str=valor.substring(0,2);
			if(!ValidaDigitos(str))
				return false;
			dia=parseInt(str,10);
			str=valor.substring(3,5);
			if(!ValidaDigitos(str))
				return false;		
			mes=parseInt(str,10);	
			str=valor.substring(6,10);
			if(!ValidaDigitos(str))
				return false;
			ano=parseInt(str,10);
			if(mes == 1 || mes == 3 || mes == 5 || mes == 7 || mes == 8 || mes == 10 || mes == 12)
			{
				if(dia < 1 || dia > 31)
					return false;
			}
			else
			{
				if(mes == 4 || mes == 6 || mes == 9 || mes == 11)
				{
					if(dia < 1 || dia > 30)
						return false;
				}
				else
					if(mes == 2)
					{
						if((ano%4) == 0)
						{
							if(dia < 1 || dia > 29)
								return false;
						}
						else
						{
							if(dia < 1 || dia > 28)
								return false;
						}
					}
					else
						return false;
			}
		}
	}
	return true;
}

function ValidaDataIniFim (DataIni, DataFim)
{
	var AnoIni, AnoFim, MesIni, MesFim, DiaIni, DiaFim;
	
	AnoIni=parseInt(DataIni.substring(6,10),10);
	AnoFim=parseInt(DataFim.substring(6,10),10);
	if(AnoIni > AnoFim)
		return false;
	else
	{
		if(AnoIni == AnoFim)
		{
			MesIni=parseInt(DataIni.substring(3,5),10);
			MesFim=parseInt(DataFim.substring(3,5),10);
			if(MesIni > MesFim)
				return false;
			else
			{
				if(MesIni == MesFim)
				{
					DiaIni=parseInt(DataIni.substring(0,2),10);
					DiaFim=parseInt(DataFim.substring(0,2),10);
					if(DiaIni > DiaFim)
						return false;
				}
			}
		}
	}
	return true;
}

function ValidaRadio(valor)
{	//retorna true se existir um radio selecionado, senao retorna falso
	var ret=false;
	
	for(i=0; i<valor.length&&!ret; i++)
		if(valor[i].checked) // Testa se existe um radio selecionado
			ret=true;
	return ret;
}

function ValidaHora(valor, ComSegundos)
{
	var hora, minuto, segundo, tam;
	
	if(ComSegundos)
		tam=8;
	else
		tam=5;
	if(valor.length != tam)
		return false;
	else
	{
		if(valor.charAt(2) != ':' || (valor.charAt(5) != ':' && ComSegundos))
			return false;
		else
		{	
			str=valor.substring(0,2);
			if(!ValidaDigitos(str))
				return false;
			hora=parseInt(str,10);
			str=valor.substring(3,5);
			if(!ValidaDigitos(str))
				return false;		
			minuto=parseInt(str,10);
			if(ComSegundos)
			{
				str=valor.substring(6,8);
				if(!ValidaDigitos(str))
					return false;
				segundo=parseInt(str,10);
			}
			if(hora < 0 || hora > 24)
				return false;
			else
			{
				if(hora == 24 && (minuto != 0 || (segundo != 0 && ComSegundos)))
					return false;
				if(minuto < 0 || minuto > 59)
					return false;
				else
					if((segundo < 0 || segundo > 59) && ComSegundos)
						return false;	
			}
		}
	}
	return true;
}

function ValidaHoraIniFim (HorarioIni, HorarioFim, ComSegundos)
{
	var HoraIni, HoraFim, MinutoIni, MinutoFim, SegundoIni, SegundoFim;
	
	HoraIni=parseInt(HorarioIni.substring(0,2),10);
	HoraFim=parseInt(HorarioFim.substring(0,2),10);
	MinutoIni=parseInt(HorarioIni.substring(3,5),10);
	MinutoFim=parseInt(HorarioFim.substring(3,5),10);
	SegundoIni=parseInt(HorarioIni.substring(6,8),10);
	SegundoFim=parseInt(HorarioFim.substring(6,8),10);
	if(HoraIni > HoraFim)
		return false;
	else
		if(HoraIni == HoraFim)
		{
			if(MinutoIni > MinutoFim)
				return false;
			else
				if(MinutoIni == MinutoFim)
					if(ComSegundos && SegundoIni > SegundoFim)
						return false;
		}
	return true;
}