var plano = '';
var pgto = '';
var valor = 0;
var desconto = 0;
var descontoqtd = 0;
var data1 = 0;
var data2 = 0;
var data3 = 0;
var evento1 = 0;
var evento1b = '';
var evento2 = 0;
var evento2b = '';
var txt_ad = '';
var pl0 = 0
var n = 0;

function abrir(n) {
	box();

	limpa();
	eval("document.all.b" + n + ".className='orelha'");

	document.all.valor_plano.value = valor_plano;
	document.all.desconto.value = desconto;
	document.all.descontoqtd.value = descontoqtd;
	document.all.data1.value = data1;
	document.all.data2.value = data2;
	document.all.data3.value = data3;
	document.all.evento1.value = evento1;
	document.all.evento1b.value = evento1b;
	document.all.evento2.value = evento2;
	document.all.evento2b.value = evento2b;
	document.all.txt_ad.value = txt_ad;
}


function ativacampo(nome, nome2){
	valuecampo = nome;
	valuecampo2 = nome2;

	limpa2();

	eval("document.all." + valuecampo + ".disabled=false");

	verevento();
}

function verevento(){
	if ((valuecampo == 'evento21') || (valuecampo == 'evento23')) {
		eval("document.all.evento11.disabled=true");
		eval("document.all.evento13.disabled=true");
		eval("document.all.evento1[0].disabled=true");
		eval("document.all.evento1[1].disabled=false");
		eval("document.all.evento1[2].disabled=true");
		eval("document.all.evento1[3].disabled=false");
	}else if ((valuecampo == 'evento22') || (valuecampo == 'evento24')) {
		eval("document.all.evento12.disabled=true");
		eval("document.all.evento14.disabled=true");
		eval("document.all.evento1[0].disabled=false");
		eval("document.all.evento1[1].disabled=true");
		eval("document.all.evento1[2].disabled=false");
		eval("document.all.evento1[3].disabled=true");
	}else if ((valuecampo == 'evento11') || (valuecampo == 'evento13')) {
		eval("document.all.evento21.disabled=true");
		eval("document.all.evento23.disabled=true");
		eval("document.all.evento2[0].disabled=true");
		eval("document.all.evento2[1].disabled=false");
		eval("document.all.evento2[2].disabled=true");
		eval("document.all.evento2[3].disabled=false");
	}else if ((valuecampo == 'evento12') || (valuecampo == 'evento14')) {
		eval("document.all.evento22.disabled=true");
		eval("document.all.evento24.disabled=true");
		eval("document.all.evento2[0].disabled=false");
		eval("document.all.evento2[1].disabled=true");
		eval("document.all.evento2[2].disabled=false");
		eval("document.all.evento2[3].disabled=true");
	}
}

function verdata(nome, n){
	valuecampo = nome;
	valuecampo2 = n;

	for (i=0;i<=2;i++){
		eval("document.all.data1[" + i + "].disabled=false");		
		eval("document.all.data2[" + i + "].disabled=false");		
	}

	eval("document.all." + valuecampo + "[" + valuecampo2 + "].checked=false");
	eval("document.all." + valuecampo + "[" + valuecampo2 + "].disabled=true");
}

function box(){

	valor = valor_plano

	valor = Arredonda(valor);

	document.all.txt_valor.innerHTML=valor;

	if (desconto > 0){
		document.all.txt_desc.innerHTML=desconto;
		document.all.txt_desc2.innerHTML=descontoqtd;
		document.all.box_desc.style.display="inline";
	}else{
		document.all.box_desc.style.display="none";
	}

	if (plano != ''){
		document.all.txt_plano.innerHTML=plano;
		document.all.box_plano.style.display="inline";
	}else{
		document.all.box_plano.style.display="none";
	}

	if (pgto != ''){
		document.all.txt_pgto.innerHTML=pgto;
		document.all.box_pgto.style.display="inline";
	}else{
		document.all.box_pgto.style.display="none";
	}

	if (txt_ad != ''){
		document.all.txt_opc.innerHTML=txt_ad;
		document.all.box_opc.style.display="inline";
	}else{
		document.all.txt_opc.innerHTML='';
		document.all.box_opc.style.display="none";
	}

}

function limpa(){
	for (i=1;i<=5;i++){
		eval("document.all.b" + i + ".className='orelha-escura'");		
	}
}

function limpa2(){
	for (i=1;i<=4;i++){
		eval("document.all." + valuecampo2 + i + ".disabled=true");		
	}
}

function sel_plano(n,nome){

	plano = nome;
	document.all.plano.value = n;
	document.all.plano_nome.value = nome;
	document.all.descontoqtd.value = descontoqtd;
	plano_preco(0);
}

function plano_preco(n){

	var txt_info='';
	pgto = "";
	valor_plano = eval("pl"+document.all.plano.value+"["+n+"]");
	document.all.valor_plano.value = valor_plano;

	box();

}

function isEmail(str) {

	var supported = 0;

	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}

	if (!supported) return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(str) && r2.test(str));
}

