window.lastId = null;

_c(function(){
	
	$("td.btOrcamento a").apply("e", "click", function(e){
		
		showAguarde();
		
		var id = this.id.toString().split("-")[1];
		window.lastId = id;
		
		new Ajax(this.href.toString(), {
			lastId: id,
			onLoad: function(){
				if(this.responseText == "Adicionado")
				{
					alert("Produto Adicionado com Sucesso!");
				}
				else
				{
					alert("Produto Removido com Sucesso!");			
				}
				changeBtOrca(this.lastId);
				hideAguarde();
			}
		});
		
		this.cancelEvent(e||window.event);
		
		return false;
		
	});
	
});

function changeBtOrca(id)
{
	var bt = $("btOrca-" + (id || window.lastId));
	
	if(bt.className && bt.className.toString().trim() == "off")
	{
		bt.innerHTML = bt.title = "Adicionar a Lista de Compras";
		bt.href = window.defaultUrl + "orcamento/add/" + (id || window.lastId);
		bt.remClass("off");
	}
	else
	{
		bt.href = window.defaultUrl + "orcamento/remove/" + (id || window.lastId);	
		bt.innerHTML = bt.title = "Remover da Lista de Compras";
		bt.addClass("off");	
	}
}