// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}

  //LAYER
  if(mostraLayerDet)
  {
	  document.getElementById(objLayer).style.left = tempX + 15;
	  document.getElementById(objLayer).style.top  = tempY + 15;
   }

  return true
}

function someAtualiza()
{
	document.getElementById("atua").innerHTML = '';
}

function delItem(obj,cod)
{
	ajax_stats = ajaxInit();
	if(ajax_stats)
	{
		vc = setInterval("vaiClareando('"+obj+"')",13);
		nreg--;

		ajax_stats.open("GET", "del_itemdesejo.php?obj="+obj+"&cod="+cod+"&pagina=<?php echo $pagina; ?>", true);
		ajax_stats.setRequestHeader("Cache-Control","no-cache");

		ajax_stats.onreadystatechange = function()
	   	{
	   		if(ajax_stats.readyState == 4)
	    	{
	    		if(ajax_stats.status == 200)
		    	{
		    		eval(ajax_stats.responseText);
		    	}
		    	else
		    	{
		       		alert(ajax_stats.statusText);
		    	}
		    }
	  	}
	  	ajax_stats.send(null);			
	}
}

var comp = false;

function delItemBuy(obj,cod)
{
	ajax_stats = ajaxInit();
	if(ajax_stats)
	{
		vc = setInterval("vaiClareando('"+obj+"')",13);

		ajax_stats.open("GET", "del_itemcompra.php?obj="+obj+"&cod="+cod, true);
		ajax_stats.setRequestHeader("Cache-Control","no-cache");

		ajax_stats.onreadystatechange = function()
	   	{
	   		if(ajax_stats.readyState == 4)
	    	{
	    		if(ajax_stats.status == 200)
		    	{
		    		eval(ajax_stats.responseText);
		    		comp = true;
					
					if(document.frmfretes.cep.value.length ==9 || document.frmfretes.pais.value != 0) { 
						pegaFretes(document.frmfretes.pais.value,document.frmfretes.cep.value,parseFloat(pegaPesoTotal(obj)).toFixed(2),document.getElementById('qtdTotal').value-1); 						
					}

		    	}
		    	else
		    	{
		       		alert(ajax_stats.statusText);
		    	}
		    }
	  	}
	  	ajax_stats.send(null);
		
		
	}
}

function pegaPesoTotal(retira)
{	
	var tot = document.getElementsByName('proPrecoTotal[]').length;
	var proPesoFrete = 0;
	for(var i=0;i<tot;i++)
	{
		idProd = document.getElementsByName('proPrecoTotal[]')[i].id.split('_')[1];
		if(retira != 'wish_'+idProd) {			
			proPesoFrete += parseFloat(document.getElementsByName('proPeso[]')[i].value)*parseInt(document.getElementById('cpedit_'+idProd).value);
		}
	}
	
	return proPesoFrete;

}

var cont = 99;
var trnp = 0;
var vc;
var diminuir = 0;
if (document.all){ //IE
	diminuir = 10;
} else { //FF
	diminuir = 3;
}

function vaiClareando(myDiv)
{
	if(cont <= 0)
	{
		cont = 99;
		trnp = 0;
		removeElement(myDiv);
		clearInterval(vc);
	}
	trnp = cont/100;
	try
	{
		document.getElementById(myDiv).style.setAttribute("filter","alpha(opacity="+cont+")");
	}
	catch(e){ }

	try
	{
		document.getElementById(myDiv).style.MozOpacity=trnp;
	}
	catch(e){ }

	try
	{
		document.getElementById(myDiv).style.opacity=trnp;
	}
	catch(e){ }
	cont-= diminuir;

}


function removeElement(divNum)
{
	var d = document.getElementById('divmor');
	var olddiv = document.getElementById(divNum);
	if(olddiv)
	{
  		d.removeChild(olddiv);
  	}
  	if(comp)
  	{
  		somaTotal();
  		comp = false;
  	}
}

function setLista(form)
{
	var vals = RetornaValores(form);
	ajax_stats = ajaxInit();
	if(ajax_stats)
	{

		if(form.desejopp[0].checked)
		{
			document.getElementById("publica").innerHTML = '<strong>P&uacute;blica</strong>';
			document.getElementById("privada").innerHTML = 'Privada';
			document.getElementById("atua").innerHTML = 'ATUALIZADO';
			setTimeout("someAtualiza()",1000);
		}
		else
		{
			document.getElementById("privada").innerHTML = '<strong>Privada</strong>';
			document.getElementById("publica").innerHTML = 'P&uacute;blica';
			document.getElementById("atua").innerHTML = 'ATUALIZADO';
			setTimeout("someAtualiza()",1000);
		}



		ajax_stats.open("GET", "atualiza_desejos.php?"+vals, true);
		ajax_stats.setRequestHeader("Cache-Control","no-cache");

		ajax_stats.onreadystatechange = function()
	   	{
	   		if(ajax_stats.readyState == 4)
	    	{
	    		if(ajax_stats.status == 200)
		    	{
		    		eval(ajax_stats.responseText);
		    	}
		    	else
		    	{
		       		alert(ajax_stats.statusText);
		    	}
		    }
	  	}
	  	ajax_stats.send(null);
	}
}




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_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MostraCombo()
{
	if(document.getElementById('pagina'))
	{
		document.getElementById('pagina').style.visibility = "";
	}
}

