// JavaScript Document
criador = "Eduardo Bastian";

//valor do frete
var valorFrete = null;
var valorFreteConta = null;

//var descontoCompra = 7;

var minValorParcCartao = 50;
var apenasCartao = false;

//total de parcelas
var totalParcelas = 6;
//nro de parcelas da compra
var nroParcelas = 1;
//valor total da compra
var valorTotal = 0;

//valor do envio por encomenda - pac
var valorPAC = 15;

var personalizarPac = false;

//forma pagamento     0- depósito   1-sedex a cobrar     2-nada por enquanto
var formaPagamento = 3;

//forma envio correio     0-sedex convecional    1-PAC     2-sedex a cobrar
var formaEnvioCorreio = 0;
//armazena a formaEnvioCorreio para backup´na troca da forma de pagamento
var backFormaEnvioCorreio = 0;

function carregaPagPrecosFisiM() {
	try {			
		for(contF=1;contF<4;contF++) {
			produtoId = 11+contF;
			if(arrConfigProdutos[produtoId][1]!="desativado") {
				str = arrConfigProdutos[produtoId][1];
				if(Number(arrConfigProdutos[produtoId][8])==0) {	
					jTabProd = Number(arrConfigProdutos[produtoId][11].split("/")[1]);
				} else {
					jTabProd = Number(arrConfigProdutos[produtoId][11].split("/")[0]);
				}
				valorBase = Number(arrConfigProdutos[produtoId][4]);		
				document.getElementById("divValor1"+contF).innerHTML = "R$ " + formataDinheiro(valorBase);
				document.getElementById("divMaxVezes"+contF).innerHTML = Number(arrConfigProdutos[produtoId][12]);
				
				if(Number(arrConfigProdutos[produtoId][12])==1) {
					document.getElementById("tabAPrazo"+cont).className = "tabInvisivel";
				} else {
					for(cont2=totalParcelas;cont2>1;cont2--) {
						strAux = (cont2)+"Vezes"+contF;
						if(cont2<=Number(arrConfigProdutos[produtoId][12])) {
							document.getElementById("div"+strAux).innerHTML = cont2 + "x de R$ " +formataDinheiro(arredondar(calcularValorTabela(valorBase, jTabProd, cont2)/cont2, 2));
						} else {
							document.getElementById("tr"+strAux).className = "tabInvisivel";
						}
					}
				}
				if(Number(arrConfigProdutos[produtoId][8])==0) {			
					document.getElementById("trPromo"+contF).className = "tabInvisivel";					
				} else {
					dataProm = arrConfigProdutos[produtoId][8];
					dataProm = dataProm.substring(0, dataProm.indexOf("$"));
					document.getElementById("divData"+contF).innerHTML = "<span class=\"fonte22\" style=\"color:#DD0000;\">Promo&ccedil;&atilde;o v&aacute;lida para todo territ&oacute;rio nacional at&eacute; dia " + dataProm + ".</span>";
				}
			}
		}
	} catch(e) {
		alert(e);
		//carregaPagPrecosFisiM();	
	}
}

function arredondarParaCima(valor) {
	if(valor-Math.floor(valor)>0) {
		return Math.floor(valor)+1;
	} else {
		return valor;
	}
}

function calcularValorTabela(valorBase, juros, nParcelas) {
	for(cont=1;cont<nParcelas;cont++) {
		valorBase += arredondarParaCima(valorBase*juros);		
	}
	return valorBase;
}

function carregaPagPrecosProd(produtoId) {
	try {
		if(Number(arrConfigProdutos[produtoId][8])==0) {	
			jTabProd = Number(arrConfigProdutos[produtoId][11].split("/")[1]);
		} else {
			jTabProd = Number(arrConfigProdutos[produtoId][11].split("/")[0]);
		}
		valorBase = Number(arrConfigProdutos[produtoId][4]);		
		document.getElementById("divValor1").innerHTML = "R$ " + formataDinheiro(valorBase);	
		document.getElementById("divMaxVezes").innerHTML = Number(arrConfigProdutos[produtoId][12]);
		if(Number(arrConfigProdutos[produtoId][12])==1) {
			document.getElementById("tabAPrazo").className = "tabInvisivel";
		} else {
			for(i=totalParcelas;i>1;i--) {
				if(document.getElementById("div" + (i) + "Vezes")!=null) {
					if(i<=Number(arrConfigProdutos[produtoId][12])) {
						document.getElementById("div" + (i) + "Vezes").innerHTML = i + "x de R$ " + formataDinheiro(arredondar(calcularValorTabela(valorBase, jTabProd, i)/i, 2));
					} else {
						document.getElementById("tr" + (i) + "Vezes").className = "tabInvisivel";
					}
				}
			}
		}
		if(Number(arrConfigProdutos[produtoId][8])==0) {			
			document.getElementById("divData").className = "tabInvisivel";
		} else {
			dataProm = arrConfigProdutos[produtoId][8];
			dataProm = dataProm.substring(0, dataProm.indexOf("$"));
			document.getElementById("divData").innerHTML = "<span class=\"fonte22\" style=\"color:#DD0000;\">Promo&ccedil;&atilde;o v&aacute;lida para todo territ&oacute;rio nacional at&eacute; dia " + dataProm + ".</span>";
		}
		carregaPacotes();
	} catch(e) {
	 	carregaPagPrecosProd(produtoId);
	}
}

function carregaPacotes() {
	primeirasAdicoes = false;
	nroProdsCompra = 0;
	scrollTo(0, 0);	
	passoCompra = 1;
	nroParcelas = 1;
	contProdutos = 0;
	valorFrete = null;
	formaEnvio = 0;
	arrCestaCompras = new Array();
	for(i=1;i<=nroPacotes;i++) {
		div = document.getElementById("divPacote"+i);
		if(div!=null) {
			switch(i-1) {				
				case 0: {
					//avalie + fonospeak
					montaPrecosPacote(div, new Array(2,10));
					break;
				}
				case 1: {
					//fonoFlex + fonospeak
					montaPrecosPacote(div, new Array(8, 10));
					break;
				}
				case 2: {
					//avalie + fonospeak + desafio dos fonemas
					montaPrecosPacote(div, new Array(2,10, 4));					
					break;
				}
				case 3: {
					//avalie + voiceReport
					montaPrecosPacote(div, new Array(2,5));					
					break;
				}
				case 4: {
					//voxMetria + voxGames
					montaPrecosPacote(div, new Array(9,7));					
					break;
				}
				case 5: {
					//voxMetria + fonoTools
					montaPrecosPacote(div, new Array(9,6));					
					break;
				}
				case 6: {
					//voxMetria + fonoView
					montaPrecosPacote(div, new Array(9,1));					
					break;
				}
				case 7: {
					//earmix + pedro
					montaPrecosPacote(div, new Array(15,3));					
					break;
				}
				case 8: {
					//pedro parque + pedro casa
					montaPrecosPacote(div, new Array(16,3));					
					break;
				}
				case 9: {
					//pedro parque + earmix
					montaPrecosPacote(div, new Array(16,15));					
					break;
				}
				case 10: {
					//adaptador andrea + voxMetria
					montaPrecosPacote(div, new Array(19,20));					
					break;
				}
				case 11: {
					//adaptador andrea + fonoview
					montaPrecosPacote(div, new Array(19,21));					
					break;
				}
				case 12: {
					//adaptador andrea + voxMetria + fonoview
					montaPrecosPacote(div, new Array(19,20,21));					
					break;
				}	
			}			
		}
		delete div;
	}		
}

function montaPrecosPacote(lugar, arrCodesProds) {			

	precoPacoteNormal = 0;
	precoPacoteDesc = 0;
	valorPacote4x = 0;
	valorPacote6x = 0;

	for(ii=0;ii<arrCodesProds.length;ii++) {
		vBase = Number(arrConfigProdutos[arrCodesProds[ii]][4]);
		dProd = Number(arrConfigProdutos[arrCodesProds[ii]][10]);
		//jProd = Number(arrConfigProdutos[arrCodesProds[ii]][11]);
		jAux = arrConfigProdutos[arrCodesProds[ii]][11].split("/");
		//juros do produto para valores em pacote
		jProd = Number(jAux[0]);
		//juros do produto para valores de tabela
		jTabProd = Number(jAux[1]);
		//número máximo de parcelas que pode dividir o produto
		maxParcelas = Number(arrConfigProdutos[arrCodesProds[ii]][12]);

		//preco antigo do produto (no caso de produto em promocao)
		pVelhoProd = arrConfigProdutos[arrCodesProds[ii]][8];
		precoPacoteDesc += (arredondarParaCima(vBase - vBase*dProd));

		if(String(pVelhoProd)!="0") {
			pVelhoProd = Number(pVelhoProd.substring(pVelhoProd.indexOf("$")+1, pVelhoProd.length));
			pVelhoProd = calcularValorTabela(Number(pVelhoProd), jTabProd, 1);
		} else {
			pVelhoProd = vBase;
		}
		valorPacote4x += arredondarParaCima((vBase - vBase*dProd)*(Math.pow((1+(jProd)), Number(4)-1)));
		valorPacote6x += arredondarParaCima((vBase - vBase*dProd)*(Math.pow((1+(jProd)), Number(6)-1)));

		precoPacoteNormal += pVelhoProd;
	}

	valorPacote4x = valorPacote4x/4;
	valorPacote6x = valorPacote6x/6;

	if(formataDinheiro(precoPacoteNormal)!=formataDinheiro(precoPacoteDesc)) {
		lugar.innerHTML = "à Vista: de <span style=\"color:#0066CC;\" class=\"fonte2\">R$ " + formataDinheiro(precoPacoteNormal) + "</span><br> por <span style=\"color:#0066CC;\" class=\"fonte2\">R$ " + formataDinheiro(precoPacoteDesc) + "</span><br>ou 4x de R$ " + formataDinheiro(valorPacote4x) + "<br>ou 6x de R$ " + formataDinheiro(valorPacote6x);
	} else {
		lugar.innerHTML = "à Vista: <span style=\"color:#0066CC;\" class=\"fonte2\">R$ " + formataDinheiro(precoPacoteNormal) + "</span><br>ou 4x de R$ " + formataDinheiro(valorPacote4x) + "<br>ou 6x de R$ " + formataDinheiro(valorPacote6x);
	}
}


codProdutoComprar = null;
passoCompra = -1;
nroProdsCompra = 0;
primeirasAdicoes = true;

function preparaCompra() {
	
	valorFreteConta = 0;
	passo = -1;
	
	selNroParcelas("chk1");
	selFormaPagamento("chkB");
	selFormaEnvioCorreio("chkSC");
	
	document.getElementById("trFrete").className = "tabInvisivel";

	document.getElementById("tdLblDesconto").className = "tdSeparadorBottom";
	document.getElementById("tdDesconto").className = "tdSeparadorBottom";
	
	habilitaOpcoesParcelas();
}

function comprarMaisProdutos(codProdutoComprar) {
	scrollTo(0, 0);	
	personalizarPac = false;
	primeirasAdicoes = true;
	nroProdsCompra = 1;
	passoCompra = 1;
	nroParcelas = 1;
	contProdutos = 0;
	valorFrete = null;
	formaEnvio = 0;
	arrCestaCompras = new Array();
	//carregaPagina("comprarProdutos.html{adicionarProduto!" + codProdutoComprar + "&" + formaEnvio + "&1");	
	carregarPaginaEm("comprarProdutos.html{adicionarProduto!" + codProdutoComprar + "&" + formaEnvio + "&1", "corpo");
}

function comprarPacote(codPacote) {	
	scrollTo(0, 0);
	personalizarPac = false;
	passoCompra = 1;
	nroParcelas = 1;
	contProdutos = 0;
	valorFrete = null;
	formaEnvio = 0;
	arrCestaCompras = new Array();
	if(Number(codPacote)!=-1) {
		primeirasAdicoes = true;
		switch(Number(codPacote)) {
			case 0: {
				produtosIds = "2@10";
				nroProdsCompra = 2;
				break;
			}
			case 1: {
				produtosIds = "8@10";
				nroProdsCompra = 2;			
				break;
			}
			case 2: {
				produtosIds = "2@10@4";
				nroProdsCompra = 3;
				break;
			}
			case 3: {
				produtosIds = "2@5";
				nroProdsCompra = 2;
				break;
			}
			case 4: {
				produtosIds = "9@7";
				nroProdsCompra = 2;
				break;
			}
			case 5: {
				produtosIds = "9@6";
				nroProdsCompra = 2;
				break;
			}
			case 6: {
				produtosIds = "9@1";
				nroProdsCompra = 2;
				break;
			}
			case 7: {
				produtosIds = "15@3";
				nroProdsCompra = 2;
				break;
			}
			case 8: {
				produtosIds = "16@3";
				nroProdsCompra = 2;
				break;
			}
			case 9: {
				produtosIds = "16@15";
				nroProdsCompra = 2;
				break;
			}
			case 10: {
				produtosIds = "19@20";
				nroProdsCompra = 2;
				break;
			}
			case 11: {
				produtosIds = "19@21";
				nroProdsCompra = 2;
				break;
			}
			case 12: {
				produtosIds = "19@20@21";
				nroProdsCompra = 3;
				break;
			}
		}
		//carregaPagina("comprarProdutos.html{adicionarProdutosCompra!" + produtosIds);
		carregarPaginaEm("comprarProdutos.html{adicionarProdutosCompra!" + produtosIds, "corpo");
	} else {
		personalizarPac = true;
		primeirasAdicoes = false;
		nroProdsCompra = 0;
		carregarPaginaEm("comprarProdutos.html", "corpo");
	}
}

function comprarPacotePersonalizado(produtosIds) {	
	scrollTo(0, 0);
	personalizarPac = false;
	passoCompra = 1;
	nroParcelas = 1;
	contProdutos = 0;
	valorFrete = null;
	formaEnvio = 0;
	arrCestaCompras = new Array();
	nroProdsCompra = produtosIds.split("@").length;
	carregarPaginaEm("comprarProdutos.html{adicionarProdutosCompra!" + produtosIds, "corpo");
}

function personalizarPacote() {
	scrollTo(0, 0);
	personalizarPac = true;
	primeirasAdicoes = false;
	nroProdsCompra = 0;	
	passoCompra = 1;
	arrCestaCompras = new Array();
	carregarPaginaEm("comprarProdutos.html", "corpo");
}

//#listaPacotes.html{personalizarPacote"

