//Coloca Mascara para CPF. Usar com o event handler onKeyUp.
function formatarCPF(campo) {
   var valor = "";
   var teste = campo.value.toUpperCase();
   for (var i = 1;i<=teste.length;i++) {
      if(!isNaN(teste.charAt(i-1))) {
         valor += teste.charAt(i-1);
         if (valor.length == 3 || valor.length == 7) {
            valor += ".";
         }
         if (valor.length == 11) {
	         valor += "-";
	     }
	  }
   }
   if (valor.length > 14){
      valor = valor.substring(0,14);
   }
   campo.value=valor;
}

function formatarCpfEspecial(campo)
{
    var valor = "";
    var teste = campo.value.toUpperCase();
    
    if (teste.length > 0 && teste.charAt(0) != '#')
    {
        for (var i = 1;i<=teste.length;i++)
        {
            if(!isNaN(teste.charAt(i-1)))
            {
                valor += teste.charAt(i-1);
                if (valor.length == 3 || valor.length == 7)
                {
                    valor += ".";
                }
                if (valor.length == 11)
                {
                    valor += "-";
                }
            }
        }
        if (valor.length > 14)
        {
            valor = valor.substring(0,14);
        }
        campo.value=valor;
    }
    else if (teste.length > 0 && teste.charAt(0) == '#')
    {
        for (var i = 1;i<=teste.length;i++)
        {
            if(!isNaN(teste.charAt(i-1)))
            {
                valor += teste.charAt(i-1);
                if (valor.length == 3 || valor.length == 7 || valor.length == 18 || valor.length == 22)
                {
                    valor += ".";
                }
                if (valor.length == 11 || valor.length == 26)
                {
                    valor += "-";
                }
                if (valor.length == 14)
                {
                    valor += "%";
                }
            }
        }
        if (valor.length > 29)
        {
            valor = valor.substring(0,29);
        }
        campo.value="#"+valor;
    }
}

function removerMascaraCPF(campo) {
	var cpfDesformatado = campo.value;
	while(cpfDesformatado.indexOf(".") != -1) {
		cpfDesformatado = cpfDesformatado.replace(".", "");
	}
	cpfDesformatado = cpfDesformatado.replace("-", "");
	return cpfDesformatado;
}