// Atualiza endereco de entrega
//
function AtualizaEnderecoEntrega(){

	var entrega_end = document.getElementById('entrega_end').value;
	var entrega_nr = document.getElementById('entrega_nr').value;
	var entrega_compl = document.getElementById('entrega_compl').value;
	var entrega_bairro = document.getElementById('entrega_bairro').value;
	var entrega_cidade = document.getElementById('entrega_cidade').value;
	var entrega_estado = document.getElementById('entrega_estado').value;
	
	if (entrega_end == '') {
		alert ("Informe o Endereço para a entrega.");
		document.getElementById('entrega_end').focus();
	} else if (entrega_nr == '') {
		alert ("Informe o Número para a entrega.");
		document.getElementById('entrega_nr').focus();
	} else if (entrega_bairro == '') {
		alert ("Informe o Biarro para a entrega.");
		document.getElementById('entrega_bairro').focus();
	} else {
		var url = "carrinhocompras_endentrega.php?end="+entrega_end+"&endnr="+entrega_nr+"&endcmp="+entrega_compl+"&endbai="+entrega_bairro+"&endcid="+entrega_cidade+"&endest="+entrega_estado;
		ajaxHTML('RetornoEndEntrega', url, 'imgs/ajax.gif', '16', '16');
		var urlredir = "index.php?page=orcamentofinalizar"; 
		parent.top.location.href = urlredir;
	}
	
}






// Atualiza frete do endereco de entrega (pagamento)
//
function AtualizaFreteEnderecoEntrega(codigoendereco) {
	var urlredir = "index.php?page=carrinhocompras_verificacao&endcep="+codigoendereco; 
	parent.top.location.href = urlredir;
}

// Imprime o boleto
//
function ImprimirBoleto(codigocompra, codigocliente) {
	var endereco = "boletos/imprimir.php"
	var parametros = "?cc="+codigocompra+"&cl="+codigocliente;
	var largura = "710";
	var altura = "600";
	window.open(endereco+parametros,"ImpressaoBoleto","toolbar=0,scrollbars=1,location=0,directories=0,copyhistory=0,status=0,menubar=0,resizable=0,width="+largura+",height="+altura+",z-lock,screenX=100,screenY=100, Left=80, Top=10"); 
}
// Atualiza forma de pgto
//
function AtualizarOrcamentoFrmPgto(frmpgto){
	var url = "carrinhocompras_atualizafrmpgto.php?frmpgto="+frmpgto;
	ajaxHTML('RetornoFrmPgto', url, 'imgs/ajax.gif', '16', '16');
}

// Configurar o valor do CEP
//
function FreteValorPedido(valor, servico, prazoentrega){

	var url = "carrinhocompras_fretevalorpedido.php?valor="+valor+"&servico="+servico+"&prazoentrega="+prazoentrega;
	ajaxHTML('RetornoCEPEntregaValor', url, 'imgs/ajax.gif', '16', '16');
	var url2 = "carrinhocompras_somafretesubtotal.php";
	ajaxHTML('CarrinhoValorTotalFinal', url2, 'imgs/ajax.gif', '16', '16');
	//AtualizarOrcamentoFrmPgto(0);
}

// Calcular CEP para entrega do pedido
//
function CalcularFrete(){
	
	if (document.getElementById('cepentrega').value == '') {
		alert ("Informe o CEP para a entrega.");
		document.getElementById('cepentrega').focus();
	} else {
		
		var txtcepentrega = document.getElementById('cepentrega').value;
		var url = "carrinhocompras_frete.php?cepentrega="+txtcepentrega;
		ajaxHTML('RetornoCEPEntrega', url, 'imgs/ajax.gif', '16', '16');
		AtualizarTotalPedido();
		AtualizarTotalFrete();
	}
}

function AtualizarTotalFrete(){
	
	var url2 = "carrinhocompras_atualizafrete.php";
	ajaxHTML('RetornoCEPEntregaValor', url2, 'imgs/ajax.gif', '16', '16');
}


// Remove itens do carrinho, pela listagem do orcamento
//
function RemoverPedido(pedido){
	var decisao = window.confirm("O pedido será removido da lista. Deseja continuar?"); 
	if (decisao){
		var url = "pedidos_remover.php?codpedido="+pedido;
		ajaxHTML('ListagemPedidos', url, 'imgs/ajax.gif', '16', '16');
		var urlredir = "index.php?page=pedidos"; 
		parent.top.location.href = urlredir;
	}
	
}
// Modo de listagem dos produtos
//
function ProdutosExibirLst(codigo, me, opcao) {
	
	if (opcao == "T") {
		var url = "index.php?page=produtos&me="+me; 
		parent.top.location.href = url;
	}
	if (opcao == "G") {
		var url = "index.php?page=produtos&codgrupo="+codigo+"&me="+me; 
		parent.top.location.href = url;
	}
	if (opcao == "M") {
		var url = "index.php?page=produtos&codmarca="+codigo+"&me="+me; 
		parent.top.location.href = url;
	}
	if (opcao == "B") {
		var url = "index.php?page=produtos&titulopesq="+codigo+"&me="+me+"&acao=pesquisar"; 
		parent.top.location.href = url;
	}
	if (opcao == "D") {
		var url = "index.php?page=produtos_destaque&titulopesq="+codigo+"&me="+me+"&acao=pesquisar"; 
		parent.top.location.href = url;
	}
	
}

