function preloadImages() {
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function mascara(o,f) {
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}

function execmascara() {
    v_obj.value=v_fun(v_obj.value)
}

function f_cpf(v) {
	v=v.replace(/\D/g,"")
	v=v.replace(/(\d{3})(\d)/,"$1.$2")
	v=v.replace(/(\d{3})(\d)/,"$1.$2")
	v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2")
	return v
}

function f_cnpj(v) {
	v=v.replace(/\D/g,"")
	v=v.replace(/^(\d{2})(\d)/,"$1.$2")
	v=v.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3")
	v=v.replace(/\.(\d{3})(\d)/,".$1/$2")
	v=v.replace(/(\d{4})(\d)/,"$1-$2")
	return v
}

function f_tel(v) {
	v=v.replace(/\D/g,"")
	v=v.replace(/^(\d\d)(\d)/g,"($1) $2")
	v=v.replace(/(\d{4})(\d)/,"$1-$2")
	return v
}

function f_cep(v) {
	v=v.replace(/\D/g,"")
	v=v.replace(/(\d{5})(\d)/,"$1-$2")
	return v
}

function f_data(v) {
	v=v.replace(/\D/g,"")
	v=v.replace(/(\d{2})(\d)/,"$1/$2")
	v=v.replace(/(\d{2})(\d)/,"$1/$2")
	
	return v
}

function f_num(v) {
	return v.replace(/\D/g,"")
}

function mudaPlanos(topico) {
	document.getElementById('planos-index').style.display = document.getElementById('chip-index').style.display = "none";
	document.getElementById(topico + '-index').style.display = "block";
	
	if (topico == "planos") {
		document.abaPlanos.src = "img/aba-planos-selec.gif";
		document.abaChip.src = "img/aba-chip.gif";
	} else {
		document.abaPlanos.src = "img/aba-planos.gif";
		document.abaChip.src = "img/aba-chip-selec.gif";
	}
}

function mudaTarifas(servico) {
	document.getElementById('aba-ata').style.zIndex = "3";
	document.getElementById('aba-xlite').style.zIndex = "2";
	document.getElementById('aba-central').style.zIndex = "1";
	document.getElementById('aba-ata').className = document.getElementById('aba-xlite').className = document.getElementById('aba-central').className = "aba";
	document.getElementById('img-ata').innerHTML = document.getElementById('img-xlite').innerHTML = document.getElementById('img-central').innerHTML = "<img src='img/aba.png' alt='' />";
	document.getElementById('tarifas-ata').style.display = document.getElementById('tarifas-xlite').style.display = document.getElementById('tarifas-central').style.display = "none";

	document.getElementById('aba-' + servico).style.zIndex = "4";
	document.getElementById('aba-' + servico).className = "aba selec";
	document.getElementById('img-' + servico).innerHTML = "<img src='img/aba-selec.png' alt='' />";
	document.getElementById('tarifas-' + servico).style.display = "block";
}

function mudaSituacao(situacao) {
	document.getElementById('dados-usuario').style.display = "block";
	document.getElementById('navegacao').style.display = "block";
	
	if (situacao == "pf") {
		document.getElementById('img-pf').innerHTML = "<img src='img/situacao-pf-selec.png' alt='' />";
		document.getElementById('img-pj').innerHTML = "<img src='img/situacao-pj.png' alt='' />";
		document.getElementById('pf').style.display = document.getElementById('i-pf').style.display = "block";
		document.getElementById('pj').style.display = document.getElementById('i-pj').style.display = "none";
	} else {
		document.getElementById('img-pj').innerHTML = "<img src='img/situacao-pj-selec.png' alt='' />";
		document.getElementById('img-pf').innerHTML = "<img src='img/situacao-pf.png' alt='' />";
		document.getElementById('pj').style.display = document.getElementById('i-pj').style.display = "block";
		document.getElementById('pf').style.display = document.getElementById('i-pf').style.display = "none";
	}
}

function formaPagamento(forma) {
	if (forma == "cartao") {
		document.getElementById('cartao').style.display = document.getElementById('i-cartao').style.display = "block";
	} else {
		document.getElementById('cartao').style.display = document.getElementById('i-cartao').style.display = "none";
	}
}

function getValue(input, tipo) {
	if (tipo == 'radio') {
		for (i=0; i<input.length; i++) {
			if (input[i].checked == true) {
				return input[i].value;
			}
		}
	}
	
	if (tipo == 'check') {
		var matriz = Array();
		var gui = String();
		var primeiro = 1;
		
		for (i=0; i<input.length; i++) {
			if (input[i].checked == true) {
				matriz[i] = input[i].value;

				if (primeiro == 1) {
					gui += matriz[i];
					primeiro = 0;
				} else {
					gui += ', ' + matriz[i];
				}
			}
		}
		
		return gui;
	}
	
	if (tipo == 'total') {
		var total = 0;
		
		input = document.formCompre.plano;
		
		for (i=0; i<input.length; i++) {
			if (input[i].checked == true) {
				if (input[i].value == "Light ATA") {
					total += 497.95;
				}
				if (input[i].value == "Light X-Lite") {
					total += 98.95;
				}
				if (input[i].value == "Light Central") {
					total += 98.95;
				}
				
				if (input[i].value == "Basic ATA") {
					total += 522.95;
				}
				if (input[i].value == "Basic X-Lite") {
					total += 123.95;
				}
				if (input[i].value == "Basic Central") {
					total += 123.95;
				}
				
				if (input[i].value == "Standard ATA") {
					total += 571.00;
				}
				if (input[i].value == "Standard X-Lite") {
					total += 174.00;
				}
				if (input[i].value == "Standard Central") {
					total += 174.00;
				}
				
				if (input[i].value == "Plus2 ATA") {
					total += 623.00;
				}
				if (input[i].value == "Plus2 X-Lite") {
					total += 224.00;
				}
				if (input[i].value == "Plus2 Central") {
					total += 224.00;
				}
				
				if (input[i].value == "Plus5 ATA") {
					total += 698.00;
				}
				if (input[i].value == "Plus5 X-Lite") {
					total += 299.00;
				}
				if (input[i].value == "Plus5 Central") {
					total += 299.00;
				}
				
				if (input[i].value == "Master ATA") {
					total += 2048.00;
				}
				if (input[i].value == "Master X-Lite") {
					total += 1649.00;
				}
				if (input[i].value == "Master Central") {
					total += 1649.00;
				}
				
				if (input[i].value == "New Chip Voip") {
					total += 149.95;
				}
			}
		}
		
		input = document.formCompre.pack;

		for (i=0; i<input.length; i++) {
			if (input[i].checked == true) {
				if (input[i].value == "New Pack1") {
					total += 15;
				}
				if (input[i].value == "New Pack2") {
					total += 25;
				}
				if (input[i].value == "New Pack3") {
					total += 50;
				}
				if (input[i].value == "New Pack4") {
					total += 100;
				}
			}
		}

		total = total.toFixed(2);
		total = total.toString();
		total = total.replace(".",",");

		return total;
	}
}

function mudaPagina(pagina) {
	function change() {
		for (i=1; i<=4; i++) {
			document.getElementById('aba-' + i).className = "";
			document.getElementById('img-' + i).innerHTML = "<img src='img/aba-compra.png' alt='' />";
			document.getElementById('compre-' + i).style.display = "none";
		}
		
		document.getElementById('aba-' + pagina).className = "selec";
		document.getElementById('img-' + pagina).innerHTML = "<img src='img/aba-compra-selec.png' alt='' />"
		document.getElementById('compre-' + pagina).style.display = "block";
	}

	if (pagina == 1) {
		change();
	}

	if (pagina == 2) {
		if (document.formCompre.nome.value == "") {
			alert('Digite o seu nome.');
			document.formCompre.nome.focus();
			return false;
		}
		
		if (document.formCompre.email.value == "") {
			alert('Digite o seu e-mail.');
			document.formCompre.email.focus();
			return false;
		}

		if (document.formCompre.email.value != "") {
			elemento = document.formCompre.email.value;
			
			if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(elemento)) {
			} else {
				alert('Digite um e-mail válido.');
				document.formCompre.email.focus();
				return false;
			}
		}
		
		if (document.formCompre.conf_email.value == "") {
			alert('Confirme o seu e-mail.');
			document.formCompre.conf_email.focus();
			return false;
		}
		
		if (document.formCompre.conf_email.value != document.formCompre.email.value) {
			alert('A confirmação de e-mail está incorreta.');
			document.formCompre.conf_email.focus();
			return false;
		}
		
		if (document.formCompre.pais.value == "") {
			alert('Digite o seu país.');
			document.formCompre.pais.focus();
			return false;
		}
		
		if (document.formCompre.estado.value == "") {
			alert('Digite o seu estado.');
			document.formCompre.estado.focus();
			return false;
		}
		
		if (document.formCompre.cidade.value == "") {
			alert('Digite a sua cidade.');
			document.formCompre.cidade.focus();
			return false;
		}
		
		if (document.formCompre.endereco.value == "") {
			alert('Digite o seu endereço.');
			document.formCompre.endereco.focus();
			return false;
		}
		
		if (document.formCompre.numero.value == "") {
			alert('Digite o nº do seu endereço.');
			document.formCompre.numero.focus();
			return false;
		}
		
		if (document.formCompre.bairro.value == "") {
			alert('Digite o seu bairro.');
			document.formCompre.bairro.focus();
			return false;
		}
		
		if (document.formCompre.cep.value == "") {
			alert('Digite o seu CEP.');
			document.formCompre.cep.focus();
			return false;
		}
		
		if (document.formCompre.senha.value == "") {
			alert('Digite a sua senha.');
			document.formCompre.senha.focus();
			return false;
		}
		
		if (document.formCompre.senha.value.length < 6) {
			alert('Sua senha deve possuir no mínimo 6 caracteres.');
			document.formCompre.senha.focus();
			return false;
		}
		
		if (document.formCompre.conf_senha.value == "") {
			alert('Confirme a sua senha.');
			document.formCompre.conf_senha.focus();
			return false;
		}
		
		if (document.formCompre.conf_senha.value != document.formCompre.senha.value) {
			alert('A confirmação de senha está incorreta.');
			document.formCompre.conf_senha.focus();
			return false;
		}

		pessoa = getValue(document.formCompre.situacao, 'radio');

		if (pessoa == "Pessoa Física") {
			if (document.formCompre.nascimento.value == "") {
				alert('Digite sua data de nascimento.');
				document.formCompre.nascimento.focus();
				return false;
			}
			
			if (document.formCompre.cpf.value == "") {
				alert('Digite o seu CPF.');
				document.formCompre.cpf.focus();
				return false;
			}
			
			if (document.formCompre.rg.value == "") {
				alert('Digite o seu RG.');
				document.formCompre.rg.focus();
				return false;
			}
			
			if (document.formCompre.fixo_pf.value == "") {
				alert('Digite o seu telefone.');
				document.formCompre.fixo_pf.focus();
				return false;
			}
		}
		
		if (pessoa == "Pessoa Jurídica") {
			if (document.formCompre.cnpj.value == "") {
				alert('Digite o seu CNPJ.');
				document.formCompre.cnpj.focus();
				return false;
			}
			
			if (document.formCompre.fixo_pj.value == "") {
				alert('Digite o seu telefone.');
				document.formCompre.fixo_pj.focus();
				return false;
			}
		}
		
		if (document.formCompre.contrato.checked == false) {
			alert('É necessário aceitar as condições do contrato New Voip antes de prosseguir.');
			document.formCompre.contrato.focus();
			return false;
		}

		change();
	}
	
	if (pagina == 3) {
		planox = getValue(document.formCompre.plano, 'radio');
		packx = getValue(document.formCompre.pack, 'radio');
		
		if (planox == null && packx == null) {
			alert('Selecione os serviços que você deseja adquirir.');
			return false;
		}
		
		change();
	}

	if (pagina == 4) {
		forma = getValue(document.formCompre.pagamento, 'radio');

		if (forma == undefined) {
			alert('Selecione a forma de pagamento.');
			document.formCompre.pagamento.focus();
			return false;
		}

		if (forma == "Cartão de crédito") {
			if (document.formCompre.titular.value == "") {
				alert('Digite o nome do titular.');
				document.formCompre.titular.focus();
				return false;
			}
			
			if (document.formCompre.num_cartao.value == "") {
				alert('Digite o número do cartão de crédito.');
				document.formCompre.num_cartao.focus();
				return false;
			}
			
			if (document.formCompre.codigo.value == "") {
				alert('Digite o código de segurança, localizado no verso do cartão.');
				document.formCompre.codigo.focus();
				return false;
			}
			
			if (document.formCompre.validade.value == "") {
				alert('Digite a data de validade.');
				document.formCompre.validade.focus();
				return false;
			}
		}
		
		change();
		
		document.getElementById('i-nome').innerHTML = document.formCompre.nome.value;
		document.getElementById('i-pais').innerHTML = document.formCompre.pais.value;
		document.getElementById('i-email').innerHTML = document.formCompre.email.value;
		document.getElementById('i-estado').innerHTML = document.formCompre.estado.value;
		document.getElementById('i-cidade').innerHTML = document.formCompre.cidade.value;
		document.getElementById('i-endereco').innerHTML = document.formCompre.endereco.value;
		document.getElementById('i-numero').innerHTML = document.formCompre.numero.value;
		document.getElementById('i-complemento').innerHTML = document.formCompre.complemento.value;
		document.getElementById('i-bairro').innerHTML = document.formCompre.bairro.value;
		document.getElementById('i-cep').innerHTML = document.formCompre.cep.value;
		document.getElementById('i-indicacao').innerHTML = document.formCompre.indicacao.value;

		document.getElementById('i-cnpj').innerHTML = document.formCompre.cnpj.value;
		document.getElementById('i-fixo-pj').innerHTML = document.formCompre.fixo_pj.value;
		document.getElementById('i-cel-pj').innerHTML = document.formCompre.cel_pj.value;

		document.getElementById('i-sexo').innerHTML = getValue(document.formCompre.sexo, 'radio');
		document.getElementById('i-nascimento').innerHTML = document.formCompre.nascimento.value;
		document.getElementById('i-cpf').innerHTML = document.formCompre.cpf.value;
		document.getElementById('i-rg').innerHTML = document.formCompre.rg.value;
		document.getElementById('i-fixo-pf').innerHTML = document.formCompre.fixo_pf.value;
		document.getElementById('i-cel-pf').innerHTML = document.formCompre.cel_pf.value;

		document.getElementById('i-planos').innerHTML = document.formCompre.planos.value = getValue(document.formCompre.plano, 'check');
		document.getElementById('i-packs').innerHTML = document.formCompre.packs.value = getValue(document.formCompre.pack, 'check');
		document.getElementById('i-total').innerHTML = document.formCompre.total.value = getValue(null, 'total');

		document.getElementById('i-pagamento').innerHTML = getValue(document.formCompre.pagamento, 'radio');

/* A linha abaixo está desabilitada pois no momento existe apenas uma operadora de cartão */
//		document.getElementById('i-operadora').innerHTML = getValue(document.formCompre.operadora, 'radio');

		document.getElementById('i-titular').innerHTML = document.formCompre.titular.value;
		document.getElementById('i-num-cartao').innerHTML = document.formCompre.num_cartao.value;
		document.getElementById('i-codigo').innerHTML = document.formCompre.codigo.value;
		document.getElementById('i-validade').innerHTML = document.formCompre.validade.value;
	}
}