function EscondeCombo()
{
	if(document.getElementById('pagina'))
	{
		document.getElementById('pagina').style.visibility = "hidden";
	}
}

var objLayer;
var mostraLayerDet;

function Mostra_Det(obj,mostra)
{
	objLayer = obj;
	mostraLayerDet = mostra;
	getMouseXY(document.onmousemove);
	if(mostra)
	{
		document.getElementById(obj).style.display = "";
		EscondeCombo();
	}
	else
	{
		document.getElementById(obj).style.display = "none";
		MostraCombo();
	}

}








var dentro = false;

function Estoque_Enter(obj,cod)
{
	obj.className = "Est_Dentro";
	obj.focus();
	document.getElementById('icoedit_'+cod).style.display = "none";
	document.getElementById('icook_'+cod).style.display = "";
	dentro = true;
}

function Estoque_Out(obj,cod)
{
	var valunit 		= document.getElementById('proPrecoVenda_'+cod).value;
	var valestoque 		= parseInt(document.getElementById('proEstoque_'+cod).value);
	if(parseInt(obj.value) > valestoque)
	{
		alert("A Quantidade digitada é maior do que a quantidade em estoque.");
		obj.focus();
	}
	else
	{

		ajax_stats = ajaxInit();
		if(ajax_stats)
		{
			ajax_stats.open("GET", "upd_itemcarrinho.php?cod="+cod+"&qtd="+obj.value+'&proPrecoVenda='+valunit+'&acao=quantidade', true);
			ajax_stats.setRequestHeader("Cache-Control","no-cache");
			ajax_stats.onreadystatechange = function()
		    {
		   	if(ajax_stats.readyState == 4)
		   	{
		   		if(ajax_stats.status == 200)
		   		{
		   			eval(ajax_stats.responseText);
					if(document.frmfretes.cep.value.length ==9 || document.frmfretes.pais.value != 0) { 
						pegaFretes(document.frmfretes.pais.value,document.frmfretes.cep.value,parseFloat(pegaPesoTotal(obj)).toFixed(2),document.getElementById('qtdTotal').value-1); 						
					}
	      		}
	      		else
	      		{
	        		alert(ajax_stats.statusText);
	      		}
		     }
		    }
		  	ajax_stats.send(null);
		}

		obj.className = "Est_Fora";
		document.getElementById('icoedit_'+cod).style.display = "";
		document.getElementById('icook_'+cod).style.display = "none";
		dentro = false;

	}
}

function Estoque_Over(obj)
{
	if(!dentro)
	{
		obj.className = "Est_Focus";
	}
}

function Estoque_OOut(obj)
{
	if(!dentro)
	{
		obj.className = "Est_Fora";
	}
}


function setPresenteOn(cod)
{
	document.getElementById('presente_on_'+cod).style.display = "";
	document.getElementById('presente_off_'+cod).style.display = "none";

	ajax_stats = ajaxInit();
	if(ajax_stats)
	{
		ajax_stats.open("GET", "upd_itemcarrinho.php?cod="+cod+"&presente=1&acao=presente", true);
		ajax_stats.setRequestHeader("Cache-Control","no-cache");
		ajax_stats.onreadystatechange = function()
	    {
	   	if(ajax_stats.readyState == 4)
	   	{
	   		if(ajax_stats.status == 200)
	   		{
	   			//alert(ajax_stats.responseText);
      		}
      		else
      		{
        		alert(ajax_stats.statusText);
      		}
	     }
	    }
	  	ajax_stats.send(null);
	}

	somaTotal();
}

function setPresenteOff(cod)
{
	document.getElementById('presente_on_'+cod).style.display = "none";
	document.getElementById('presente_off_'+cod).style.display = "";

	ajax_stats = ajaxInit();
	if(ajax_stats)
	{
		ajax_stats.open("GET", "upd_itemcarrinho.php?cod="+cod+"&presente=0&acao=presente", true);
		ajax_stats.setRequestHeader("Cache-Control","no-cache");
		ajax_stats.onreadystatechange = function()
	    {
	   	if(ajax_stats.readyState == 4)
	   	{
	   		if(ajax_stats.status == 200)
	   		{
	   			//alert(ajax_stats.responseText);
      		}
      		else
      		{
        		alert(ajax_stats.statusText);
      		}
	     }
	    }
	  	ajax_stats.send(null);
	}
}

