// JavaScript Document
var HTMLCarregando  = '<span> <img src="imagens/loaded.gif" width="16" height="16"> </span>';
var qtdeInicialCarrinho = 0;

function atualizarQtdeItem(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 = HTMLCarregando;
 }

}

function strZero(str){
 var i=0;
 while(str.charAt(i)==' ')
     i++; 	  
	 	 
 if (str.length == i)
    return true;
 else return false;	
}



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 IrPara(url)
{
	location.href=url;
}


function trocaOverRapido(i)
{	
	document.getElementById('imenurap'+i).style.background='#0752A3';
	document.getElementById('imenurap'+i).style.color='#FFFFFF';	
}
function trocaOutRapido(i)
{
	document.getElementById('imenurap'+i).style.background='';
	document.getElementById('imenurap'+i).style.color='#000000';	
}

function irPaginacao(queryString, pagina) {
	location.href = "./?" + queryString + pagina;
}

function mudarOrdem(queryString, ordem){
	location.href = "./?" + queryString + "&ordem=" + ordem;
}

function buscar(){
 var query = "";
 
 if (document.getElementById("chavesimples").value == '') {
     alert("Por favor, preencha o campo Busca Simples!");
	 document.getElementById("chavesimples").focus();
 } else {	 
	          query = "./?query=" + document.getElementById("chavesimples").value;
			query = query + "&tipo=" + 'buscaSimples';			
               location.href = query;
		   }
}

function buscaDetalhada() {
 var query = "";
 
 /* if (document.getElementById("chavedetalhada").value == '') {
     alert("Por favor, preencha o campo Busca Detalhada!");
	 document.getElementById("chavedetalhada").focus();
 } else {	 
	            query = "./?query=" + document.getElementById("chavedetalhada").value;
			  			  
			   if (document.getElementById("categoria").value != '')
				 query = query + "&idc=" + document.getElementById("categoria").value;
			   			   
			   query = query + "&tipoBusca=" + selecionarTipoPesq();
			   
                  query = query + "&tipo=" + 'buscaDetalhada';
			   location.href = query;
		   }*/
	            query = "./?query=" + document.getElementById("chavedetalhada").value;
			  			  
			   if (document.getElementById("categoria").value != '')
				 query = query + "&idc=" + document.getElementById("categoria").value;
			   			   
			   query = query + "&tipoBusca=" + selecionarTipoPesq();
			   
                  query = query + "&tipo=" + 'buscaDetalhada';
			   location.href = query;

}

function selecionarTipoPesq(){
 var i;
 for (i=0;i<document.formPesquisa.tipoBusca.length;i++) {
       if (document.formPesquisa.tipoBusca[i].checked)
          break;
 }
 return document.formPesquisa.tipoBusca[i].value;
} 

function infoSemEstoque() {
 alert("Atenção: Produto Indisponível em Estoque!");	
}

function adiciona_qtde(id, qtde){
 var campo = document.getElementById(id);
 var valor = (campo.value == '') ? qtde : parseInt(campo.value) + qtde; 
 if (valor <= 0)
     valor = 1;
 else if (valor > 999)
      valor = 999;
	  
 campo.value = valor; 
}

function comprarDetalhe(id) {
 document.getElementById('btnComprarDetalhe').innerHTML = HTMLCarregando;
 xajax_comprarDetalhe(id, document.getElementById("qtd"+id).value);
}


function comprar(id) {	
  xajax_comprarDetalhe(id, document.getElementById("qtd"+id).value);
}

function tipoPessoa(){
 document.getElementById("txtCPF").innerHTML = (document.getElementById("tipo_pessoa").value == "F") ? "CPF" : "CNPJ";
}

function cidadesCadClientes(estiloCidade) {
 document.getElementById('caixaCidade').innerHTML = HTMLCarregando;
  xajax_cidadesCadClientes(document.getElementById('fk_estado').value);
}

