function ValidaData(campo)
{
	if( campo != '' )
	{
		var strData = campo.value;
		var pos = strData.indexOf("/",0);
		if (pos < 0){
			return false
		}
		dia = strData.slice(0,pos);

		pos2 = strData.indexOf("/",pos+1);
		if (pos < 0){
			return false
		}
		mes = strData.slice(pos+1,pos2);
	
		ano = strData.substr(pos2+1);

		dia= "00" + dia;
		dia = dia.substring(dia.length-2,dia.length);
   		mes= "00" + mes;
		mes = mes.substring(mes.length-2,mes.length);

		if (strData.value != '' && strData.length != 10) {
			return false;
		}
			
		if (isNaN(dia) || isNaN(mes) || isNaN(ano)) {
			return false;
		}
	
		if (ano.length < 4){
			return false;
		}

		if (mes < 1 || mes > 12) {
			return false;
		}

		if (dia < 1 || dia > 31) {
			return false;
		}

		if ((mes==4 || mes==6 || mes==9 || mes==11) && dia==31) {
			return false;
		}

		if (strData.indexOf("_") != -1) {
			return false;
		}

		if (mes == 2) {
			var isleap = (ano % 4 == 0 && (ano % 100 != 0 || ano % 400 == 0))
			if (dia>29 || (dia==29 && !isleap)) {
				return false;
			}
		}
	}
	return true;
}


function mascaraData(e, campo) 
{
	var cod='';
	
	if(document.all) {
      cod = e.keyCode;
   } else {
      cod = e.which;
   }

	if(cod == 8 || cod == 0) {
      return; //backspace or tab
   }

	if((cod < 48 || cod > 57) && cod != 47){
	  cod=0;
	  campo.focus();
	}
	
	tam = campo.value.length;

   if(tam > 9) return false;

	var caract = String.fromCharCode(cod);

	if((tam == 2 || tam == 5) && cod != 47) {
      campo.value += '/';
      return true;
   }

	if((tam == 2 || tam == 5) && cod == 47) return true;
	if((tam != 2 || tam != 5) && cod == 47) return false;

   return;
}


function DataMaior(dataini, datafim)
{   
   var Dt_inicial = dataini.value.substr(6,4) + dataini.value.substr(3,2) + dataini.value.substr(0,2);
   var Dt_final   = datafim.value.substr(6,4) + datafim.value.substr(3,2) + datafim.value.substr(0,2);
   if (Dt_inicial > Dt_final) {
      return (true);
    }  
   else {
      return (false);
     } 
}


function DataMenor(dataini, datafim)
{   
   var Dt_inicial = dataini.value.substr(6,4) + dataini.value.substr(3,2) + dataini.value.substr(0,2);
   var Dt_final   = datafim.value.substr(6,4) + datafim.value.substr(3,2) + datafim.value.substr(0,2);
   if (Dt_inicial < Dt_final) {
      return (true);
    }  
   else {
      return (false);
     } 
}

function DataIgual(dataini, datafim)
{
  if (!DataMaior(dataini, datafim) && !DataMenor(dataini, datafim)) {
      return (true);
    }  
   else {
      return (false);
     }   
}

function GetDia(Data)
{  
   return (Data.value.substr(0,2));
}


function GetMes(strData)
{
   return (strData.value.substr(3,2));
}


function GetAno(strData)
{  
   return (strData.value.substr(6,4));
}


function DataInversa(strData)
{
    var aaaa = "0000";
    var mm = "00";
    var dd = "00";

    if (strData.value.length == 10){
        aaaa = GetAno(strData);
        mm   = GetMes(strData);
        dd   = GetDia(strData);
    }
    return (aaaa + mm + dd);
}

function comparaMenorIgualHoje(data)
{
	
    var dia = parseInt(data.substr(0,2),10);
    var mes = parseInt(data.substr(3,2),10);
    var ano = parseInt(data.substr(6,4),10);

    var hoje = new Date();
    var diahoje = parseInt(hoje.getDate());
    var meshoje = parseInt(hoje.getMonth()+1);
    var anohoje = parseInt(hoje.getYear());
    
    if(navigator.appName == "Netscape"){    	
    	anohoje = anohoje + 1900;
	}
    
    if( ano > anohoje ) return false;
    else {
	    if( ano < anohoje ) return true;
	    else { //ano igual
		    if( mes > meshoje ) return false;
		    if( mes < meshoje ) return true;
			if( dia > diahoje ) return false;
		    if( dia < diahoje ) return true;
		    if( mes == meshoje ) return true;
			if( dia == diahoje ) return true;	    	    
	    }
    }
    return false;
}

// compara se ano (valor) recebido é maior que o ano atual
function comparaAnoMaior(ano){
     
     var hoje = new Date(); 
     var anohoje = parseInt(hoje.getYear());	 	
      
     if(navigator.appName == "Netscape"){    	
        anohoje = anohoje + 1900;
	 }
	
	 if( ano > anohoje ) return false;
	 
	 return true;
}

function comparaMenorHoje(data)
{
    var dia = parseInt(data.substr(0,2),10);
    var mes = parseInt(data.substr(3,2),10);
    var ano = parseInt(data.substr(6,4),10);

    var hoje = new Date();
    
    var diahoje = parseInt(hoje.getDate());
    var meshoje = parseInt(hoje.getMonth()+1);
    var anohoje = parseInt(hoje.getYear());
    
    if(navigator.appName == "Netscape"){    	
    	anohoje = anohoje + 1900;
	}
    
    if( ano > anohoje ) return false;
    else {
	    if( ano < anohoje ) return true;
	    else { //ano igual
		    if( mes > meshoje ) return false;
		    if( mes < meshoje ) return true;
			if( dia > diahoje ) return false;
		    if( dia < diahoje ) return true;	    	    
	    }
    }
    return false;
}