function comprar() {
	document.formCompre.submit();
}

totalDuvidas = 99;
atual = '';

function abreDuvidas(duvida) {
	for (i=1; i<=totalDuvidas; i++) {
		if (i!=52) {
			document.getElementById('bullet-' + i).innerHTML = "<img src='img/topico-duvidas-fechado.png' alt='' />";
			document.getElementById('topico-' + i).style.color = "#003a71";
			document.getElementById('duvida-' + i).style.display = "none";
		}
	}

	if (atual != duvida) {
		document.getElementById('bullet-' + duvida).innerHTML = "<img src='img/topico-duvidas-aberto.png' alt='' />";
		document.getElementById('topico-' + duvida).style.color = "#d22f34";
		document.getElementById('duvida-' + duvida).style.display = "block";
		atual = duvida;
	} else {
		document.getElementById('bullet-' + duvida).innerHTML = "<img src='img/topico-duvidas-fechado.png' alt='' />";
		document.getElementById('topico-' + duvida).style.color = "#003a71";
		document.getElementById('duvida-' + duvida).style.display = "none";
		atual = '';
	}
}

function contato() {
	if (document.formContato.nome.value == "") {
		alert('Digite o seu nome.');
		document.formContato.nome.focus();
		return false;
	}
	
	if (document.formContato.email.value == "") {
		alert('Digite o seu e-mail.');
		document.formContato.email.focus();
		return false;
	}
	
	if (document.formContato.email.value != "") {
		elemento = document.formContato.email.value;
		
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(elemento)) {
		} else {
			alert('Digite um e-mail válido.');
			document.formContato.email.focus();
			return false;
		}
	}

	if (document.formContato.dpto.value == "") {
		alert('Selecione um departamento.');
		document.formContato.dpto.focus();
		return false;
	}
	
	if (document.formContato.mensagem.value == "") {
		alert('Digite a mensagem.');
		document.formContato.mensagem.focus();
		return false;
	}

	document.formContato.submit();
}
