// JavaScript Document
// Verifica se o obj foi digitado corretamente
function verificaDia(obj) 
{
	dia = document.getElementById(obj);
	valor = dia.value;
	tam = dia.value.length;
	re = /[a-z]/gi
	resultado = re.exec(valor);
	resultadonum = valor.split(re);
     
	if (dia.value != "")
	{
		if(resultado || tam != "2" || resultadonum > 31 || resultadonum == 00)
	 	{
		   alert ("Digite o dia corretamente");
		   dia.focus();
		   return false;
     	}	 
	}
	 
	//document.formenvio.dia.select();
	//document.formenvio.dia.value="";
}

// Verifica se o Mes foi digitado corretamente
function verificaMes(obj) 
{
	
	mes = document.getElementById(obj);
	valor = mes.value;
	tam = mes.value.length;
	re = /[a-z]/gi
	resultado = re.exec(valor);
	resultadonum = valor.split(re);
     
	if (mes.value != "")
	{
	 	if(resultado || tam != "2" || resultadonum > 12 || resultadonum == 00)
		{
		   alert ("Digite o mês corretamente");
		   mes.focus();
		   return false;
		}
	}
	 
    //document.formenvio.mes.select();
    //document.formenvio.mes.value="";s
}

// Verifica se o Ano foi digitado corretamente
function verificaAno(objD,objM,objA) 
{
	pDia  = document.getElementById(objD);
	pMes = document.getElementById(objM);
	pAno = document.getElementById(objA);
	
	valor = pAno.value;
	tam = pAno.value.length;
	re = /[a-z]/gi
	resultado = re.exec(valor);
	resultadonum = valor.split(re);
     
	if (pAno.value != "")
	{
	 
		if(resultado || tam != "4" || resultadonum < 1900 || resultadonum > 9999) 
		{
		   alert ("Digite o ano corretamente");
		   pAno.focus();
		   return false;
	    }
		 
		else
		{
		   /*dia=pDia.value;
		   mes=pMes.value;
		   ano=pAno.value;*/
		   checkdate(pDia,pMes,pAno);
		   return true;
		}
	}
}

// Verifica se a data completa é válida
function checkdate(pDia,pMes,pAno)
{
	if (chkdate(pDia,pMes,pAno) == false) 
	{
	  alert("A data digitada é inválida. Digite novamente");
	  pDia.focus();
	  return false;
	}
	
	else
		return true;
    
}

// Verifica se a data completa é válida
function chkdate(pDia,pMes,pAno)
{
	
	var intDay = parseInt(pDia, 10);
	var intMonth = parseInt(pMes, 10);
	var intYear = parseInt(pAno, 10);

	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && 
		 (intDay > 31 || intDay < 1)) 
	{
  		return false;
	}
	
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intDay < 1)) 
	{
  		return false;
	}

	if (intMonth == 2)
	{
		if (intDay < 1)
		{
   			return false;
  		}
  		
		if (LeapYear(intYear) == true) 
		{
   			if (intDay > 29) 
			{
    			return false;
   			}
  		}
  		
		else 
		{
   			if (intDay > 28) 
			{
    			return false;
   			}
  		}
	}
	
	return true;
}

// Verifica se o ano é bissexto
function LeapYear(intYear) 
{
	if (intYear % 100 == 0) 
	{
	  if (intYear % 400 == 0) { return true; }
	} 
	
	else 
	{
	  if ((intYear % 4) == 0) { return true; }
	}
	
	return false;
}
