/*****  Funções criadas por Rafael B. Lima ******/

/***********

Formatação e Verificação de Datas

- Coloca a barra automática;
- Só perimite digitar números;
- No dia, permite digitar de 01 a 31, no mês de 01 a 12 e no ano de 1000 a 2999;
- Possui validação para verificação dos meses com 30 e 31 dias;
- Possui tratamento exclusivo para o mês de fevereiro

Obs: Ainda não possui validação para ano bissexto

******/

function ajustaData(tecla,campo){
if(navigator.appName == 'Microsoft Internet Explorer'){
	tecla = tecla.keyCode;
	}
else{
    tecla = tecla.charCode;
	}
	
	valorCampo = campo.value;
	tam = valorCampo.length;
if(tecla == 0){
return true;
}
else{
	if(tam==0){ //Dia Dezena
		if((tecla > 47 && tecla < 52)||(tecla == 0))
			return true;
		else
			return false;
	}
	else if(tam==1){ //Dia Unidade
		if((tecla >= 48 && tecla <= 57)||(tecla == 0))
			if((campo.value.substring(0,1) == 3) && (tecla < 48 || tecla > 49))
				return false;
			else if((campo.value.substring(0,1)==0) && (tecla==48))
				return false
			else
				return true;
		else
			return false;
	}
	else if(tam==2){ //Barra A
		if( tecla >= 47 && tecla <= 49 ){
			if(tecla != 47)
				campo.value = campo.value + "/";
			return true;
		}
		else
			return false;
	}
	else if(tam==3){ //Mes Dezena
		if( tecla==48 || tecla==49 )
			return true;
		else
			return false;
	}
	else if(tam==4){ //Mes Unidade
		if( tecla >= 48 && tecla <= 57 )
			if((campo.value.substring(3,4)==1) && (tecla < 48 || tecla > 50))
				return false;
			else if((campo.value.substring(3,4)==0) && (tecla==48))
				return false
			else
				return true;
		else
			return false;
	}
	else if(tam==5){ //Barra B
		if(!verData(campo))
			return false;
		if( (tecla >= 49 && tecla <= 50) || (tecla==47) ){
			if(tecla != 47)
				campo.value = campo.value + "/";
			return true;
		}
		else
			return false;
	}
	else if(tam==6){ //Ano Milhar
		if( tecla >= 49 && tecla <= 50 ){
			return true;
		}
		else
			return false;
	}
	else if(tam >= 7 && tam <= 9){ //Ano Cent, Dez e Uni
		if( tecla >= 48 && tecla <= 57 )
			return true;
		else
			return false;
	}
	else
		return false;
  }//fim else
}

function verData(campo){ //Verif se o mes possui 31 dias e 30 para fevereiro
		var dia = campo.value.substring(0,2);
		var mes = campo.value.substring(3,5);
		
		if(mes==02 && dia>=30){
			alert("O mes de fevereiro nao possui "+dia+" dias!");
			campo.focus();
			return false;
		}

			
		if((mes==02 || mes==04 || mes==06 || mes==09 || mes==11) && (dia>=31)){
			alert("O mes informado nao possui "+dia+" dias!");
			campo.focus();
			return false;
		}

		if(mes>12 || mes<01 || dia>31 || dia<01){
			return false;
		}

		return true;
}
//******* Fim da da função de Formatação e Verificação de Datas*******//



/*************

Filtro de teclas - Somente números 

Permite ao usuário digitar somente números de 0 a 9;
Chamar no input: onKeyPress="return somenteNumeros(event);"

*********/

function somenteNumeros(tecla){

if(navigator.appName == "Microsoft Internet Explorer"){
	tecla = tecla.keyCode;
}else{
	tecla = tecla.charCode;
}

if((tecla >= 48 && tecla <= 57) || (tecla == 0)){
	return true;
}

else{ return false }

}

//******Fim da função Filtro de Teclas ******/

function formatar(src, mask){
  var i = src.value.length;
  var saida = mask.substring(0,1);
  var texto = mask.substring(i)
if (texto.substring(0,1) != saida)
  {
    src.value += texto.substring(0,1);
  }
}

function DataFormat(campo,evt){
	var isN4=(document.layers)?true:false;
	var isIE=(document.all)?true:false;
	var isDOM=(document.getElementById&&!document.all)?true:false;
	var tamanho=document.getElementById(campo.id).value.length;
	if (tamanho<=9){
		alert("A data no formato dd/mm/aaaa");
		document.getElementById(campo.id).value="";
		document.getElementById(campo.id).focus();
		return false;
	}
	//alert(tamanho);
	/*if(isDOM){
		var key=evt.which;
		if((key>94&&key<107)||(key>47&&key<58)||(key==8)){
			if(tamanho==2||tamanho==5){
				if(key==8||key==127){
					document.getElementById(campo.id).value+=''
					alert("errado 5");
				}else{
					document.getElementById(campo.id).value+='/'
					//alert("errado 6");
				}
			}
		}else{
			return false
				alert("errado 1");
		}
	}
	if(isIE){
		if((window.event.keyCode>94&&window.event.keyCode<107)||(window.event.keyCode>47&&window.event.keyCode<58)||(window.event.keyCode==8)){
			if(tamanho==2||tamanho==5){
				if(window.event.keyCode==8||window.event.keyCode==127){
					document.getElementById(campo.id).value+=''
					alert("errado 3");
				}else{
					document.getElementById(campo.id).value+='/'
					alert("errado 4");
				}
			}
		}else{
			return false
			alert("errado 2");
		}
	}*/
}
