var xmlhttp;
if (window.XMLHttpRequest){
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
}else if (window.ActiveXObject){
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}else{
  alert("Seu navegador não suporta esse recurso.");
}


function hideAll(){
	document.getElementById("div_enviar").style.display = "none";
	document.getElementById("div_retirar").style.display = "none";
	document.getElementById("escolhe_frete_div").style.display= "none";
	document.getElementById("modo_pg_div").style.display= "none";
	document.getElementById("confirm_table").style.display = "none";
	
}

function callDetail(codproduct){
	document.getElementById("detalhes"+codproduct).style.display = "block";
	document.getElementById("detalhar_link"+codproduct).style.display = "none";
	document.getElementById("ocultar_detalhar_link"+codproduct).style.display = "inline";
}

function hideDetail(codproduct){
	document.getElementById("detalhes"+codproduct).style.display = "none";
	document.getElementById("detalhar_link"+codproduct).style.display = "inline";
	document.getElementById("ocultar_detalhar_link"+codproduct).style.display = "none";
}


function showEntrega(modo){

	if(modo == "enviar"){
		document.getElementById("div_enviar").style.display= "block";
		document.getElementById("div_retirar").style.display= "none";
		document.getElementById("modo_pg_div").style.display= "none";
		document.getElementById("confirm_table").style.display = "none";
		document.getElementById("modo_entrega").value = "enviar";
		
		
	  }else if(modo == "retirar"){
		document.getElementById("div_enviar").style.display= "none";
		document.getElementById("div_retirar").style.display= "block";
		document.getElementById("escolhe_frete_div").style.display= "none";
		document.getElementById("modo_pg_div").style.display= "block";
		document.getElementById("confirm_table").style.display = "none";
		document.getElementById("modo_entrega").value = "retirar";
		showResume(0);
		
	  }
		
}

function hideBelowAddress(){
	document.getElementById("escolhe_frete_div").style.display= "none";
	document.getElementById("modo_pg_div").style.display= "none";
	document.getElementById("confirm_table").style.display = "none";
}

function calculoFrete(){
	
	hideBelowAddress();
	
	//PARAMETROS NECESSÁRIO PARA RODAR ESSA FUNÇAO
	var peso = document.getElementById("peso_field");
	var ctyresume = document.getElementById("cty_resume_field");
	var state = document.getElementById("state_field");
	
	
	var resultsbox = document.getElementById("escolhe_frete_div");
	resultsbox.innerHTML="<img src='templates/default/loading.gif'>";
	
	var url = "ajax_procs/ajax_calcfrete.php?peso="+peso.value+"&cty_resume="+ctyresume.value+"dada&state="+state.value+"";
	
	
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			resultsbox.style.display="block";
			resultsbox.innerHTML=xmlhttp.responseText;
			window.scrollBy(0,100);
		}
	};	
	
}


function showResume(opcao) {
	
	document.getElementById("opt_frete").value=opcao;
	
	total = document.getElementById("valor_total").value;
	points = document.getElementById("valor_points").value;
	
	if(opcao == 0){
		var url = "ajax_procs/ajax_calcpgto.php?total="+total+"&points="+points;
	}
	else{
		frete = document.getElementById("radio_frete_"+opcao).value;
		var url = "ajax_procs/ajax_calcpgto.php?total="+total+"&points="+points+"&frete="+frete;
	}
	
	var resultsbox = document.getElementById("modo_pg_div");
	resultsbox.innerHTML="<img src='templates/default/loading.gif'>";
	
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			resultsbox.innerHTML=xmlhttp.responseText;
			resultsbox.style.display="block";
			document.getElementById("confirm_table").style.display="none";
			document.getElementById("carrinho_pagto_boleto").focus();
			window.scrollBy(0,100);
			
		}
	};		
}

