// JavaScript por DOUGLAS GOMES DE SOUZA
// FUN��ES VIA AJAX

try {
	xmlhttp = new XMLHttpRequest();
} catch(ee) {
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
}
try {
	xmlhttp2 = new XMLHttpRequest();
} catch(ee) {
	try {
		xmlhttp2 = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp2 = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp2 = false;
		}
	}
}

function new_ajax_get(div,valor)
{
	var carrega = document.getElementById(div);
	carrega.innerHTML = "Carregando...";
	
	var aspd = new AJAX();
	aspd.url = valor;
	aspd.processaresultado = function (recebe){
							var docu = document.getElementById(div);		
							docu.innerHTML = recebe;
						}
	aspd.conectar();
}

function alteraPacote(id,quant) {
	xmlhttp.open("GET", "/loja/carrinho/alteraPacote/"+id+"/"+quant, true);
	xmlhttp.onreadystatechange=function() {
		    // apenas quando o estado for "completado"
			if (xmlhttp.readyState == 1) {
				  document.getElementById("ladoA").innerHTML = "<center><img src=/img/loading.gif></center>";
				  document.getElementById("carrinhoBotoes").style.visibility="hidden";
			}
			/*if (xmlhttp.readyState == 2) {
				  document.getElementById("status").innerHTML = "<h3>Carregado</h3>";
			}
			if (xmlhttp.readyState == 3) {
				  document.getElementById("status").innerHTML = "<h3>Selecione o jogo para exibir</h3>";
			}
			*/
			if (xmlhttp.readyState == 4) {
        		// apenas se o servidor retornar "OK"
		        if (xmlhttp.status == 200) {
        	    // procura pela div e insere o conteudo
            	// retornado nela, como texto HTML
					document.getElementById("ladoA").innerHTML = xmlhttp.responseText;
					document.getElementById("carrinhoBotoes").style.visibility="visible";
					var sURL = unescape(window.location.pathname);
					//window.location.href = sURL;
					window.location.reload();
        		}
		    }
		}
	xmlhttp.send(null)
}

function alteraPacoteFixo(id,quant) {
	xmlhttp.open("GET", "/loja/carrinho/alteraPacoteFixo/"+id+"/"+quant, true);
	xmlhttp.onreadystatechange=function() {
		    // apenas quando o estado for "completado"
			if (xmlhttp.readyState == 1) {
				  document.getElementById("ladoA").innerHTML = "<center><img src=/img/loading.gif></center>";
				  document.getElementById("carrinhoBotoes").style.visibility="hidden";
			}
			/*if (xmlhttp.readyState == 2) {
				  document.getElementById("status").innerHTML = "<h3>Carregado</h3>";
			}
			if (xmlhttp.readyState == 3) {
				  document.getElementById("status").innerHTML = "<h3>Selecione o jogo para exibir</h3>";
			}
			*/
			if (xmlhttp.readyState == 4) {
        		// apenas se o servidor retornar "OK"
		        if (xmlhttp.status == 200) {
        	    // procura pela div e insere o conteudo
            	// retornado nela, como texto HTML
					document.getElementById("ladoA").innerHTML = xmlhttp.responseText;
					document.getElementById("carrinhoBotoes").style.visibility="visible";
					var sURL = unescape(window.location.pathname);
					window.location.href = sURL;
        		}
		    }
		}
	xmlhttp.send(null)
}

function alteraRefeicao(id,quant) {
	xmlhttp.open("GET", "/loja/carrinho/alteraRefeicao/"+id+"/"+quant, true);
	xmlhttp.onreadystatechange=function() {
		    // apenas quando o estado for "completado"
			if (xmlhttp.readyState == 1) {
				  document.getElementById("ladoA").innerHTML = "<center><img src=/img/loading.gif></center>";
				  document.getElementById("carrinhoBotoes").style.visibility="hidden";
			}
			/*if (xmlhttp.readyState == 2) {
				  document.getElementById("status").innerHTML = "<h3>Carregado</h3>";
			}
			if (xmlhttp.readyState == 3) {
				  document.getElementById("status").innerHTML = "<h3>Selecione o jogo para exibir</h3>";
			}
			*/
			if (xmlhttp.readyState == 4) {
        		// apenas se o servidor retornar "OK"
		        if (xmlhttp.status == 200) {
        	    // procura pela div e insere o conteudo
            	// retornado nela, como texto HTML
					document.getElementById("ladoA").innerHTML = xmlhttp.responseText;
					document.getElementById("carrinhoBotoes").style.visibility="visible";
					var sURL = unescape(window.location.pathname);
					window.location.href = sURL;
        		}
		    }
		}
	xmlhttp.send(null)
}

