var expDays=60;
var expDate = new Date();
var cookieName = "Recently viewed";
var priceText = "Regular price:";
var salePriceText = "Sale price:";
expDate.setTime(expDate.getTime()+(expDays*24*60*60*1000))

function recsetCookie(name,id,imageurl,saleprice)
{
	var theCookie,totalCookie,nextCookie,cookieArray;
	var ctr = 0 ;
	var recentItemArray = new Array();
	var the_cookie = unescape(document.cookie);
	if(the_cookie.indexOf(id) > -1)
	{
		var exists = true;
	}
	else
	{
		theCookie = escape("productName:" + name + "|id:"  + id + "|imageurl:" + imageurl + "|sp:" + saleprice) + "; expires=" + expDate.toGMTString() + "; path=/";
		if(the_cookie.length > 0)
		{
			var cookieArray = the_cookie.split(";");
			for(i=0;i<cookieArray.length;i++)
			{
				if(cookieArray[i].indexOf(cookieName) > -1)
				{
					recentItemArray[ctr] = cookieArray[i];
					ctr = ctr + 1;
				}
			}
			if(ctr > 0)
			{
				for(i=0;i<ctr;i++)
				{
					if(i == 3) break;
					nextCookie = i + 1;
					passCookie = recentItemArray[i].split("=");
					document.cookie = cookieName + nextCookie + "=" + passCookie[1] + "; expires=" + expDate.toGMTString() + "; path=/";
				}
				document.cookie = cookieName + "0=" + theCookie;
			}
			else
			{
				document.cookie = cookieName + "0=" + theCookie;
			}
		}
		else
		{
			document.cookie = cookieName + "0=" + theCookie;
		}
	}
}

function recwriteCookie()
{
	var name,id,imageurl,price,saleprice,Lname,Lid,Limageurl,Lprice,Lsaleprice,cookieVal;
	Lname = "productName:";
	Lid = "id:";
	Limageurl = "imageurl:";
	Lprice = "p:";
	Lsaleprice = "sp:";
	var nvp = new Array();
	var recentItemArray = new Array();
	var the_cookie = unescape(document.cookie);
	var ctr = 0 ;
	if(the_cookie.length > 0)
	{
		var cookieArray = the_cookie.split(";");
		for(i=0;i<cookieArray.length;i++)
		{
			if(cookieArray[i].indexOf(cookieName) > -1)
			{
				recentItemArray[ctr] = cookieArray[i];
				ctr = ctr + 1;
			}
		}
		if(recentItemArray.length>0)
		{
			document.write("<table width='736' align='left' border='0' cellpadding='0' cellspacing='0'><tr><td class='product-description-h' valign='middle' align='left' height='30'>Recently Viewed Items...</td></tr><tr><td class='list-section2' valign='top' align='left'><table width='733' align='left' border='0' cellpadding='0' cellspacing='0'><tr><td valign='top' align='left'><table align='left' border='0' cellpadding='0' cellspacing='0'><tr>");
			for(j=0;j<recentItemArray.length;j++)
			{
				name = "";
				id = "";
				imageurl = "";
				price = "";
				saleprice = "";							
				cookieVal = unescape(recentItemArray[j])
				nvp = cookieVal.split("=");
				nvp = nvp[1].split("|");
				for(k=0;k<nvp.length;k++)
				{
					if(nvp[k].indexOf("productName:") > -1)
						name = nvp[k].substr(Lname.length);
					if(nvp[k].indexOf("id:") > -1)
						id = nvp[k].substr(Lid.length);
					if(nvp[k].indexOf("imageurl:") > -1)
						imageurl = nvp[k].substr(Limageurl.length);
					if(nvp[k].indexOf("sp:") > -1)
						saleprice = nvp[k].substr(Lsaleprice.length);
				}
				document.write("<td valign='top' align='left'><table width='170' align='left' border='0' cellpadding='0' cellspacing='0'><tr><td valign='top' align='left'><table width='170' align='left' border='0' cellpadding='0' cellspacing='0'><tr><td valign='top' align='left'><img src='http://yst.funbrandstoys.com/images/prod-border-top.jpg' alt='' width='170' height='10'></td></tr><tr><td class='prod-border-center' valign='middle' width='170' align='center' height='130'><a href=" + id + "><img src=" + imageurl + " alt=" + name + " vspace='0' width='125' align='center' border='0' height='125' hspace='0'></a></td></tr><tr><td valign='top' align='left'><img src='http://yst.funbrandstoys.com/images/prod-border-bottom.jpg' alt='' width='170' height='10'></td></tr></table></td></tr><tr><td class='list-prod-name' valign='top' align='left'><a href=" + id + ">" + name + "</a></td></tr><tr><td class='sale-price' valign='top' align='left'>" + salePriceText + saleprice + "</td></tr></table></td>");
				if(j<3)
				{
					document.write("<td valign='middle' width='17' align='center'>&nbsp;</td>");
				}
			}
			document.write("</tr></table></td></tr></table></td></tr></table>");
		}
	}
}