function ValidaForm3(){

	if (!isEmail(document.all.em_tec.value)){
		alert("Atenção! E-mail inválido.");
		document.all.em_tec.focus();
		return false;
	}else if (document.all.nome.value.length < 2){
		alert("Atenção! Nome do contato não pode ser nulo.");
		document.all.nome.focus();
		return false;
	}else if (document.all.cpf.value.length < 2){
		alert("Atenção! CPF do contato não pode ser nulo.");
		document.all.cpf.focus();
		return false;
	}else if (!validaCPF(document.all.cpf.value)){
		alert("Atenção! CPF do contato não pode ser inválido.");
		document.all.cpf.focus();
		return false;
	}else if (document.all.cep.value.length < 2){
		alert("Atenção! O campo cep não pode ser nulo.");
		document.all.cep.focus();
		return false;
	}else if (document.all.endereco.value.length < 2){
		alert("Atenção! O campo endereço não pode ser nulo.");
		document.all.endereco.focus();
		return false;
	}else if (document.all.num.value.length < 1){
		alert("Atenção! O campo num (número) não pode ser nulo.");
		document.all.num.focus();
		return false;
	}else if (document.all.bairro.value.length < 2){
		alert("Atenção! O campo bairro não pode ser nulo.");
		document.all.bairro.focus();
		return false;
	}else if (document.all.cidade.value.length < 2){
		alert("Atenção! O campo cidade não pode ser nulo.");
		document.all.cidade.focus();
		return false;
	}else if (document.all.uf.value.length < 2){
		alert("Atenção! O campo uf não pode ser nulo.");
		document.all.uf.focus();
		return false;
	}else if (document.all.telefone2.value.length < 2){
		alert("Atenção! O campo telefone não pode ser nulo.");
		document.all.telefone2.focus();
		return false;
	}

	return true;
}

function ValidaForm1(){

	if (document.all.cnpj.value!=''){
		if (!validaCNPJ(document.all.cnpj.value)){
			//alert("Atenção! CNPJ não pode ser inválido.");
			document.all.cnpj.focus();
			if(document.all.cnpj.value == 'xx.xxx.xxx/xxxx-xx'){
				document.all.cnpj.value = '';
			}
			return false;
		}else if(document.all.plano.value < 1){
			alert("Atenção! Você deve escolher um dos planos.");
			return false;
		}
	}else if(document.all.cnpj.value == ''){
		alert("Atenção! CNPJ não pode ser inválido.");
		document.all.cnpj.focus();
		document.all.cnpj.value = '';
		return false;
	}
	return true;
}

function InStr(n, s1, s2){
	var numargs=InStr.arguments.length;	
	if(numargs<3)
		return n.indexOf(s1)+1;
	else
		return s1.indexOf(s2, n)+1;
}

function Arredonda(n){
	return Math.round(n*100)/100;
}

function CPFVal(s){
        var i; 
	var rtrn = true;
        var c = s.substr(0,9); 
        var dv = s.substr(9,2); 
        var d1 = 0; 
        for (i = 0; i < 9; i++) d1 += c.charAt(i)*(10-i);
        if (d1 == 0) rtrn = false; 
        d1 = 11 - (d1 % 11); 
        if (d1 > 9) d1 = 0; 
        if (dv.charAt(0) != d1) rtrn = false; 
        d1 *= 2; 
        for (i = 0; i < 9; i++) d1 += c.charAt(i)*(11-i);
        d1 = 11 - (d1 % 11); 
        if (d1 > 9) d1 = 0; 
        if (dv.charAt(1) != d1) rtrn = false;
	return rtrn;
}

function validaCNPJ(CNPJ) {
	erro = new String;
	if (CNPJ.length < 18) erro += "ERRO - LEIA COM ATENÇÃO!\nO CNPJ deve ter 18 caracteres, incluindo pontos e barras - (Ex.: 00.000.000/0001-00)\n\n";
	if (CNPJ.value == '') erro += "ERRO - LEIA COM ATENÇÃO!\nVocê precisa digitar o CNPJ, incluindo pontos e barras - (Ex.: 00.000.000/0001-00)\n\n";
	if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
		if (erro.length == 0) erro += "ERRO - LEIA COM ATENÇÃO!\nVocê não digitou os pontos e barras no CNPJ! (Ex.: 00.000.000/0001-00)\n\n";
	}

	if(document.layers && parseInt(navigator.appVersion) == 4){
		x = CNPJ.substring(0,2);
		x += CNPJ.substring(3,6);
		x += CNPJ.substring(7,10);
		x += CNPJ.substring(11,15);
		x += CNPJ.substring(16,18);
		CNPJ = x;	
	} else {
		CNPJ = CNPJ.replace(".","");
		CNPJ = CNPJ.replace(".","");
		CNPJ = CNPJ.replace("-","");
		CNPJ = CNPJ.replace("/","");
	}
	var nonNumbers = /\D/;
	if (nonNumbers.test(CNPJ)) erro += "A verificação de CNPJ suporta apenas números! \n\n";	
	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])){
		erro +="Digito verificador do CNPJ inválido!";
	}
	if (erro.length > 0){
		alert(erro);
		return false;
	}
	return true;
}

function validaCPF(cpf) {
	valor = true;
	erro = new String;
	if (cpf.length < 11) erro += "São necessários 11 digitos para verificação do CPF! \n\n"; 

	if(document.layers && parseInt(navigator.appVersion) == 4){
		x = cpf.substring(0,3);
		x += cpf.substring(4,7);
		x += cpf.substring(8,11);
		cpf = x;	
	} else {
		cpf = cpf.replace(".","");
		cpf = cpf.replace(".","");
		cpf = cpf.replace(".","");
		cpf = cpf.replace("-","");
	}
	var nonNumbers = /\D/;
	if (nonNumbers.test(cpf)) erro += "A verificação de CPF suporta apenas números! \n\n";	
	if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
		  erro += "Número de CPF inválido!"
	}
	var a = [];
	var b = new Number;
	var c = 11;
	for (i=0; i<11; i++){
		a[i] = cpf.charAt(i);
		if (i < 9) b += (a[i] *  --c);
	}
	if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
	b = 0;
	c = 11;
	for (y=0; y<10; y++) b += (a[y] *  c--); 
	if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
	if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
		erro +="Digito verificador do CPF inválido!";
	}
	if (erro.length > 0){
		alert(erro);
		return false;
	}
	return true;
}