// JavaScript Document
//captura o codigo da tecla pressionada 
function Mascara(evt, objeto, mask) {
     
    var LetrasU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var LetrasL = 'abcdefghijklmnopqrstuvwxyz';
    var Letras  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    var Numeros = '0123456789';
    var Fixos  = '().-:/ '; 
    var Charset = " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_/`abcdefghijklmnopqrstuvwxyz{|}~";

    evt = (evt) ? evt : (window.event) ? window.event : "";
    var value = objeto.value;
    if (evt) {
         var ntecla = (evt.which) ? evt.which : evt.keyCode;
         tecla = Charset.substr(ntecla - 32, 1);
         if (ntecla < 32) return true;

         var tamanho = value.length;
         if (tamanho >= mask.length) return false;

         var pos = mask.substr(tamanho,1); 
         while (Fixos.indexOf(pos) != -1) {
              value += pos;
              tamanho = value.length;
              if (tamanho >= mask.length) return false;
              pos = mask.substr(tamanho,1);
         }

         switch (pos) {
               case '#' : if (Numeros.indexOf(tecla) == -1) return false; break;
               case 'A' : if (LetrasU.indexOf(tecla) == -1) return false; break;
               case 'a' : if (LetrasL.indexOf(tecla) == -1) return false; break;
               case 'Z' : if (Letras.indexOf(tecla) == -1) return false; break;
               case '*' : objeto.value = value; return true; break;
               default : return false; break;
         }
    }
    objeto.value = value; 
    return true;
}

var wd
function parseelement(thisone)
  {    
   wd="w"
   var tempnum=thisone.value
   for (i=0;i<tempnum.length;i++)
   {
    if (tempnum.charAt(i)==",")
    {
     wd="d"
     break
    }
   }
   if (wd=="w")
    thisone.value=tempnum+",00"
   else
   {
    if (tempnum.charAt(tempnum.length-2)==",")
    {
     thisone.value=tempnum+"0"
    }
    else if (tempnum.charAt(tempnum.length-1)==",")
    {
     thisone.value=tempnum+"00"
    }
   }
}


var codTecla;

document.onkeyup=handleKeyboardAction;
function handleKeyboardAction(e){
	if (!e) var e = window.event; 
	codTecla = e.keyCode;
}

function verificaCodigo(){
	var retorno = false;
	if (codTecla==37 || codTecla==38 || codTecla==39 || codTecla==40 ){
		retorno = true;
	}
	return retorno;
}

//o formato eh esse: 11/11/1111
function mascaraData(campo){
	if(verificaCodigo())
		return;
	var reg = /\D/g;
	numero = campo.value.replace(reg,"");	
	retorno = "";
	for(cont=0;cont<numero.length;cont++){
		switch(cont){			
			case 2:				
				if(numero.charAt(cont)!='/'){
					retorno += '/';
					retorno += numero.charAt(cont);					
				}
				else
					retorno += numero.charAt(cont);
			break;
			case 4:				
				if(numero.charAt(cont)!='/'){
					retorno += '/';
					retorno += numero.charAt(cont);					
				}
				else
					retorno += numero.charAt(cont);
			break;
			default:
				retorno += numero.charAt(cont);
			break;
		}
	}
	if(retorno.length > 10){
		campo.value = retorno.substr(0,10);
		return;
	}
	campo.value = retorno;
}

//o formato eh esse: 111.111.111-11
function mascaraCpf(campo){
	if(verificaCodigo())
		return;
	var reg = /\D/g;
	numero = campo.value.replace(reg,"");	
	retorno = "";	
	for(cont=0;cont<numero.length;cont++){
		switch(cont){
			case 3:
				retorno += '.';
				retorno += numero.charAt(cont);	
			break;
			case 6:
				retorno += '.';
				retorno += numero.charAt(cont);	
			break;
			case 9:
				retorno += '-';
				retorno += numero.charAt(cont);	
			break;
			default:
				retorno += numero.charAt(cont);	
			break;
		}
	}	
	if(retorno.length > 14){
		campo.value = retorno.substr(0,11);
		return;
	}	
	campo.value=retorno;
}

//o formato eh esse: 11.111.111-11
function mascaraRg(campo){
	if(verificaCodigo())
		return;
	var reg = /\D/g;
	numero = campo.value.replace(reg,"");	
	retorno = "";	
	
	for(cont=0;cont<numero.length;cont++){
		switch(cont){
			case 2:
				retorno += '.';
				retorno += numero.charAt(cont);	
			break;
			case 5:
				retorno += '.';
				retorno += numero.charAt(cont);	
			break;
			case 8:
				retorno += '-';
				retorno += numero.charAt(cont);	
			break;
			default:
				retorno += numero.charAt(cont);	
			break;
		}
	}	
	if(retorno.length > 13){
		campo.value = retorno.substr(0,13);
		return;
	}	
	campo.value=retorno;
}


//o formato eh esse: 55 71 9999-9999
function mascaraTelPaisDDD(campo){
   if(verificaCodigo())
		return; 
	var reg = /\D/g;
	numero = campo.value.replace(reg,"");	
	retorno = "";	
	
	for(cont=0;cont<numero.length;cont++){
		switch(cont){
			case 2:
				retorno += ' ';
				retorno += numero.charAt(cont);	
			break;			
			case 4:
				retorno += ' ';
				retorno += numero.charAt(cont);	
			break;
			case 8:
				retorno += '-';
				retorno += numero.charAt(cont);	
			break;
			default:
				retorno += numero.charAt(cont);	
			break;
		}
	}	
	if(retorno.length > 15){
		campo.value = retorno.substr(0,15);
		return;
	}	
	campo.value=retorno;
}

//o formato eh esse: 55 71 9999-9999
function mascaraTelDDD(campo){
   if(verificaCodigo())
		return; 
	var reg = /\D/g;
	numero = campo.value.replace(reg,"");	
	retorno = "";	
	
	for(cont=0;cont<numero.length;cont++){
		switch(cont){
			case 2:
				retorno += ' ';
				retorno += numero.charAt(cont);	
			break;
			case 6:
				retorno += ' ';
				retorno += numero.charAt(cont);	
			break;			
			default:
				retorno += numero.charAt(cont);	
			break;
		}
	}	
	if(retorno.length > 12){
		campo.value = retorno.substr(0,12);
		return;
	}	
	campo.value=retorno;
}

function casSql(valor){
	var reg = /\D/g;
	numero = campo.value.replace(reg,"");
}

function mascaraCep(e,src,mask) {
    if(window.event){ 
        _TXT = e.keyCode; 
    } 
    else if(e.which) { 
        _TXT = e.which; 
    }
    if(_TXT > 47 && _TXT < 58){ 
         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); 
         } 
         return true; 
    } else { 
        if (_TXT != 8){ 
            return false; 
        } 
        else{ 
            return true; 
        }
    }
}