function alteraRefeicaoBrinde(id,variacao,kit_id,linha_id) {
	
	quantidadeAlterada = Number(document.getElementById('b_quant_'+ id).value);
	
	if (parseInt(variacao) == 0) {
		quant = quantidadeAlterada-ultimaQuantidadeValida;
		
		if (quant+ultimaQuantidadeValida < 0) {
			quant = 0;
		}
	} else {
		quant = variacao;
	}
	
	/*	
	elemento = 'b_quant_'+ id;
	quantidadeAlterada = document.getElementById(elemento);
	qA = Number(quantidadeAlterada.value)+variacao;
	quantidadeAlterada.value = qA;
	quant = qA;
	quant_b = parseInt(eval(variacao));
	*/	
	
	if (linha_id) {
		linha = "/"+linha_id;
	} else {
		linha = "";
	}
	
	// efetua operacao no banco de dados
	xmlhttp.open("GET", "/loja/carrinho/alteraRefeicaoBrinde/"+id+"/"+quant+"/"+kit_id+"/"+linha, true);
	xmlhttp.onreadystatechange=function() {
		    // apenas quando o estado for "completado"
			if (xmlhttp.readyState == 1) {
				  document.getElementById("tableBrindes").innerHTML = "<p style='display: block; margin-left: 120px;'><img src=/img/loading.gif></p>";
			}
			
			if (xmlhttp.readyState == 4) {
        		// apenas se o servidor retornar "OK"
		        if (xmlhttp.status == 200) {
        	    // procura pela div e insere o conteudo
            	// retornado nela, como texto HTML
					document.getElementById("tableBrindes").innerHTML = xmlhttp.responseText;
					//var sURL = unescape(window.location.pathname);
					//window.location.href = sURL;
        		}
		    }
		}
	xmlhttp.send(null);
}

function alteraRefeicaoTeste(id,variacao,kit_id,linha_id,is_busca) {
	
	elemento = 'quant_'+ id;
	quantidadeAlterada = document.getElementById(elemento);
	qA = Number(quantidadeAlterada.value)+variacao;
	quantidadeAlterada.value = qA;
	quant = qA;
	// atualiza subtotal
	preco = parseFloat(eval(document.getElementById("preco_"+id).value));
	quant_b = parseInt(eval(variacao));
	
	// subtotal
	if (variacao == 0) {
		subtotal = (qA-ultimaQuantidadeValida) * preco;
	} else {
		subtotal = variacao * preco;
	}
	totalatual = parseFloat(eval(document.getElementById("pacoteTotal").value));
	total = totalatual + subtotal;
	
	document.getElementById("viewTotal").innerHTML = moeda(total);
	document.getElementById("pacoteTotal").value=total;
	
	if (linha_id) {
		linha = "/"+linha_id;
	} else {
		linha = "";
	}
	// efetua operacao no banco de dados
	xmlhttp.open("GET", "/loja/carrinho/alteraRefeicaoTeste/"+id+"/"+quant+"/"+kit_id+"/"+linha, true);
	xmlhttp.onreadystatechange=function() {
		    // apenas quando o estado for "completado"
			if (xmlhttp.readyState == 1) {
				  document.getElementById("tablePacotes").innerHTML = "<p style='display: block; margin-left: 120px;'><img src=/img/loading.gif></p>";
			}
			
			if (xmlhttp.readyState == 4) {
        		// apenas se o servidor retornar "OK"
		        if (xmlhttp.status == 200) {
                        // procura pela div e insere o conteudo
                        // retornado nela, como texto HTML
                            if (is_busca != 1) {
                                document.getElementById("tablePacotes").innerHTML = xmlhttp.responseText;
                            } else {
                                var sURL = unescape(window.location.pathname);
                                window.location.href = sURL;
                            }
        		}
		    }
		}
	xmlhttp.send(null)
}

