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

function setCookie(name,id,imageurl,price,saleprice,orderable)
{
	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 + "|p:" + price + "|out:" + orderable) + "; 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 writeCookie()
{
	var name,id,imageurl,price,saleprice,orderable,Lname,Lid,Limageurl,Lprice,Lsaleprice,Lorderable,cookieVal;
	Lname = "productName:";
	Lid = "id:";
	Limageurl = "imageurl:";
	Lprice = "p:";
	Lsaleprice = "sp:";
	Lorderable = "out:";
	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("<div class='recently-view-item-box'><div class='pro-fea-head'><h2>Recently viewed item</h2></div><div class='rvi-main-box'><table width='' border='0' align='left' cellpadding='0' cellspacing='0'><tr>");
			for(j=0;j<recentItemArray.length;j++)
			{
				name = "";
				id = "";
				imageurl = "";
				price = "";
				saleprice = "";	
				orderable = "";						
				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);
					if(nvp[k].indexOf("p:") > -1)
						price = nvp[k].substr(Lprice.length);
					if(nvp[k].indexOf("out:") > -1)
						orderable = nvp[k].substr(Lorderable.length);
				}
				document.write("<td width='153' align='left' valign='top' ><table width='153' border='0' align='left' cellpadding='0' cellspacing='0' class='btm-brd'><tr><td width='153' align='left' valign='top'><table width='153' border='0' align='center' cellpadding='0' cellspacing='0'><tr><td width='153' height='130' align='center' valign='middle'><a href=" + id + "><img src=" + imageurl + " vspace='0' align='center' border='0' hspace='0'></a></td></tr><tr><td align='left' valign='top' class='category-text-area'><h2><a href=" + id + ">" + name + "</a></h2></td></tr><tr><td align='left' valign='top' class='pro-listing-retail-price'>");
				
				if (price != "" && saleprice != ""){document.write("<span>" + price + "</span> " + saleprice + "");}
				
				else if (price != ""){document.write("" + price + "");}
				
				else if (saleprice != ""){document.write("" + saleprice + "");}
				
				document.write("</td></tr></table></td></tr></table></td>");
				
				
				if(j<3)
				{
					document.write("<td width=\"18\" align=\"center\" valign=\"middle\"><img src=\"http://yst.berktree.com/images/spacer.gif\" width=\"42\" height=\"1\"></td>");
				}
			}
			document.write("</table></div></div>");
		}
	}
}
