
function popup(url,name,features) {
	// Tamanho da janela
	var width = /width=(\d+)/i.exec(features);
	wwidth = width[1];
	var height = /height=(\d+)/i.exec(features);
	wheight = height[1];

	// Tamanho da tela
	swidth  = screen.availWidth;
	sheight = screen.availHeight;

	// Distância do topo esquerdo
	_left = parseInt((swidth / 2) - (wwidth / 2));
	_top = parseInt((sheight / 2) - (wheight / 2));

	features.replace("/top=\d+,?/","");
	features.replace("/left=\d+,?/","");

	features += ',top='+ _top +',left='+ _left;


	//Abre a Janela
	jan = window.open(url,name,features);
	jan.focus();
}


function pularcampo(campo,alvo,maxlength){    
	if (document.getElementById(campo).value.length==maxlength)
	{
		document.getElementById(alvo).focus();
	}
}




//******************************************************************************
//Validação do cadastro
//******************************************************************************

function TrocaDivCadastrese(tipoPessoa)
    {
        var CPF   = document.getElementById('CPF');
        var CNPJ  = document.getElementById('CNPJ');
        switch (tipoPessoa)
        {
        
            case 'CPF' :
                CPF.style.display                        ='block';
                CNPJ.style.display                       ='none';
                document.Form1.txHidden.value            = 'CPF';
                //alert(document.Form1.txHidden.value);
            break;
            default :
                CPF.style.display                        ='none';
                CNPJ.style.display                       ='block';
                document.Form1.txHidden.value            ='CNPJ';
                //alert(document.Form1.txHidden.value);
            break;
        }
        
    }

function validaCadastro(cpf_cnpj)
{
    if (cpf_cnpj.txHidden.value == "CPF")
        {
			var CPF = cpf_cnpj.txCPF.value;
			//alert(CPF);
			if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
				CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||
				CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
				CPF == "88888888888" || CPF == "99999999999")
				{
					alert("CPF Invalido");
					return false;
				}
			soma = 0;
			for (i=0; i < 9; i ++)
				{
					soma += parseInt(CPF.charAt(i)) * (10 - i);
				}
			resto = 11 - (soma % 11);
			if (resto == 10 || resto == 11)
				{
					resto = 0;
				}
			if (resto != parseInt(CPF.charAt(9)))
				{
					alert("CPF Invalido!");
					return false;
				}
			soma = 0;
			for (i = 0; i < 10; i ++)
				{
					soma += parseInt(CPF.charAt(i)) * (11 - i);
				}
			resto = 11 - (soma % 11);
			if (resto == 10 || resto == 11)
				{
					resto = 0;
				}
			if (resto != parseInt(CPF.charAt(10)))
				{
					alert("O CPF deve possuir exatamente 11 dígitos numericos!");
					return false;
				}
			else
				{	
					return true;
				}
		}
    else
{
	//verifica a quantidade de caracteres digitados
	//e o transforma em um numero inteiro.
	cnpj = cpf_cnpj.txCNPJ.value;
	tamanho = cpf_cnpj.txCNPJ.value;
	tamanho = tamanho.length;
	tamanho = parseInt(tamanho,10);
	if(tamanho < 14 || tamanho >14){
		alert("O CNPJ deve possui exatamente 14 digitos");
		return (false);
	}
	else{
		if(cpf_cnpj.txCNPJ.value == "00000000000000" || 
		   cpf_cnpj.txCNPJ.value == "11111111111111" || 
		   cpf_cnpj.txCNPJ.value == "22222222222222" || 
		   cpf_cnpj.txCNPJ.value == "33333333333333" || 
		   cpf_cnpj.txCNPJ.value == "44444444444444" || 
		   cpf_cnpj.txCNPJ.value == "55555555555555" || 
		   cpf_cnpj.txCNPJ.value == "66666666666666" || 
		   cpf_cnpj.txCNPJ.value == "77777777777777" || 
		   cpf_cnpj.txCNPJ.value == "88888888888888" || 
		   cpf_cnpj.txCNPJ.value == "99999999999999"){
			alert("O CNPJ nao pode ser composto apenas por numeros repetidos");
			return (false);
		}
		else{
			//logica da validade
			n1 = cpf_cnpj.txCNPJ.value.substring(0,1);
			n2 = cpf_cnpj.txCNPJ.value.substring(1,2);
			n3 = cpf_cnpj.txCNPJ.value.substring(2,3);
			n4 = cpf_cnpj.txCNPJ.value.substring(3,4);
			n5 = cpf_cnpj.txCNPJ.value.substring(4,5);
			n6 = cpf_cnpj.txCNPJ.value.substring(5,6);
			n7 = cpf_cnpj.txCNPJ.value.substring(6,7);
			n8 = cpf_cnpj.txCNPJ.value.substring(7,8);
			n9 = cpf_cnpj.txCNPJ.value.substring(8,9);
			n10 = cpf_cnpj.txCNPJ.value.substring(9,10);
			n11 = cpf_cnpj.txCNPJ.value.substring(10,11);
			n12 = cpf_cnpj.txCNPJ.value.substring(11,12);
			n13 = cpf_cnpj.txCNPJ.value.substring(12,13);
			n14 = cpf_cnpj.txCNPJ.value.substring(13,14);

			n1 = parseInt(n1,10);
			n2 = parseInt(n2,10);
			n3 = parseInt(n3,10);
			n4 = parseInt(n4,10);
			n5 = parseInt(n5,10);
			n6 = parseInt(n6,10);
			n7 = parseInt(n7,10);
			n8 = parseInt(n8,10);
			n9 = parseInt(n9,10);
			n10 = parseInt(n10,10);
			n11 = parseInt(n11,10);
			n12 = parseInt(n12,10);
			n13 = parseInt(n13,10);
			n14 = parseInt(n14,10);

			//conta para identificação do 13 digito
			soma = n1 * 5;
			soma += n2 * 4;
			soma += n3 * 3;
			soma += n4 * 2;
			soma += n5 * 9;
			soma += n6 * 8;
			soma += n7 * 7;
			soma += n8 * 6;
			soma += n9 * 5;
			soma += n10 * 4;
			soma += n11 * 3;
			soma += n12 * 2;

			divisao = soma / 11;
			divisao = parseInt(divisao,10);
			soma = soma - (11 * divisao);

			if(soma == 0 || soma == 1){
				resultado1 = 0;
			}
			else{
				resultado1 = 11 - soma;
			}
			//conta para identificacao do 14 digito
			if(resultado1 == n13){
				soma = n1 * 6;
				soma += n2 * 5;
				soma += n3 * 4;
				soma += n4 * 3;
				soma += n5 * 2;
				soma += n6 * 9;
				soma += n7 * 8;
				soma += n8 * 7;
				soma += n9 * 6;
				soma += n10 * 5;
				soma += n11 * 4;
				soma += n12 * 3;
				soma += n13 * 2;

				divisao = soma / 11;
				divisao = parseInt(divisao,10);
				soma = soma - (11 * divisao);

				if(soma == 0 || soma == 1){
					resultado2 = 0;
				}
				else{
					resultado2 = 11 - soma;
				}
				if(resultado2 == n14){
					return (true);
					}
				else{
					alert("CNPJ Invalido");
					return (false);
				}
			}else{
				alert ("CNPJ Invalido");
				return (false);
			}
		}
	}
}
}