function showDialog(title,message,type,autohide) {
	
	xmlhttp.open("GET", "/loja/infoproduto/"+message, true);
	xmlhttp.onreadystatechange=function() 
	{
		if (xmlhttp.readyState == 4) {
     		// apenas se o servidor retornar "OK"
	        if (xmlhttp.status == 200) {
       	    // procura pela div e insere o conteudo
           	// retornado nela, como texto HTML
				message = xmlhttp.responseText;
	
  if(!type) {
    type = 'error';
  }
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogmask;
  if(!document.getElementById('dialog')) {
    dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';
    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);;
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;
  } else {
    dialog = document.getElementById('dialog');
    dialogheader = document.getElementById('dialog-header');
    dialogtitle = document.getElementById('dialog-title');
    dialogclose = document.getElementById('dialog-close');
    dialogcontent = document.getElementById('dialog-content');
    dialogmask = document.getElementById('dialog-mask');
	
	document.body.removeChild(dialogmask);
	document.body.removeChild(dialog);
	
	dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';
    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);;
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;
	
	
    dialogmask.style.visibility = "visible";
    dialog.style.visibility = "visible";
  }
  dialog.style.opacity = .00;
  dialog.style.filter = 'alpha(opacity=0)';
  dialog.alpha = 0;
  var width = document.body.clientWidth;
  var height = document.body.clientHeight;
  var left = 2;
  var top = 2;
  var dialogwidth = dialog.offsetWidth;
  var dialogheight = dialog.offsetHeight;
  var topposition = top + (height / 3) - (dialogheight / 2);
  var leftposition = left + (width / 2) - (dialogwidth / 2);
  dialog.style.top = topposition + "px";
  dialog.style.left = leftposition + "px";
  dialogheader.className = type + "header";
  dialogtitle.innerHTML = title;
  dialogcontent.className = type;
  dialogcontent.innerHTML = message;
  var content = document.getElementById(WRAPPER);
  dialogmask.style.height = content.offsetHeight*3 + 'px';
  dialog.timer = setInterval("fadeDialog(1)", TIMER);
  if(autohide) {
    dialogclose.style.visibility = "hidden";
    window.setTimeout("hideDialog()", (autohide * 1000));
  } else {
    dialogclose.style.visibility = "visible";
  }
  
  
         	}
		}
	}
	xmlhttp.send(null)



}

function popupMudaEntrega(title,message,type,autohide) {
	
	var entregaEndereco;
	entregaEndereco = document.getElementById('frmEntregaEndereco').value;
	var entregaNumero;
	entregaNumero = document.getElementById('frmEntregaNumero').value;
	var entregaComplemento;
	entregaComplemento = document.getElementById('frmEntregaComplemento').value;
	var entregaReferencia;
	entregaReferencia = document.getElementById('frmEntregaReferencia').value;
	var entregaBairro;
	entregaBairro = document.getElementById('frmEntregaBairro').value;
	var entregaCidade;
	entregaCidade = document.getElementById('frmEntregaCidade').value;
	var entregaEstado;
	entregaEstado = document.getElementById('frmEntregaEstado').value;
	var entregaLocalidade;
	entregaLocalidade = document.getElementById('frmEntregaLocalidade').value;
	
	xmlhttp.open("GET", "/loja/agendaentrega/muda/", true);
	xmlhttp.onreadystatechange=function() 
	{
		if (xmlhttp.readyState == 4) {
     		// apenas se o servidor retornar "OK"
	        if (xmlhttp.status == 200) {
       	    // procura pela div e insere o conteudo
           	// retornado nela, como texto HTML
				message = xmlhttp.responseText;
	
  if(!type) {
    type = 'error';
  }
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogmask;
  if(!document.getElementById('dialog')) {
    dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';
    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);;
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;
  } else {
    dialog = document.getElementById('dialog');
    dialogheader = document.getElementById('dialog-header');
    dialogtitle = document.getElementById('dialog-title');
    dialogclose = document.getElementById('dialog-close');
    dialogcontent = document.getElementById('dialog-content');
    dialogmask = document.getElementById('dialog-mask');
	
	document.body.removeChild(dialogmask);
	document.body.removeChild(dialog);
	
	dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';
    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);;
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;
	
	
    dialogmask.style.visibility = "visible";
    dialog.style.visibility = "visible";
  }
  dialog.style.opacity = .00;
  dialog.style.filter = 'alpha(opacity=0)';
  dialog.alpha = 0;
  var width = document.body.clientWidth;
  var height = document.body.clientHeight;
  var left = 2;
  var top = 2;
  var dialogwidth = dialog.offsetWidth;
  var dialogheight = dialog.offsetHeight;
  var topposition = top + (height / 3) - (dialogheight / 2);
  var leftposition = left + (width / 2) - (dialogwidth / 2);
  dialog.style.top = topposition + "px";
  dialog.style.left = leftposition + "px";
  dialogheader.className = type + "header";
  dialogtitle.innerHTML = title;
  dialogcontent.className = type;
  dialogcontent.innerHTML = message;
  var content = document.getElementById(WRAPPER);
  dialogmask.style.height = content.offsetHeight*2 + 'px';
  dialog.timer = setInterval("fadeDialog(1)", TIMER);
  if(autohide) {
    dialogclose.style.visibility = "hidden";
    window.setTimeout("hideDialog()", (autohide * 1000));
  } else {
    dialogclose.style.visibility = "visible";
  }
  
  
         	}
		}
	}
	xmlhttp.send(null)



}


