﻿
	function ResumoCesta()
	{
		try
		{
			
			cabecalho.PublicaResumoCesta(callback_ResumoCesta);
		} 
		catch(err)
		{
			AlertaErroAjax(err);
			
		}
	}

	function callback_ResumoCesta(res)
	{
		if (res.error)
		{
			AlertaErroAjax(res.error);

		}	
		
		if (res.value)
		{
			
			if (res.value[0])
			{
				document.getElementById("QtdeItens").innerHTML = res.value[0];
			}/*
			if (res.value[1])
			{
				document.getElementById("ValorItens").innerHTML = res.value[1];
			}
			*/

		}
	}
	function Topo_Identifica()
	{
		try
		{

			cabecalho.Identifica(callback_Topo_Identifica);
		} 
		catch(err)
		{
			AlertaErroAjax(err);

		}
	}

	function callback_Topo_Identifica(res)
	{
		if (res.error)
		{
			AlertaErroAjax(res.error);

		}	
		
		if (res.value)
		{
			if (document.getElementById("identificacao"))
			{
			    document.getElementById("identificacao").innerHTML = res.value;
			}
			

		}
	}

    // (1) cabecalho.ascx.cs
	function Topo_DataCompleta()
	{
		try
		{

			cabecalho.RetornaDataCompleta(callback_Topo_DataCompleta);
		} 
		catch(err)
		{
			AlertaErroAjax(err);

		}
	}

	function callback_Topo_DataCompleta(res)
	{
		if (res.error)
		{
			AlertaErroAjax(res.error);

		}	
		
		if (res.value)
		{
			if (document.getElementById("DataCompleta"))
			{
			    document.getElementById("DataCompleta").innerHTML = res.value;
			}
	
		}
	}
	
	// (1) cabecalho.ascx.cs
	function ExcluiCookieLogin()
	{
		cabecalho.ExcluiCookieLogin(ExcluiCookieLogin_callback)
	}
	
	function ExcluiCookieLogin_callback(retorno)
	{
		try
		{
			if(retorno.error == null)
			{
				if(retorno.value)
				{
					window.location.href = retorno.value;
				}
			}
			else
			{
				AlertaErroAjax(retorno.error);
			}
		}
		catch(ex)
		{
			AlertaErroAjax(ex);
		}
	}
	
	// (4) includes/cabecalho.ascx
    //     includes/rodape.ascx
	function Buscar(local)
	{


		/*
		try
		{
		*/
			var mensagem = "";
			var fabricante = "";
			var palavra;
			var categoria;
			var aux_palavra;
			
			//Replace em javascript utiliza expressão regular; nestes casos abaixo, só será removida a primeira ocorrência de cada caractere.
			//Acrescentada a remoção tbm no C#			
			
			if (local == "Topo")
			{
				palavra = document.getElementById("cabecalho_txtBusca").value.replace("[", "").replace("]", "").replace("(", "").replace(")", "").replace("&", "").replace("%", "");
				categoria = document.getElementById("cabecalho_DropLojas").options[document.getElementById("cabecalho_DropLojas").selectedIndex].value;
				
				if (document.getElementById("cabecalho_DropFabricantes"))
				{
					fabricante = document.getElementById("cabecalho_DropFabricantes").options[document.getElementById("cabecalho_DropFabricantes").selectedIndex].value;
				}
				else
				{
					fabricante = 0;
				}
			}
			if (local == "Rodape")
			{
				palavra = document.getElementById("rodape_txtBusca").value.replace("[", "").replace("]", "").replace("(", "").replace(")", "").replace("&", "").replace("%", "");
				categoria = document.getElementById("rodape_DropLojas").options[document.getElementById("rodape_DropLojas").selectedIndex].value;
				
				if (document.getElementById("rodape_DropFabricantes"))
				{
					fabricante = document.getElementById("rodape_DropFabricantes").options[document.getElementById("rodape_DropFabricantes").selectedIndex].value;
				}
				else
				{
					fabricante = 0;
				}
			}

            aux_palavra = palavra;

			if(palavra == "" && (categoria == "" || categoria == "0") && (fabricante == "" || fabricante == "0"))
			{
				mensagem+="Valor inválido (todos os campos vazios ou inválidos) !\n";
			}

			if (mensagem == "" && aux_palavra.replace(/-/g,"") == "")
			{
				mensagem+="Valor inválido (todos os campos vazios ou inválidos) !\n";
			}			

			if(mensagem != "")
			{
				alert(mensagem);
				TrocaImagem("loading", "hidden");
			}
			else
			{
				cabecalho.ArmazenaDadosBusca(palavra.toString(), categoria.toString(), fabricante.toString(), Redirect_callback_ArmazenaDadosBusca);				
			}
		/*
		}
		catch(err)
		{
			AlertaErroAjax(err);
			TrocaImagem("loading", "hidden");
		}
		*/
	}

	function Redirect_callback_ArmazenaDadosBusca(res)
	{
		if (res.error)
		{
			AlertaErroAjax(res.error);
		}

		if (res.value)
		{
			if (res.value == "1")
			{
				alert("Valor inválido (todos os campos vazios ou inválidos) !\n");
			}
			else if (res.value == "2")
			{
				alert("Você tentou buscar uma palavra proibida !\n");
			}	
			else
			{
				window.location.href = res.value;
			}
		}
	}
	
	function ArmazenaDadosBusca(bfc, bp, bq, bo, bs, bcr, bcc, bprIni, bprFim, totalItens) {
	    busca.ArmazenaDadosBusca(bfc, bp, bq, bo, bs, bcr, bcc, bprIni, bprFim, totalItens);
    }

    function ArmazenaDadosCategoria(cfc, cp, cq, co, cs, ccr, cc, cprIni, cprFim) {
	    categoria.ArmazenaDadosCategoria(cfc, cp, cq, co, cs, ccr, cc, cprIni, cprFim);
    }

    function ArmazenaDadosFabricante(fc, fp, fq, fo, fs, fcr, fprIni, fprFim) { 
	    fabricante.ArmazenaDadosFabricante(fc, fp, fq, fo, fs, fcr, fprIni, fprFim);
    }
	
	function abreChat(URL)
    {
	    var pop_window = window.open(URL,"Ajuda","width=350,height=350,resizable=0,toolbar=0,location=0,directories=0,status=0,menubar=0");
	    pop_window.focus();
    }

    function MostraHistoricoNavegacao()
    {
        if (document.getElementById("divHistoricoNavegacao"))
        {
            MostraCookieProdutosNavegados();
            MostraCookieProdutosBuscados();
            MostraCookieProdutosAdicionados();
            MostraCookieCategoriasNavegadas();
        }
    }
