function Mascara(tipo, campo, teclaPress) {
	if (window.event)
	{
		var tecla = teclaPress.keyCode;
	} else {
		tecla = teclaPress.which;
	}

	var s = new String(campo.value);
	// Remove todos os caracteres à seguir: ( ) / - . e espaço, para tratar a string denovo.
	s = s.replace(/(\.|\(|\)|\/|\-| )+/g,'');

	tam = s.length + 1;

	if ( tecla != 9 && tecla != 8 ) {
		switch (tipo)
		{
			case 'CPF' :
			if (tam > 3 && tam < 7)
			campo.value = s.substr(0,3) + '.' + s.substr(3, tam);
			else if (tam >= 7 && tam < 10)
			campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,tam-6);
			else if (tam >= 10 && tam < 12)
			campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,3) + '-' + s.substr(9,tam-9);
			break;

			case 'CNPJ' :
			if (tam > 2 && tam < 6)
			campo.value = s.substr(0,2) + '.' + s.substr(2, tam);
			else if (tam >= 6 && tam < 9)
			campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,tam-5);
			else if (tam >= 9 && tam < 13)
			campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,tam-8);
			else if (tam >= 13 && tam < 15)
			campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,4)+ '-' + s.substr(12,tam-12);
			break;

			case 'TELDDD' :
			if (tam > 2 && tam < 4)
			campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,tam);
			else if (tam >= 7 && tam < 11)
			campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,4) + '-' + s.substr(6,tam-6);
			break;

			case 'TEL' :
			if (tam > 4 && tam < 6)
			campo.value = s.substr(0,4) + '-' + s.substr(4,tam-4);
			break;

			case 'CEP' :
			if (tam > 5 && tam < 7)
			campo.value = s.substr(0,5) + '-' + s.substr(5,tam-3);
			break;

			case 'DATA' :
			if (tam > 2 && tam < 4)
			campo.value = s.substr(0,2) + '/' + s.substr(2, tam);
			else if (tam > 4 && tam < 11)
			campo.value = s.substr(0,2) + '/' + s.substr(2,2) + '/' + s.substr(4,tam-4);
			
			case 'DIAMES' :
			if (tam > 2 && tam < 4)
			campo.value = s.substr(0,2) + '/' + s.substr(2, tam);
			break;
		}
	}
	
	return isNumber(campo, teclaPress);
}

function isCPFCNPJ(campo, pType){
   if(campo.length == 0){return false;}

   var campo_filtrado = "", valor_1 = " ", valor_2 = " ", ch = "";
   var valido = false;
        
   for (i = 0; i < campo.length; i++){
      ch = campo.substring(i, i + 1);
      if (ch >= "0" && ch <= "9"){
         campo_filtrado = campo_filtrado.toString() + ch.toString()
         valor_1 = valor_2;
         valor_2 = ch;
      }
      if ((valor_1 != " ") && (!valido)) valido = !(valor_1 == valor_2);
   }
   if (!valido) campo_filtrado = "12345678912";

   if (campo_filtrado.length < 11){
      for (i = 1; i <= (11 - campo_filtrado.length); i++){campo_filtrado = "0" + campo_filtrado;}
   }

   if(pType <= 1){
      if ( ( campo_filtrado.substring(9,11) == checkCPF( campo_filtrado.substring(0,9) ) ) && ( campo_filtrado.substring(11,12)=="") ){return true;}
   }

   if((pType == 2) || (pType == 0)){
      if (campo_filtrado.length >= 14){
         if ( campo_filtrado.substring(12,14) == checkCNPJ( campo_filtrado.substring(0,12) ) ){ return true;}
      }
   }
   
   return false;
}

function checkCNPJ(vCNPJ){
   var mControle = "";
   var aTabCNPJ = new Array(5,4,3,2,9,8,7,6,5,4,3,2);
   for (i = 1 ; i <= 2 ; i++){
      mSoma = 0;
      for (j = 0 ; j < vCNPJ.length ; j++)
         mSoma = mSoma + (vCNPJ.substring(j,j+1) * aTabCNPJ[j]);
      if (i == 2 ) mSoma = mSoma + ( 2 * mDigito );
      mDigito = ( mSoma * 10 ) % 11;
      if (mDigito == 10 ) mDigito = 0;
      mControle1 = mControle ;
      mControle = mDigito;
      aTabCNPJ = new Array(6,5,4,3,2,9,8,7,6,5,4,3);
   }
   return( (mControle1 * 10) + mControle );
}

function checkCPF(vCPF){
   var mControle = ""
   var mContIni = 2, mContFim = 10, mDigito = 0;
   for (j = 1 ; j <= 2 ; j++){
      mSoma = 0;
      for (i = mContIni ; i <= mContFim ; i++)
         mSoma = mSoma + (vCPF.substring((i-j-1),(i-j)) * (mContFim + 1 + j - i));
      if (j == 2 ) mSoma = mSoma + ( 2 * mDigito );
      mDigito = ( mSoma * 10 ) % 11;
      if (mDigito == 10) mDigito = 0;
      mControle1 = mControle;
      mControle = mDigito;
      mContIni = 3;
      mContFim = 11;
   }
   return( (mControle1 * 10) + mControle );
}

// somente números
function isNumber(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}
// somente números - fim


// validação de email
function isEmail(campo)
{
	
	var valor = campo.value;
	
	if (valor == "")
	{
		err("E-mail");
		return false;
	}
	
	apos = valor.indexOf("@");
	dotpos = valor.lastIndexOf(".");
	strng = valor;
	
	if (apos < 1 || dotpos - apos < 2)
	{
		err("E-mail");
		return false;
	}
	
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (strng.match(illegalChars))
	{
		err("E-mail");
		return false;
	}	
	
	return true;
}
// validação de email - fim


// se o campo estiver vazio
function isVazio(campo, texto)
{

	if (campo.value == "")
	{
		err(texto);
		return true;
	}
	
	return false;
	
} // se o campo estiver vazio - fim


// o campo deve ter determinado tamanho
function isLenght(campo, texto, tamanho)
{
	
	if (campo.value.length != tamanho)
	{
		errTamanho(texto, tamanho);
		campo.focus();
		return false;
	}
	
	return true;
	
} // o campo deve ter determinado tamanho - fim


// se o campo estiver checado
function isChecked(campo, texto)
{
	
	var itemChecked = false;  

	for(var i = 0 ; i < campo.length ; i++)  
	{  
		
    if(campo[i].checked)  
    {  
      itemChecked = true;  
    }
    
	}

	if (itemChecked == false)
	{
		errRadio(texto);
		return false;
	}
	
	return true;
	
} // se o campo estiver vazio - fim


// erro geral
function err(campo)
{
	GB_showCenter('Cecilia Dale', '/includes/err.php?err='+campo, 114, 362)
}

function errTamanho(campo, tamanho)
{
	GB_showCenter('Cecilia Dale', '/includes/err.php?err='+campo+'&tamanho='+tamanho, 114, 362)
}

function errRadio(campo)
{
	GB_showCenter('Cecilia Dale', '/includes/err.php?err='+campo+'&tipo=radio', 114, 362)
}