function buscaLogradouro(cep) {
	
	document.getElementById("tdButtonCep").innerHTML = "Localizando, aguarde ...";
	cep = cep.replace(/\D/g,"");
	xmlhttp.open("GET", "/loja/buscalogradouro/"+cep, true);
	xmlhttp.onreadystatechange=function() {
		    // apenas quando o estado for "completado"
			if (xmlhttp.readyState == 4) {
        		// apenas se o servidor retornar "OK"
		        if (xmlhttp.status == 200) {
        	    // procura pela div e insere o conteudo
            	// retornado nela, como texto HTML
					eval(xmlhttp.responseText);
        		}
				document.getElementById("tdButtonCep").innerHTML = '<input type="button" name="button" id="button" style="font-size:10px;" value="Ok" onclick="javascript:buscaLogradouro(document.getElementById(\'formCEP\').value);" />';
		    }
		}
	xmlhttp.send(null);
}

function exibeCamposEndereco(valor) {
	
	if (valor) {
		
		if (navigator.appName.indexOf("Explorer") >= 0 && parseInt(navigator.appVersion) < 7) {	
			tipo = "block";	
		} else {
			tipo = "table-row";
		}
		
		document.getElementById("trEstado").style.display = tipo;
		document.getElementById("trCidade").style.display = tipo;
		document.getElementById("trBairro").style.display = tipo;
		document.getElementById("trLogradouro").style.display = tipo;
		document.getElementById("tdEndereco").style.display = tipo;
		document.getElementById("tdNumero").style.display = tipo;
		document.getElementById("tdBloco").style.display = tipo;
		document.getElementById("tdApartamento").style.display = tipo;
		document.getElementById("tdComplemento").style.display = tipo;
		document.getElementById("tdReferencia").style.display = tipo;

	} else {
		document.getElementById("trEstado").style.display = "none";
		document.getElementById("trCidade").style.display = "none";
		document.getElementById("trBairro").style.display = "none";
		document.getElementById("trLogradouro").style.display = "none";
		document.getElementById("tdEndereco").style.display = "none";
		document.getElementById("tdNumero").style.display = "none";
		document.getElementById("tdBloco").style.display = "none";
		document.getElementById("tdApartamento").style.display = "none";
		document.getElementById("tdComplemento").style.display = "none";
		document.getElementById("tdReferencia").style.display = "none";
	}
}

function listaCidades(uf) {
	document.getElementById("tdBairro").innerHTML = "<select><option>-</option></select>";
	document.getElementById("tdCidade").innerHTML = "Aguarde...";
	xmlhttp.open("GET", "/loja/cidadesebairros/cidades/"+uf, true);
	xmlhttp.onreadystatechange=function() {
		    // apenas quando o estado for "completado"
			if (xmlhttp.readyState == 4) {
        		// apenas se o servidor retornar "OK"
		        if (xmlhttp.status == 200) {
        	    // procura pela div e insere o conteudo
            	// retornado nela, como texto HTML
					document.getElementById("tdCidade").innerHTML = xmlhttp.responseText;
        		} else {
        			document.getElementById("tdCidade").innerHTML = "";
        		}
		    }
		}
	xmlhttp.send(null)
}

