/*Calculo CORREIOS*/
function calculaSedexCorreios(totalGeral)
{
	var Servico = '40010';
	var CepOrigem = document.getElementById("CepOrigem").value;
	var CepDestino = document.getElementById("CepDestino").value;
	var Peso = document.getElementById("Peso").value;
	var MaoPropria = document.getElementById("MaoPropria").value;
	var Aviso = document.getElementById("AvisoRecebimento").value;
	var Valor = document.getElementById("ValorDeclarado").value;
	var Resposta = document.getElementById("Resposta").value;

	document.getElementById("div_aguarde").style.display='';
	document.getElementById("opcoesenvio").style.display='none';

	$.ajax({
		url: "ajax.php",
		type: 'post',
		cache: false,
		data: {acao: 'calculaSedexCorreios', Servico: Servico, CepOrigem: CepOrigem, CepDestino: CepDestino, Peso: Peso, MaoPropria: MaoPropria, Aviso: Aviso, Valor: Valor, Resposta: Resposta, TotalGeral: totalGeral},
		dataType: 'json',
		success: function(retorno)
		{
			$("#tipoFrete").html(retorno.aviso);
			$("#totalFrete").html(retorno.tarifa);
			$("#td_total_geral").html(retorno.totalgeral);
			document.getElementById("div_aguarde").style.display='none';
			document.getElementById("opcoesenvio").style.display='';
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus);
			document.getElementById("div_aguarde").style.display='none';
			document.getElementById("opcoesenvio").style.display='';
		}
	});
}
function calculaESedexCorreios(totalGeral)
{
	var Servico = '81019';
	var CepOrigem = document.getElementById("CepOrigem").value;
	var CepDestino = document.getElementById("CepDestino").value;
	var Peso = document.getElementById("Peso").value;
	var MaoPropria = document.getElementById("MaoPropria").value;
	var Aviso = document.getElementById("AvisoRecebimento").value;
	var Valor = document.getElementById("ValorDeclarado").value;
	var Resposta = document.getElementById("Resposta").value;
	
	document.getElementById("div_aguarde").style.display='';
	document.getElementById("opcoesenvio").style.display='none';
	
	$.ajax({
		url: "ajax.php",
		type: 'post',
		cache: false,
		data: {acao: 'calculaESedexCorreios', Servico: Servico, CepOrigem: CepOrigem, CepDestino: CepDestino, Peso: Peso, MaoPropria: MaoPropria, Aviso: Aviso, Valor: Valor, Resposta: Resposta, TotalGeral: totalGeral},
		dataType: 'json',
		success: function(retorno)
		{
			$("#tipoFrete").html(retorno.aviso);
			$("#totalFrete").html(retorno.tarifa);
			$("#td_total_geral").html(retorno.totalgeral);
			document.getElementById("div_aguarde").style.display='none';
			document.getElementById("opcoesenvio").style.display='';
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus);
			document.getElementById("div_aguarde").style.display='none';
			document.getElementById("opcoesenvio").style.display='';
		}
	});
}
function calculaPacCorreios(totalGeral)
{
	var Servico = '41106';
	var CepOrigem = document.getElementById("CepOrigem").value;
	var CepDestino = document.getElementById("CepDestino").value;
	var Peso = document.getElementById("Peso").value;
	var MaoPropria = document.getElementById("MaoPropria").value;
	var Aviso = document.getElementById("AvisoRecebimento").value;
	var Valor = document.getElementById("ValorDeclarado").value;
	var Resposta = document.getElementById("Resposta").value;

	document.getElementById("div_aguarde").style.display='';
	document.getElementById("opcoesenvio").style.display='none';

	$.ajax({
		url: "ajax.php",
		type: 'post',
		cache: false,
		data: {acao: 'calculaPacCorreios', Servico: Servico, CepOrigem: CepOrigem, CepDestino: CepDestino, Peso: Peso, MaoPropria: MaoPropria, Aviso: Aviso, Valor: Valor, Resposta: Resposta, TotalGeral: totalGeral},
		dataType: 'json',
		success: function(retorno)
		{
			$("#tipoFrete").html(retorno.aviso);
			$("#totalFrete").html(retorno.tarifa);
			$("#td_total_geral").html(retorno.totalgeral);
			document.getElementById("div_aguarde").style.display='none';
			document.getElementById("opcoesenvio").style.display='';
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus);
			document.getElementById("div_aguarde").style.display='none';
			document.getElementById("opcoesenvio").style.display='';
		}
	});
}

/*Fim do Calculo CORREIOS*/

function valida_cor(value,idCombo)
{
	if(value == '')
	{
		$("#corDivItem"+idCombo).css('display', 'none');
	}else{
		$("#corDivItem"+idCombo).css('display', '');
	}
}

