// JavaScript Document

function sendRequestHaveWant(data,productid)
{
	$.ajax({
	   type: "POST",
	   url: ajaxUrl + "myproducts.ajax.php",
	   data: data,
	   dataType: "xml",
	   success: function(xml){
				if ((xml.getElementsByTagName("error").length) > 0)
				{
					alert("Error");
				}
				
				// check what action was executed and show correct button
				var action = xml.getElementsByTagName("action")[0].firstChild.nodeValue;
				if (action == "addhaveitem")
				{
					showDeleteButton(productid);
				}
				else if (action == "addwantitem")
				{
					showEditButton(productid);
				}
				else
				{
					showAddButton(productid);
				}
	   }
 	});
} 
function deleteMyItem(prevstatus,productid)
{
	var userid = $("#have_userid").val();
	data = 'action=deleteitem&myproducts[productid]=' + productid + '&myproducts[userid]=' + userid + '&myproducts[prevstatus]=' + prevstatus;
	sendRequestHaveWant(data,productid);
}
function addMyItem(status,prevstatus,productid)
{
	var userid = $("#have_userid").val();
	data = 'action=additem&myproducts[productid]=' + productid + '&myproducts[userid]=' + userid  + '&myproducts[status]=' + status + '&myproducts[prevstatus]=' + prevstatus + '&myproducts[stayupdated]=0';
	sendRequestHaveWant(data,productid);
}

function showAddButton(productid)
{
	$('[title="wanthave' + productid + '"]').show();
	$('[title="want' + productid + '"]').hide();
	$('[title="have' + productid + '"]').hide();
}

function showEditButton(productid)
{
	$('[title="want' + productid + '"]').show();
	$('[title="wanthave' + productid + '"]').hide();
	$('[title="have' + productid + '"]').hide();
}

function showDeleteButton(productid)
{
	$('[title="have' + productid + '"]').show();
	$('[title="want' + productid + '"]').hide();
	$('[title="wanthave' + productid + '"]').hide();
}