// MÁSCARA TELEFONE
/********************************************************************************/
function FormataCNPJ(Campo, teclapres)
{

   if(window.event)
   {
    var tecla = teclapres.keyCode;
   } else  tecla = teclapres.which;

   var vr = new String(Campo.value);
   vr = vr.replace(".", "");
   vr = vr.replace(".", "");
   vr = vr.replace("/", "");
   vr = vr.replace("-", "");
   tam = vr.length + 1;

   
   if (tecla != 9 && tecla != 8){
      if (tam > 2 && tam < 6)
         Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
      if (tam >= 6 && tam < 9)
         Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
      if (tam >= 9 && tam < 13)
         Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
      if (tam >= 13 && tam < 15)
         Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
      }
}
/********************************************************************************/
// MÁSCARA E-MAIL
/********************************************************************************/

function FormatarMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){ 
                    return true; 
                }
    }else{
        return false;
        }
}
// Somente Número
/********************************************************************************/
function SomenteNumero(e){
    var tecla=(window.event)?event.keyCode:e.which;
    if((tecla > 47 && tecla < 58)) return true;
    else{
    if (tecla != 8) return false;
    else return true;
    }
}

// JavaScript Document
/********************************************************************************/
/* Variáveis globais                                                            */
/*------------------------------------------------------------------------------*/
/*                                                                              */
if (document.layers){
	document.captureEvents(Event.KEYDOWN | Event.KEYUP);
	document.onkeydown = PTATMascaraNumeroTecla;
	document.onkeyup   = PTATMascaraNumeroExibe;
}
/*                                                                              */
/*------------------------------------------------------------------------------*/
/********************************************************************************/



/********************************************************************************/
/* Função: PTATMascaraNumero                                                    */
/*------------------------------------------------------------------------------*/
/* Autor:                                                                       */
/*   Nome: Frank Gindri Brandolff                                               */
/*   Email: frank@pta.com.br                                                    */
/*   Data: 01/10/2001                                                           */
/*------------------------------------------------------------------------------*/
/* Descrição: Classe responsável por criar o objeto MascaraNumero. Conta a quan-*/
/*            tidade de numeros que farão parte da máscara através da própria   */
/*            máscara passada como parâmetro.                                   */
/*------------------------------------------------------------------------------*/
/* Parâmetros: Nome: o nome da variável que receberá o objeto do tipo           */
/*                   PTATMascaraNumero deverá obrigatiramente ser passado como	*/
/*                   uma string neste parâmetro.                                */
/*             Mascara: máscara de entrada que delimita a quantidade de números */
/*                   a serem entrados. Delimita também o tamanho do TextBox.    */
/*                   A máscara é do tipo ___*__*__, ou seja, reconhece o carac- */
/*                   tere sublinhado (_) como sendo posição de número e qualquer*/
/*                   outro caractere com osendo pertencenta à máscara.          */
/*------------------------------------------------------------------------------*/
/* Retorno: Objeto PTATMascaraNumero.                                           */
/*------------------------------------------------------------------------------*/
/* Dependências: Nenhum.                                                        */
/*------------------------------------------------------------------------------*/
/*                                                                              */
function PTATMascaraNumero(Nome, Mascara, SaidaComMascara, ValorEntrada, autoTab){

	//propriedades
	this.ComMascara  = SaidaComMascara;
	this.Mascara     = Mascara;
	this.autoTab     = autoTab;
	this.Valor       = '';
	this.Nome        = Nome;
	this.Qtde        = 0;
	this.Objeto      = null;
	this.Form        = '';
	this.lastKey     = 0;

	var Auxiliar     = '';

	//contar quantos numeros terá a mascara
	for (i=0; i<Mascara.length; i++){
		if (Mascara.charAt(i)=='_') this.Qtde++;
	}

	for (i = 0; i < ValorEntrada.length; i++){
		if ((ValorEntrada.charCodeAt(i)>=48) && (ValorEntrada.charCodeAt(i)<=57)) Auxiliar += ValorEntrada.charAt(i);
	}
	this.Valor = Auxiliar;

	//métodos
	this.Exibe     = PTATMascaraNumeroExibe;
	this.Tecla     = PTATMascaraNumeroTecla;
	this.Blur      = PTATMascaraNumeroBlur;
	this.NextField = PTATMascaraNumeroNext;
	
	return this;
}
/*                                                                              */
/*------------------------------------------------------------------------------*/
/********************************************************************************/


function PTATMascaraNumeroBlur(){
	this.Objeto = (this.Objeto == null) ? document.getElementById(this.Nome) : this.Objeto;
	if (this.Valor=='')
		this.Objeto.value = '';
}

