function trocaCss(obj)
{
	obj = document.getElementById(obj);
	if(obj.style.visibility == 'hidden' || obj.style.visibility == '')
		obj.style.visibility = 'visible';
	else
		obj.style.visibility =  'hidden';
}

function trocaDisplay()
{
	var i, aux=document.getElementById('formCarrinho').elements;
	for(i=0;i<aux.length;i++)
	{
		document.getElementById('formCarrinho').elements[i].readOnly = !document.getElementById('formCarrinho').elements[i].readOnly;
		document.getElementById('formCarrinho').elements[i].disabled =  !document.getElementById('formCarrinho').elements[i].disabled;

	}
	trocaCss('carregandoCarrinho');
}

function atualizaItens()
{
	var biscoitos = document.cookie.split(';'), aux, total=0;

	for(var i=0;i<biscoitos.length;i++)
	{
		aux = biscoitos[i].split('=');
		if(/carrinho/.test(aux[0]))
			total = total + parseInt(aux[1]);	
	}
	
	//Troca o valor
	document.getElementById('divItens').innerHTML = document.getElementById('divItens').innerHTML.replace(/[0-9]{1,}/, total);
}

function atualizaTotal()
{
	try
	{
		var http = new XMLHttpRequest()
	}
	catch(ee)
	{
		try
		{
		 	var http = new ActiveXObject("Msxml2.XMLHTTP")
		}
		catch(e)
		{
			try
			{
				var http = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch(E)
			{
				var http = false
			}
		}
	}
	var aux="acao=total";
	http.open("POST", "carrinho_ajax.php" ,true)
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	http.setRequestHeader("Content-length", aux.length);
    	http.setRequestHeader("Connection", "close");
	trocaDisplay();
	http.onreadystatechange=function() {
		if (http.readyState==4)
		{ 
			if (http.status == 200) {
				var retorno = http.responseXML;
				var er = /([0-9]{1,3}\.){0,}[0-9]{1,},[0-9]{1,}/;
			try
			{
				document.getElementById('total_itens').innerHTML =retorno.getElementsByTagName('total_itens').item(0).firstChild.nodeValue;
				document.getElementById('frete').innerHTML =retorno.getElementsByTagName('frete').item(0).firstChild.nodeValue;
				retorno = retorno.getElementsByTagName('total').item(0).firstChild.nodeValue;
				document.getElementById('total').innerHTML = document.getElementById('total').innerHTML.replace(er, retorno);
}
			catch(E)
			{}
			}
		}
	}
	http.send(aux);
	trocaDisplay();
}

function removeProdutoCarrinho(item)
{	
	var linha = item.parentNode.parentNode; 
	var id = linha.id.replace("produto_",'');
	try
	{
		var http = new XMLHttpRequest()
	}
	catch(ee)
	{
		try
		{
		 	var http = new ActiveXObject("Msxml2.XMLHTTP")
		}
		catch(e)
		{
			try
			{
				var http = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch(E)
			{
				var http = false
			}
		}
	}

	var aux="acao=remover&produto="+id;
	http.open("POST", "carrinho_ajax.php" ,true)
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	http.setRequestHeader("Content-length", aux.length);
    	http.setRequestHeader("Connection", "close");
	trocaDisplay();
	http.onreadystatechange=function() {
		if (http.readyState==4)
		{ 
			if (http.status == 200) {
				var retorno=http.responseXML;
				retorno = retorno.getElementsByTagName('retorno').item(0).firstChild.nodeValue;
				if(eval(retorno))
				{
					item.parentNode.parentNode.remove();
					atualizaItens();
					atualizaTotal();
				}
				else
					alert('Erro ao remover o produto!');
			}
		}
	}
	http.send(aux);
	trocaDisplay();	
}



function atualizaQuantidade(item) {	
	try
	{
		var http = new XMLHttpRequest()
	}
	catch(ee)
	{
		try
		{
		 	var http = new ActiveXObject("Msxml2.XMLHTTP")
		}
		catch(e)
		{
			try
			{
				var http = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch(E)
			{
				var http = false
			}
		}
	}

	var aux="acao=quantidade&quantidade="+item.value+"&produto="+item.name.replace(/quantidade_/,'');
	http.open("POST", "carrinho_ajax.php" ,true)
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	http.setRequestHeader("Content-length", aux.length);
    	http.setRequestHeader("Connection", "close");
	trocaDisplay();
	http.onreadystatechange=function() {
		if (http.readyState==4)
		{ 
			if (http.status == 200) {
				var retorno=http.responseXML;
				retorno = retorno.getElementsByTagName('retorno').item(0).firstChild.nodeValue;
				var er = /([0-9]{1,3}\.){0,}[0-9]{1,},[0-9]{1,}/;
				var total = document.getElementById(item.name.replace('quantidade', 'total_produto'));
				total.innerHTML = total.innerHTML.replace(er, retorno);
				atualizaItens();
				atualizaTotal();
			}
		}
	}
	http.send(aux);
	trocaDisplay();	
}

function atualizaBairro(item) {	
	try
	{
		var http = new XMLHttpRequest()
	}
	catch(ee)
	{
		try
		{
		 	var http = new ActiveXObject("Msxml2.XMLHTTP")
		}
		catch(e)
		{
			try
			{
				var http = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch(E)
			{
				var http = false
			}
		}
	}
	var aux="acao=atualizaBairro&bairro="+item.value;
	http.open("POST", "carrinho_ajax.php" ,true)
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	http.setRequestHeader("Content-length", aux.length);
    	http.setRequestHeader("Connection", "close");
	trocaDisplay();
	http.onreadystatechange=function() {
		if (http.readyState==4)
		{

			if (http.status == 200) {
				var retorno=http.responseXML;
				var cell = item.parentNode; 
				atualizaItens();
				atualizaTotal();
			}
		}
	}
	document.getElementById("carregandoCarrinho").style.visibility = "visible";
	http.send(aux);
	trocaDisplay();
	
}




