function isInWishList(item){
	var cval = readCookie("ambergWishListItems");	
	if ( cval == null )
		return false;
	
	if ( cval.indexOf(item) == -1 )
		return false;
	else
		return true;
}

function wishListAdd(pitem){
	if ( !cookiesEnabled() )
		return false;

	var item;
	
	if (pitem != null )
		item = pitem;
	else
		item = getPageItem();
	
	appendCookie("ambergWishListItems",item);
	
	if ( document.getElementById('wlb_' + item) )
		document.getElementById('wlb_' + item).src='../images/removefromwishlist.jpg';
	
	var myEvent = new WoopraEvent("AddWishListItem"); // Initialize event and give it a name
	myEvent.addProperty("item", item);	
	myEvent.fire();
	
	return false;
}

function wishListRemove(pitem){
	if ( !cookiesEnabled() )
		return false;
	
	var item;
	
	if (pitem != null )
		item = pitem;
	else
		item = getPageItem();
	
	var cval = readCookie("ambergWishListItems");
	if ( cval == null )
		return false;
		
	// replace the given item name with ""
	var nval = cval.replace((item + "|"),"");
	// recreate the cookie
	createCookie("ambergWishListItems",nval,10);
	
	if ( document.getElementById('wlb_' + item) )
		document.getElementById('wlb_' + item).src='../images/addtowishlist.jpg';
	
	var myEvent = new WoopraEvent("RemoveWishListItem"); // Initialize event and give it a name
	myEvent.addProperty("item", item);	
	myEvent.fire();
	
	return true;
}

function toggleWishList(pitem){
	if ( pitem != null )
		inWL = isInWishList(pitem);
	else
		inWL = isInWishList(getPageItem());
	
	if ( inWL )
		wishListRemove(pitem);
	else
		wishListAdd(pitem);	
}

function getPageItem(){
	// this function returns the item name as it will be stored in the cookie probably from a hidden field
	return document.getElementById('page_item').value;
}

function displayWishListButton(pitem){
	if ( !cookiesEnabled() )
		return false;

	var item;

	if ( pitem != null ){
		inWL = isInWishList(pitem);
		item = pitem;
	} else {
		inWL = isInWishList(getPageItem());
		item = getPageItem();
	}
	document.write("<br>");
	if ( inWL ){		
		if ( pitem != null )
			document.write("<a href='#' onclick=\"toggleWishList('" + pitem + "');return false;\" id='wishlistlink'><img src='../images/removefromwishlist.jpg' id='wlb_" + item + "' border='0' /></a>");
		else
			document.write("<a href='#' onclick='toggleWishList();return false;' id='wishlistlink'><img src='../images/removefromwishlist.jpg' id='wlb" + item + "' border='0' /></a>");
	} else {
		if ( pitem != null )
			document.write("<a href='#' onclick=\"toggleWishList('" + pitem + "');return false;\" id='wishlistlink'><img src='../images/addtowishlist.jpg' id='wlb_" + item + "' border='0' /></a>");
		else
			document.write("<a href='#' onclick='toggleWishList();return false;' id='wishlistlink'><img src='../images/addtowishlist.jpg' id='wlb_" + item + "' border='0' /></a>");
	}
	//document.write("&nbsp;<img src='../images/wishlist_help.jpg'><br>")
	//document.write("<br>");
	document.write(" <a href='../view_wishlist.php'><img src='../images/viewwl.jpg' border='0' /></a> <a href='../contactus.php'><img src='../images/sendwl.jpg' border='0' /></a>");
	return true;
}

function cookiesEnabled(){
	if ( readCookie("amberg_cookie_test") == "1" )
		return true;
	else
		return false;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function appendCookie(name,value){
	var cval = readCookie(name);
	var nval = "";
	if ( cval != null )
		nval = cval + value + "|";
	else 
		nval = value + "|";
	
	createCookie(name,nval,10);
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


// when this file is loaded we will write to a cookie called test and this will tell us whether cookies are enabled or not
createCookie("amberg_cookie_test","1");