// Modo de listagem dos produtos
//
function ProdutosMarca(marca) {
	
	var url = "index.php?page=produtos&codmarca="+marca; 
	parent.top.location.href = url;
}

// Adiciona itens no carrinho
//
function AdicionarCarrinho1(produto, temtabela){ 
	

	
	alert(document.getElementById('tabcor').value);
}

function AdicionarCarrinho(produto, temtabela){
	
	if (temtabela == "S") {
		if (document.getElementById('qtd_'+produto).value == '') {
			alert ("Informe a quantidade!");
			document.getElementById('qtd_'+produto).focus();
			return;
		} 
		
		if (document.getElementById('tabcor').value == '0') {
			alert ("Selecione a Cor!");
			document.getElementById('tabcor').focus();
			return;
		} else {
			if (document.getElementById('tabcor').value == '-1')	var tabcorpro = "-1";
			else	var tabcorpro = document.getElementById('tabcor').value;
		}
		
		if (document.getElementById('tabtam').value == '0') {
			alert ("Selecione o tamanho!");
			document.getElementById('tabtam').focus();
			return;
		} else {
			if (document.getElementById('tabtam').value == '-1')	var tabtampro = "-1";
			else	var tabtampro = document.getElementById('tabtam').value;
		}
		
		if (document.getElementById('tabmod').value == '0') {
			alert ("Selecione o modelo!");
			document.getElementById('tabmod').focus();
			return;
		} else {
			if (document.getElementById('tabmod').value == '-1')	var tabmodpro = "-1";
			else	var tabmodpro = document.getElementById('tabmod').value;
		}
		
		if (document.getElementById('tabvts').value == '0') {
			alert ("Selecione a Voltagem!");
			document.getElementById('tabvts').focus();
			return;
		} else {
			if (document.getElementById('tabvts').value == '-1')	var tabvtspro = "-1";
			else	var tabvtspro = document.getElementById('tabvts').value;
		}
		
		
		if (document.getElementById('qtd_'+produto).value != '0') {
			var qtd = document.getElementById('qtd_'+produto).value;
			var url = "carrinhocompras_adicionar.php?produto="+produto+"&qtd="+qtd+"&cor="+tabcorpro+"&tam="+tabtampro+"&mod="+tabmodpro+"&vts="+tabvtspro;
			ajaxHTML('MostraCarrinho', url, 'imgs/ajax.gif', '16', '16');
			document.getElementById('qtd_'+produto).value = "";
			//alert("Produto adicionado com sucesso!");
			//parent.top.location.href = "index.php?page=carrinhocompras";
			t = setTimeout('vai_para_carrinho()',1000);
		} else {
			alert ("A quantidade deve ser maior que zero!");
			document.getElementById('qtd_'+produto).focus();
		}
	} else {
		if (document.getElementById('qtd_'+produto).value == '') {
			alert ("Informe a quantidade!");
			document.getElementById('qtd_'+produto).focus();
		} else {
			if (document.getElementById('qtd_'+produto).value != '0') {
				var tabmodpro = "-1";
				var tabtampro = "-1";
				var tabcorpro = "-1";
				var tabvtspro = "-1";
				var qtd = document.getElementById('qtd_'+produto).value;
				var url = "carrinhocompras_adicionar.php?produto="+produto+"&qtd="+qtd+"&cor="+tabcorpro+"&tam="+tabtampro+"&mod="+tabmodpro+"&vts="+tabvtspro;
				ajaxHTML('MostraCarrinho', url, 'imgs/ajax.gif', '16', '16');
				document.getElementById('qtd_'+produto).value = "";
				t = setTimeout('vai_para_carrinho()',1000);
				//alert("Produto adicionado com sucesso!");
			} else {
				alert ("A quantidade deve ser maior que zero!");
				document.getElementById('qtd_'+produto).focus();
			}	
		}
	}
	
	
	
}
function vai_para_carrinho() {
		//alert('teste');
		parent.top.location.href = "index.php?page=carrinhocompras";
	}

// Remove itens do carrinho, pela listagem do orcamento
//
function RemoverCarrinho(produto){
	var decisao = window.confirm("O produto será removido da lista. Deseja continuar?"); 
	if (decisao){
		var url = "carrinhocompras_remover.php?produto="+produto;
		ajaxHTML('ListagemCarrinho', url, 'imgs/ajax.gif', '16', '16');
		AtualizarTotalPedido();
		alert ("Produto removido com sucesso.");
		var urlredir = "index.php?page=carrinhocompras"; 
		parent.top.location.href = urlredir;
	}
	
}

