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 isEmail(email){
	var suportado = 0;
	EmailInv = false;

	if (window.RegExp) {
		var tempReg = /a/;
		if (tempReg.test("a")){
			suportado = 1;
		}
	}

	if (!suportado){
		if(((email.indexOf(".") <= 0) || (email.indexOf("@") <= 0)) || ((email.lastIndexOf(".") == (email.length - 1)) || (email.lastIndexOf("@") == (email.length -1)))){
			EmailInv = true;
		}
	}else{
		var tmp1 = /(@.*@)|(\.\.)|(@\.)|(^\.)/;
		var tmp2 = /^.+@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
		if(tmp1.test(email) || !tmp2.test(email)){
			EmailInv = true;
		}
	}

	return !EmailInv;
}

/*function isCPF(cpf){
	cpf = cpf.replace(/\D/g,"");
	var c = cpf.substr(0,9);
	var v = cpf.substr(9,2);
	var d = 0;
	var val = true;
	var equals = 0;

	for(i=0;i<9;i++)
		d += c.charAt(i) * (10-i);
	d = ((11- (d % 11)) > 9) ? 0 : 11 - (d % 11);
	v.charAt(0) != d ? $val=false : null;
	d *= 2;
	for (i=0;i<9;i++)
        d += c.charAt(i) * (11-i);
	d = (11 - (d % 11)) > 9 ? 0 : 11 - (d % 11);
	v.charAt(1) != d ? val = false : null;

	for(i=0;i<10;i++)
		if (cpf.charAt(i) != cpf.charAt(i+1))
			break;
		else
			equals++

	equals >= 10 ? val=false : null;
	alert(val);
	return val;
}*/

function isCPF(cpf){
	var cpf_limpo = cpf.replace(/\D/g,"");
	if (cpf_limpo.length !=11)
		return false;

	var soma = 0;
	for (var i=0; i<9; i++)
		soma += parseInt(cpf_limpo.charAt(i)) * (10-i);

	if (soma == 0)
		return false;

	primeiro_digito = 11 - soma % 11;
	if (primeiro_digito > 9)
		primeiro_digito = 0;

	if (cpf_limpo.substr(9,1) != primeiro_digito)
		return false;

	var soma = 0;
	for (var i=0; i<10; i++)
		soma += parseInt(cpf_limpo.charAt(i)) * (11-i);

	var segundo_digito = 11 - soma % 11;

	if (segundo_digito > 9)
		segundo_digito = 0;
	if (cpf_limpo.substr(10,1) != segundo_digito)
		return false;

	return true;
}

function isCNPJ(CNPJ) {
	while(/\D/.test(CNPJ))
		CNPJ = CNPJ.replace(/\D/,"");
	if (CNPJ.length < 14)
		return false;
	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	for (i=0; i<12; i++){
		a[i] = CNPJ.charAt(i);
		b += a[i] * c[i+1];
	}
	if ((x = b % 11) < 2) {
		a[12] = 0
	} else {
		a[12] = 11-x
	}
	b = 0;
	for (y=0; y<13; y++) {
		b += (a[y] * c[y]);
	}
	if ((x = b % 11) < 2) {
		a[13] = 0;
	} else {
		a[13] = 11-x;
	}

	if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13]))
		return false;
	else
		return true;
}


function mascara(expressao, evento) {
	var e = evento;
	var ascii;

	if (e.keyCode)
		ascii = e.keyCode;
	else if (e.which)
		ascii = e.which;
	chr = String.fromCharCode(ascii);
	if(ascii == 8 || ascii == 9 || ascii == 13 || ascii == 116){
		return true;
	}
	var regx = new RegExp(expressao);
	return (regx.test(chr));
}