function somaTotal()
{
	var tot = document.getElementsByName('proPrecoTotal[]').length;
	var proSubTotal = 0;
	var proPeso = 0;
	var proQtd = 0;
	var idProd = '';
	for(var i=0;i<tot;i++)
	{
		idProd = document.getElementsByName('proPrecoTotal[]')[i].id.split('_')[1];

		proSubTotal += parseFloat(document.getElementsByName('proPrecoTotal[]')[i].value);
		proPeso += parseFloat(document.getElementsByName('proPeso[]')[i].value)*parseInt(document.getElementById('cpedit_'+idProd).value);

		proQtd += parseInt(document.getElementById('cpedit_'+idProd).value)
	}
	document.getElementById('qtdTotal').value = proQtd;

	var proFrete = 0;
	if(document.getElementById('proFrete').value)
	{
		proFrete = parseFloat(document.getElementById('proFrete').value);
	}


	document.frmfretes.pesoProdutos.value = proPeso;
	document.getElementById('proPeso').value = proPeso;
	//alert(document.frmfretes.pesoProdutos.value);


	document.getElementById('proSubTotallbl').innerHTML = 'R$ ' + proSubTotal.toFixed(2).replace('.',',');

	 document.getElementById('proTotalLbl').innerHTML = '&nbsp;' + (proSubTotal + proFrete).toFixed(2).replace('.',',');
}




function pegaFretes(pais,cep,peso,itens)
{
	
	document.getElementById('proFrete').value='';
	ajax_cep = ajaxInit();	
	if(ajax_cep)
	{
		ajax_cep.open("GET", "fretes.php?pais="+pais+"&cep="+cep+"&pesoProduto="+peso+"&itens="+itens, true);
		ajax_cep.setRequestHeader("Cache-Control","no-cache");
		ajax_cep.onreadystatechange = function()
	    {
	   	if(ajax_cep.readyState == 4)
	   	{
	   		if(ajax_cep.status == 200)
	   		{
	   			document.getElementById('fretes').innerHTML = ajax_cep.responseText;
	   			//alert(ajax_cep.responseText);
      		}
      		else
      		{
        		alert(ajax_cep.statusText);
      		}
	     }
	    }
	  	ajax_cep.send(null);
	}
}

/*POST DE CARRINHO*/
function postCarrinho()
{

	var args=postCarrinho.arguments;
	var proFrete 	= args[0];
	var proPais		= args[1];
	var proCep 		= args[2];
	var proTipoFrete= args[3];
	var proPeso 	= args[4];
	
	
	//DEVERÁ MOSTRAR A MENSAGEM CASO O CEP FOR SELECIONADO
	if(proPais == "0"){
		if(proCep == ""){
			alert('Por favor, informe o CEP de sua atual localidade. Se não souber clique em Não sei meu CEP');
			document.getElementById('cep').focus();
			return false;
		}
		if (proFrete == ""){
			alert('Por favor, uma forma de envio deverá ser informada.')
			return false;
		}
	}
	else {
		if (proFrete == ""){
			alert('Select shipping method !!')
			return false;
		}
	}
	

	ajax_car = ajaxInit();
	if(ajax_car)
	{
		ajax_car.open("GET", "carrinho_passo_um_post.php?pais="+proPais+"&cep="+proCep+"&frete="+proFrete+"&tipofrete="+proTipoFrete+"&proPesoTotal="+proPeso, true);
		ajax_car.setRequestHeader("Cache-Control","no-cache");
		ajax_car.onreadystatechange = function()
	    {
	   	if(ajax_car.readyState == 4)
	   	{
	   		if(ajax_car.status == 200)
	   		{
	   			//alert(ajax_car.responseText);
	   			eval(ajax_car.responseText);
      		}
      		else
      		{
        		alert(ajax_car.statusText);
      		}
	     }
	    }
	  	ajax_car.send(null);
	}
}


/*MASCARA CEP*/
function MM_formtCep(e,src,mask) {
	if(window.event) { _TXT = e.keyCode; }
	else if(e.which) { _TXT = e.which; }
	if(_TXT > 47 && _TXT < 58) {
	 var i = src.value.length; var saida = mask.substring(0,1); var texto = mask.substring(i)
	 if (texto.substring(0,1) != saida) { src.value += texto.substring(0,1); }
	    return true; } else { if (_TXT != 8) { return false; }
	 else { return true; }
	}
}

function saidaCep(obj)
{
	var str = obj.value;
	var prefix;
	var sufix;

	if(str.substr(5,1)!='-')
	{
		prefix 	= str.substr(0,5);
		sufix 	= str.substr(5,3);
		obj.value = prefix+'-'+sufix;
	}

}