function adicionarProdutosCompra(arrProdPacote) {
	if(arrProdPacote.indexOf("@")!=-1) {
		arrProdPacote = arrProdPacote.split("@");
		for(i=0;i<Number(arrProdPacote.length);i++)	{
			//alert(i + "/" + arrProdPacote.length + "  : " + arrConfigProdutos[Number(arrProdPacote[i])][1]);
			//adicionarProduto(Number(arrProdPacote[i]), 0, 1);
			funcao = "adicionarProduto(" + arrProdPacote[i] + ", 0, 1)";
			window.setTimeout(funcao, i*250);
		}
	}
}

function addProdutoSimples(codProd) {
	adicionarProduto(codProd, 0, 1);
}

function setaValorFrete(objeto, idLugar) {
	if(objeto.value!=-1 && objeto.value!=null && objeto.value!="") {
		valorFrete = Number(getValorFrete(objeto.value));
		//document.getElementById(idLugar).innerHTML = "R$ " + formataDinheiro(valorFrete);
		//document.getElementById("trValores").className = "trVisivel";
		document.getElementById("trEnvio").className = "trVisivel";
		//document.getElementById("trConfig").className = "trVisivel";
		//document.getElementById("trOpcoes1").className = "trVisivel";
		document.getElementById("trFrete").className = "trVisivel";
		document.getElementById("tdLblDesconto").className = "";
		document.getElementById("tdDesconto").className = "";
		document.getElementById("imgPasso3").src = "images/barPasso3.jpg";		
		
		if(formaEnvioCorreio==0) {
			selFormaEnvioCorreio("chkSC");
		} else if(formaEnvioCorreio==1) {
			selFormaEnvioCorreio("chkPAC");
		} else if(formaEnvioCorreio==2) {
			//selFormaEnvioCorreio("chkSC");
		} else if(formaEnvioCorreio==3) {
			selFormaEnvioCorreio("chkDown");
		}
		
	} else {
		//document.getElementById("trValores").className = "tabInvisivel";
		document.getElementById("trEnvio").className = "tabInvisivel";	
		document.getElementById("trFrete").className = "tabInvisivel";
		document.getElementById("tdLblDesconto").className = "tdSeparadorBottom";
		document.getElementById("tdDesconto").className = "tdSeparadorBottom";
		
		document.getElementById("imgPasso3").src = "images/barPasso3_2.jpg";	
		
		valorFrete = 0;
	}
	calcularValores();
}

function calcularMaiorParcela() {
	maiorParcela = 1;
	for(i=0;i<arrCestaCompras.length;i++) {
		if(arrConfigProdutos[arrCestaCompras[i][0]][12]>maiorParcela) {
			maiorParcela = arrConfigProdutos[arrCestaCompras[i][0]][12];
		}
	}
	return maiorParcela;
}

/*
function calcularMaiorValorProd(produtoId) {
	for(i=1;i<=4;i++) {
		if(arrCestaCompras[produtoId]!=null) {
			if(Number(arrConfigProdutos[arrCestaCompras[produtoId][0]][3+i])==0) {
				return Number(arrConfigProdutos[arrCestaCompras[produtoId][0]][3+i-1]);
			}
		} else {
			return i-1;
		}
	}
}
*/
function calcularMaiorValorProd(produtoId) {
	for(i=1;i<=4;i++) {
		if(Number(arrConfigProdutos[produtoId][3+i])==0) {
			return Number(arrConfigProdutos[produtoId][3+i-1]);
		}
	}
}

function calcularMaiorParcProd(produtoId) {
	str = "produtoId: " + produtoId;
	for(i=1;i<=4;i++) {
		str += "\n  " + arrConfigProdutos[produtoId][3+i];
		if(Number(arrConfigProdutos[produtoId][3+i])==0) {
			//alert(str + " >>> " + (i-1));
			return (i-1);
		}
	}
	//alert(str + " >>>> " + (i-1));
	return 4;
}

function calcularFreteSedex(campoValor, campoErro) {
	if(validaCEP(campoValor, campoErro)) {
		tempCEP = document.getElementById(campoValor).value.replace("-", "");
		calcularFrete(tempCEP, '85501090', '1', campoErro);
		document.getElementById(campoErro).innerHTML = document.getElementById(campoErro).innerHTML;		
	} else {
		document.getElementById(campoErro).innerHTML = "<span style=\"color:#FF4444;fonte-weight::bold;\">CEP Inválido.</span>";		
	}
}

