var httpDef = 'http://' + document.location.hostname;
var httpsDef = 'https://' + document.location.hostname;
var cmpCombo = "";
var setMuniCli = "";
var qs=new Array();
var rand_no=0;

function consultaPrazoEntregaAjax(){
	try{
		if (document.getElementById('divMsgErro') != undefined)
			hideHtmlElement('divMsgErro');
	}catch(e){}
	
	showHtmlElement('loading');
	var id = document.frmDet.codMcr.value;
	var t = document.frmDet.tipMercConj.value;
	var cep1 = document.frmDet.cep1.value;
	var cep2 = document.frmDet.cep2.value;	
	retrieveURLPadrao('/prazoEntrega.do?acao=CEP&microZona=&uf=&municipio=&tipoMercadoria='+t+'&codMercadoria='+id+'&cep1='+cep1+'&cep2='+cep2,'divMostrarConsultaCep');
}

function fecharJanelaConsulta(){
	retrieveURLPadrao('/pages/pedido/consultaPrazoEntrega.jsp','divMostrarConsultaCep');
	hideHtmlElement('divConsultaPrazoEntrega');	
}

function consultaMicroZonaAjax(a, ori){
	var d = a.split("#");
	document.frmDet.microZona.value = d[7];
	document.frmDet.uf.value = d[4];
	document.frmDet.municipio.value = d[3];

	var id = document.frmDet.codMcr.value;
	var t = document.frmDet.tipMercConj.value;
	var microZona = document.frmDet.microZona.value;
	var uf = document.frmDet.uf.value;
	var municipio = document.frmDet.municipio.value;
	var cep1="";
	var cep2="";
	retrieveURLPadrao('/prazoEntrega.do?acao=MICRO&microZona='+microZona+'&uf='+uf+'&municipio='+municipio+'&tipoMercadoria='+t+'&codMercadoria='+id+'&cep1='+cep1+'&cep2='+cep2,'divMostrarConsultaCep');
}
function abrirDHTML(){
	try{
		document.getElementById('BannerDhtml').style.top = '550px';
		document.getElementById('BannerDhtml').style.zIndex = 9999;
		setTimeout("fecharAutoDiv()", 20000);
	}catch(e){}
}
function fecharAutoDiv(){
	try{
		document.getElementById('BannerDhtml').style.display = 'none';
	}catch(e){}
}
function fechardhtml(){
	try{
		document.getElementById('BannerDhtml').style.display = 'none';
	}catch(e){}
}

//Inicialização das variaveis
var variaveis=location.search.replace(/\x3F/,"").replace(/\x2B/g," ").split("&");
if(variaveis!=""){
    for(i=0;i<variaveis.length;i++){
        nvar=variaveis[i].split("=")
        qs[nvar[0]]=unescape(nvar[1])
    }
}