/********************************************************************************/
/* Função: PTATMascaraNumeroExibe                                               */
/*------------------------------------------------------------------------------*/
/* Autor:                                                                       */
/*   Nome: Frank Gindri Brandolff                                               */
/*   Email: frank@pta.com.br                                                    */
/*   Data: 01/10/2001                                                           */
/*------------------------------------------------------------------------------*/
/* Descrição: Método destinado a exibir os valores digitados juntamente com sua */
/*            máscara.                                                          */
/*------------------------------------------------------------------------------*/
/* Parâmetros: Nenhum.                                                          */
/*------------------------------------------------------------------------------*/
/* Retorno: Nenhum.                                                             */
/*------------------------------------------------------------------------------*/
/* Dependências: Nenhum.                                                        */
/*------------------------------------------------------------------------------*/
/*                                                                              */
function PTATMascaraNumeroExibe(){
	var conteudo, qtdvalor;
	conteudo = '';
	qtdvalor = 0;
	var ValorEntrada;
	this.Objeto = (this.Objeto == null) ? document.getElementById(this.Nome) : this.Objeto;

	for (i = 0; i < this.Mascara.length; i++){
		if (qtdvalor<this.Valor.length){
			if (this.Mascara.charAt(i)!='_'){
				conteudo += this.Mascara.charAt(i);
			}
			else{
				conteudo += this.Valor.charAt(qtdvalor);
				qtdvalor++;
			}
		}
		else{
			conteudo += this.Mascara.charAt(i);
		}
	}

	this.Objeto.value = conteudo;

	if (PTATMascaraNumeroExibe.arguments.length > 0){
    if (((this.lastKey >= 48) && (this.lastKey <= 57)) || ((this.lastKey >= 96) && (this.lastKey <= 105))){
      if (this.autoTab && this.Valor.length == this.Qtde) this.NextField();
    }
	}
	this.lastKey = 0;
}
/*                                                                              */
/*------------------------------------------------------------------------------*/
/********************************************************************************/



/********************************************************************************/
/* Função: PTATMascaraNumeroTecla                                               */
/*------------------------------------------------------------------------------*/
/* Autor:                                                                       */
/*   Nome: Frank Gindri Brandolff                                               */
/*   Email: frank@pta.com.br                                                    */
/*   Data: 01/10/2001                                                           */
/*------------------------------------------------------------------------------*/
/* Descrição: Função que trata a entrada de dados, filtra as teclas pressiona-  */
/*            das para aceitar apenas números. Verifica se a tecla backspace    */
/*            foi pressionada para excluir o último número digitado na proprie- */
/*            dade Valor. Verifica se a tecla delete foi pressionada para esva- */
/*            ziar a propriedade Valor.                                         */
/*------------------------------------------------------------------------------*/
/* Parâmetros: e: o evento realizado no input text da máscara.                  */
/*------------------------------------------------------------------------------*/
/* Retorno: Nenhum.                                                             */
/*------------------------------------------------------------------------------*/
/* Dependências: Nenhum.                                                        */
/*------------------------------------------------------------------------------*/
/*                                                                              */
function PTATMascaraNumeroTecla (e){
	var whichCode;
	this.Objeto = (this.Objeto == null) ? document.getElementById(this.Nome) : this.Objeto;

	if (document.layers){
		whichCode = e.which;
	}
	else{
		whichCode = (window.Event) ? e.which : e.keyCode;
	}
  this.lastKey = whichCode;
	var key;

	if (((whichCode >= 48) && (whichCode <= 57)) || ((whichCode >= 96) && (whichCode <= 105))){
		//a tecla pressionada é um número
		if (this.Valor.length < this.Qtde){
			if (whichCode > 95){
				key = String.fromCharCode(whichCode-48);
			}
			else{
				key = String.fromCharCode(whichCode);
			}
			this.Valor += key;
		}
	}
	else{
		//se a tecla pressionada não for um numero
		switch (whichCode){
			case 8://Tecla backspace
				//apagar o último caracter;
				this.Valor = this.Valor.substring(0,this.Valor.length-1);
			break;
			case 46://tecla delete
				//apaga todo o conteúdo da máscara;
				this.Valor = '';
			break;
		}
	}
	return 0;
}



/********************************************************************************/
/* Função: PTATMascaraNumeroNext                                                */
/*------------------------------------------------------------------------------*/
/* Autor:                                                                       */
/*   Nome: Frank Gindri Brandolff                                               */
/*   Email: frank@pta.com.br                                                    */
/*   Data: 01/10/2001                                                           */
/*------------------------------------------------------------------------------*/
/* Descrição: Função que trata de mudar o foco da mascara para o proximo campo  */
/*            do Form.                                                          */
/*------------------------------------------------------------------------------*/
/* Parâmetros: Nenhum.                                                          */
/*------------------------------------------------------------------------------*/
/* Retorno: Nenhum.                                                             */
/*------------------------------------------------------------------------------*/
/* Dependências: Nenhum.                                                        */
/*------------------------------------------------------------------------------*/
/*                                                                              */
function PTATMascaraNumeroNext(){
  this.Objeto = (this.Objeto == null) ? document.getElementById(this.Nome) : this.Objeto;
  var theIndex = this.Objeto.tabIndex;
  var theForm = this.Objeto.form;
  for (var i = 0; i<theForm.elements.length; i++){
    if (theForm.elements[i].tabIndex == (theIndex+1)){
      theForm.elements[i].focus();
      break;
    }
  }
}
/*                                                                              */
/*------------------------------------------------------------------------------*/
/********************************************************************************/