function atualiza_cor(idTamanho, idProduto)
{
	$.ajax({
		url: "ajax.php",
		type: 'post',
		cache: false,
		dataType: 'html',		
		data: {acao: 'atualiza_cor', id_tamanho: idTamanho, id_produto: idProduto},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus);
		},
		success: function(retorno)
		{
			$("#corDivItem"+idProduto).html(decode(retorno));
			$("#estoqueDivItem"+idProduto).html('');
		}
	});	
}

function atualiza_estoque(idCor, idProduto)
{
	var idTamanho = document.getElementById("tamanhoItem"+idProduto).value;
	$.ajax({
		url: "ajax.php",
		type: 'post',
		cache: false,
		dataType: 'html',		
		data: {acao: 'atualiza_estoque', id_tamanho: idTamanho, id_cor: idCor, id_produto: idProduto},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus);
		},
		success: function(retorno)
		{
			$("#estoqueDivItem"+idProduto).html(decode(retorno));
			if(idCor=='')
			{
				$("#estoqueDivItem"+idProduto).html('');
			}
		}
	});	
}

function somarPesos(valor)
{

	document.getElementById("somaPesos").value =  parseInt(document.getElementById("somaPesos").value) + parseInt(valor);
}
function mostrarPesos()
{
	document.getElementById("pesoKilo").value = parseInt(document.getElementById("somaPesos").value)/1000;
	document.getElementById("Peso").value = document.getElementById("pesoKilo").value;
}
function zerarPesos()
{
	document.getElementById("pesoKilo").value = 0;
}
function somarValores(valor)
{
	document.getElementById("somaValores").value =  parseFloat(document.getElementById("somaValores").value) + parseFloat(valor);
}
function somarQtdes()
{
	document.getElementById("somaQtdes").value =  parseInt(document.getElementById("somaQtdes").value) + parseInt(1);
}
function zerarValores()
{
	document.getElementById("somaValores").value =  0;
}
function zerarQtdes()
{
	document.getElementById("somaQtdes").value =  0;
}
function mostraValores(valorId,qtdeId)
{
	document.getElementById(valorId).value = formata_moeda(document.getElementById("somaValores").value);
	document.getElementById(qtdeId).value = document.getElementById("somaQtdes").value;
	document.getElementById("somaValores").value =  0;
	document.getElementById("somaQtdes").value = 0;
}
function verificaNumero(obj,idChk)
{
	if (obj.value<=0|obj.value>=100)
	{
		alert("Quantidade Inválida.");
		obj.value="1";
	}
}
function getKeyCode(e)
{
if (window.event)
return window.event.keyCode;
else if (e)
return e.which;
else
return null;
}
function keyRestrict(e, validchars) { // v3.0
var key='', keychar='';
key = getKeyCode(e);
if (key == null) return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();
validchars = validchars.toLowerCase();
if (validchars.indexOf(keychar) != -1)
return true;
if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
return true;
return false;
}
function setValorLBox(valor)
{
	/*alert(valor);*/
	document.getElementById("NomeLBox").value=valor;
}
function visualizar_descricao_produto(id_produto,descProduto)
{
	$.ajax({
		url: "ajax.php",
		type: 'post',
		cache: false,
		data: {acao: 'visualizar_descricao_produto', id_produto: id_produto},
		dataType: 'json',
		success: function(retorno)
		{
			$("#div_dialog").html(decode(retorno.html));
			$("#div_dialog").dialog('destroy');
				$("#div_dialog").dialog({ 
					height: 250,
					show: 'slide',
					width: 400,
					hide: 'slide',
					modal: true,	
					resizable: false,
					draggable: false,
					title: descProduto
				});
			
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus);
		}
	});	
	
}
function mostra_itens(id_div,id_produto,id_produtoitem,assinante)
{
	var tamSelecinado = document.getElementById("tamanhoItem"+id_produtoitem).value;
	var corSelecionada = document.getElementById("corItem"+id_produtoitem).value;
	
	if(tamSelecinado=='')
	{
		alert("Selecione o Tamanho.");
		document.getElementById("tamanhoItem"+id_produtoitem).focus();
		return;
	}
	
	if(corSelecionada=='')
	{
		alert("Selecione a Cor.");
		document.getElementById("corItem"+id_produtoitem).focus();
		return;
	}
	
	//var qtdeEstoque = document.getElementById(id_produto).value;
	var qtdeEstoque = $("#"+id_produto).val();
	
	var itemProduto = document.getElementById("idProdutoItem"+id_div).value;
	var qtdeEscolhida = document.getElementById("qtde"+id_produtoitem).value;
	
	if(document.getElementById("qtdeItem"+itemProduto) == null)
	{
		var qtdeJaSel = 0;
	}
	else
	{
		var qtdeJaSel = document.getElementById("qtdeItem"+itemProduto).value;
	}
	
	if((parseInt(qtdeEscolhida)+parseInt(qtdeJaSel))>parseInt(qtdeEstoque))
	{
		alert("Quantidade selecionada é maior do que a quantidade no estoque.");
		document.getElementById("qtde"+id_produtoitem).value = 1;
		document.getElementById("qtde"+id_produtoitem).focus();
		return;
	}
	else
	{
		$.ajax({
			url: "ajax.php",
			type: 'post',
			cache: false,
			data: {acao: 'atualiza_opt_itens', id_optin_item: itemProduto, checked: false, assinante: assinante, qtde: qtdeJaSel},
			dataType: 'json',
			success: function(retorno)
			{
				//$("#td_total_geral").html(retorno.total_geral);
				//document.getElementById("div_aguarde").style.display='none';
				//carrinho_exibir();
				var qtdeTotal = parseInt(qtdeEscolhida)+parseInt(qtdeJaSel);
			
				$.ajax({
					url: "ajax.php",
					type: 'post',
					cache: false,
					data: {acao: 'atualiza_opt_itens', id_optin_item: itemProduto, checked: true, assinante: 'N', qtde: qtdeTotal},
					dataType: 'json',
					success: function(retorno)
					{
						//$("#td_total_geral").html(retorno.total_geral);
						document.getElementById("div_aguarde").style.display='none';
						carrinho_exibir('Produto adicionado ao carrinho');
					},
					error: function(XMLHttpRequest, textStatus, errorThrown)
					{
						alert(textStatus);
						document.getElementById("div_aguarde").style.display='none';
					}
				});
			},
			error: function(XMLHttpRequest, textStatus, errorThrown)
			{
				alert(textStatus);
				document.getElementById("div_aguarde").style.display='none';
			}
		});
	}
	
	/*
	if(document.getElementById("Item"+id_div).style.display == "none")
	{
		document.getElementById("Item"+id_div).style.display = "";	
	}
	else
	{
		document.getElementById("Item"+id_div).style.display = "none";	
	}
	*/

	
}
function atualiza_opt_itens(obj,assinante,caixa1,caixa2,caixa3)
{
	if(obj.checked==true)
	{
		if(caixa1=="1")
		{
			if(document.getElementById("caixa1"+obj.id).value=='')
			{
				alert("Este é um produto personalizado, todas as informações são obrigatórias.");
				obj.checked=false;
				return;
			}
		}
		if(caixa2=="1")
		{
			if(document.getElementById("caixa2"+obj.id).value=='')
			{
				alert("Este é um produto personalizado, todas as informações são obrigatórias.");
				obj.checked=false;
				return;
			}
		}
		if(caixa3=="1")
		{
			if(document.getElementById("caixa3"+obj.id).value=='')
			{
				alert("Este é um produto personalizado, todas as informações são obrigatórias.");
				obj.checked=false;
				return;
			}
		}
	}
	document.getElementById("div_aguarde").style.display='';
	var intQtde = document.getElementById("qtde"+obj.id).value;

	if(obj.checked==true)
	{
		document.getElementById("qtde"+obj.id).disabled=true;
	}
	if(obj.checked==false)
	{
		document.getElementById("qtde"+obj.id).disabled=false;
		document.getElementById("qtde"+obj.id).focus;
	}

	$.ajax({
		url: "ajax.php",
		type: 'post',
		cache: false,
		data: {acao: 'atualiza_opt_itens', id_optin_item: obj.id, checked: obj.checked, assinante: assinante, qtde: intQtde},
		dataType: 'json',
		success: function(retorno)
		{
			$("#td_total_geral").html(retorno.total_geral);
			document.getElementById("div_aguarde").style.display='none';
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus);
			document.getElementById("div_aguarde").style.display='none';
		}
	});
}
function limitar_texto(nome,texto,idproduto)
{
	var textoLimitado = texto;
	var qtdeCaracteresNome = nome.length;
	var qtdeCaracteresTexto = texto.length;
	var somaCaracteres = parseInt(qtdeCaracteresNome) + parseInt(qtdeCaracteresTexto);
	if (somaCaracteres > 200)
	{
	textoLimitado = texto.substr(0,parseInt(200) - parseInt(qtdeCaracteresNome)) + '...';
	}
	var txtInicio = "<a href=javascript:visualizar_descricao_produto("+idproduto+",";
	var txtFinal = nome + ")"+"> (mais...)</a>";
	var txtJunto = txtInicio + txtFinal;
	document.write(textoLimitado);
}
function mostrarHREF(nome,texto,idproduto)
{
	var textoLimitado = texto;
	var qtdeCaracteresNome = nome.length;
	var qtdeCaracteresTexto = texto.length;
	var somaCaracteres = parseInt(qtdeCaracteresNome) + parseInt(qtdeCaracteresTexto);
	if (somaCaracteres > 200)
	{
		document.getElementById('href'+idproduto).style.display="";
	}
}
function criaTabelaProdutos(idEvento)
{
	$.ajax({
		url: "ajax.php",
		type: 'post',
		cache: false,
		dataType: 'html',		
		data: {acao: 'montar_produtos', id_evento: idEvento},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus);
		},
		success: function(retorno)
		{
			$("#tabelaProdutos").html(decode(retorno));
		}
	});	
}
function excluir_item(objQtde,itemProduto,assinante)
{
	var qtdeItem = document.getElementById(objQtde).value;

	$.ajax({
		url: "ajax.php",
		type: 'post',
		cache: false,
		data: {acao: 'atualiza_opt_itens', id_optin_item: itemProduto, checked: false, assinante: assinante, qtde: qtdeItem},
		dataType: 'json',
		success: function(retorno)
		{
			//$("#td_total_geral").html(retorno.total_geral);
			document.getElementById("div_aguarde").style.display='none';
			carrinho_exibir('');
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus);
			document.getElementById("div_aguarde").style.display='none';
		}
	});
	
}
function endEntrega(obj)
{
	if(obj.id=="optEndCad")
	{
		document.getElementById("optEndNovo").checked = false;
		document.getElementById("novoendereco").disabled = true;
		document.getElementById("novonumero").disabled = true;
		document.getElementById("novocomplemento").disabled = true;
		document.getElementById("novocep").disabled = true;
		document.getElementById("novocidade").disabled = true;
		document.getElementById("novoestado").disabled = true;
	}

	if(obj.id=="optEndNovo")
	{
		document.getElementById("optEndCad").checked = false;
		document.getElementById("novoendereco").disabled = false;
		document.getElementById("novonumero").disabled = false;
		document.getElementById("novocomplemento").disabled = false;
		document.getElementById("novocep").disabled = false;
		document.getElementById("novocidade").disabled = false;
		document.getElementById("novoestado").disabled = false;
		document.getElementById("novoendereco").focus();
	}

}
function calculaEntrega(valorTotal,codProduto,obj)
{
	
	//var Servico = '40010'; //Codigo do Serviço de SEDEX
	var Servico = obj.value;
	
	if(document.getElementById("optEndCad").checked==true)
	{
		var CepDestino = document.getElementById("CepDestino").value;
	}
	
	if(document.getElementById("optEndNovo").checked==true)
	{
		var CepDestino = document.getElementById("novocep").value;
	}
	
	//Colocar o CEP de Origem da Mercadoria
	var CepOrigem = "05083120";
	
	var MaoPropria = document.getElementById("MaoPropria").value;
	var Aviso = document.getElementById("AvisoRecebimento").value;
	var Valor = document.getElementById("ValorDeclarado").value;
	var Resposta = document.getElementById("Resposta").value;
	
	var ValUltEnt = document.getElementById("valUltEnt"+codProduto).value;
	
	if(document.getElementById("ValorTotal").value!=0)
	{
		valorTotal=document.getElementById("ValorTotal").value;
	}

	document.getElementById("totalEnvio"+codProduto).style.display='none';
	document.getElementById("div_aguarde"+codProduto).style.display='';

	$.ajax({
		url: "ajax.php",
		type: 'post',
		cache: false,
		data: {acao: 'calculaEntrega', Servico: Servico, CepOrigem: CepOrigem, CepDestino: CepDestino, MaoPropria: MaoPropria, Aviso: Aviso, Valor: Valor, Resposta: Resposta, TotalGeral: valorTotal, CodProduto: codProduto, ValUltEnt: ValUltEnt},
		dataType: 'json',
		success: function(retorno)
		{
			$("#totalEnvio"+codProduto).html(retorno.tarifa);
			$("#td_total_geral").html(retorno.totalgeral);
			document.getElementById("ValorTotalEnvio").value=(parseInt(document.getElementById("ValorTotalEnvio").value)+parseInt(retorno.tarifa))-parseInt(document.getElementById("valUltEnt"+codProduto).value);
			document.getElementById("valUltEnt"+codProduto).value=retorno.tarifa;
			document.getElementById("ValorTotal").value=retorno.totalgeral;
			document.getElementById("div_aguarde"+codProduto).style.display='none';
			document.getElementById("totalEnvio"+codProduto).style.display='';
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus);
			document.getElementById("div_aguarde"+codProduto).style.display='none';
			document.getElementById("totalEnvio"+codProduto).style.display='';
		}
	});

}

