/*------------------------------------------------|
| ADICIONAR AO CARRINHO                           |
|------------------------------------------------*/

function adicionarCarrinho(produto, accao, qtd, img, aviso, bt)
{
	var els = new Array();
	
	if(document.getElementById(img)) els["img"] = document.getElementById(img);
		
	els["aviso"] 	= document.getElementById(aviso);
	els["bt"] 	 	= document.getElementById(bt);
	els["qtd"] 	 	= document.getElementById(qtd);
	els["tot_prod"] = document.getElementById("carrinho_tot_prod");
	
	if(els["bt"].src.indexOf("alterar") > -1)
	{
		accao = "editar";
	}
	
	var xmlhttp = false;
	
	if (!xmlhttp && typeof XMLHttpRequest != "undefined") 
	{
		xmlhttp = new XMLHttpRequest();
	} 
	else 
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
		
	url = "/loja/comprar.php?remote=1&prod=" + produto + "&qtd=" + els["qtd"].value;
	
	if(accao.length > 0)
		url = url + "&action=" + accao;
	
	xmlhttp.open("GET", url, true);
	
	xmlhttp.onreadystatechange = function() 
	{
		if (xmlhttp.readyState==4) 
		{
			if (xmlhttp.status == 200) 
			{
				msg = xmlhttp.responseText;
				
				// se nao for erro
				if(msg.indexOf("#") == -1)
				{
					// se for adicionar
					if(accao != "editar")
					{
						// aumentar o numero de items no carrinho
						els["tot_prod"].innerHTML = (parseInt(els["tot_prod"].innerHTML) + 1) + "";
						if(els["img"]) els["img"].style.border = "3px #E15618 solid";	
						els["bt"].src = "/img/bt_alterar_pt.gif";						
					}
				}
				
				msg = msg.replace("#", "");
				
				els["aviso"].innerHTML = msg;
			} 
			else 
			{
				els["aviso"].innerHTML  = "Não foi possível adicionar o produto.";
			}
		}
	}
	
	xmlhttp.send(null);	
}