function enviarCadClientes() {
 document.getElementById('caixaBtnEnviar').innerHTML = HTMLCarregando;
 document.getElementById('txtMsg').innerHTML = '';
 xajax.call('enviarCadClientes', [xajax.getFormValues('formCadCliente')]);
}

function enviarContato() {
  document.getElementById('msgContato').innerHTML = "";
 document.getElementById('caixaBtnEnviar').innerHTML = HTMLCarregando; 
 xajax.call('enviarContato', [xajax.getFormValues('formContato')]);
}

function enviarLogin() {
 document.getElementById('caixaBtnEnviarLogin').innerHTML = HTMLCarregando;
 document.getElementById('msgLogin').style.height = "15px";
 document.getElementById('msgLogin').innerHTML = '';
 xajax.call('enviarLogin', [xajax.getFormValues('formLoginCliente')]);
}

function enviarNovoClientes() {
 document.getElementById('caixaBtnNovoCadastro').innerHTML = HTMLCarregando;
 document.getElementById('msgNovoCadastro').innerHTML = '';
 xajax.call('enviarNovoClientes', [xajax.getFormValues('formLoginCliente')]);
}

function logout() {
   if (confirm("Tem certeza que deseja sair do sistema?"))
   xajax_logout();
}

function excluirCesta(id) {
 if (confirm("Tem certeza que deseja excluir este item?")) {
     document.getElementById('btnExcluir' + id).innerHTML = HTMLCarregando;
      xajax_excluirCesta(id);
 }
}

function ocultarMsg() {
  document.getElementById('lnMsg').style.display = 'none';
}

function finalizarPedido(total, desconto) {
  
  var desconto = document.getElementById("desconto");
  var resultado = "";
	
	if(desconto.checked == true) {
		
		resultado = "S";
	}
	else {
		
		resultado = "N";
	}
 document.getElementById('caixaBtnFinalizar').innerHTML = HTMLCarregando;	

 xajax_validaPedido(total, resultado);
}

function ocultarMsgSemEstoque() {
 document.getElementById('quadroMsgSemEstoque').style.display = 'none';	
}

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("http://www.powercel.com.br/novo/imagens/loaded.gif");
}

function pedidoPendente(id, mes, ano) {
 document.formPedidos.idPedido.value = id;
 document.formPedidos.mes.value = mes;
 document.formPedidos.ano.value = ano;
 document.formPedidos.submit();
}

function finalizarPedidoPendente(idPedido) {
 document.getElementById('caixaBtnFinalizar').innerHTML = HTMLCarregando;
 xajax_finalizaPedidoPendente(idPedido);
}

function enviarDataPedido() {
 document.getElementById('caixaEnviar').innerHTML = HTMLCarregando;
 xajax_enviarDataPedido(document.getElementById("campoMes").value, document.getElementById("campoAno").value);
}

function voltarDataPedido() {
 document.formPedidos.submit();	
}

function selecaoFormaPagamento(tipo) {
 document.getElementById("linhaValorFrete").style.display = "none";
 document.getElementById("linhaValorTotalGeral").style.display = "none";
 document.getElementById('caixaBtnFinalizar').innerHTML = HTMLCarregando;
 xajax_calcularFrete(tipo);
}

function enviarIndentificaDeposito() {
 document.getElementById('caixaBtnIndentifica').innerHTML = HTMLCarregando;
 xajax.call('enviarIndentificaDeposito', [xajax.getFormValues('formIndentificaDeposito')]);
}

function enviarCodigoDesbloqueio() {
 document.getElementById('caixaBtnCodigo').innerHTML = HTMLCarregando;
 xajax.call('enviarCodigoDesbloqueio', [xajax.getFormValues('formCodigoDesbloqueio')]);
}

function abrir(id){
   window.open('?idp='+id, '');
}

function mudarTipoCliente() {
 document.getElementById("msgTipo1").style.display = "none";
 document.getElementById("msgTipo2").style.display = "none";
 document.getElementById("msgTipo3").style.display = "none";
 document.getElementById("msgTipo" + document.getElementById("fk_tipo_cliente").value).style.display = "";  
}