function showConfirmTable(opt) {
	// ESSA FUNÇÃO MOSTRA A TABELA FINAL DE VALORES
	// OCULTA A OPÇÃO DE PAGAMENTO
	// E O BOTÃO PARA CONFIRMAR
	var modo_entrega = document.getElementById("modo_entrega").value;
	var receber = document.getElementById("receber").value;
	
	var opt_frete = document.getElementById("opt_frete").value;
	var frete = document.getElementById("frete_final_value").value;
	var desconto = document.getElementById("select_discount_number").value; 
	
	//ESITE UMA DIFERENÇA ENTRE O PARAMETRO DA FUNÇÃO SETATTRIBUTE()
	//ENTRE OS NAVEGADORES E POR ISSO EXISTE A CORREÇÃO ABAIXO
	if(navigator.appName == "Microsoft Internet Explorer"){
		var classe = "className";
	}else{
		var classe = "class";
	}
	
	if(opt==1){
		//COMPRA COM CARBONUS
		var url = "ajax_procs/ajax_calcfinal.php?total="+total+"&frete="+frete+"&desconto="+desconto;
		document.getElementById("opcao_pagto_boleto").setAttribute(classe, "off_opcao_pagto");
		document.getElementById("opcao_pagto_bonus").setAttribute(classe, "opcao_pagto");
		document.getElementById("opcao_pagto_cartao").setAttribute(classe, "off_opcao_pagto");
		document.getElementById("carrinho_pagto_bonus").setAttribute(classe, "carrinho_pagto_carbonus");
		document.getElementById("carrinho_pagto_boleto").setAttribute(classe, "off_button_pagto");
		document.getElementById("modo_pagto").value = "bonus";
	}else if(opt==0){
		//COMPRA COM BOLETO
		var url = "ajax_procs/ajax_calcfinal.php?total="+total+"&frete="+frete+"&boleto=true&desconto="+desconto;
		document.getElementById("opcao_pagto_bonus").setAttribute(classe, "off_opcao_pagto");
		document.getElementById("opcao_pagto_cartao").setAttribute(classe, "off_opcao_pagto");
		document.getElementById("opcao_pagto_boleto").setAttribute(classe, "opcao_pagto");
		//document.getElementById("carrinho_pagto_bonus").setAttribute(classe, "off_button_pagto");
		//document.getElementById("carrinho_pagto_boleto").setAttribute(classe, "carrinho_pagto_boleto");
		document.getElementById("modo_pagto").value = "boleto";
	}else{
		opt = 3;
		//COMPRA COM CARTAO
		var max_allowed = document.getElementById("max_allowed").value;
		
		if(opt_frete>0){
			var address = document.getElementById("address").value;
			var number = document.getElementById("number").value;
			var complement = document.getElementById("complement").value;
			var zip_code = document.getElementById("zip_code").value;
			var area = document.getElementById("area").value;
			var cty_resume = document.getElementById("cty_resume_field").value;
			var state = document.getElementById("state_field").value;
			
			var stringtoadd = "&address="+address+"&number="+number+"&complement="+complement+"&zip_code="+zip_code+"&area="+area+"&cty_resume="+cty_resume+"&state="+state;
			
		}else{
			var stringtoadd = "";
		}
		
		var url = "ajax_procs/ajax_calcfinal.php?total="+total+"&opt_frete="+opt_frete+"&frete="+frete+"&cartao=true&desconto="+desconto+"&max_allowed="+max_allowed+"&modo_entrega="+modo_entrega+"&receber="+receber+stringtoadd;
		document.getElementById("opcao_pagto_bonus").setAttribute(classe, "off_opcao_pagto");
		document.getElementById("opcao_pagto_boleto").setAttribute(classe, "off_opcao_pagto");
		document.getElementById("opcao_pagto_cartao").setAttribute(classe, "opcao_pagto");
		//document.getElementById("carrinho_pagto_bonus").setAttribute(classe, "off_button_pagto");
		//document.getElementById("carrinho_pagto_boleto").setAttribute(classe, "carrinho_pagto_boleto");
		document.getElementById("modo_pagto").value = "cartao";
	}
	
	var resultsbox = document.getElementById("confirm_table");
	resultsbox.innerHTML="<img src='templates/default/loading.gif'>";
	
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			resultsbox.innerHTML=xmlhttp.responseText;
			resultsbox.style.display="block";
			if(opt!=3){document.getElementById("button_confirma_final").focus();}
			window.scrollBy(0,100);
		}
	};		
		
}