function popUpProdutos(produtoCompraId) {
	mostraTelaEscura();
	
	document.getElementById("txtEstado").style.visibility = "hidden";
	
	if(produtoCompraId==null) {
		carregarPaginaDadosEm('popUpProdutos.html*' + -1 + "&" + 1 + "&" + "Adicionar" + "&" + "validaAdicaoProduto()&", 'divGeral');
	} else {
		v1 = arrCestaCompras[produtoCompraId][0];
		v2 = arrCestaCompras[produtoCompraId][2];
		v3 = "Ok";
		v4 = "salvarModificacoesCompra()";
		
		carregarPaginaDadosEm('popUpProdutos.html*' + v1 + "&" + v2 + "&" + v3 + "&" + v4 + "&disabled=\"disabled\" style=\"background-color:#FFFFFF;border: 1px solid #AED7FF;width:300px;\"", 'divGeral');
	}
	
	document.getElementById("divGeral").className = "divPopUpProdutos";
	document.getElementById("divGeral").style.height = "450px";
	document.getElementById("divGeral").style.left = Math.floor(wTela/2 - 500/2) + "px";
	yTopo = ((document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop);
	document.getElementById("divGeral").style.top = (yTopo+Math.floor(hTela/2)-(410/2)) + "px";
	document.getElementById("divGeral").style.visibility = "visible";
	
}

function fechaPopUpProd() {
	document.getElementById("divGeral").style.height = "405px";
	document.getElementById("txtEstado").style.visibility = "visible";
	fecharDiv();
}

function montaComboProdutos(itemIdSel, comboId, onChange, onKeyUp) {
	
	if(comboId==null) {
		comboId = "txtProdutos";
	}
	if(onChange==null) {
		onChange = montaOpcoesProdutos;
	}
	if(onKeyUp==null) {
		onKeyUp = montaOpcoesProdutos;
	}

	tempArray1 = new Array();
	for(i=0;i<arrConfigProdutos.length;i++) {
		if(arrConfigProdutos[i][1]!="desativado" && arrConfigProdutos[i][6]!="2") {
			tempArray1.push(arrConfigProdutos[i][1] + "@" + i);
		}
	}
	tempArray1.sort();

	if(ie) {
		selProd = document.getElementById(comboId);
		newProd = document.createElement('option');
		newProd.text = "< Selecione um produto >";
		newProd.value = -1;
		selProd.add(newProd);
		for(i=0;i<tempArray1.length;i++) {
			newProd = document.createElement('option');
			tempValue = tempArray1[i];
	
			prodN = tempValue.substring(0, tempValue.indexOf("@"));
			prodV = tempValue.substring(tempValue.indexOf("@")+1, tempValue.length);
	
			newProd.text = prodN;
			newProd.value = prodV;			
			selProd.add(newProd);			
		}
	} else {  
		document.getElementById(comboId).innerHTML = "<option value=\""+-1+"\"> &lt; Selecione um produto &gt;</option>";
		for(i=0;i<tempArray1.length;i++) {
			tempValue = tempArray1[i];			
			prodN = tempValue.substring(0, tempValue.indexOf("@"));
			prodV = tempValue.substring(tempValue.indexOf("@")+1, tempValue.length);
			document.getElementById(comboId).innerHTML += "<option value=\"" + prodV + "\">" + prodN + "</option>";
		}			
	}
	try {		
		document.getElementById("nomeProduto").innerHTML = "<center><strong>Selecione o produto que você deseja comprar.</center></strong>";
		document.getElementById("tabPrecos").className = "tabInvisivel";
		document.getElementById("imgProd").innerHTML = "";
	} catch(e) {}

	try {
		if(itemIdSel!=null) {
			document.getElementById(comboId).selectedIndex = Number(itemIdSel);
			onChange();
		}
	} catch(e) {		
	}

	addEvento(document.getElementById(comboId), "change", onChange, true);
	addEvento(document.getElementById(comboId), "keyup", onKeyUp, true);
}

function montaOpcoesProdutos() {
	prodId = document.getElementById("txtProdutos").value;
	if(prodId!=-1) {
		
		if(arrConfigProdutos[prodId][8]=="#") {
			//PRODUTO NÃO ESTÁ SENDO VENDIDO
			document.getElementById("nomeProduto").innerHTML = "<center><strong>" + (arrConfigProdutos[prodId][1]) + " será disponibilizado para compra pelo site nos próximos dias.</center></strong>";
			document.getElementById("tabPrecos").className = "tabInvisivel";
			document.getElementById("imgProd").innerHTML = "";
			document.getElementById("imgPromocao").className = "tabInvisivel";
			document.getElementById("txtProdutos").value = -1;			
		} else {			
			if(Number(arrConfigProdutos[prodId][8])==0) {
				document.getElementById("imgPromocao").className = "tabInvisivel";
				jTabProd = Number(arrConfigProdutos[prodId][11].split("/")[1]);	
			} else {		
				jTabProd = Number(arrConfigProdutos[prodId][11].split("/")[0]);	
				document.getElementById("imgPromocao").className = null;			
			}
			document.getElementById("tabPrecos").className = "tabGrid";
			document.getElementById("nomeProduto").innerHTML = "<span class=\"fonteG14\" align=\"center\">" + arrConfigProdutos[prodId][1] + "</span>";		
			document.getElementById("imgProd").innerHTML = "<img name=\"imgProd\" width=\"100px\" height=\"100px\" src=\"images/logos/" + arrConfigProdutos[prodId][3] + "\">";		

			valorBase = Number(arrConfigProdutos[prodId][4]);		
			
			for(i=1;i<=totalParcelas;i++) {							
				document.getElementById("par"+i).innerHTML = "";
				document.getElementById("total"+i).innerHTML = "";
				if(i<=Number(arrConfigProdutos[prodId][12])) {
					document.getElementById(i+"x").className = "trVisivel";
					document.getElementById("par"+i).innerHTML = "R$ " + formataDinheiro(arredondar(calcularValorTabela(valorBase, jTabProd, i)/i, 2));
					document.getElementById("total"+i).innerHTML = "R$ " + formataDinheiro(calcularValorTabela(valorBase, jTabProd, i));
				} else {
					document.getElementById(i+"x").className = "tabInvisivel";
				}
			}
			if(Number(arrConfigProdutos[prodId][12])>4) {
				document.getElementById("tdAvisoCartao").className = "trVisivel";
			} else {
				document.getElementById("tdAvisoCartao").className = "tabInvisivel";
			}
			document.getElementById("tdTxtProdutosErro").className = "tdAsteriscoOk";
			document.getElementById("tdProd").className = "compFormContainer";		
		}
	} else {
		document.getElementById("nomeProduto").innerHTML = "<center><strong>Selecione o produto que você deseja comprar.</center></strong>";
		document.getElementById("tabPrecos").className = "tabInvisivel";
		document.getElementById("imgProd").innerHTML = "";
		document.getElementById("imgPromocao").className = "tabInvisivel";
	}
}

function editarProdutoCompra(prodId, formaEnvio, quantidade) {
	nroLinha = -1;
	for(i=0;i<arrCestaCompras.length;i++) {		
		if(prodId==arrCestaCompras[i][0]) {
			nroLinha = i+1;
			break;
		}
	}	
	document.getElementById('gridProdutos').rows[nroLinha].cells[1].innerHTML = Number(quantidade);
		
	for(i=0;i<arrCestaCompras.length;i++) {
		if(arrCestaCompras[i][0]==prodId) {
			arrCestaCompras[i][2] = Number(quantidade);
		}
	}
	apenasCartao = false;
	refreshGrid();
	fechaPopUpProd();
}

function adicionarProduto(prodId, formaEnvio, quantidade) {
	
	/*formaEnvio
	0 - correio
	1 - download
	*/
	
	nroLinha = -1;
	for(i=0;i<arrCestaCompras.length;i++) {		
		if(prodId==arrCestaCompras[i][0]) {
			nroLinha = i+1;
			break;
		}
	}	
	
	if(String(arrConfigProdutos[prodId][8])!="0") {
		document.getElementById("trDescontoProm").className = "trVisivel";	
	}		
	
	precoProd = Number(arrConfigProdutos[prodId][3+Number(nroParcelas)]);		
	if(precoProd==0) {
		for(ii=Number(nroParcelas)-1;ii>0;ii--) {
			precoProd = Number(arrConfigProdutos[prodId][3+ii])*ii;
			if(precoProd!=0) {
				precoProd = arredondar((precoProd), 0);
				break;
			}
		}
	} else {
		precoProd = arredondar((precoProd*nroParcelas), 0);
	}		
	
	//produto novo
	if(nroLinha==-1) {
		if(formaEnvio==null)
			formaEnvio = 0;
		if(quantidade==null)
			quantidade = 1;	
		
		if(prodId!=null && prodId!=-1) {
			prodId = Number(prodId);
			arrCestaCompras[contProdutos] = new Array(prodId, formaEnvio, quantidade);				
			contProdutos++;
			if(contProdutos==1) {
				
				if(String(arrConfigProdutos[prodId][8])!="0") {
					document.getElementById('gridProdutos').rows[1].cells[0].innerHTML = "<div style=\"cursor:pointer;\" onMouseMove=\"javascript:mostraHint('PRODUTO EM PROMOÇÃO!', event, 180);\" onMouseOut=\"javascript:escondeAjuda();\"  align=\"center\"><img src=\"images/estrela.gif\" width=\"18\" height=\"17\"></div>";
				} else {
					document.getElementById('gridProdutos').rows[1].cells[0].innerHTML = "";
				}
				
				document.getElementById('gridProdutos').rows[1].cells[1].innerHTML = arrConfigProdutos[prodId][1];
				if(String(arrConfigProdutos[prodId][8])!="0") {
					document.getElementById('gridProdutos').rows[1].cells[1].innerHTML += "&nbsp;&nbsp;<span style=\"color:#FF0000\">(promoção)</span>";
				}				
				document.getElementById('gridProdutos').rows[1].cells[2].innerHTML = quantidade;
				
				document.getElementById('gridProdutos').rows[1].cells[3].innerHTML = ("R$ " + formataDinheiro(precoProd));
				
				document.getElementById('gridProdutos2').rows[1].cells[1].innerHTML = arrConfigProdutos[prodId][1];
				if(String(arrConfigProdutos[prodId][8])!="0") {
					document.getElementById('gridProdutos2').rows[1].cells[1].innerHTML += "&nbsp;&nbsp;<span style=\"color:#FF0000\">(promoção)</span>";
				}
				document.getElementById('gridProdutos2').rows[1].cells[2].innerHTML = quantidade;
				document.getElementById('gridProdutos2').rows[1].cells[3].innerHTML = ("R$ " + formataDinheiro(precoProd));
				
				document.getElementById('gridProdutos').rows[1].cells[4].innerHTML = "<div style=\"cursor:pointer;\" onClick=\"deletarProduto(this.parentNode.parentNode.id);\" align=\"center\"><img src=\"images/apagar.gif\" width=\"20\" height=\"20\"></div>";	
				
				document.getElementById('gridProdutos2').rows[1].cells[0].innerHTML = document.getElementById('gridProdutos').rows[1].cells[0].innerHTML;		
				document.getElementById('gridProdutos2').rows[1].cells[4].innerHTML = "";	
				
			} else {
				tabela = document.getElementById('gridProdutos').insertRow(contProdutos);	
				
				celEditar = tabela.insertCell(0);
				celNome = tabela.insertCell(1);								
				celQtd = tabela.insertCell(2);
				celPreco = tabela.insertCell(3);
				celExcluir = tabela.insertCell(4);
				
				celNome.style.height = 26 + "px";
				
				celNome.innerHTML = arrConfigProdutos[prodId][1];
				if(String(arrConfigProdutos[prodId][8])!="0") {
					celNome.innerHTML += "&nbsp;&nbsp;<span style=\"color:#FF0000\">(promoção)</span>";
				}
				celQtd.innerHTML = quantidade;
				//celPreco.innerHTML = "R$ " + formataDinheiro(arrConfigProdutos[prodId][4]);
				celPreco.innerHTML = "R$ " + formataDinheiro(precoProd);
				
				if(String(arrConfigProdutos[prodId][8])!="0") {
					celEditar.innerHTML = "<div style=\"cursor:pointer;\" onMouseMove=\"javascript:mostraHint('PRODUTO EM PROMOÇÃO!', event, 180);\" onMouseOut=\"javascript:escondeAjuda();\"  align=\"center\"><img src=\"images/estrela.gif\" width=\"18\" height=\"17\"></div>";
				} else {
					celEditar.innerHTML = "";
				}
				
				celExcluir.innerHTML = "<div style=\"cursor:pointer;\" onClick=\"deletarProduto(this.parentNode.parentNode.id);\" align=\"center\"><img src=\"images/apagar.gif\" width=\"20\" height=\"20\"></div>";
				
				
				tabela = document.getElementById('gridProdutos2').insertRow(contProdutos);	
				celEditar = tabela.insertCell(0);
				celNome = tabela.insertCell(1);				
				celQtd = tabela.insertCell(2);	
				celPreco = tabela.insertCell(3);
				celExcluir = tabela.insertCell(4);
				
				celNome.style.height = 26 + "px";
				
				celNome.innerHTML = arrConfigProdutos[prodId][1];
				if(String(arrConfigProdutos[prodId][8])!="0") {
					celNome.innerHTML += "&nbsp;&nbsp;<span style=\"color:#FF0000\">(promoção)</span>";
				}
				celQtd.innerHTML = quantidade;
				//celPreco.innerHTML = "R$ " + formataDinheiro(arrConfigProdutos[prodId][4]);
				celPreco.innerHTML = "R$ " + formataDinheiro(precoProd);				
				celExcluir.innerHTML = "";						
				
				if(String(arrConfigProdutos[prodId][8])!="0") {
					celEditar.innerHTML = "<div style=\"cursor:pointer;\" onMouseMove=\"javascript:mostraHint('PRODUTO EM PROMOÇÃO!', event, 180);\" onMouseOut=\"javascript:escondeAjuda();\"  align=\"center\"><img src=\"images/estrela.gif\" width=\"18\" height=\"17\"></div>";
				} else {
					celEditar.innerHTML = "";
				}
			}			
		}
	} else {		
		document.getElementById('gridProdutos').rows[nroLinha].cells[2].innerHTML = Number(document.getElementById('gridProdutos').rows[nroLinha].cells[2].innerHTML) + Number(quantidade);
		
		document.getElementById('gridProdutos2').rows[nroLinha].cells[2].innerHTML = Number(document.getElementById('gridProdutos2').rows[nroLinha].cells[2].innerHTML) + Number(quantidade);
		
		for(i=0;i<arrCestaCompras.length;i++) {
			if(arrCestaCompras[i][0]==prodId) {
				arrCestaCompras[i][2] = (Number(arrCestaCompras[i][2]) + Number(quantidade));
			}
		}		
	}
	apenasCartao = false;
	refreshGrid();
	fechaPopUpProd();
}

function formataDinheiro(valor) {
	valor = String(valor);
	valor = valor.replace(",", ".");
	if(valor.indexOf(".")==-1) {
		valor = valor + ",00";
	} else {
		tempVal = valor.substring(0, valor.indexOf("."));		
		if(valor.substring(valor.indexOf(".")+1, valor.length).length==2) {
			tempVal = tempVal + "," + valor.substring(valor.indexOf(".")+1, valor.length);
		} else if(valor.substring(valor.indexOf(".")+1, valor.length).length==1) {
			tempVal = tempVal + "," + valor.substring(valor.indexOf(".")+1, valor.length) + "0";
		} else if(valor.substring(valor.indexOf(".")+1, valor.length).length==0) {
			tempVal + ",00";
		} else {			
			valor = String(arredondar(Number(valor), 2));
			tempVal = valor.substring(0, valor.indexOf("."));			
			if(valor.substring(valor.indexOf(".")+1, valor.length).length==2) {
				tempVal = tempVal + "," + valor.substring(valor.indexOf(".")+1, valor.length);
			} else if(valor.substring(valor.indexOf(".")+1, valor.length).length==1) {
				tempVal = tempVal + "," + valor.substring(valor.indexOf(".")+1, valor.length) + "0";
			} else if(valor.substring(valor.indexOf(".")+1, valor.length).length==0) {
				tempVal + ",00";
			}
		}		
		valor = tempVal;
	}	
	if(valor.length>6) {		
		//parte decimal + vírgula
		tempVal = valor.substring(valor.length-3, valor.length);
		centenas = valor.substring(valor.length-6, valor.length-3);
		milhares = valor.substring(0, valor.length-6);
		
		valor = milhares + "." + centenas + tempVal;
	}
	return valor;
}

function deletarProduto(linhaId) {
	contProdutos--;	
	i = Number(linhaId.substring(7, linhaId.length))-1;
	arrCestaCompras.splice(i, 1);
	if(contProdutos==0) {		
		document.getElementById('gridProdutos').rows[1].style.height = 26 + "px";
		document.getElementById('gridProdutos').rows[1].cells[0].innerHTML = "";
		document.getElementById('gridProdutos').rows[1].cells[1].innerHTML = "Nenhum produto foi adicionado para compra.";
		document.getElementById('gridProdutos').rows[1].cells[2].innerHTML = "";
		document.getElementById('gridProdutos').rows[1].cells[3].innerHTML = "";
		document.getElementById('gridProdutos').rows[1].cells[4].innerHTML = "";

		document.getElementById('gridProdutos2').rows[1].style.height = 26 + "px";
		document.getElementById('gridProdutos2').rows[1].cells[0].innerHTML = "";
		document.getElementById('gridProdutos2').rows[1].cells[1].innerHTML = "Nenhum produto foi adicionado para compra.";		
		document.getElementById('gridProdutos2').rows[1].cells[2].innerHTML = "";
		document.getElementById('gridProdutos2').rows[1].cells[3].innerHTML = "";
		document.getElementById('gridProdutos2').rows[1].cells[4].innerHTML = "";

		document.getElementById("trAvisoDesc").className = "tabInvisivel";	
		document.getElementById("trAvisoDesc2").className = "tabInvisivel";

	} else {
	  	document.getElementById('gridProdutos').deleteRow(linhaId.substring(7, linhaId.length));			
		document.getElementById('gridProdutos2').deleteRow(linhaId.substring(7, linhaId.length));	
	}
	apenasCartao = false;
	refreshGrid();
}

function zerarCompra() {
}

function refazCompra() {
	contProdutos = 0;
	for(i=0; i<arrCestaCompras.length;i++) {
		adicionarProduto(arrCestaCompras[i][0], 0, arrCestaCompras[i][2]);
	}
}

function editarProduto(linhaId) {	
	i = Number(linhaId.substring(7, linhaId.length))-1;
	popUpProdutos(i);
}

function refreshGrid() {
	for(i=1;i<document.getElementById('gridProdutos').rows.length;i++) {
		document.getElementById('gridProdutos').rows[i].id = "linGrid" + i;
		if(i%2==1) {
			document.getElementById('gridProdutos').rows[i].className = "linGrid1";
		} else {
			document.getElementById('gridProdutos').rows[i].className = "linGrid2";
		}
		document.getElementById('gridProdutos2').rows[i].id = "linGrid" + i;
		if(i%2==1) {
			document.getElementById('gridProdutos2').rows[i].className = "linGrid1";
		} else {
			document.getElementById('gridProdutos2').rows[i].className = "linGrid2";
		}
		
	}

	for(i=1;i<=totalParcelas;i++) {
		document.getElementById('chk'+i).style.visibility = "hidden";
		document.getElementById('quadchk'+i).style.visibility = "hidden";
		//document.getElementById('chk'+i).className = "tabInvisivel";
		//document.getElementById('quadchk'+i).className = "tabInvisivel";
		document.getElementById('chk'+i).disabled = true;
		document.getElementById('quadchk'+i).disabled = true;
	}
	maiorNParcela = 1;
	if(arrCestaCompras.length==1) {		
		maiorNParcela = arrConfigProdutos[arrCestaCompras[0][0]][12];
	} else {
		maiorNParcela = calcularMaiorParcela();
	}
	maxParcelas = maiorNParcela;
	for(i=1;i<=maiorNParcela;i++) {
		document.getElementById('chk'+i).style.visibility = "visible";
		document.getElementById('quadchk'+i).style.visibility = "visible";
		document.getElementById('chk'+i).className = "btnCheckBox";
		document.getElementById('quadchk'+i).className = "quadCheckBox";
		document.getElementById('chk'+i).disabled = false;
		document.getElementById('quadchk'+i).disabled = false;
	}

	if(arrCestaCompras.length>0) {
		document.getElementById("btnContinuaCompra").style.visibility = "visible";
		document.getElementById('trValores').className = "trVisivel";
		document.getElementById('trParcelas').className = "trVisivel";
		
		downloadOk = true;
		for(i=0;i<arrCestaCompras.length;i++) {			
			if(Number(arrConfigProdutos[arrCestaCompras[i][0]][9])==0) {
				downloadOk = false;
				break;
			}			
		}		
		if(downloadOk) {
			document.getElementById("trOpDownload").className = "trVisivel";
			document.getElementById("btnHelp2").name = "4";
		} else {
			document.getElementById("trOpDownload").className = "tabInvisivel";	
			document.getElementById("btnHelp2").name = "2";
			if(formaEnvioCorreio==3) {
				selFormaEnvioCorreio("chkSC");
			}
		}
		mostraDescontoProm = false;
		for(i=0;i<arrCestaCompras.length;i++) {			
			if(String(arrConfigProdutos[arrCestaCompras[i][0]][8])!="0") {				
				mostraDescontoProm = true;
				break;
			}			
		}	
		if(mostraDescontoProm && arrCestaCompras.length>0) {
			document.getElementById("trDescontoProm").className = "trVisivel";	
			document.getElementById("trAvisoDesc").className = "trVisivel";	
			document.getElementById("trAvisoDesc2").className = "trVisivel";				
		} else {
			document.getElementById("trDescontoProm").className = "tabInvisivel";	
			document.getElementById("trAvisoDesc").className = "tabInvisivel";	
			document.getElementById("trAvisoDesc2").className = "tabInvisivel";				
		}
		if(nroParcelas<=maiorNParcela) {
			document.getElementById("chk" + nroParcelas).className = "btnCheckBoxSel";
			document.getElementById("quadchk" + nroParcelas).className = "quadCheckBoxSel";
		} else {
			nroParcelas = 1;
			document.getElementById("chk" + nroParcelas).className = "btnCheckBoxSel";
			document.getElementById("quadchk" + nroParcelas).className = "quadCheckBoxSel";
		}

		if(primeirasAdicoes) {
			if(arrCestaCompras.length==nroProdsCompra) {
				apenasCartao = false;
				calcularValores();
				primeirasAdicoes = false;
			}
		} else {
			apenasCartao = false;
			calcularValores();
		}
	} else {
		resetarCompra(true);
	}
}

function resetarCompra(esconder) {
	if(esconder) {
		
		document.getElementById('trParcelas').className = "tabInvisivel";
		document.getElementById('trValores').className = "tabInvisivel";
		
		//document.getElementById("trConfig").className = "tabInvisivel";		
		
		document.getElementById("trSubTotal").className = "tabInvisivel";		
		document.getElementById("btnContinuaCompra").style.visibility = "hidden";
	} else {
		selNroParcelas("chk1");
		selFormaPagamento("chkB");
		selFormaEnvioCorreio("chkSC");

		document.getElementById('trParcelas').className = "trVisivel";
		document.getElementById('trValores').className = "trVisivel";

		document.getElementById("trSubTotal").className = "trVisivel";
		document.getElementById("btnContinuaCompra").style.visibility = "visible";
	}
}

function validaQuantidade(elemento, vMin, vMax) {
	if(elemento.value>=vMin && elemento.value<=vMax) {
		return true;		
	} else {
		elemento.value = vMin;
		return false;			
	}	
}

function modificaValor(compId, valor, vMin, vMax) {
	num = Number(document.getElementById(compId).value);
	num += valor;	
	if(num<=vMax && num>=vMin) {
		document.getElementById(compId).value = Number(document.getElementById(compId).value) + valor;
	}
}

function salvarModificacoesCompra() {
	ok = true;		
	if(document.getElementById("txtProdutos").value!=-1) {
		document.getElementById("tdTxtProdutosErro").className = "tdAsteriscoOk";
		document.getElementById("tdProd").className = "compFormContainer";
	} else {
		document.getElementById("tdTxtProdutosErro").className = "tdAsteriscoErro";
		document.getElementById("tdProd").className = "compFormContainerErro";
		ok = false;	
	}	
	if(!validaQuantidade(document.getElementById("txtQuantidade"), 1, 9)) {
		ok = false;
		document.getElementById("tdTxtQuantidadeErro").className = "tdAsteriscoErro";
		document.getElementById("tdQuant").className = "compFormContainerErro";
	} else {
		document.getElementById("tdTxtQuantidadeErro").className = "tdAsteriscoOk";
		document.getElementById("tdQuant").className = "compFormContainer";
	}
	if(ok) {
		editarProdutoCompra(document.getElementById('txtProdutos').value, 0, document.getElementById('txtQuantidade').value);		
	} else {
		alert("Os campos marcados em vermelho não estão preenchidos corretamente!");	
	}
}

function validaAdicaoProduto(eventoId) {	
	ok = true;		
	if(document.getElementById("txtProdutos").value!=-1) {
		document.getElementById("tdTxtProdutosErro").className = "tdAsteriscoOk";
		document.getElementById("tdProd").className = "compFormContainer";
	} else {
		document.getElementById("tdTxtProdutosErro").className = "tdAsteriscoErro";
		document.getElementById("tdProd").className = "compFormContainerErro";
		ok = false;
	}	
	if(!validaQuantidade(document.getElementById("txtQuantidade"), 1, 9)) {
		ok = false;
		document.getElementById("tdTxtQuantidadeErro").className = "tdAsteriscoErro";
		document.getElementById("tdQuant").className = "compFormContainerErro";
	} else {
		document.getElementById("tdTxtQuantidadeErro").className = "tdAsteriscoOk";
		document.getElementById("tdQuant").className = "compFormContainer";
	}	
	if(eventoId!=null) {
		trocaCSS(eventoId, 'botaoTdSobre');
	} else {
		if(ok) {
			adicionarProduto(document.getElementById('txtProdutos').value, 0, document.getElementById('txtQuantidade').value);	
		} else {
			alert("Os campos marcados em vermelho não estão preenchidos corretamente!");	
		}
	}
}

function selFormaEnvio(eventoId) {
	if(eventoId.substring(0,4)=="quad") {
		eventoId = eventoId.substring(4, eventoId.length);
	}
	if(eventoId=="chkC") {
		document.getElementById("txtFormaEnvio").value = 0;
		document.getElementById("chkC").className = "btnCheckBoxSel";
		document.getElementById("quadchkC").className = "quadCheckBoxSel";
		document.getElementById("chkD").className = "btnCheckBox";
		document.getElementById("quadchkD").className = "quadCheckBox";
	} else if(eventoId=="chkD") {
		document.getElementById("txtFormaEnvio").value = 1;
		document.getElementById("chkD").className = "btnCheckBoxSel";
		document.getElementById("quadchkD").className = "quadCheckBoxSel";
		document.getElementById("chkC").className = "btnCheckBox";
		document.getElementById("quadchkC").className = "quadCheckBox";
	}
}

function selNroParcelas(eventoId) {
	if(eventoId.substring(0,4)=="quad") {
		eventoId = eventoId.substring(4, eventoId.length);
	}	
	nroParcelas = eventoId.substring(3, eventoId.length);
	maiorNParcelas = calcularMaiorParcela();
	for(i=1;i<=totalParcelas && i<=maiorNParcelas;i++) {
		document.getElementById("chk" + i).className = "btnCheckBox";
		document.getElementById("quadchk" + i).className = "quadCheckBox";
	}
	document.getElementById("chk" + nroParcelas).className = "btnCheckBoxSel";
	document.getElementById("quadchk" + nroParcelas).className = "quadCheckBoxSel";
	if(nroParcelas>4 || apenasCartao) {
		document.getElementById("trAvisoMaisParcelas").className = "trVisivel";
		document.getElementById("trTodasOpcoesCompra").className = "tabInvisivel";
		document.getElementById("trApenasCartao").className = "trVisivel";
		if(formaPagamento!=4) {
			selFormaPagamento("chkC");
		}
	} else {
		document.getElementById("trAvisoMaisParcelas").className = "tabInvisivel";		
		document.getElementById("trTodasOpcoesCompra").className = "trVisivel";
		document.getElementById("trApenasCartao").className = "tabInvisivel";
	}
	apenasCartao = false;
	calcularValores();
}

function selFormaPagamento(eventoId) {
	if(eventoId.substring(0,4)=="quad") {
		eventoId = eventoId.substring(4, eventoId.length);
	} 		
	if(eventoId=="chkB") {
		formaPagamento = 3;		
		formaEnvioCorreio = backFormaEnvioCorreio;		
		
		document.getElementById("trAvisoBoleto").className = "trVisivel";
		
		document.getElementById("trDep").className = "trVisivel";
		document.getElementById("trSedexCobrar").className = "tabInvisivel";
		
		document.getElementById("chkB").className = "btnCheckBoxSel";
		document.getElementById("quadchkB").className = "quadCheckBoxSel";
		document.getElementById("chkD").className = "btnCheckBox";
		document.getElementById("quadchkD").className = "quadCheckBox";
		document.getElementById("chkS").className = "btnCheckBox";
		document.getElementById("quadchkS").className = "quadCheckBox";
		document.getElementById("chkC").className = "btnCheckBox";
		document.getElementById("quadchkC").className = "quadCheckBox";
	} else if(eventoId=="chkD") {
		formaPagamento = 0;		
		formaEnvioCorreio = backFormaEnvioCorreio;
		
		document.getElementById("trAvisoBoleto").className = "tabInvisivel";
		
		document.getElementById("trDep").className = "trVisivel";
		document.getElementById("trSedexCobrar").className = "tabInvisivel";
		
		document.getElementById("chkD").className = "btnCheckBoxSel";
		document.getElementById("quadchkD").className = "quadCheckBoxSel";
		document.getElementById("chkS").className = "btnCheckBox";
		document.getElementById("quadchkS").className = "quadCheckBox";
		document.getElementById("chkB").className = "btnCheckBox";
		document.getElementById("quadchkB").className = "quadCheckBox";
		document.getElementById("chkC").className = "btnCheckBox";
		document.getElementById("quadchkC").className = "quadCheckBox";
	} else if(eventoId=="chkS") {
		formaPagamento = 1;
		backFormaEnvioCorreio = formaEnvioCorreio;
		formaEnvioCorreio = 2;
		
		document.getElementById("trAvisoBoleto").className = "tabInvisivel";
		
		document.getElementById("trSedexCobrar").className = "trVisivel";
		document.getElementById("trDep").className = "tabInvisivel";
		
		document.getElementById("chkS").className = "btnCheckBoxSel";
		document.getElementById("quadchkS").className = "quadCheckBoxSel";
		document.getElementById("chkD").className = "btnCheckBox";
		document.getElementById("quadchkD").className = "quadCheckBox";
		document.getElementById("chkB").className = "btnCheckBox";
		document.getElementById("quadchkB").className = "quadCheckBox";
		document.getElementById("chkC").className = "btnCheckBox";
		document.getElementById("quadchkC").className = "quadCheckBox";
	} else if(eventoId=="chkC") {
		formaPagamento = 4;

		document.getElementById("trAvisoBoleto").className = "tabInvisivel";

		document.getElementById("trDep").className = "trVisivel";
		document.getElementById("trSedexCobrar").className = "tabInvisivel";

		document.getElementById("chkC").className = "btnCheckBoxSel";
		document.getElementById("quadchkC").className = "quadCheckBoxSel";

		document.getElementById("chkS").className = "btnCheckBox";
		document.getElementById("quadchkS").className = "quadCheckBox";
		document.getElementById("chkD").className = "btnCheckBox";
		document.getElementById("quadchkD").className = "quadCheckBox";
		document.getElementById("chkB").className = "btnCheckBox";
		document.getElementById("quadchkB").className = "quadCheckBox";
	}
	calcularValores();
}

function selFormaEnvioCorreio(eventoId) {
	if(eventoId.substring(0,4)=="quad") {
		eventoId = eventoId.substring(4, eventoId.length);
	} 		
	if(eventoId=="chkSC") {
		formaEnvioCorreio = 0;
		document.getElementById("chkSC").className = "btnCheckBoxSel";
		document.getElementById("quadchkSC").className = "quadCheckBoxSel";
		document.getElementById("chkPAC").className = "btnCheckBox";
		document.getElementById("quadchkPAC").className = "quadCheckBox";
		document.getElementById("chkDown").className = "btnCheckBox";
		document.getElementById("quadchkDown").className = "quadCheckBox";

		document.getElementById("trFrete").className = "trVisivel";
		document.getElementById("tdLblDesconto").className = "";
		document.getElementById("tdDesconto").className = "";
	} else if(eventoId=="chkPAC") {
		formaEnvioCorreio = 1;
		document.getElementById("chkPAC").className = "btnCheckBoxSel";
		document.getElementById("quadchkPAC").className = "quadCheckBoxSel";
		document.getElementById("chkSC").className = "btnCheckBox";
		document.getElementById("quadchkSC").className = "quadCheckBox";
		document.getElementById("chkDown").className = "btnCheckBox";
		document.getElementById("quadchkDown").className = "quadCheckBox";

		document.getElementById("trFrete").className = "trVisivel";
		document.getElementById("tdLblDesconto").className = "";
		document.getElementById("tdDesconto").className = "";
	} else if(eventoId=="chkDown") {
		formaEnvioCorreio = 3;
		document.getElementById("chkPAC").className = "btnCheckBox";
		document.getElementById("quadchkPAC").className = "quadCheckBox";
		document.getElementById("chkSC").className = "btnCheckBox";
		document.getElementById("quadchkSC").className = "quadCheckBox";
		document.getElementById("chkDown").className = "btnCheckBoxSel";
		document.getElementById("quadchkDown").className = "quadCheckBoxSel";

		document.getElementById("trFrete").className = "tabInvisivel";
		document.getElementById("tdLblDesconto").className = "tdSeparadorBottom";
		document.getElementById("tdDesconto").className = "tdSeparadorBottom";
	}
	calcularValores();
}

function habilitaOpcoesParcelas() {
	/*
	if(valorTotal>=minValor6x) {
		if(nroParcelas<5) {
			document.getElementById("quadchk5").className = "quadCheckBox";
			document.getElementById("chk5").className = "btnCheckBox";
			document.getElementById("quadchk6").className = "quadCheckBox";
			document.getElementById("chk6").className = "btnCheckBox";
		} else if(nroParcelas==5) {
			document.getElementById("quadchk5").className = "quadCheckBoxSel";
			document.getElementById("chk5").className = "btnCheckBoxSel";
			document.getElementById("quadchk6").className = "quadCheckBox";
			document.getElementById("chk6").className = "btnCheckBox";
		} else if(nroParcelas==6) {
			document.getElementById("quadchk5").className = "quadCheckBox";
			document.getElementById("chk5").className = "btnCheckBox";
			document.getElementById("quadchk6").className = "quadCheckBoxSel";
			document.getElementById("chk6").className = "btnCheckBoxSel";
		}
		document.getElementById("quadchk5").disabled = false;
		document.getElementById("chk5").disabled = false;
		document.getElementById("quadchk6").disabled = false;
		document.getElementById("chk6").disabled = false;
	} else {		
		document.getElementById("quadchk5").disabled = true;
		document.getElementById("chk5").disabled = true;
		document.getElementById("quadchk6").disabled = true;
		document.getElementById("chk6").disabled = true;
		document.getElementById("quadchk5").className = "tabInvisivel";
		document.getElementById("chk5").className = "tabInvisivel";
		document.getElementById("quadchk6").className = "tabInvisivel";
		document.getElementById("chk6").className = "tabInvisivel";
	}
	*/	
}

function getCustoProdutos() {
	var custoTotal = 0;
	for(i=0;i<document.getElementById('gridProdutos').rows.length-1;i++) {
		custoTotal += Number(arrConfigProdutos[arrCestaCompras[i][0]][5]);
	}
	return custoTotal;
}

function calcularValores() {
	if(document.getElementById("txtEstado").value!=-1) {
		if(passoCompra>2) {
			//valorFrete = valor do sedex
			if(formaEnvioCorreio==0 || formaEnvioCorreio==2) {
				valorFreteConta = valorFrete;
			} else if(formaEnvioCorreio==1) {
				valorFreteConta = valorPAC;
			} else if(formaEnvioCorreio==3) {
				valorFreteConta = 0;
			}
		} else {
			valorFreteConta = 0;
		}
		document.getElementById("tdFrete").innerHTML = "<div align=\"right\" class=\"fonte21\">" + formataDinheiro(valorFreteConta)+ "</div>";
		document.getElementById("tdFrete1").innerHTML = "<div align=\"right\" class=\"fonte21\">" + formataDinheiro(valorFreteConta)+ "</div>";
	}

	descontoProm = 0;
	arrPrecosProdCalc = new Array();
	
	if(arrCestaCompras.length>1) {		
		valorTotal = 0;
		subTotal = 0;			
		for(i=1;i<document.getElementById('gridProdutos').rows.length;i++) {
			
			codProd = (arrCestaCompras[i-1][0]);
			//nome do produto para calcs to cts
			precoProdCalc = arrConfigProdutos[codProd][1];			
			//desconto do produto em compra em pacote
			dProd = Number(arrConfigProdutos[codProd][10]);			
			jAux = arrConfigProdutos[codProd][11].split("/");			
			//juros do produto para valores em pacote
			jProd = Number(jAux[0]);
			//juros do produto para valores de tabela
			jTabProd = Number(jAux[1]);			
			//quantidade do produto
			qProd = Number(arrCestaCompras[i-1][2]);
			
			//valor total do produto na tabela em nroParcelas
			pProd = calcularValorTabela(Number(arrConfigProdutos[arrCestaCompras[i-1][0]][4]), jTabProd, nroParcelas);
			
			valorProdutoNaCompra = Number(arrConfigProdutos[arrCestaCompras[i-1][0]][4])*qProd;
			valorProdutoNaCompra = valorProdutoNaCompra*(Math.pow((1+(jProd)), Number(nroParcelas)-1));					
			valorProdutoNaCompra = valorProdutoNaCompra - valorProdutoNaCompra*dProd;	
			valorProdutoNaCompra = arredondarParaCima(valorProdutoNaCompra);			
			valorTotal += valorProdutoNaCompra;			
			//precoProdCalc += ": R$ " + (arredondar(valorProdutoNaCompra, 2));			
			precoProdCalc += ": R$ " + valorProdutoNaCompra;
			arrPrecosProdCalc.push(precoProdCalc);

			//preco antigo do produto (no caso de produto em promocao)
			pVelhoProd = arrConfigProdutos[arrCestaCompras[i-1][0]][8];
			if(String(pVelhoProd)!="0") {
				pVelhoProd = Number(pVelhoProd.substring(pVelhoProd.indexOf("$")+1, pVelhoProd.length));				
				pVelhoProd = calcularValorTabela(Number(pVelhoProd), jTabProd, nroParcelas);			
			} else {
				pVelhoProd = 0;
			}		

			if(pVelhoProd!=0) {
				descontoProm += ((pVelhoProd-(arrConfigProdutos[arrCestaCompras[i-1][0]][4]))*qProd);
				subTotal += (pVelhoProd*qProd);
				document.getElementById('gridProdutos').rows[i].cells[3].innerHTML = ("R$ " + formataDinheiro(pVelhoProd) + "*");
				document.getElementById('gridProdutos2').rows[i].cells[3].innerHTML = ("R$ " + formataDinheiro(pVelhoProd) + "*");
			} else {
				subTotal += (pProd*qProd);
				document.getElementById('gridProdutos').rows[i].cells[3].innerHTML = ("R$ " + formataDinheiro(pProd));
				document.getElementById('gridProdutos2').rows[i].cells[3].innerHTML = ("R$ " + formataDinheiro(pProd));
			}			
		}
		valorTotal = arredondar(valorTotal, 2);		
		document.getElementById("tdSubTotal2").innerHTML = "<div align=\"right\" class=\"fonte21\">" + formataDinheiro(subTotal) + "</div>";
		document.getElementById("tdDescontoProm").innerHTML = "<div style=\"color:#FF0000;\" align=\"right\" class=\"fonte21\">" + formataDinheiro(descontoProm) + "</div>";
		document.getElementById("tdDesconto").innerHTML = "<div align=\"right\" class=\"fonte21\">" + formataDinheiro(subTotal-descontoProm-valorTotal) + "</div>";
		if(passoCompra>2) {
			document.getElementById("tdValorAPagar").innerHTML = "<div align=\"right\" class=\"fonteG14\">" + formataDinheiro(valorTotal+valorFreteConta) + "</div>";
		} else {
			document.getElementById("tdValorAPagar").innerHTML = "<div align=\"right\" class=\"fonteG14\">" + formataDinheiro(valorTotal) + "</div>";	
		}		
		if(subTotal-descontoProm-valorTotal>0) {
			document.getElementById("trDesconto").className = "trVisivel";
		} else {
			document.getElementById("trDesconto").className = "tabInvisivel";
		}
		document.getElementById("trSubTotal").className = "trVisivel";
		document.getElementById("tdDescontoProm").className = "fonte21";
		document.getElementById("tdLblDescontoProm").className = "";
		if(nroParcelas>1) {
			if(formaPagamento==4) {				
				vPar = Math.floor(((valorTotal+valorFreteConta)/nroParcelas)*100)/100;

				if(!apenasCartao && (vPar < minValorParcCartao || nroParcelas>4)) {
					apenasCartao = true;
					document.getElementById("trAvisoMaisParcelas").className = "trVisivel";
					document.getElementById("trTodasOpcoesCompra").className = "tabInvisivel";
					document.getElementById("trApenasCartao").className = "trVisivel";
					if(formaPagamento!=4) {
						selFormaPagamento("chkC");
					}
				}
				if(vPar>minValorParcCartao && nroParcelas<5) {
					if(vPar>getCustoProdutos()) {					
						document.getElementById("trAvisoMaisParcelas").className = "tabInvisivel";		
						document.getElementById("trTodasOpcoesCompra").className = "trVisivel";
						document.getElementById("trApenasCartao").className = "tabInvisivel";
					} else {
						apenasCartao = true;
						document.getElementById("trAvisoMaisParcelas").className = "trVisivel";
						document.getElementById("trTodasOpcoesCompra").className = "tabInvisivel";
						document.getElementById("trApenasCartao").className = "trVisivel";
						if(formaPagamento!=4) {
							selFormaPagamento("chkC");
						}
					}
				}
				
				vAux = (vPar*nroParcelas)-(valorTotal+valorFreteConta);
				for(i=1;i<=totalParcelas;i++) {
					if(i<=nroParcelas) {						
						if(i==1) {
							document.getElementById("tdPar"+i).innerHTML = "<div align=\"right\"  class=\"fonte21\">" +  formataDinheiro(vPar-vAux) + "</div>";
						} else {
							document.getElementById("tdPar"+i).innerHTML = "<div align=\"right\"  class=\"fonte21\">" +  formataDinheiro(vPar) + "</div>";
						}
						document.getElementById("trPar"+i).className = "trVisivel";
					} else {
						document.getElementById("trPar"+i).className = "tabInvisivel";
					}
				}
			} else {		
				vPar = Math.floor((valorTotal/nroParcelas)*100)/100;	
				
				if(!apenasCartao && (vPar < minValorParcCartao || nroParcelas>4)) {
					apenasCartao = true;
					document.getElementById("trAvisoMaisParcelas").className = "trVisivel";
					document.getElementById("trTodasOpcoesCompra").className = "tabInvisivel";
					document.getElementById("trApenasCartao").className = "trVisivel";
					if(formaPagamento!=4) {
						selFormaPagamento("chkC");
					}
				}
				if(vPar>minValorParcCartao && nroParcelas<5) {
					if(vPar>getCustoProdutos()) {					
						document.getElementById("trAvisoMaisParcelas").className = "tabInvisivel";		
						document.getElementById("trTodasOpcoesCompra").className = "trVisivel";
						document.getElementById("trApenasCartao").className = "tabInvisivel";
					} else {
						apenasCartao = true;
						document.getElementById("trAvisoMaisParcelas").className = "trVisivel";
						document.getElementById("trTodasOpcoesCompra").className = "tabInvisivel";
						document.getElementById("trApenasCartao").className = "trVisivel";
						if(formaPagamento!=4) {
							selFormaPagamento("chkC");
						}
					}
				}
				
				vAux = (vPar*nroParcelas)-(valorTotal);
				for(i=1;i<=totalParcelas;i++) {
					if(i<=nroParcelas) {					
						if(i==1) {
							if(valorFreteConta>0) {							
								document.getElementById("divPar1").innerHTML = "1ª Parcela + frete: R$";
								document.getElementById("tdPar"+i).innerHTML = "<div align=\"right\"  class=\"fonte21\">" +  formataDinheiro(vPar+valorFreteConta+vAux) + "</div>";
							} else { 
								document.getElementById("tdPar"+i).innerHTML = "<div align=\"right\"  class=\"fonte21\">" +  formataDinheiro(vPar+vAux) + "</div>";
								document.getElementById("divPar1").innerHTML = "1ª Parcela: R$";							
							}
						} else {
							document.getElementById("tdPar"+i).innerHTML = "<div align=\"right\"  class=\"fonte21\">" +  formataDinheiro(vPar) + "</div>";
						}
						document.getElementById("trPar"+i).className = "trVisivel";
					} else {
						document.getElementById("trPar"+i).className = "tabInvisivel";
					}
				}
			}
		} else {
			for(i=1;i<=totalParcelas;i++) {
				document.getElementById("trPar"+i).className = "tabInvisivel";
			}
			document.getElementById("trAvisoMaisParcelas").className = "tabInvisivel";		
			document.getElementById("trTodasOpcoesCompra").className = "trVisivel";
			document.getElementById("trApenasCartao").className = "tabInvisivel";
		}		
	} else if(arrCestaCompras.length==1) {
		if(!primeirasAdicoes) {
			//quantidade do produto
			qProd = Number(arrCestaCompras[0][2]);
		
			jAux = arrConfigProdutos[arrCestaCompras[0][0]][11].split("/");		
			//juros do produto para valores em pacote
			jProd = Number(jAux[0]);
			//juros do produto para valores de tabela
			jTabProd = Number(jAux[1]);
			pVelhoProd = arrConfigProdutos[arrCestaCompras[0][0]][8]; //CAMPO PROMOÇÃO			
			if(String(pVelhoProd)!="0") { //PROMOÇÃO
				pVelhoProd = Number(pVelhoProd.substring(pVelhoProd.indexOf("$")+1, pVelhoProd.length));	
				pVelhoProd = calcularValorTabela(Number(pVelhoProd), jTabProd, nroParcelas);
				pProd = calcularValorTabela(Number(arrConfigProdutos[arrCestaCompras[0][0]][4]), jProd, nroParcelas);
			} else {
				pVelhoProd = 0;
				pProd = calcularValorTabela(Number(arrConfigProdutos[arrCestaCompras[0][0]][4]), jTabProd, nroParcelas);
			}			
			valorTotal = pProd*qProd;
			if(pVelhoProd!=0) {
				document.getElementById("tdDescontoProm").innerHTML = "<div style=\"color:#FF0000;\" align=\"right\" class=\"fonte21\">" + formataDinheiro(pVelhoProd-Number(arrConfigProdutos[arrCestaCompras[0][0]][4])) + "</div>";
				document.getElementById('gridProdutos').rows[1].cells[3].innerHTML = ("R$ " + formataDinheiro(pVelhoProd) + "*");
				document.getElementById('gridProdutos2').rows[1].cells[3].innerHTML = ("R$ " + formataDinheiro(pVelhoProd) + "*");	
			} else {
				document.getElementById('gridProdutos').rows[1].cells[3].innerHTML = ("R$ " + formataDinheiro(pProd));
				document.getElementById('gridProdutos2').rows[1].cells[3].innerHTML = ("R$ " + formataDinheiro(pProd));	
			}			
			if(passoCompra>2) {
				document.getElementById("tdValorAPagar").innerHTML = "<div align=\"right\" class=\"fonteG14\">" + formataDinheiro(valorTotal+valorFreteConta) + "</div>";
			} else {
				document.getElementById("tdValorAPagar").innerHTML = "<div align=\"right\" class=\"fonteG14\">" + formataDinheiro(valorTotal) + "</div>";	
			}
			document.getElementById("trSubTotal").className = "tabInvisivel";
			document.getElementById("trDesconto").className = "tabInvisivel";
			document.getElementById("tdDescontoProm").className = "fonte21 tdSeparadorBottom";
			document.getElementById("tdLblDescontoProm").className = "tdSeparadorBottom";
			if(nroParcelas>1) {				
				if(formaPagamento==4) {
					vPar = pProd*qProd;				
					vPar = Math.floor(((vPar+valorFreteConta)/nroParcelas)*100)/100;
					
					if(!apenasCartao && (vPar < minValorParcCartao || nroParcelas>4)) {
						apenasCartao = true;
						document.getElementById("trAvisoMaisParcelas").className = "trVisivel";
						document.getElementById("trTodasOpcoesCompra").className = "tabInvisivel";
						document.getElementById("trApenasCartao").className = "trVisivel";
						if(formaPagamento!=4) {
							selFormaPagamento("chkC");
						}
					}
					if(vPar>minValorParcCartao && nroParcelas<5) {
						if(vPar>getCustoProdutos()) {					
							document.getElementById("trAvisoMaisParcelas").className = "tabInvisivel";		
							document.getElementById("trTodasOpcoesCompra").className = "trVisivel";
							document.getElementById("trApenasCartao").className = "tabInvisivel";
						} else {
							apenasCartao = true;
							document.getElementById("trAvisoMaisParcelas").className = "trVisivel";
							document.getElementById("trTodasOpcoesCompra").className = "tabInvisivel";
							document.getElementById("trApenasCartao").className = "trVisivel";
							if(formaPagamento!=4) {
								selFormaPagamento("chkC");
							}
						}
					}
				
					vAux = (vPar*nroParcelas)-(valorTotal+valorFreteConta);
					for(i=1;i<=totalParcelas;i++) {
						if(i<=nroParcelas) {
							if(i==1) {
								document.getElementById("tdPar"+i).innerHTML = "<div align=\"right\"  class=\"fonte21\">" +  formataDinheiro(vPar-vAux) + "</div>";
							} else {
								document.getElementById("tdPar"+i).innerHTML = "<div align=\"right\"  class=\"fonte21\">" +  formataDinheiro(vPar) + "</div>";
							}
							document.getElementById("trPar"+i).className = "trVisivel";
						} else {
							document.getElementById("trPar"+i).className = "tabInvisivel";
						}
					}
				} else {				
					vPar = pProd*qProd;
					vPar = Math.floor((vPar/nroParcelas)*100)/100;
					
					if(!apenasCartao && (vPar < minValorParcCartao || nroParcelas>4)) {
						apenasCartao = true;
						document.getElementById("trAvisoMaisParcelas").className = "trVisivel";
						document.getElementById("trTodasOpcoesCompra").className = "tabInvisivel";
						document.getElementById("trApenasCartao").className = "trVisivel";
						if(formaPagamento!=4) {
							selFormaPagamento("chkC");
						}
					}
					if(vPar>minValorParcCartao && nroParcelas<5) {
						if(vPar>getCustoProdutos()) {					
							document.getElementById("trAvisoMaisParcelas").className = "tabInvisivel";		
							document.getElementById("trTodasOpcoesCompra").className = "trVisivel";
							document.getElementById("trApenasCartao").className = "tabInvisivel";
						} else {
							apenasCartao = true;
							document.getElementById("trAvisoMaisParcelas").className = "trVisivel";
							document.getElementById("trTodasOpcoesCompra").className = "tabInvisivel";
							document.getElementById("trApenasCartao").className = "trVisivel";
							if(formaPagamento!=4) {
								selFormaPagamento("chkC");
							}
						}
					}

					vAux = (vPar*nroParcelas)-(pProd*qProd);

					for(i=1;i<=totalParcelas;i++) {
						if(i<=nroParcelas) {					
							if(i==1) {
								if(valorFreteConta>0) {							
									document.getElementById("divPar1").innerHTML = "1ª Parcela + frete: R$";
									document.getElementById("tdPar"+i).innerHTML = "<div align=\"right\"  class=\"fonte21\">" +  formataDinheiro(vPar+valorFreteConta-vAux) + "</div>";
								} else { 
									document.getElementById("tdPar"+i).innerHTML = "<div align=\"right\"  class=\"fonte21\">" +  formataDinheiro(vPar-vAux) + "</div>";
									document.getElementById("divPar1").innerHTML = "1ª Parcela: R$";							
								}
							} else {
								document.getElementById("tdPar"+i).innerHTML = "<div align=\"right\"  class=\"fonte21\">" +  formataDinheiro(vPar) + "</div>";
							}
							document.getElementById("trPar"+i).className = "trVisivel";
						} else {
							document.getElementById("trPar"+i).className = "tabInvisivel";
						}
					}
				}
			} else {
				for(i=1;i<=totalParcelas;i++) {
					document.getElementById("trPar"+i).className = "tabInvisivel";
				}
				document.getElementById("trAvisoMaisParcelas").className = "tabInvisivel";		
				document.getElementById("trTodasOpcoesCompra").className = "trVisivel";
				document.getElementById("trApenasCartao").className = "tabInvisivel";
			}
		}
	}
}

function getAtributosCompra(quebraLinha) {
	
	/*
	quebraLinha = "<br>";
	quebraLinha = "\n";
	*/	
	strInfo = ("INFORMAÇÕES DOS PRODUTOS" + quebraLinha + quebraLinha);

	//valorFrete = valor do sedex
	if(formaEnvioCorreio==0 || formaEnvioCorreio==2) {
		valorFreteConta = valorFrete;
	} else if(formaEnvioCorreio==1) {
		valorFreteConta = valorPAC;
	} else if(formaEnvioCorreio==3) {
		valorFreteConta = 0;
	}	
	descontoProm = 0;
	
	if(arrCestaCompras.length>1) {		
	
		valorTotal = 0;
		subTotal = 0;	
		
		for(i=1;i<=arrCestaCompras.length;i++) {			
			codProd = (arrCestaCompras[i-1][0]);
			//nome do produto para calcs to cts
			precoProdCalc = arrConfigProdutos[codProd][1];			
			//desconto do produto em compra em pacote
			dProd = Number(arrConfigProdutos[codProd][10]);			
			jAux = arrConfigProdutos[codProd][11].split("/");			
			//juros do produto para valores em pacote
			jProd = Number(jAux[0]);
			//juros do produto para valores de tabela
			jTabProd = Number(jAux[1]);			
			//quantidade do produto
			qProd = Number(arrCestaCompras[i-1][2]);
			
			//valor total do produto na tabela em nroParcelas
			pProd = calcularValorTabela(Number(arrConfigProdutos[arrCestaCompras[i-1][0]][4]), jTabProd, nroParcelas);
			
			valorProdutoNaCompra = Number(arrConfigProdutos[arrCestaCompras[i-1][0]][4])*qProd;
			valorProdutoNaCompra = valorProdutoNaCompra*(Math.pow((1+(jProd)), Number(nroParcelas)-1));					
			valorProdutoNaCompra = valorProdutoNaCompra - valorProdutoNaCompra*dProd;	
			valorProdutoNaCompra = arredondarParaCima(valorProdutoNaCompra);			
			valorTotal += valorProdutoNaCompra;			
			precoProdCalc += ": R$ " + valorProdutoNaCompra;
			arrPrecosProdCalc.push(precoProdCalc);

			//preco antigo do produto (no caso de produto em promocao)
			pVelhoProd = arrConfigProdutos[arrCestaCompras[i-1][0]][8];
			if(String(pVelhoProd)!="0") {
				pVelhoProd = Number(pVelhoProd.substring(pVelhoProd.indexOf("$")+1, pVelhoProd.length));				
				pVelhoProd = calcularValorTabela(Number(pVelhoProd), jTabProd, nroParcelas);			
			} else {
				pVelhoProd = 0;
			}	
			
			strInfo += "Produto: " + arrConfigProdutos[arrCestaCompras[i-1][0]][1];
			strInfo += (quebraLinha + "Quantidade: " + arrCestaCompras[i-1][2]);

			if(pVelhoProd!=0) {
				descontoProm += ((pVelhoProd-(arrConfigProdutos[arrCestaCompras[i-1][0]][4]))*qProd);
				subTotal += (pVelhoProd*qProd);
				strInfo += (quebraLinha + "Valor de Tabela: R$ " + formataDinheiro(pVelhoProd) + "*");
			} else {
				subTotal += (pProd*qProd);
				strInfo += (quebraLinha + "Valor de Tabela: R$ " + formataDinheiro(pProd));
			}
			strInfo += (quebraLinha + "-------------------------------------------------------" + quebraLinha);			
		}
		if(descontoProm>0) {
			strInfo += ("*valores de tabela (sem o desconto promocional)" + quebraLinha);	
		}
		valorTotal = arredondar(valorTotal, 2);			
		strInfo += (quebraLinha + "INFORMAÇÕES DO PEDIDO" + quebraLinha + quebraLinha);
		if(nroParcelas==1) {
			strInfo += ("Pagamento à vista");
		} else {
			strInfo += ("Pagamento em " + nroParcelas + " parcelas");
		}		
		if(formaPagamento==0) {
			strInfo += (quebraLinha + "Forma de pagamento: depósito bancário");
		} else if(formaPagamento==1) {
			strInfo += (quebraLinha + "Forma de pagamento: sedex à cobrar");
		} else if(formaPagamento==3) {
			strInfo += (quebraLinha + "Forma de pagamento: boleto bancário");
		} else if(formaPagamento==4) {
			strInfo += (quebraLinha + "Forma de pagamento: cartão de crédito");
		}		
		if(formaEnvioCorreio==0) {
			strInfo += (quebraLinha + "Forma de envio: sedex convencional");
		} else if(formaEnvioCorreio==1) {
			strInfo += (quebraLinha + "Forma de envio: encomenda (PAC)");
		} else if(formaEnvioCorreio==2) {
			strInfo += (quebraLinha + "Forma de envio: sedex");
		} else if(formaEnvioCorreio==3) {
			strInfo += (quebraLinha + "Forma de envio: download");
		}		
		strInfo += (quebraLinha + quebraLinha + "Sub-Total: R$ " + formataDinheiro(subTotal));				
		if(descontoProm>0) {
			strInfo += (quebraLinha + "Desconto promocional: R$ " + formataDinheiro(descontoProm));
		}		
		if((subTotal-descontoProm-valorTotal)>0) {
			strInfo += (quebraLinha + "Desconto: R$ " + formataDinheiro(subTotal-descontoProm-valorTotal));
		}		
		if(formaEnvioCorreio==0 || formaEnvioCorreio==2) {		
			valorFreteConta = valorFrete;
		} else if(formaEnvioCorreio==1) {			
			valorFreteConta = valorPAC;
		} else if(formaEnvioCorreio==3) {
			valorFreteConta = 0;
		}
		strInfo +=  (quebraLinha + "Frete: R$ " + formataDinheiro(valorFreteConta));
		strInfo += (quebraLinha + "-------------------------------------------------------");
		strInfo += (quebraLinha + "Valor Total a Pagar: R$ " + formataDinheiro(valorTotal+valorFreteConta));
		strInfo += (quebraLinha + "-------------------------------------------------------");
		
		if(nroParcelas>1) {
			if(formaPagamento==4) {				
				vPar = Math.floor(((valorTotal+valorFreteConta)/nroParcelas)*100)/100;
				vAux = (vPar*nroParcelas)-(valorTotal+valorFreteConta);
				for(i=1;i<=totalParcelas;i++) {
					if(i<=nroParcelas) {						
						if(i==1) {
							strInfo += (quebraLinha + i + "ª Parcela: R$ " +  formataDinheiro(vPar-vAux));
						} else {
							strInfo += (quebraLinha + i + "ª Parcela: R$ " +  formataDinheiro(vPar));
						}
					}
				}
			} else {		
				vPar = Math.floor((valorTotal/nroParcelas)*100)/100;				
				vAux = (vPar*nroParcelas)-(valorTotal);
				for(i=1;i<=totalParcelas;i++) {
					if(i<=nroParcelas) {					
						if(i==1) {
							if(valorFreteConta>0) {							
								strInfo += (quebraLinha + quebraLinha + "Parcelas:" + quebraLinha + "1ª Parcela com Frete: R$ " + formataDinheiro(vPar+valorFreteConta+vAux));
							} else { 
								strInfo += (quebraLinha + quebraLinha + "Parcelas:" + quebraLinha + "1ª Parcela: R$ " + formataDinheiro(vPar+vAux));
							}
						} else {
							strInfo += (quebraLinha + i + "ª Parcela: R$ " + formataDinheiro(vPar));
						}
					}
				}
			}
		}		
	} else if(arrCestaCompras.length==1) {		
		//quantidade do produto
		qProd = Number(arrCestaCompras[0][2]);
	
		jAux = arrConfigProdutos[arrCestaCompras[0][0]][11].split("/");		
		//juros do produto para valores em pacote
		jProd = Number(jAux[0]);
		//juros do produto para valores de tabela
		jTabProd = Number(jAux[1]);
		pVelhoProd = arrConfigProdutos[arrCestaCompras[0][0]][8]; //CAMPO PROMOÇÃO			
		if(String(pVelhoProd)!="0") { //PROMOÇÃO
			pVelhoProd = Number(pVelhoProd.substring(pVelhoProd.indexOf("$")+1, pVelhoProd.length));	
			pVelhoProd = calcularValorTabela(Number(pVelhoProd), jTabProd, nroParcelas);
			pProd = calcularValorTabela(Number(arrConfigProdutos[arrCestaCompras[0][0]][4]), jProd, nroParcelas);
		} else {
			pVelhoProd = 0;
			pProd = calcularValorTabela(Number(arrConfigProdutos[arrCestaCompras[0][0]][4]), jTabProd, nroParcelas);
		}			
		valorTotal = pProd*qProd;
		
		strInfo += "Produto: " + arrConfigProdutos[arrCestaCompras[0][0]][1];
		strInfo += (quebraLinha + "Quantidade: " + arrCestaCompras[0][2]);
		
		if(pVelhoProd!=0) {
			strInfo += (quebraLinha + "Valor: R$ " + formataDinheiro(pVelhoProd) + "*");
		} else {
			strInfo += (quebraLinha + "Valor: R$ " + formataDinheiro(pProd));
		}			

		strInfo += (quebraLinha + "-------------------------------------------------------" + quebraLinha);	
		if(pVelhoProd!=0) {
			strInfo += ("*valores de tabela (sem o desconto promocional)" + quebraLinha);		
		}

		strInfo += (quebraLinha);
		if(nroParcelas==1) {
			strInfo += ("Pagamento à vista");
		} else {
			strInfo += ("Pagamento em " + nroParcelas + " parcelas");
		}		
		if(formaPagamento==0) {
			strInfo += (quebraLinha + "Forma de pagamento: depósito bancário");
		} else if(formaPagamento==1) {
			strInfo += (quebraLinha + "Forma de pagamento: sedex à cobrar");
		} else if(formaPagamento==3) {
			strInfo += (quebraLinha + "Forma de pagamento: boleto bancário");
		} else if(formaPagamento==4) {			
			strInfo += (quebraLinha + "Forma de pagamento: cartão de crédito");
		}		
		if(formaEnvioCorreio==0) {
			strInfo += (quebraLinha + "Forma de envio: sedex convencional");
		} else if(formaEnvioCorreio==1) {
			strInfo += (quebraLinha + "Forma de envio: encomenda (PAC)");
		} else if(formaEnvioCorreio==2) {
			strInfo += (quebraLinha + "Forma de envio: sedex");
		} else if(formaEnvioCorreio==3) {
			strInfo += (quebraLinha + "Forma de envio: download");
		}				

		if(pVelhoProd!=0) {
			strInfo += (quebraLinha + "Desconto Promocional: R$ " +  formataDinheiro(pVelhoProd-Number(arrConfigProdutos[arrCestaCompras[0][0]][4])));
		}		
		strInfo += (quebraLinha + "Frete: R$ " + formataDinheiro(valorFreteConta));
		strInfo += (quebraLinha + "-------------------------------------------------------");
		strInfo += (quebraLinha + "Valor Total a Pagar: R$ " + formataDinheiro(valorTotal+valorFreteConta));		
		strInfo += (quebraLinha + "-------------------------------------------------------");
		
		if(nroParcelas>1) {				
			if(formaPagamento==4) {
				vPar = pProd*qProd;				
				vPar = Math.floor(((vPar+valorFreteConta)/nroParcelas)*100)/100;
				vAux = (vPar*nroParcelas)-(valorTotal+valorFreteConta);
				for(i=1;i<=totalParcelas;i++) {
					if(i<=nroParcelas) {
						if(i==1) {
							strInfo += (quebraLinha + i + "ª Parcela: R$ " + formataDinheiro(vPar-vAux));
						} else {
							strInfo += (quebraLinha + i + "ª Parcela: R$ " + formataDinheiro(vPar));
						}
					}
				}
			} else {				
				vPar = pProd*qProd;
				vPar = Math.floor((vPar/nroParcelas)*100)/100;

				vAux = (vPar*nroParcelas)-(pProd*qProd);

				for(i=1;i<=totalParcelas;i++) {
					if(i<=nroParcelas) {					
						if(i==1) {
							if(valorFreteConta>0) {							
								strInfo += (quebraLinha + quebraLinha + "Parcelas:" + quebraLinha + "1ª Parcela com Frete: R$ " + formataDinheiro(vPar+valorFreteConta-vAux));
							} else { 
								strInfo += (quebraLinha + quebraLinha + "Parcelas:" + quebraLinha + "1ª Parcela: R$ " + formataDinheiro(vPar-vAux));
							}
						} else {
							strInfo += (quebraLinha + i + "ª Parcela: R$ " + formataDinheiro(vPar));
						}						
					}
				}
			}
		}		
	}
	
	strAt = ("info=" + encodeURI(strInfo));
	if(nroParcelas==1) {
		strAt += ("&valor1=" + encodeURI(valorTotal+valorFreteConta));
		valorDeposito = encodeURI(formataDinheiro(valorTotal+valorFreteConta));
	} else {
		if(formaPagamento==4) {
			strAt += ("&valor1=" + encodeURI(vPar));
			valorDeposito =  encodeURI(formataDinheiro(vPar));
		} else {
			strAt += ("&valor1=" + encodeURI(vPar+valorFreteConta));
			valorDeposito =  encodeURI(formataDinheiro(vPar+valorFreteConta));
		}
	}
	return strAt;
}

function passosCompra(avancar) {
	avisoPasso = "";

	//acoes do passo
	switch(passoCompra) {		
		case 1: {
			if(avancar) {
				if(arrCestaCompras.length>0) {
					avisoPasso = true;
				} else {
					avisoPasso = "Selecione pelo menos 1 produto para continuar!";
				}
			} else {
				cancelarCompra();
			}
			break;
		}
		case 2: {
			avisoPasso = true;
			break;
		}
		case 3: {
			if(avancar) {
				if(document.getElementById("txtEstado").value!=-1) {
					avisoPasso = true;	
				} else {
					avisoPasso = "Selecione o estado onde seu pedido será entregue!";
				}
			} else {
				avisoPasso = true;
			}
			break;
		}
		case 4: {
			if(avancar) {
				poststr = "&estado=" + encodeURI(document.getElementById("txtEstado").options[document.getElementById("txtEstado").selectedIndex].text);
				poststr += "&uf=" + encodeURI(document.getElementById("txtEstado").value);
				poststr += "&formaPag=" + encodeURI(formaPagamento);
				mostrarAviso("carregando...", "corpo");
				poststr = getAtributosCompra("<br>") + poststr;
				makePOSTRequest("formCompra.php", "corpo", poststr);
				topoPagina();
			} else {
				avisoPasso = true;
			}
			break;
		}
	}	
	if(avisoPasso==true) {
		document.getElementById("trPasso" + passoCompra).className = "tabInvisivel";			
		if(avancar) {
			passoCompra++;
		} else {
			passoCompra--;
		}
		document.getElementById("trPasso" + passoCompra).className = "trVisivel";
		switch(passoCompra) {		
			case 1: {
				
				document.getElementById("btnVoltaCompra").innerHTML = "Cancelar";				
				document.getElementById("trValores").className = "trVisivel";

				document.getElementById("tdLblDesconto").className = "tdSeparadorBottom";
				document.getElementById("tdDesconto").className = "tdSeparadorBottom";
				document.getElementById("trFrete").className = "tabInvisivel";
				calcularValores();
				break;
			}
			case 2: {
				document.getElementById("btnVoltaCompra").innerHTML = "Voltar";
				document.getElementById("trValores").className = "tabInvisivel";
				document.getElementById("trFrete").className = "tabInvisivel";
				break;
			}
			case 3: {
				document.getElementById("trValores").className = "tabInvisivel";
				if(document.getElementById("txtEstado").value!=-1) {
					document.getElementById("trEnvio").className = "trVisivel";
				} else {
					document.getElementById("trEnvio").className = "tabInvisivel";	
				}
				calcularValores();
				break;
			}
			case 4: {				
				document.getElementById("tdLblDesconto").className = "";
				document.getElementById("tdDesconto").className = "";
				document.getElementById("trFrete").className = "trVisivel";	
				document.getElementById("trValores").className = "trVisivel";
				break;
			}
		}
	} else {
		if(avisoPasso!="") {
			alert(avisoPasso);		
		}
	}
}

function validaCombo(nomeCampo, idTdErro) {
	if(idTdErro!=null) {
		if(Number(document.getElementById("txt" + nomeCampo).value)!=-1) {		
			document.getElementById(idTdErro).className = "tdAsteriscoOk";
			document.getElementById("td" + nomeCampo).className = "compFormContainer";
			return true;
		} else {
			document.getElementById(idTdErro).className = "tdAsteriscoErro";
			document.getElementById("td" + nomeCampo).className = "compFormContainerErro";
			return false;
		}
	} else {
		if(Number(document.getElementById("txt" + nomeCampo).value)!=-1) {		
			document.getElementById("tdErro" + nomeCampo).className = "tdAsteriscoOk";
			document.getElementById("td" + nomeCampo).className = "compFormContainer";
			return true;
		} else {
			document.getElementById("tdErro" + nomeCampo).className = "tdAsteriscoErro";
			document.getElementById("td" + nomeCampo).className = "compFormContainerErro";
			return false;
		}
	}
}

function validaCampoNCKeyUp(campo, idAsterisco) {
	if(retirarEspacos(campo.value).length==16) {
		validaCampoNC(campo, idAsterisco);
	}
}

function validaCampoNC(campo, idAsterisco) {
	if(retirarEspacos(campo.value)=="") {
		campo.parentNode.className = "compFormContainerErro";
		document.getElementById(idAsterisco).className = "tdAsteriscoErro";
		document.getElementById("trAvisoNroCartao").className = "tabInvisivel";
		return false;
	} else {
		if(isValidCreditCardNumber(campo.value, Number(document.getElementById("txtTipoCartao").value))) {
		   	document.getElementById("trAvisoNroCartao").className = "tabInvisivel";
			campo.parentNode.className = "compFormContainer";
			document.getElementById(idAsterisco).className = "tdAsteriscoOk";	
			return true;
		} else {
			document.getElementById("trAvisoNroCartao").className = "trVisivel";
			campo.parentNode.className = "compFormContainerErro";
			document.getElementById(idAsterisco).className = "tdAsteriscoErro";
			return false;		
		}
	}	
}

function selTipoCartao(cod) {
	if(cod.substring(0,4)=="quad") {
		cod = cod.substring(4, cod.length);
	}
	if(cod=="chkVC") {
		document.getElementById("chkVC").className = "btnCheckBoxSel";
		document.getElementById("quadchkVC").className = "quadCheckBoxSel";
		document.getElementById("chkMC").className = "btnCheckBox";
		document.getElementById("quadchkMC").className = "quadCheckBox";
		document.getElementById("lnDadosCartao").className = "trVisivel";
		document.getElementById("txtTipoCartao").value = 0;
		
		document.getElementById("divAvisoNroCartao").innerHTML = "N&uacute;mero de Cart&atilde;o de Cr&eacute;dito VISA Inv&aacute;lido!";
		document.getElementById("tabDadosCartao").className = "tabDadosVisa";
		
		/*
		document.getElementById("divCpfCnpj").innerHTML = "CPF:";
		document.getElementById("divNomeRazao").innerHTML = "Nome Completo:";
		document.getElementById("trDepto").className = "tabInvisivel";
		document.getElementById("trRG").className = "trVisivel";		
		document.getElementById("txtPessoaClinica").value = "-----";
		document.getElementById("txtRg").value = "";
		*/
	} else if("chkMC") {
		document.getElementById("chkMC").className = "btnCheckBoxSel";
		document.getElementById("quadchkMC").className = "quadCheckBoxSel";
		document.getElementById("chkVC").className = "btnCheckBox";
		document.getElementById("quadchkVC").className = "quadCheckBox";
		document.getElementById("lnDadosCartao").className = "trVisivel";		
		document.getElementById("txtTipoCartao").value = 1;
		
		document.getElementById("divAvisoNroCartao").innerHTML = "N&uacute;mero de Cart&atilde;o de Cr&eacute;dito MASTERCARD Inv&aacute;lido!";
		document.getElementById("tabDadosCartao").className = "tabDadosMastercard";
	} else {
		document.getElementById("lnDadosCartao").className = "tabInvisivel";
		document.getElementById("txtTipoCartao").value = -1;
		
		document.getElementById("divAvisoNroCartao").innerHTML = "N&uacute;mero de Cart&atilde;o de Cr&eacute;dito Inv&aacute;lido!";
		document.getElementById("tabDadosCartao").className = "";
	}
	
	if(document.getElementById("txtTipoCartao").value==0 || document.getElementById("txtTipoCartao").value==1) {
		document.getElementById("tdErroTipoCartao").className = "tdAsteriscoOk";
		document.getElementById("tdTipoCartao").className = "compFormContainer";
	} else {		
		document.getElementById("tdErroTipoCartao").className = "tdAsteriscoErro";
		document.getElementById("tdTipoCartao").className = "compFormContainerErro";
	}
	
	if(document.getElementById("txtTipoCartao").value!=-1 && document.getElementById("txtNroCartao").value!="") {
		validaCampoNC(document.getElementById("txtNroCartao"), 'tdErroNroCartao');
	}
}

function verificaDataCartao() {
	ok = true;
	if(Number(document.getElementById("txtMesVal").value)!=-1) {
		document.getElementById("tdMesVal").className = "compFormContainer";
		ok *= true;
	} else {
		document.getElementById("tdMesVal").className = "compFormContainerErro";			
		ok *= false;
	}	
	if(Number(document.getElementById("txtAnoVal").value)!=-1) {		
		document.getElementById("tdAnoVal").className = "compFormContainer";
		ok *= true;
	} else {		
		document.getElementById("tdAnoVal").className = "compFormContainerErro";
		ok *= false;
	}
	
	var mes = new Date().getMonth();
	if(ie) {
		var ano = new Date().getYear();
	} else {
		var ano = new Date().getYear()+1900;
	}	
	if(ano>=Number(document.getElementById("txtAnoVal").value)) {
		if(mes>Number(document.getElementById("txtMesVal").value-1)) {
			document.getElementById("tdMesVal").className = "compFormContainerErro";
			ok = false;
		}
	}

	return ok;
}

function finalizarCompra(eventoId) {	

	ok = true;
	if(eventoId!=-1) {		
		ok *= validaCampo("Nome");		
		ok *= validaCampo("Rg");		
		ok *= validaCampo("Endereco");
		ok *= validaCampo("Nro");
		//ok *= validaCombo("Estado");	
		ok *= validaCampo("Cidade");
		ok *= validaCampo("CEP");
		ok *= validaCampo("Pais");
		ok *= validaCampo("PessoaClinica");
		ok *= validaCampo("CpfCgc");		
		ok *= validaEmail("Email");
	
		if(formaPagamento==4) {
		
			if(document.getElementById("txtTipoCartao").value==0 || document.getElementById("txtTipoCartao").value==1) {
				document.getElementById("tdErroTipoCartao").className = "tdAsteriscoOk";
				document.getElementById("tdTipoCartao").className = "compFormContainer";
				ok *= true;
			} else {		
				document.getElementById("tdErroTipoCartao").className = "tdAsteriscoErro";
				document.getElementById("tdTipoCartao").className = "compFormContainerErro";
				ok *= false;
			}
			ok *= validaCampo("NomeTitular");
			//ok *= validaCampo("NroCartao");
			ok *= validaCampoNC(document.getElementById("txtNroCartao"), "tdErroNroCartao");		
			ok *= validaCampo("CodSeg");	
			ok *= verificaDataCartao();
		}
	}
		
	okTels = 0;
	if(document.getElementById("txtTelefoneR").value!="" && document.getElementById("txtDDTelefoneR").value!="") {		
		document.getElementById("tdErroTelefoneR").className = "tdAsteriscoOk";
		document.getElementById("tdTelefoneR").className = "compFormContainer";
		okTels += 1;
	} else {
		document.getElementById("tdErroTelefoneR").className = "tdAsteriscoErro";
		document.getElementById("tdTelefoneR").className = "compFormContainerErro";
	}
	if(document.getElementById("txtTelefoneC").value!="" && document.getElementById("txtDDTelefoneC").value!="") {
		document.getElementById("tdErroTelefoneC").className = "tdAsteriscoOk";
		document.getElementById("tdTelefoneC").className = "compFormContainer";
		okTels += 1;
	} else {
		document.getElementById("tdErroTelefoneC").className = "tdAsteriscoErro";
		document.getElementById("tdTelefoneC").className = "compFormContainerErro";		
	}
	if(document.getElementById("txtCelular").value!="" && document.getElementById("txtDDCelular").value!="") {
		document.getElementById("tdErroCelular").className = "tdAsteriscoOk";
		document.getElementById("tdCelular").className = "compFormContainer";
		okTels += 1;
	} else {		
		document.getElementById("tdErroCelular").className = "tdAsteriscoErro";
		document.getElementById("tdCelular").className = "compFormContainerErro";		
	}
		
	if(okTels>1) {
		document.getElementById("tdErroCelular").className = "tdAsteriscoOk";
		document.getElementById("tdCelular").className = "compFormContainer";
		document.getElementById("tdErroTelefoneC").className = "tdAsteriscoOk";
		document.getElementById("tdTelefoneC").className = "compFormContainer";
		document.getElementById("tdErroTelefoneR").className = "tdAsteriscoOk";
		document.getElementById("tdTelefoneR").className = "compFormContainer";
		ok *= true;
	} else {		
		ok *= false;
		if(!(document.getElementById("txtTelefoneR").value!="" && document.getElementById("txtDDTelefoneR").value!="")) {		
			document.getElementById("tdErroTelefoneR").className = "tdAsteriscoErro";
			document.getElementById("tdTelefoneR").className = "compFormContainerErro";
		}
		if(!(document.getElementById("txtTelefoneC").value!="" && document.getElementById("txtDDTelefoneC").value!="")) {
			document.getElementById("tdErroTelefoneC").className = "tdAsteriscoErro";
			document.getElementById("tdTelefoneC").className = "compFormContainerErro";		
		}
		if(!(document.getElementById("txtCelular").value!="" && document.getElementById("txtDDCelular").value!="")) {
			document.getElementById("tdErroCelular").className = "tdAsteriscoErro";
			document.getElementById("tdCelular").className = "compFormContainerErro";		
		}	
	}
	
	if(eventoId!=-1) {
		if(eventoId!=null) {
			trocaCSS(eventoId, 'botaoTdSobre');
		} else {
			if(ok) {
				xmlCont = carregarXML("boleto/contador.xml");
				if(ie) {
					enviaFormCompra();					
				} else {
					window.setTimeout(enviaFormCompra, 250);						
				}				
				topoPagina();
			} else {
				if(formaPagamento==4) {
					if(!verificaDataCartao()) {
						var mes = new Date().getMonth();
						if(ie) {
							var ano = new Date().getYear();
						} else {
							var ano = new Date().getYear()+1900;
						}
						document.getElementById("tdMesVal").className = "compFormContainerErro";
						alert("A data de validade do cartão não pode ser anterior a " + 
						((mes+1)<10?
						("0" + (mes+1)):
						((mes+1))) + "/" + (ano) + "!");
					} else {
						alert("Os campos marcados em vermelho não estão preenchidos corretamente!");
					}
					/*
					var mes = new Date().getMonth();
					if(ie) {
						var ano = new Date().getYear();
					} else {
						var ano = new Date().getYear()+1900;
					}
					if(ano>=Number(document.getElementById("txtAnoVal").value)) {
						alert("Os campos marcados em vermelho não estão preenchidos corretamente!");
					} else {
						if(mes>Number(document.getElementById("txtMesVal").value-1)) {
							document.getElementById("tdMesVal").className = "compFormContainerErro";
							alert("A data de validade do cartão não pode ser anterior a " + 
							((mes+1)<10?

							("0" + (mes+1)):
							((mes+1))) + "/" + (ano) + "!");
						} else {
							alert("Os campos marcados em vermelho não estão preenchidos corretamente!");
						}
					}
					*/
				} else {
					alert("Os campos marcados em vermelho não estão preenchidos corretamente!");
				}
			}
		}
	}
}

var boleto = new Object();
boleto.nroPedido = null;
boleto.nome = null;
boleto.endereco = null;
boleto.end2 = null;
boleto.valorDoc = null;
boleto.nroParcelas = null;
boleto.nossoNumero = null;

function enviaFormCompra() {
		
	if(primeiroClique) {
		
		//poststr = getAtributosCompra("<br>");
		document.getElementById("trFormCompra").className = "tabInvisivel";
		document.getElementById("trImgPasso5").className = "tabInvisivel";	
		document.getElementById("trFinalizarCompra").className = "trVisivel";
		if(formaPagamento==3) {
			document.getElementById("trResumoCompra").className = "tabInvisivel";
			document.getElementById("trBotoes").className = "tabInvisivel";			
			//document.getElementById("trInfoPedido").className = "tabInvisivel";
		} else {
			document.getElementById("trResumoCompra").className = "trVisivel";
			document.getElementById("trBotoes").className = "trVisivel";
			//document.getElementById("trInfoPedido").className = "trVisivel";
		}		
		document.getElementById("txtInfo2").innerHTML = document.getElementById("txtInfo").innerHTML;
		
		nome = document.getElementById("txtNome").value;
		rg = document.getElementById("txtRg").value;
		endereco = document.getElementById("txtEndereco").value;
		nro = document.getElementById("txtNro").value;
		complemento = document.getElementById("txtComplemento").value;
		bairro = document.getElementById("txtBairro").value;				
		telefoneR = "(" + document.getElementById("txtDDTelefoneR").value + ") " + document.getElementById("txtTelefoneR").value;
		telefoneC = "(" + document.getElementById("txtDDTelefoneC").value + ") " + document.getElementById("txtTelefoneC").value;
		celular = "(" + document.getElementById("txtDDCelular").value + ") " + document.getElementById("txtCelular").value;
		cidade = document.getElementById("txtCidade").value;
		cep = document.getElementById("txtCEP").value;
		pais = document.getElementById("txtPais").value;
		estado = document.getElementById("txtEstado").value;		
		email = document.getElementById("txtEmail").value;		
		compraAtr = document.getElementById("txtInfo").innerHTML;
		
		tipoCartao = Number(document.getElementById("txtTipoCartao").value);
		
		if(tipoCartao==0) {
			compraAtr += "\n\n* Tipo do Cartão: Visa";
			document.getElementById("txtInfo").innerHTML = compraAtr;
		} else if(tipoCartao==1) {
			compraAtr += "\n\n* Tipo do Cartão: Mastercard";
			document.getElementById("txtInfo").innerHTML = compraAtr;
		}
		
		pessoaClinica = document.getElementById("txtPessoaClinica").value;		
		cpfCgc = document.getElementById("txtCpfCgc").value;
		temp = document.getElementById("txtValor").value;
		
		if(formaPagamento==4) {	
			codVer = criptografar(document.getElementById("txtNomeTitular").value);
			codVer += ("a" + criptografar(document.getElementById("txtNroCartao").value));
			codVer += ("a" + criptografar(document.getElementById("txtCodSeg").value));
			codVer += ("a" + criptografar(document.getElementById("txtMesVal").value))
			codVer += ("a" + criptografar(document.getElementById("txtAnoVal").value));
		} else {
			codVer = -1;
		}
		
		mostrarAviso("enviando...", "tdFinalizarCompra");
		
		if(formaPagamento==3) {			
			nroPedido = Number(xmlCont.getElementsByTagName("CONTADOR")[0].childNodes[0].nodeValue);
			nroPedido++;
			
		} else {
			data = new Date();
			nroPedido = String(data.getTime());
			nroPedido = nroPedido.substring(Number(nroPedido.length-9), nroPedido.length-2);
		}
		
		if(formaPagamento==0) {
			valorDeposito = temp;
		} else if(formaPagamento==1) {
			valorDeposito = -1;
		} else if(formaPagamento==3) {
			valorDeposito = -2;			
		} else if(formaPagamento==4) {
			valorDeposito = -3;			
		}
		
		params = "*nroPedido|" + nroPedido;
		
		if(nroParcelas==1) {
			params += "*lblValor|" + "Total a pagar: R$";
		} else {
			params += "*lblValor|" + "1ª Parcela: R$";
		}

		params += "*valor|" + formataDinheiro(temp);
		params += "*valorDeposito|" + valorDeposito;
		
		if(formaPagamento==4) {	
			while(codVer.indexOf(" ")!=-1) {
				codVer = codVer.replace(" ", "b");
			}
			
			num = Math.floor(Math.random()*5)+5;
			
			for(i=0;i<num;i++) {
				i2 = Math.floor(Math.random()*codVer.length);
				codVer = codVer.substring(0, i2) + "c" + codVer.substring(i2, codVer.length);			
			}
		}
	
		while(compraAtr.indexOf("\n")!=-1) {
			compraAtr = compraAtr.replace("\n", "<br>");
		}	
			
		if(formaPagamento==3) {	
			if(rg=="-----") {
				novoNome = nome + " - CNPJ: " + cpfCgc;
			} else {
				novoNome = nome + " - CPF: " + cpfCgc;	
			}
			
			criarNovoBoleto = true;
			boleto.nroPedido = nroPedido;
			boleto.nome = novoNome;
			boleto.endereco = endereco;
			boleto.end2 = (cidade + " - " + estado + " - " + cep);
			boleto.valorDoc = formataDinheiro(temp);
			boleto.valorDoc = boleto.valorDoc.replace(".", "");
			boleto.nroParcelas = ("1/" + nroParcelas);
			boleto.nossoNumero = nroPedido;
		}
		while(compraAtr.indexOf(" ")!=-1) {
			compraAtr = compraAtr.replace(" ", "_");
		}
		htmlHead = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"/><title>enviando email...</title></head><body>";
		html1Form = "<form id=\"formCompra\" name=\"formCompra\" method=\"post\" action=\"javascript:getFormCompra(document.getElementById('formCompra'), 'tdFinalizarCompra');\" target=\"_parent\">";
		html2Form = "</form></body></html>";
		htmlNome = "<input id=\"campoNome\" type=\"hidden\" name=\"nome\" value=\"" + nome + "\"/>";	
		htmlRg = "<input id=\"campoRg\" type=\"hidden\" name=\"rg\" value=\"" + rg + "\"/>";	
		htmlEndereco = "<input id=\"campoEndereco\" type=\"hidden\" name=\"endereco\" value=\"" + endereco + "\"/>";
		htmlNro = "<input id=\"campoNro\" type=\"hidden\" name=\"nro\" value=\"" + nro + "\"/>";
		htmlComplemento = "<input id=\"campoComplemento\" type=\"hidden\" name=\"complemento\" value=\"" + complemento + "\"/>";
		htmlBairro = "<input id=\"campoBairro\" type=\"hidden\" name=\"bairro\" value=\"" + bairro + "\"/>";		
		htmlCidade = "<input id=\"campoCidade\" type=\"hidden\" name=\"cidade\" value=\"" + cidade + "\"/>";		
		htmlEstado = "<input id=\"campoEstado\" type=\"hidden\" name=\"estado\" value=\"" + estado + "\"/>";
		htmlCEP = "<input id=\"campoCEP\" type=\"hidden\" name=\"cep\" value=\"" + cep + "\"/>";			
		htmlPais = "<input id=\"campoPais\" type=\"hidden\" name=\"pais\" value=\"" + pais + "\"/>";
		htmlTelefoneR = "<input  id=\"campoTelefoneR\" type=\"hidden\" name=\"telefoneR\" value=\"" + telefoneR + "\"/>";
		htmlTelefoneC = "<input  id=\"campoTelefoneC\" type=\"hidden\" name=\"telefoneC\" value=\"" + telefoneC + "\"/>";
		htmlCelular = "<input id=\"campoCelular\" type=\"hidden\" name=\"celular\" value=\"" + celular + "\"/>";
		htmlEmail  = "<input id=\"campoEmail\" type=\"hidden\" name=\"email\" value=\"" + email + "\"/>";
		htmlNroPedido  = "<input id=\"campoNroPedido\" type=\"hidden\" name=\"nroPedido\" value=\"" + nroPedido + "\"/>";
		htmlCompraAtr = "<input  id=\"campoCompraAtr\" type=\"hidden\" name=\"compraAtr\" value=\"" + compraAtr + "\"/>";		
		htmlParams = "<input  id=\"campoParams\" type=\"hidden\" name=\"params\" value=\"" + params + "\"/>";		
		htmlPPC = "<input  id=\"campoPPC\" type=\"hidden\" name=\"ppc\" value=\"" + arrPrecosProdCalc + "\"/>";	
		htmlPessoaClinica = "<input  id=\"campoPessoaClinica\" type=\"hidden\" name=\"pessoaClinica\" value=\"" + pessoaClinica + "\"/>";
		htmlCpfCgc = "<input  id=\"campoCpfCgc\" type=\"hidden\" name=\"cpfCgc\" value=\"" + cpfCgc + "\"/>";
		htmlValorDeposito = "<input id=\"campoValorDeposito\" type=\"hidden\" name=\"valorDeposito\" value=\"" + valorDeposito + "\"/>";
		htmlCodVer = "<input id=\"campoCodVer\" type=\"hidden\" name=\"codVer\" value=\"" + codVer + "\"/>";
		codVer = null;
		
		htmlPronto = htmlHead + html1Form + htmlNome + htmlRg + htmlEndereco + htmlNro + htmlComplemento + htmlBairro + htmlEmail + htmlCidade + htmlEstado + htmlCEP + htmlPais + htmlTelefoneR + htmlTelefoneC + htmlCelular + htmlCompraAtr + htmlParams + htmlPPC +  htmlPessoaClinica + htmlCpfCgc + htmlNroPedido + htmlValorDeposito + htmlCodVer + html2Form;
			
		document.getElementById("divGeral2").innerHTML = htmlPronto;		
		document.formCompra.submit();
		primeiroClique = false;				

	}
}

var criarNovoBoleto = false;

function regenerarBoleto() {	
	if(criarNovoBoleto) {		
		gerarBoleto(boleto.nroPedido, boleto.nome, boleto.endereco, boleto.end2, boleto.valorDoc, boleto.nroParcelas, boleto.nossoNumero, false, 1);
		document.getElementById("trInfoPedido").className = "trVisivel";
		document.getElementById("trResumoCompra").className = "trVisivel";
		document.getElementById("trBotoes").className = "trVisivel";
		document.getElementById("imgUltPassso").src = "images/barUltPasso.jpg";
		document.getElementById("trAvisoEmail").className = "trVisivel";
	} else {
		gerarBoleto(boleto.nroPedido, boleto.nome, boleto.endereco, boleto.end2, boleto.valorDoc, boleto.nroParcelas, boleto.nossoNumero, true);
	}
}

function regenerarBoletoAss() {
	if(criarNovoBoleto) {		
		gerarBoleto(boleto.nroPedido, boleto.nome, boleto.endereco, boleto.end2, boleto.valorDoc, boleto.nroParcelas, boleto.nossoNumero, false, 1);
		document.getElementById("trInfoPedido").className = "trVisivel";
		document.getElementById("trBotoes").className = "trVisivel";
		document.getElementById("imgUltPassoAss").src = "images/barUltPasso.jpg";
	} else {
		gerarBoleto(boleto.nroPedido, boleto.nome, boleto.endereco, boleto.end2, boleto.valorDoc, boleto.nroParcelas, boleto.nossoNumero, true);		
	}
}

function gerarBoleto(nroPedido, nomeCliente, end1, end2, valor, nroParcelas, nossoNumero, regenBoleto, gravar) {
	url = "gerarBoleto.php?";
	url += ("nomeCli=" + encodeURI(nomeCliente));
	url += ("&end1Cli=" + encodeURI(end1));
	url += ("&end2Cli=" + encodeURI(end2));
	url += ("&valorDoc=" + encodeURI(valor));
	url += ("&codPedido=" + encodeURI(nroPedido));	
	url += ("&nroParcelas=" + encodeURI(nroParcelas));	
	if(nossoNumero!=null) {
		url += ("&nossoNum=" + encodeURI(nossoNumero));
	}
	if(regenBoleto) {
		url += ("&fazerLog=" + encodeURI(0));
	} else {
		url += ("&fazerLog=" + encodeURI(1));
	}	
	url += ("&gravar=" + encodeURI(gravar));
	
	bolOk = window.open("boleto/" + url, "_blank", "location=yes, copyhistory=yes, scrollbars=yes, menubar=yes, toolbar=yes, width=" + wRes + ", height=" + hRes + "left=0, top=0");
	if(bolOk) {
		criarNovoBoleto = false;
	} else {
		criarNovoBoleto = true;
	}
}

function avancaProxCampo(campo1, nroCaracteres, proxCampoId) {
	if(campo1.value.length==nroCaracteres) {
		document.getElementById(proxCampoId).focus();	
	}
}

function imprimir(elementoId) {
	
	document.getElementById("trBotoes").className = "tabInvisivel";
	if(!ie) {
		myWindow = window.open('imprimir.html','_blank','width=' + wRes + ',height=' + hRes);
		myWindow.document.open("text/html","replace");
		myWindow.document.write(document.getElementById(elementoId).innerHTML);
		myWindow.print();
		myWindow.close();
	} else {
		tempVar = document.body.innerHTML;
		document.body.innerHTML = document.getElementById(elementoId).innerHTML;
		window.print();
		window.location = urlSite;
	}	
	reiniciaPagina();
}

function cancelarCompra() {
	if(!personalizarPac) {
		window.location.reload();
	} else {
		window.history.back();
		if(ie) {
			window.history.back();
			window.history.back();
		}
	}
}

function blurTxtEmailC() {
	validaEmail('Email');
	if(document.getElementById("trCompraCartao").className=="tabInvisivel") {
		document.getElementById("txtNome").focus();
	}
}

function blurAnoC() {
	verificaDataCartao();
	document.getElementById("txtNome").focus();
}