function addFavourite(productid)
{
	var currentValue = readCookie("cedarfavs");
	var newValue;

	if(currentValue)
	{
		if(currentValue.indexOf(productid) > -1) return;
		else newValue = currentValue + "," + productid;
	}
	else newValue = productid;

	var expiryDate = new Date();
	expiryDate.setTime(expiryDate.getTime()+(365*24*60*60*1000));
	var expires = "; expires=" + expiryDate.toGMTString();

	document.cookie = "cedarfavs=" + newValue + ";" + expires + "; path=/";

	//Change "add" to "remove"
	var link = document.getElementById("addfav"+productid);
	link.href = "javascript:removeFavourite(" + productid + ");";
	link.innerHTML = "Remove from favourites";
}

function removeFavourite(productid)
{
	var currentValue = readCookie("cedarfavs");

	currentValue = currentValue.replace(productid + ",", "");
	currentValue = currentValue.replace("," + productid, "");
	currentValue = currentValue.replace(productid, "");

	var expiryDate = new Date();
	expiryDate.setTime(expiryDate.getTime()+(365*24*60*60*1000));
	var expires = "; expires=" + expiryDate.toGMTString();

	document.cookie = "cedarfavs=" + currentValue + expires + "; path=/";

	//Change "remove" to "add"
	var link = document.getElementById("addfav"+productid);
	link.href = "javascript:addFavourite(" + productid + ");";
	link.innerHTML = "Add to favourites";
}

//Foir use on "my favourites" page, not only removes cookie but hides product div
function killFavourite(productid)
{
	var currentValue = readCookie("cedarfavs");

	currentValue = currentValue.replace(productid + ",", "");
	currentValue = currentValue.replace("," + productid, "");
	currentValue = currentValue.replace(productid, "");

	var expiryDate = new Date();
	expiryDate.setTime(expiryDate.getTime()+(365*24*60*60*1000));
	var expires = "; expires=" + expiryDate.toGMTString();

	document.cookie = "cedarfavs=" + currentValue + expires + "; path=/";

	//Hide the product from display & update results counter (favcounter)
	var favdiv = document.getElementById("favdiv"+productid);
	favdiv.style.display = "none";
	var favcounter = document.getElementById("favcounter");
	var currentCount = favcounter.innerHTML;
	currentCount--;
	favcounter.innerHTML = currentCount;
}

function readCookie(cookiename)
{
	var nameEQ = cookiename+"=";
	var cookieArray = document.cookie.split(";");
	for(i=0; i<cookieArray.length; i++)
	{
		var thisCookie = cookieArray[i];
		while(thisCookie.charAt(0) == " ") thisCookie = thisCookie.substring(1,thisCookie.length);
		if(thisCookie.indexOf(nameEQ) == 0)
		{
			return thisCookie.substring(nameEQ.length,thisCookie.length);
		}
		else
		{
			continue;
		}
	}
	return null;
}