function closeSale(){

	//ESTA FUNÇÃO APENAS PASSA OS PARÊMETROS SELECIONADOS PELO USUÁRIO 
	//PARA UM SCRIPT QUE REFAZ TODOS OS CÁLCULOS E VALIDAÇÕES E LOGO
	//CONCLUI A VENDA FAZENDO O INSERT EM TODAS AS TABELAS
	
	var modo_entrega = document.getElementById("modo_entrega").value;
	
	
	var address = document.getElementById("address").value;
	var number = document.getElementById("number").value;
	var complement = document.getElementById("complement").value;
	var zip_code = document.getElementById("zip_code").value;
	var area = document.getElementById("area").value;
	var ctyresume = document.getElementById("cty_resume_field").value;
	var state = document.getElementById("state_field").value;
	
	var desconto = document.getElementById("select_discount_number").value;
	var frete = document.getElementById("frete_final_value").value;
	var modo_pagto = document.getElementById("modo_pagto").value;
	
	
	
	//alert(modo_entrega+" "+opt_frete+" "+ctyresume+" "+state+" "+modo_pagto);
	
	window.location = "?show=fechar_pedido&frete="+frete+"&cty_resume="+ctyresume+
	"&state="+state+"&modo_pgto="+modo_pagto+"&address="+address+"&number="+number+
	"&complement="+complement+"&zip_code="+zip_code+"&area="+area+"&desconto="+desconto
	;
}


//-------------------------------------------------------------------------------/
//FUNÇÕES DE ADICIONAR E REMOVER ITENS DO CARRINHO DE COMPRAS
//
function removeItem(id_item){
	var url = "../servicos/vitrine/ajax_procs/ajax_add_remove.php?action=remove&id_item="+id_item+"&fidbuyer="+fidbuyer;
	
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			window.location.reload();
		}
	};
} 


function removeItemPespec(id_item){


	var url = "../servicos/vitrine/ajax_procs/ajax_add_remove.php?action=remove_pe&id_item="+id_item+"&fidbuyer="+fidbuyer;
	
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			window.location.reload();
		}
	};
}

function addItem(codproduct){
	
	var fidbuyer = document.getElementById("fidbuyer").value;
	var quant = document.getElementById("quant_"+codproduct).value;
	
	var url = "../servicos/vitrine/ajax_procs/ajax_add_remove.php?action=add&id_item="+codproduct+"&quant="+quant+"&fidbuyer="+fidbuyer;
	
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			document.getElementById("alerta_adicionar_"+codproduct).innerHTML = xmlhttp.responseText;
			document.getElementById("alerta_adicionar_"+codproduct).style.display = "inline";
		}
	};		
	
}


function showProBig(cdisplay, codproduct){
	document.getElementById("popbigpic").style.display = cdisplay;
	//document.getElementById("bigpiccell").innerHTML = "<img src='img/produtos/"+codproduct+"' style='width:500px; z-index:1000'>";
	var bigpiccell = document.getElementById("servicecell");
	bigpiccell.innerHTML = "<img src='img/produtos/"+codproduct+"' style='width:500px; z-index:1000'>";
	//alert("ShowProBig Clicado");
}


function goToLoja(){
	var selectionBox = document.getElementById("lojaSelection");
	var selIndex = selectionBox.options.selectedIndex;
	var newSel = selectionBox.options[selIndex].value;
	
	window.location.href = "?f="+newSel+"";


	
}

