// JavaScript Document
var HTMLCarregando  = '<span> <img src="imagens/loaded.gif" width="16" height="16"> </span>';
var HTMLCarregando2  = '<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0"><tr><td><img src="imagens/loaded.gif" width="16" height="16"></td></tr></table>';
var qtdeInicialCarrinho = 0;

var col = 1;
	function verificaLinha()
	{
		if (screen.width >= '1440') 
		{
			if( col >5)
			{
				document.write('<div style="width:80%" id="linha"></div>'); 
				col =1;
			}
		}
		else	 if (screen.width >= '1200') 
		{
			if( col >4)
			{
				document.write('<div style="width:80%" id="linha"></div>'); 
				col =1;
			}
		}

		else	 if (screen.width >= '1024') 
		{
			if( col >3)
			{
				document.write('<div id="linha"></div>'); 
				col =1;
			}
		}	

		else	 if (screen.width >= '800') 
		{
			if( col >2)
			{
				document.write('<div id="linha"></div>'); 
				col =1;
			}
		}				
	}
	
 function irPaginacao(queryString, pagina) {
	location.href = "./?" + queryString + pagina;
  }
 
 function mudarOrdem(queryString, ordem){
	location.href = "./?" + queryString + "&ordem=" + ordem;
 }

function MM_preloadImages() { 
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function preloadCarregando(){
	MM_preloadImages("imagens/loaded.gif");
}

function abaInfo(id) {
 for (i=1; i<=4; i++) {
       document.getElementById("abaDetalhe" + i).style.display = "none";
	  document.getElementById("imgAbaDetalhe"+i+"1").src = "imagens/fundoAba20.jpg";
	  document.getElementById("centroAbaDetalhe"+i).style.backgroundImage = "url(imagens/fundoAba21.jpg)";
       document.getElementById("lnkAbaDetalhe"+i).style.color = "#666666";
       document.getElementById("imgAbaDetalhe"+i+"2").src = "imagens/fundoAba22.jpg";
	  }
	   
 document.getElementById("abaDetalhe" + id).style.display = "";
 document.getElementById("imgAbaDetalhe"+id+"1").src = "imagens/fundoAba10.jpg";
 document.getElementById("centroAbaDetalhe"+id).style.backgroundImage = "url(imagens/fundoAba11.jpg)";
 document.getElementById("lnkAbaDetalhe"+id).style.color = "#FFFFFF";
 document.getElementById("imgAbaDetalhe"+id+"2").src = "imagens/fundoAba12.jpg";
}

function comprarDetalhe(id) {
 document.getElementById("caixaComprarDetalhe").innerHTML = HTMLCarregando2;
 xajax_comprarDetalhe(id);
}

function deletarCarrinho(id) {
 if (confirm("Tem certeza que deseja deletar?")) {
     document.getElementById("caixaDeletar" + id).innerHTML = HTMLCarregando2;
     xajax_deletarCarrinho(id);
 }
}

function qtdeAnterior(qtde) {
 qtdeInicialCarrinho = qtde; 
}

function qtdeKeyUp(tecla, id) {
 var keychar = String.fromCharCode(tecla); 

 if (parseInt(document.getElementById('qtde'+id).value)==0) {
     document.getElementById('qtde'+id).value = '';
     tecla = 8;
 }
 //alert(("0123456789").indexOf(keychar));
 
 if ( (!strZero(document.getElementById('qtde'+id).value)) && ( /*((("0123456789").indexOf(keychar) > -1)) || */ (1) || (tecla==8) || (tecla==46)) )
    atualizarQtdeItem(id);  
}

function atualizarQtdeItem(id) {
 
 //alert('qtde'+id);
 
 if ( (strZero(document.getElementById('qtde'+id).value)) || (parseInt(document.getElementById('qtde'+id).value)==0) )	
	 document.getElementById('qtde'+id).value = '1';
	
if (parseInt(document.getElementById('qtde'+id).value) != parseInt(qtdeInicialCarrinho)) {     
     xajax_atualizarQtdeProduto(id, document.getElementById('qtde'+id).value);
	qtdeInicialCarrinho = document.getElementById('qtde'+id).value;
	document.getElementById('caixaQtde'+id).innerHTML = HTMLCarregando2;
 }

}

function strZero(str){
 var i=0;
 while(str.charAt(i)==' ')
     i++; 	  
	 	 
 if (str.length == i)
    return true;
 else return false;	
}

function calcularFrete() {
 document.getElementById("msgErro").style.display = "none";
 document.getElementById("caixaOkFrete").innerHTML = HTMLCarregando;
 xajax_calcularFrete(document.getElementById("cep1").value + document.getElementById("cep2").value);
} 

function paddingRightHTMLCarregando(valor) {
 return '<div style="padding-right:' + valor + 'px;">' + HTMLCarregando + '</div>';
}

function avancarPedido() {
 document.getElementById("caixaBtnAvancar").innerHTML = paddingRightHTMLCarregando(50);
 xajax_avancarPedido();
}

function enviarLogin() {
 document.getElementById("btnIdentifica").innerHTML = HTMLCarregando;
 xajax.call('enviarLogin', [xajax.getFormValues('formIdentificacao')]);
}

function mudarOpcaoIdentifica() {
 if (document.getElementById("opcaoNaoCadastrado").checked) {
     document.getElementById("loginSenha").value = "";
	 document.getElementById("loginCep").focus();
  }
 else {
	      document.getElementById("loginCep").value = "";
		  document.getElementById("loginSenha").focus();
         }
}

function enviarCadastro() {
  if (document.getElementById("isento").checked)
      document.getElementById("inscricao_estadual").value = "ISENTO";
  else document.getElementById("inscricao_estadual").value = document.getElementById("aux_inscricao_estadual").value;
 
 document.getElementById("caixaEnviarCadastro").innerHTML = HTMLCarregando;
 xajax.call('enviarCadastro', [xajax.getFormValues('formCadastro')]);
}

function mudarTipoPessoa() {
  if (document.getElementById("tipo_pessoa").value == "F") {
	  document.getElementById("caixaPessoaJuridica").style.display = "none";
	  document.getElementById("caixaPessoaFisica").style.display = "inline";
  }
  else {
	          document.getElementById("caixaPessoaFisica").style.display = "none";
			  document.getElementById("caixaPessoaJuridica").style.display = "inline";        	  
          }
}

function mudarAltTipoPessoa() {
var agt = navigator.userAgent;
var name = navigator.appName;

  if (document.getElementById("tipo_pessoa").value == "F") {
  	if (agt.indexOf("MSIE 7.0") && (name == "Microsoft Internet Explorer")) {
	  document.getElementById("tabelaCarrinho").style.height = '985px';
	  document.getElementById("caixaPessoaJuridica").style.display = "none";
	  document.getElementById("caixaPessoaFisica").style.display = "inline";
	} else {
	 document.getElementById("tabelaCarrinho").style.height = '890px';
	 document.getElementById("caixaPessoaJuridica").style.display = "none";
	 document.getElementById("caixaPessoaFisica").style.display = "inline";
	}
  }
  else {
	  if (agt.indexOf("MSIE 7.0") && (name == "Microsoft Internet Explorer")) {
			  document.getElementById("tabelaCarrinho").style.height = '905px';			  
			  document.getElementById("caixaPessoaFisica").style.display = "none";
			  document.getElementById("caixaPessoaJuridica").style.display = "inline";
	  } else {
		  	  document.getElementById("tabelaCarrinho").style.height = '810px';			  
			  document.getElementById("caixaPessoaFisica").style.display = "none";
			  document.getElementById("caixaPessoaJuridica").style.display = "inline";
	  }
  }
}

function isento_ie() {

 if (document.getElementById("isento").checked) { 
     document.getElementById("aux_inscricao_estadual").className = "caixa";
	 document.getElementById("aux_inscricao_estadual").value = "ISENTO";
	 document.getElementById("aux_inscricao_estadual").value = "ISENTO";
     document.getElementById("aux_inscricao_estadual").disabled = "disabled";
	 document.getElementById("msgInscricaoEstadual").style.display = "none";
 } else {
             document.getElementById("aux_inscricao_estadual").value = "";
             document.getElementById("aux_inscricao_estadual").disabled = "";	 
            } 
}

function buscarCepEnd() {
 document.getElementById("caixaBuscarCepEnd").innerHTML = HTMLCarregando; 
 document.getElementById("endereco").disabled = "disabled";
 document.getElementById("bairro").disabled = "disabled";
 document.getElementById("fk_estado").disabled = "disabled";
 document.getElementById("fk_cidade").disabled = "disabled";
 
 xajax_buscarCepEnd(document.getElementById("cep").value);
}

function buscarCepEndALT() {
 document.getElementById("caixaBuscarCepEndALT").innerHTML = HTMLCarregando; 
 document.getElementById("enderecoALT").disabled = "disabled";
 document.getElementById("bairroALT").disabled = "disabled";
 document.getElementById("fk_estadoALT").disabled = "disabled";
 document.getElementById("fk_cidadeALT").disabled = "disabled";
 
 xajax_buscarCepEndALT(document.getElementById("cepALT").value);
}

function mudarCidadeCadastro() {
 document.getElementById("caixaFkCidade").innerHTML = HTMLCarregando;
 xajax_mudarCidadeCadastro(document.getElementById("fk_estado").value);
}

function mudarCidadeCadastroALT() {
 document.getElementById("caixaFkCidadeALT").innerHTML = HTMLCarregando;
 xajax_mudarCidadeCadastroALT(document.getElementById("fk_estadoALT").value);
}

function enviarEnderecoEntrega() {
 document.getElementById("caixaEnderecoEntrega").innerHTML = HTMLCarregando;
 xajax.call('enviarEnderecoEntrega', [xajax.getFormValues('formCadastro')]);
}

function alterarDadosCadastrais() {
 document.getElementById("caixaDadosCadastrais").innerHTML = HTMLCarregando;
 xajax.call('alterarDadosCadastrais', [xajax.getFormValues('formCadastro')]);
}

function alterarSenha() {
 document.getElementById("caixaSenha").innerHTML = HTMLCarregando;
 xajax.call('alterarSenha', [xajax.getFormValues('formCadastro')]);
}

function alterarEmail() {
 document.getElementById("caixaEmail").innerHTML = HTMLCarregando;
 xajax.call('alterarEmail', [xajax.getFormValues('formCadastro')]);
}

function enviarEnderecoEntregaALT() {
 document.getElementById("caixaEnderecoEntregaALT").innerHTML = HTMLCarregando;
 xajax.call('enviarEnderecoEntregaALT', [xajax.getFormValues('formCadastro')]);
}

function mudarPagamento(id, altura) {
 document.getElementById("caixaCartao").style.display = "none";
 document.getElementById("caixaBoleto").style.display = "none";
 document.getElementById("caixaTransferencia").style.display = "none";
 document.getElementById(id).style.display = "inline";

  document.getElementById("tabelaCarrinho").style.height = altura + 'px';

 document.getElementById("imgCartao").src = "imagens/cartao1.jpg";
 document.getElementById("imgBoleto").src = "imagens/boleto1.jpg";
 //document.getElementById("imgTransferencia").src = "imagens/transferencia1.jpg";

 if (id == "caixaCartao") {
    document.getElementById("imgCartao").src = "imagens/cartao2.jpg";
	document.getElementById("formaPagamento").value = "1";
 }
 else
 if (id == "caixaBoleto") {
    document.getElementById("imgBoleto").src = "imagens/boleto2.jpg";
	document.getElementById("formaPagamento").value = "2";
 }
 else {
          //  document.getElementById("imgTransferencia").src = "imagens/transferencia2.jpg";
		//	document.getElementById("formaPagamento").value = "3";
          }  
}

function fecharCartao() {
 document.getElementById("caixaFecharCartao").innerHTML = HTMLCarregando2;
 xajax.call('fecharCartao', [xajax.getFormValues('formCadastro')]);
}

function fecharBoleto() {
 document.getElementById("caixaFecharBoleto").innerHTML = HTMLCarregando2;
 xajax.call('fecharBoleto', [xajax.getFormValues('formCadastro')]);
}

function fecharTransferencia() {
 document.getElementById("caixaFecharTransferencia").innerHTML = HTMLCarregando2;
 xajax.call('fecharTransferencia', [xajax.getFormValues('formCadastro')]);
}

function sair() {
 if (confirm("Tem certeza que deseja sair do Panel de Controle?"))
      xajax_logout();	
}

function cancelarAlteracao() {
 if (confirm("Tem certeza que cancelar a alteração?"))
     location.href = "./?pagina=painel";
}

function ocultarMsg() {
 document.getElementById("msgPainelControle").style.display = "none";
 document.getElementById("tabelaCarrinho").style.height = '260px';
}

function pesquisarPainel() {
 document.getElementById("caixaPesqPainel").innerHTML = HTMLCarregando;
 xajax.call('pesquisarPainel', [xajax.getFormValues('formCadastro')]);
}

function enviarEsqueceuSenha() {
 document.getElementById("caixaEnviarEsqueceu").innerHTML = HTMLCarregando;
 document.getElementById("conteudoMsgEnvia").style.display = "none";
 xajax_enviarEsqueceuSenha(document.getElementById("emailEsqueceu").value);
}

function verPedidoAnterior(id) {
 document.formVerPedido.listaIdPedido.value = id;
 
 var tipoPesquisa = "";
 
 if (document.getElementById("tipoPesquisaA").checked)
     tipoPesquisa = "A";
 else
 if (document.getElementById("tipoPesquisaT").checked)
     tipoPesquisa = "T";
  else
 if (document.getElementById("tipoPesquisaN").checked)
     tipoPesquisa = "N";
  else
 if (document.getElementById("tipoPesquisaD").checked)
     tipoPesquisa = "D";
 
 document.formVerPedido.tipoPesquisa.value = tipoPesquisa;
 document.formVerPedido.numPedido.value = document.formCadastro.numPedido.value;
 document.formVerPedido.mes_inicial.value = document.formCadastro.mes_inicial.value;
 document.formVerPedido.ano_inicial.value = document.formCadastro.ano_inicial.value;
 document.formVerPedido.mes_final.value = document.formCadastro.mes_final.value;
 document.formVerPedido.ano_final.value = document.formCadastro.ano_final.value;
 document.formVerPedido.submit();
}

function voltarPedidosAnteriores() {
 document.formPedidosAnteriores.submit();
}

function popupRecomendar(id){
   var jan = window.open('recomendarProduto.php?id='+id,'recomendar'+id,'resizable=no,scrollbars=no,width=650,height=470');
   jan.focus();
}

function focusOpcaoLogin(id) {
 document.getElementById(id).checked = true;
 mudarOpcaoIdentifica();	
}

function enviarContato(){
  document.getElementById("contatoCarregando").innerHTML = HTMLCarregando;
  document.getElementById("contatoCarregando").style.display = "";
  document.getElementById("btnEnviarContato").style.display = "none";
  xajax.call('enviarContato', [xajax.getFormValues('formContato')]);
}

function IrPara(url) {
	location.href = url;
}

function solicitaProposta(id) {
 var f = document.formProposta;
 f.proposta.value = id;
 f.submit();
}

/**
* Modal
*/

 function tela() 
 {
	var altura = window.document.body.offsetHeight;
	var largura = window.document.body.offsetWidth;

	document.getElementById("boxBlock").style.display = "";
	document.getElementById("boxBlock").style.width = largura+"px";
	document.getElementById("boxBlock").style.height = altura+"px";
}

function passaData(valor)
{
	if(valor > 2)
	{
		document.getElementById('AnoValidade').focus();
	}
}