var NUM_DIGITOS_CPF  = 11;
var NUM_DIGITOS_CNPJ = 14;
var NUM_DGT_CNPJ_BASE = 8;
String.prototype.lpad = function(pSize, pCharPad)
{
	var str = this;
	var dif = pSize - str.length;
	var ch = String(pCharPad).charAt(0);
	for (; dif>0; dif--) str = ch + str;
	return (str);
} //String.lpad

String.prototype.trim = function()
{
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
} //String.trim


function unformatNumber(pNum)
{
	return String(pNum).replace(/\D/g, "").replace(/^0+/, "");
} //unformatNumber


function formatCpfCnpj(pCpfCnpj, pUseSepar, pIsCnpj)
{
	if (pIsCnpj==null) pIsCnpj = false;
	if (pUseSepar==null) pUseSepar = true;
	var maxDigitos = pIsCnpj? NUM_DIGITOS_CNPJ: NUM_DIGITOS_CPF;
	var numero = unformatNumber(pCpfCnpj);
 
	numero = numero.lpad(maxDigitos, '0');
	if (!pUseSepar) return numero;

	if (pIsCnpj)
	{
		reCnpj = /(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/;
		numero = numero.replace(reCnpj, "$1.$2.$3/$4-$5");
	}
	else
	{
		reCpf  = /(\d{3})(\d{3})(\d{3})(\d{2})$/;
		numero = numero.replace(reCpf, "$1.$2.$3-$4");
	}
	return numero;
} //formatCpfCnpj

function dvCpfCnpj(pEfetivo, pIsCnpj)
{
	if (pIsCnpj==null) pIsCnpj = false;
	var i, j, k, soma, dv;
	var cicloPeso = pIsCnpj? NUM_DGT_CNPJ_BASE: NUM_DIGITOS_CPF;
	var maxDigitos = pIsCnpj? NUM_DIGITOS_CNPJ: NUM_DIGITOS_CPF;
	var calculado = formatCpfCnpj(pEfetivo, false, pIsCnpj);
	calculado = calculado.substring(2, maxDigitos);
	var result = "";

	for (j = 1; j <= 2; j++)
	{
		k = 2;
		soma = 0;
		for (i = calculado.length-1; i >= 0; i--)
		{
			soma += (calculado.charAt(i) - '0') * k;
			k = (k-1) % cicloPeso + 2;
		}
		dv = 11 - soma % 11;
		if (dv > 9) dv = 0;
		calculado += dv;
		result += dv
	}

	return result;
} //dvCpfCnpj

function isCpf(pCpf)
{
	var numero = formatCpfCnpj(pCpf, false, false);
	var base = numero.substring(0, numero.length - 2);
	var digitos = dvCpfCnpj(base, false);
	var algUnico, i;

	// Valida dígitos verificadores
	if (numero != base + digitos) return false;

	algUnico = true;
	for (i=1; i<NUM_DIGITOS_CPF; i++)
	{
		algUnico = algUnico && (numero.charAt(i-1) == numero.charAt(i));
	}
	return (!algUnico);
} //isCpf


function isCnpj(pCnpj)
{
	var numero = formatCpfCnpj(pCnpj, false, true);
	var base = numero.substring(0, NUM_DGT_CNPJ_BASE);
	var ordem = numero.substring(NUM_DGT_CNPJ_BASE, 12);
	var digitos = dvCpfCnpj(base + ordem, true);
	var algUnico;

	// Valida dígitos verificadores
	if (numero != base + ordem + digitos) return false;

	algUnico = numero.charAt(0) != '0';
	for (i=1; i<NUM_DGT_CNPJ_BASE; i++)
	{
		algUnico = algUnico && (numero.charAt(i-1) == numero.charAt(i));
	}
	if (algUnico) return false;

	if (ordem == "0000") return false;
	return (base == "00000000"
	|| parseInt(ordem, 10) <= 300 || base.substring(0, 3) != "000");
} //isCnpj


function isCpfCnpj(pCpfCnpj)
{
	var numero = pCpfCnpj.replace(/\D/g, "");
	if (numero.length > NUM_DIGITOS_CPF)
	return isCnpj(pCpfCnpj)
	else
	return isCpf(pCpfCnpj);
}

function formataIdFederal(campo, e, tipo)
{
	if(campo.value.length > 13)
	{
		if(tipo == 1)
		{
			return false;
		}
		if( typeof window.event != "undefined" )
		{
			if (window.event.keyCode != 45 && window.event.keyCode != 46 && window.event.keyCode != 8 && window.event.keyCode != 0 )
			{
				var str = campo.value;
				str = str.replace('.','');
				str = str.replace('.','');
				str = str.replace('-','');
				str = str.replace('/','');
				temp = str.substr(0,2);
				if(temp.length == 2)
				temp += '.';
				temp += str.substr(2,3);
				if(temp.length == 6)
				temp += '.';
				temp += str.substr(5,3);
				if(temp.length == 10)
				temp += '/';
				temp += str.substr(8,4);
				if(temp.length == 15)
				temp += '-';
				temp += str.substr(12,2);
				campo.value= temp;
			}

		}else
		{
			if (e.which != 45 && e.which != 46 && e.which != 8 && e.which != 32 && e.which != 13 && e.which != 0    )
			{

				var str = campo.value;
				str = str.replace('.','');
				str = str.replace('.','');
				str = str.replace('-','');
				str = str.replace('/','');

				temp = str.substr(0,2);
				if(temp.length == 2)
				temp += '.';
				temp += str.substr(2,3);
				if(temp.length == 6)
				temp += '.';
				temp += str.substr(5,3);
				if(temp.length == 10)
				temp += '/';
				temp += str.substr(8,4);
				if(temp.length == 15)
				temp += '-';
				temp += str.substr(12,2);
				campo.value= temp;
			}

		}
		
	}else
	{
		if( typeof window.event != "undefined" )
		{
			if (window.event.keyCode != 45 && window.event.keyCode != 46 && window.event.keyCode != 8 && window.event.keyCode != 0 )
			{
				var str = campo.value;
				str = str.replace('.','');
				str = str.replace('.','');
				str = str.replace('/','');
				str = str.replace('-','');

				temp = str.substr(0,3) ;
				if(temp.length == 3)
				temp += '.';
				temp += str.substr(3,3);
				if(temp.length == 7)
				temp += '.';
				temp += str.substr(6,3);
				if(temp.length == 11)
				temp += '-';
				temp += str.substr(9,2);
				campo.value= temp;
			}
		}else
		{

			if (e.which != 45 && e.which != 46 && e.which != 8 && e.which != 32 && e.which != 13 &&  e.which != 0  )
			{
				var str = campo.value;
				str = str.replace('.','');
				str = str.replace('.','');
				str = str.replace('/','');
				str = str.replace('-','');
				temp = str.substr(0,3) ;
				if(temp.length == 3)
				temp += '.';
				temp += str.substr(3,3);
				if(temp.length == 7)
				temp += '.';
				temp += str.substr(6,3);
				if(temp.length == 11)
				temp += '-';
				temp += str.substr(9,2);
				campo.value= temp;
			}
		}
	}
}

function keypress(e)
{
	var x = '';
	if (document.all)
	{
		var evnt = window.event;
		x = evnt.keyCode;
	}
	else
	{
		x = e.keyCode;
	}
	return x;
}
function formataCEP(campo, event)
{
	var tecla = keypress(event);
	if(tecla != 8)
	{
		if (tecla != 109)
		if (campo.value.length == 5)
		campo.value += '-';
	}
}