function listaCidadesComRota(uf) {
	document.getElementById("tdBairro").innerHTML = "<select><option>-</option></select>";
	document.getElementById("tdCidade").innerHTML = "Aguarde...";
	xmlhttp.open("GET", "/loja/cidadesebairros/cidadescomrota/"+uf, true);
	xmlhttp.onreadystatechange=function() {
		    // apenas quando o estado for "completado"
			if (xmlhttp.readyState == 4) {
        		// apenas se o servidor retornar "OK"
		        if (xmlhttp.status == 200) {
        	    // procura pela div e insere o conteudo
            	// retornado nela, como texto HTML
					document.getElementById("tdCidade").innerHTML = xmlhttp.responseText;
        		} else {
        			document.getElementById("tdCidade").innerHTML = "";
        		}
		    }
		}
	xmlhttp.send(null)
}

function listaBairros(cidade) {
	document.getElementById("tdBairro").innerHTML = "Aguarde...";
	xmlhttp.open("GET", "/loja/cidadesebairros/bairros/"+cidade, true);
	xmlhttp.onreadystatechange=function() {
		    // apenas quando o estado for "completado"
			if (xmlhttp.readyState == 4) {
        		// apenas se o servidor retornar "OK"
		        if (xmlhttp.status == 200) {
        	    // procura pela div e insere o conteudo
            	// retornado nela, como texto HTML
					document.getElementById("tdBairro").innerHTML = xmlhttp.responseText;
        		} else {
        			document.getElementById("tdBairro").innerHTML = "";
        		}
		    }
		}
	xmlhttp.send(null)
}

function listaBairros2(cidade) {
	document.getElementById("tdBairro").innerHTML = "Aguarde...";
	xmlhttp.open("GET", "/loja/cidadesebairros/bairros2/"+cidade, true);
	xmlhttp.onreadystatechange=function() {
		    // apenas quando o estado for "completado"
			if (xmlhttp.readyState == 4) {
        		// apenas se o servidor retornar "OK"
		        if (xmlhttp.status == 200) {
        	    // procura pela div e insere o conteudo
            	// retornado nela, como texto HTML
					document.getElementById("tdBairro").innerHTML = xmlhttp.responseText;					
        		} else {
        			document.getElementById("tdBairro").innerHTML = "";
        		}
		    }
		}
	xmlhttp.send(null)
}

function outroBairro() {
	document.getElementById("tdBairro").innerHTML = '<input name="formBAIRRO" type="text" id="textfield11" size="32">';
}

// Adicionado por Julio Fiuza
function listaHorariosExpress(intRota,strData) {
	document.getElementById("tdHorario").innerHTML = "Aguarde...";
	xmlhttp.open("GET", "/loja/agendaentrega/horariosexpress/"+strData+"/"+intRota, true);
	xmlhttp.onreadystatechange=function() {
		    // apenas quando o estado for "completado"
			if (xmlhttp.readyState == 4) {
        		// apenas se o servidor retornar "OK"
		        if (xmlhttp.status == 200) {
        	    // procura pela div e insere o conteudo
            	// retornado nela, como texto HTML
					document.getElementById("tdHorario").innerHTML = xmlhttp.responseText;
        		} else {
        			document.getElementById("tdHorario").innerHTML = "";
        		}
		    }
		}
	xmlhttp.send(null)
}
// FIM
function troca(codigo,id,exceto,etapa,dia) {
	xmlhttp.open("GET", "/loja/troca/processa/"+codigo+"/"+id+"/false/"+exceto, true);
	xmlhttp.onreadystatechange=function() 
	{
		if (xmlhttp.readyState == 4) {
     		// apenas se o servidor retornar "OK"
	        if (xmlhttp.status == 200) {
       	    // procura pela div e insere o conteudo
           	// retornado nela, como texto HTML
				document.getElementById(id).innerHTML = xmlhttp.responseText;
			}
		}
	}
	xmlhttp.send(null)
	
	xmlhttp2.open("GET", "/loja/troca/processa/"+codigo+"/"+id+"/true/"+exceto+"/"+etapa+"/"+dia, true);
	xmlhttp2.onreadystatechange=function() 
	{
		if (xmlhttp2.readyState == 4) {
     		// apenas se o servidor retornar "OK"
	        if (xmlhttp2.status == 200) {
       	    // procura pela div e insere o conteudo
           	// retornado nela, como texto HTML
				document.getElementById(id+"_btn").innerHTML = xmlhttp2.responseText;				
				//hideDialog;
				//jQuery('#overlay').overlay().close();
				tb_remove();
			}
		}
	}
	xmlhttp2.send(null)	
	
}