// atualiza qtd de itens do produto, pela listagem do orcamento
//
function AtualizarCarrinho(produto){
	
	if (document.getElementById('qtd_'+produto).value != '0') {
		document.getElementById('OrcamentoProdutosAvisoQtd').className = "div_ocultar";
		var qtd = document.getElementById('qtd_'+produto).value;
		var url = "carrinhocompras_alterar.php?produto="+produto+"&qtd="+qtd+"&acao=ap";
		ajaxHTML('ProdutoQtd_'+produto, url, 'imgs/ajax.gif', '16', '16');
		AtualizarTotalCarrinho();
		AtualizarTotalPedido();
	} else {
		alert ("A quantidade deve ser maior que zero!");
		document.getElementById('qtd_'+produto).focus();
	}

}

// atualiza total geral do orcamento
//
function AtualizarTotalCarrinho(){
	var url = "carrinhocompras_alterar.php?acao=at";
	ajaxHTML('CarrinhoValorTotal', url, 'imgs/ajax.gif', '16', '16');
}

// atualiza total pedido
//
function AtualizarTotalPedido(){
	var url = "carrinhocompras_somafretesubtotal.php";
	ajaxHTML('CarrinhoValorTotalFinal', url, 'imgs/ajax.gif', '16', '16');
}


// Finaliza o orçamento
//
function FinalizarOrcamento(tipocliente){
	
	if (tipocliente == "NC") {
		var cnpj = document.getElementById('cnpj').value;
		var nome = document.getElementById('nome').value;
		var ie = document.getElementById('ie').value;
		var endereco = document.getElementById('endereco').value;
		var cidade = document.getElementById('cidade').value;
		var telefone = document.getElementById('telefone').value;
		var email = document.getElementById('email').value;
		var site = document.getElementById('site').value;
		var tipo = document.getElementById('tipo').value;
		var url = "carrinhocompras_enviar.php?tipoc=NC&cnpj="+cnpj+"&nome="+nome+"&ie="+ie+"&endereco="+endereco+"&cidade="+cidade+"&telefone="+telefone+"&email="+email+"&site="+site+"&tipo="+tipo;
		ajaxHTML('RetornoNC', url, 'imgs/ajax.gif', '16', '16');
		
	} else {
		var cnpj = document.getElementById('cnpjcliente').value;
		var url = "carrinhocompras_enviar.php?tipoc=JC&cnpj="+cnpj;
		ajaxHTML('RetornoJC', url, 'imgs/ajax.gif', '16', '16');
		
	}
	
	document.getElementById('OrcamentoEscolha').className = 'div_ocultar';
	document.getElementById('EOJC').className = 'div_ocultar';
	document.getElementById('EONC').className = 'div_ocultar';
	document.getElementById("OrcamentoRetorno").innerHTML = "<span class='titulo_interna'><center><br><b>O orçamento foi enviado com sucesso.  <br><br>Em breve entraremos em contato!</b><br><br><br><br></center></span>";

}

// Validar o orçamento
//
function ValidarOrcamento(tipocliente){
	if (tipocliente == "NC") {
		ValidaFormNC(document.formNC);
	} else {
		ValidaFormJC(document.formJC);
	}
}

// Valida Cadastro JA É Cliente
//
function ValidaFormJC(form){   
	if (form.cnpjcliente.value == ''){
		alert('Informe o CNPJ.');
		form.cnpjcliente.focus();
		return ;
	}
	FinalizarOrcamento('JC');
	form.reset();
}

// Valida Cadastro Nao É Cliente
//
function verificaemail(email) {       
	var achou_ponto=false;
	var achou_arroba=false;
	var achou_caracter=false;
	for (var i=0; i<email.length; i++) 
	{
		if (email.charAt(i)=="@") achou_arroba=true;
		else if (email.charAt(i)==".") achou_ponto=true;
		else if (email.charAt(i)!=" ") achou_caracter=true;
	}
	return (achou_ponto & achou_arroba & achou_caracter);
}
function ValidaFormNC(form){   
	if (form.nome.value == ''){
		alert('Informe o nome da empresa.');
		form.nome.focus();
		return ;
	}
	if (form.cnpj.value == ''){
		alert('Informe o CNPJ.');
		form.cnpj.focus();
		return ;
	}
	if (form.endereco.value == ''){
		alert('Informe o endereço.');
		form.endereco.focus();
		return ;
	}
	if (form.uf.value == '0'){
		alert('Informe o estado.');
		form.uf.focus();
		return ;
	}
	if (form.cidade.value == '-1'){
		alert('Informe a cidade.');
		form.cidade.focus();
		return ;
	}
	if (form.telefone.value == ''){
		alert('Informe o telefone.');
		form.telefone.focus();
		return ;
	}
	if (form.email.value != ''){
		if(!verificaemail(form.email.value)) {   
			alert('Você deve informar corretamente o campo de e-mail!');
			form.email.focus();
			return;
		}
	} else {
		alert('Informe o e-mail.');
		form.email.focus();
		return ;
	}
	if (form.tipo.value == '0'){
		alert('Informe a área de atuação da empresa.');
		form.tipo.focus();
		return ;
	}
	FinalizarOrcamento('NC');
	form.reset();
}
function CalculaF(cep) {
	if (cep.length == 8 )
	{
		CalcularFrete();
	}
}