function CBChecaParametroCrossSite(CBParam,CBSplit){
	CBParam_ = '';
	CBParam_ = CBParam;
	if (CBSplit == 'S'){
		// REMOVE "|" e "?" do parametro
		if (CBParam_.indexOf('|')  != -1) {			// remove numero randomico do
			var c_tmp = CBParam_.split('|');	// final do parametro
			CBParam_ = c_tmp[0];
		}
		if (CBParam_.indexOf('?')  != -1) {			// remove numero randomico do
			var c_tmp = CBParam_.split('?');	// final do parametro
			CBParam_ = c_tmp[0];			
		}
	}
	else{
		if ( CBParam_.indexOf("?") != -1  ) {
			CBParam_ = '';
		}
	}
	var AUx;
	var AUlista = new Array();
	AUlista[0] = "#@"; AUlista[1] = "#$"; AUlista[2] = "\\>"; AUlista[3] = "'"; AUlista[4] = "="; AUlista[5] = "("; AUlista[6] = ")";  
	AUlista[7] = ">"; AUlista[8] = "<"; AUlista[9] = "*"; AUlista[10] = "}"; AUlista[11] = "{";  AUlista[12] = "select";
	AUlista[13] = "update"; AUlista[14] = "delete"; AUlista[15] = "where"; AUlista[16] = "union"; AUlista[17] = "from"; 
	AUlista[18] = "join"; AUlista[19] = "table"; AUlista[20] = "sysdate"; AUlista[21] = "varchar"; AUlista[22] = "integer";
	AUlista[23] = "&";
	for (AUx in AUlista){
		if ( CBParam_.indexOf(AUlista[AUx]) != -1 ) {
			CBParam_ = '';
		} 
	}
	return CBParam_;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  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 MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function QueryString(variavel){
	return qs[variavel];
}

function bannerMcr(mcr, tipMC, banner){
	detalharMercadoria(mcr, '', '', '', '', tipMC, '', '', '', banner, 'banner_interno');
}
function detalharMercadoria(mcr, isCel, codLoc, nmLoc, codOpe, tipMC, idVtr, idDepVtr, nmVtr, banner, origem){
	
	var fd = document.detalhar;
	fd.codMcr.value = mcr;
	fd.tipMercConj.value = tipMC;
	try{
		if (banner){
			if (parseInt(banner) > 0){
				fd.s_icid.value=banner;
			}
		}
	}catch(e){}
	try{
		var r = CBChecaParametroCrossSite(QueryString("s_pfm"), 'N');
			
		if (r && r !="undefined"){
			fd.s_pfm.value=r;
		}else{
			if (origem){
				fd.s_pfm.value=origem;
			}else{
				fd.s_pfm.value="especie";
			}
		}
	}catch(e){}
	try{
		if (idVtr){
			if (parseInt(idVtr) > 0){
				fd.strVitrine.value = idVtr + "_" + nmVtr + "_" + idDepVtr + "_" + mcr;
			}
		}
	}catch(e){}
	fd.action = httpDef + "/detalharItemVenda.do";
	fd.submit();
}

function trazerProdutosMarca(action,m,n, nt){
	buscaByMarcas(action,m,0,0,0,'L',n, nt);
}
function buscaByMarcas(action, m, d, p, t, v, n, nt){

	var vForm = document.forms[0];

	vForm.totalRegistros.value = t;
	vForm.pagina.value = p;
	vForm.codigoDepartamento.value = d;
	vForm.codMarca.value = m;
	vForm.chaveBusca.value = n;
	vForm.s_pfm.value = nt;
	if (v == undefined)
		v = '';
	if (v != '')
		vForm.view.value = v;

	vForm.action = httpDef + action;
	vForm.method = 'get';
	vForm.submit();
}
function mostrarMarcas(){
	var obj = document.getElementById('filtroMarcas');

	if(obj.style.display == '') {
		obj.style.display = 'none';
	} else {
		obj.style.display = '';
	}
}

function fOpenWin(url, w, h, l, t, scr, title){
	var j;
	var titulo;
	if (title==undefined)
		title = '';
	if (title == '')
		titulo = '_OpenWin';
	else
		titulo = title;
	
	if (scr == undefined)
		j = open(url,titulo,'location=no,resize=no, width=' + w + ',height=' + h + ',left=' + l + ',top=' + t + ',maximized=0,scrollbars=auto');
	else
		j = open(url,titulo,'location=no,resize=no, width=' + w + ',height=' + h + ',left=' + l + ',top=' + t + ',maximized=1,scrollbars=1');
		
	j.focus();
}

function verificarNomeClienteHome(ckN){
			
	var saudacao = "";
	var now = new Date();
	var hora = now.getHours();
	
	if (hora >= 12 && hora < 18)
		saudacao = "Boa tarde";
	else if (hora >= 18 && hora < 24)
		saudacao = "Boa noite";
	else if (hora >=0 && hora < 12)
		saudacao = "Bom dia";
	
	if (ckN != null && ckN != ""){
		document.getElementById("divNomeClienteHome").innerHTML = saudacao + " <strong>" + ckN + "</strong> <a href=\"javascript:logoff();\" class=\"fonteAzul\">(desconectar-se)</a>";
	}else{
		document.getElementById("divNomeClienteHome").innerHTML = saudacao + " <a href=\"javascript:fctIdentificar();\" class=\"fonteAzul\">(identifique-se)</a>";
	}
}
function retrieveURLNomeCliente(url){
	ajaxProcessoNomeCli(url,'processStateChangeCliente');
}
function ajaxProcessoNomeCli(url, orsc){
	if (window.XMLHttpRequest) { // Non-IE browsers
      	req = new XMLHttpRequest();
      	if (orsc != '')
      		req.onreadystatechange = eval(orsc);
      	try {
        	req.open("POST", url, true);
        	req.setRequestHeader('content-type', 'text/html; charset=ISO-8859-1');
        	req.setRequestHeader('Cache-Control', 'no-cache, no-store');
      	}catch (e) {
        	alert(e);
      	}
      	req.send(null);
    }else if (window.ActiveXObject) { // IE
      	req = new ActiveXObject("Microsoft.XMLHTTP");
      	if (req) {
      		if (orsc != '')
        		req.onreadystatechange = eval(orsc);
        	req.open("POST", url, true);
        	req.setRequestHeader('content-type', 'text/html; charset=ISO-8859-1');
        	req.setRequestHeader('Cache-Control', 'no-cache, no-store');
        	req.send();
      	}
    }
}
function processStateChangeCliente() {
   if (req.readyState == 4) { // Complete
      if (req.status == 200) { // OK response
      
      	var ckN = req.responseText;
		var saudacao = "";
		var now = new Date();
		var hora = now.getHours();
		
		if (hora >= 12 && hora < 18)
			saudacao = "Boa tarde";
		else if (hora >= 18 && hora < 24)
			saudacao = "Boa noite";
		else if (hora >=0 && hora < 12)
			saudacao = "Bom dia";
		
		if (ckN){
			document.getElementById("divNomeClienteHome").innerHTML = saudacao + " <strong>" + ckN + "</strong> <a href=\"javascript:logoff();\" class=\"fonteAzul\">(desconectar-se)</a>";
		}else{
			document.getElementById("divNomeClienteHome").innerHTML = saudacao + " <a href=\"javascript:fctIdentificar();\" class=\"fonteAzul\">(identifique-se)</a>";
		}
	  } 
   }
}
function fctSenhaRetiraLoja(){
	window.location.href = httpsDef + "/pedido/meusPedidos.do?paramDestino=senhaRetiraLoja";
}
function fctSegundaViaBoleto(){
	window.location.href = httpsDef + "/pedido/meusPedidos.do?paramDestino=segundaViaBoleto";
}
function fctMeusPedidos(){
	window.location.href = httpsDef + "/pedido/meusPedidos.do";
}
function fctMeusPedidosLojaFisica(){
	window.location.href = httpsDef + "/pedido/meusPedidos.do?paramDestino=listaPedidosFisica";
}
function fctAcessarCadastro(){
	window.location.href = httpsDef + "/cliente/cliente.do";
}
function fctAlterarSenha(){
	window.location.href = httpsDef + "/cliente/alterarSenha.do";
}
function fctIdentificar(origem){
	if (origem==undefined)
		window.location.href = httpsDef + "/cliente/meuCadastro.do";
	else
		window.location.href = httpsDef + "/cliente/meuCadastro.do?oriPag="+origem;
}
function logoff() {
	window.location.href = 'https://' + location.hostname + '/pkmslogout';
}
function _execute(action){
	var form = document.forms[0];
	form.action = action;
	form.submit();
}
function _abrirDepartamento(action, dep, banner){
	var form = document.forms[0];
	form.totalRegistros.value = 0;
	form.pagina.value = 0;
	form.codigoDepartamento.value = dep;

	try{	
		if (banner == undefined) banner = "";
		if (banner){
			if (parseInt(banner) > 0){
				fd.s_icid.value=banner;
			}
		}
	}catch(e){}
		
	form.action = action;
	form.submit();
}

function goLink(link){
	var a = '';
	var n = '';
	var v = '';
	
	if (link.indexOf('?')<0){
		var a = link;
	}else{
		link = link.replace(/&amp;/g,"&"); // replace All o "caracter HTML" de "&amp;" para "&"
		var a = link.substr( 0, link.indexOf('?') );
		var ll = link.substr( link.indexOf('?') +1 );
		var cc = ll.split('&');
		for (var i=0;i<cc.length;i++){
			if ("p0p1p2p3p4p5p6p7p8p9p10".indexOf(cc[i].substr(0,cc[i].indexOf('='))) < 0){
				n+=cc[i].substr(0,cc[i].indexOf('=')) + "|";
				v+=cc[i].substr(cc[i].indexOf('=')+1) + "|";
			}
		}
		if (n.length>0){
			if (n.substr(n.length-1)=='|'){
				if ("p0p1p2p3p4p5p6p7p8p9p10".indexOf( n.substr(0,n.length-1) ) < 0){
					n = n.substr(0,n.length-1);
					v = v.substr(0,v.length-1);
				}
			}
		}
	}
	
	cFR(a, n, v );
}
function cFR(a,n,v){
	document.fRedicionar.action = a;
	inputs = document.fRedicionar.elements;
	for(i=0;i<inputs.length;i++){
		c = eval('document.fRedicionar.p'+i);
		c.name='p'+i;
		c.value='';
	}
	var sn = n.split('|');
	var sv = v.split('|');
	for (var i=0;i<sn.length;i++){
		c = eval('document.fRedicionar.p'+i);
		c.name=sn[i];
		c.value=sv[i];
	}
	document.fRedicionar.submit();
}

function avitaBuscaPorEnter(event, l){

	var keycode;
	try{
		keycode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	}catch(e){
		return true;
	}
	if (keycode==13){
		document.frmConsulta.submit();
		return false;
	}
	return true;
}
function buscarPalavras(l){
/*
	var _x = document.frmConsulta.textoBusca.value;
	if ( _x == "" || _x == " " || _x == "  "){
		alert('Favor informar a palavra que deseja buscar.');
		document.frmConsulta.textoBusca.focus();
		return;
	}
	document.vitrineForm.listaDepartamentos.value = document.frmConsulta.departamentos.value;
	document.vitrineForm.chaveBusca.value = document.frmConsulta.textoBusca.value;
	document.vitrineForm.codSubSetor.value = "0";
	document.vitrineForm.action = l;
*/
	document.frmConsulta.submit();
}
function buscarPalavrasByTags(l, p, n){
	document.vitrineForm.listaDepartamentos.value = "02030405060708091011121314";
	document.vitrineForm.chaveBusca.value = p;
	document.vitrineForm.s_pfm.value = n;
	document.vitrineForm.action = l;
	document.vitrineForm.method = 'get';
	document.vitrineForm.submit();
}
function buscarPalavrasOnSubmit(l){
	document.vitrineForm.listaDepartamentos.value = document.frmConsulta.departamentos.value;
	document.vitrineForm.chaveBusca.value = document.frmConsulta.textoBusca.value;
	document.vitrineForm.action = l;
	return true;
}

function showHtmlElement(id){
	document.getElementById(id).style.display = 'block';
}
function hideHtmlElement(id){
	document.getElementById(id).style.display = 'none';
}
function mudarClass(obj, cl){
	obj.className = cl;
}
function loadElementsForm(vForm){
	var campos = '';
	if (vForm!= null){
		for(var i=0;i<vForm.elements.length;i++){		
		   	if (i==0){
	    		campos = vForm.elements[i].name + "=" + escape(vForm.elements[i].value);
	    	}else{
	    		campos += "&" + vForm.elements[i].name + "=" + escape(vForm.elements[i].value);
	    	}
	    }
    }    
    return campos;
}
function soNumEndEntrega(event){
	var keycode;
	try{
		keycode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	}catch(e){
		return true;
	}
	if (keycode==8 || keycode==9 || keycode==37 || keycode==39) return true;
	if(keycode < 48 || keycode > 57) {
		alert('Somente números devem ser informados!!!');
		return false;
	}
	return true;
}
function Tab(e, c){
   	if (e.value.length == e.maxLength){
		var _c = eval(c);
		_c.focus();
	}
}
function openChatOnLine(){
   // if (rand_no == 0){
//	rand_no = (Math.floor(Math.random() * 3) + 1);
//    }

//    if (rand_no == 1) {
//            window.open('https://server.iad.liveperson.net/hc/44904379/?cmd=file&file=visitorWantsToChat&site=44904379&imageUrl=https://server.iad.liveperson.net/hcp/Gallery/ChatButton-Gallery/Portuguese/General/1a/&referrer='+escape(document.location),'chat44904379','width=475,height=400,resizable=yes');
//    } else {
//            fOpenWin("https://chat.apoiocasasbahia.com.br", 400, 550, 100, 100, null, '_ChatOnLine_');
//    }

 	fOpenWin("https://chat.apoiocasasbahia.com.br", 400, 550, 100, 100, null, '_ChatOnLine_');
}
function soNum(obj,event){
	var keycode;
	try{
		keycode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	}catch(e){
		return true;
	}
	if (keycode==8 || keycode==9 || keycode==37 || keycode==39) return true;
	if(keycode < 48 || keycode > 57) return false;
	return true;
}
function zerarDadosAunica(){
	try{
		s.pageName = "";
		s.channel = "";
		s.pageType = "";
		s.events = "";
		s.server="";
		s.prop1="";
		s.prop2="";
		s.prop3="";
		s.prop4="";
		s.prop8="";
		s.prop9="";
		s.prop10="";
		s.prop11="";
		s.prop12="";
		s.prop13="";
		s.prop14="";
		s.prop16="";
		s.prop18="";
		s.campaign="";
		s.state="";
		s.zip="";
		s.products="";
		s.purchaseID="";
		s.eVar1="";
		s.eVar2="";
		s.eVar3="";
		s.eVar4="";
		s.eVar5="";
		s.eVar6="";
		s.eVar7="";
		s.eVar8="";
		s.eVar9="";
		s.eVar10="";
		s.eVar11="";
		s.eVar12="";
		s.eVar17="";
		s.eVar18="";
		s.eVar19="";
		s.eVar20="";
		s.eVar21="";
		s.eVar22="";
	}catch(e){}
}

function mudarMenu(id, imgCor){
	var sLI = eval("document.getElementById('LI-" + id + "')");
	sLI.style.backgroundImage = "url('" + imgCor + "')"; 
}

function mudarEstilo(obj, tip){
	obj.className = 'menuSubSetor' + tip;
}