function retroca(codigo,id,etapa,dia) {
	
	xmlhttp.open("GET", "/loja/troca/retroca/"+codigo+"/"+id+"/"+etapa+"/"+dia+"/false", true);
	xmlhttp.onreadystatechange=function() 
	{
		if (xmlhttp.readyState == 4) {
     		// apenas se o servidor retornar "OK"
	        if (xmlhttp.status == 200) {
       	    // procura pela div e insere o conteudo
           	// retornado nela, como texto HTML
				document.getElementById(id).innerHTML = xmlhttp.responseText;
			}
		}
	}
	xmlhttp.send(null)
	
	xmlhttp2.open("GET", "/loja/troca/retroca/"+codigo+"/"+id+"/"+etapa+"/"+dia+"/true", true);
	xmlhttp2.onreadystatechange=function() 
	{
		if (xmlhttp2.readyState == 4) {
     		// apenas se o servidor retornar "OK"
	        if (xmlhttp2.status == 200) {
       	    // procura pela div e insere o conteudo
           	// retornado nela, como texto HTML
				document.getElementById(id+"_btn").innerHTML = xmlhttp2.responseText;				
				//hideDialog;
			}
		}
	}
	xmlhttp2.send(null)	
	
}

function abretroca(title,codigo,etapa,dia,id,type,autohide) {
	
	xmlhttp.open("GET", "/loja/troca/lista/"+codigo+"/"+etapa+"/"+dia+"/"+id, true);
	xmlhttp.onreadystatechange=function() 
	{
		if (xmlhttp.readyState == 4) {
     		// apenas se o servidor retornar "OK"
	        if (xmlhttp.status == 200) {
       	    // procura pela div e insere o conteudo
           	// retornado nela, como texto HTML
				message = xmlhttp.responseText;
	
  if(!type) {
    type = 'error';
  }
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogmask;
  if(!document.getElementById('dialog')) {
    dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';
    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);;
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;
  } else {
    dialog = document.getElementById('dialog');
    dialogheader = document.getElementById('dialog-header');
    dialogtitle = document.getElementById('dialog-title');
    dialogclose = document.getElementById('dialog-close');
    dialogcontent = document.getElementById('dialog-content');
    dialogmask = document.getElementById('dialog-mask');
	
	document.body.removeChild(dialogmask);
	document.body.removeChild(dialog);
	
	dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';
    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);;
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;
	
	
    dialogmask.style.visibility = "visible";
    dialog.style.visibility = "visible";
  }
  dialog.style.opacity = .00;
  dialog.style.filter = 'alpha(opacity=0)';
  dialog.alpha = 0;
  var width = document.body.clientWidth;
  var height = document.body.clientHeight;
  var left = 2;
  var top = 2;
  var dialogwidth = dialog.offsetWidth;
  var dialogheight = dialog.offsetHeight;
  var topposition = top + (height / 3) - (dialogheight / 2);
  var leftposition = left + (width / 2) - (dialogwidth / 2);
  dialog.style.top = topposition + "px";
  dialog.style.left = leftposition + "px";
  dialogheader.className = type + "header";
  dialogtitle.innerHTML = title;
  dialogcontent.className = type;
  dialogcontent.innerHTML = message;
  var content = document.getElementById("dialog");
  dialogmask.style.height = content.offsetHeight*3 + 'px';
  dialog.timer = setInterval("fadeDialog(1)", (autohide * 1000));
  if(autohide) {
    dialogclose.style.visibility = "hidden";
    window.setTimeout("hideDialog()", (autohide * 1000));
  } else {
    dialogclose.style.visibility = "visible";
  }
  
  
         	}
		}
	}
	xmlhttp.send(null)

}

function hideDialog() {
	document.body.removeChild(dialogmask);
	document.body.removeChild(dialog);
}
