var expDays=60;
var expDate = new Date();
var cookieName = "Recently viewed";
var priceText = "Regular price:";
var salePriceText = "Sale ";
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 == 2) 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("<table border='0' align='left' cellspacing='0' cellpadding='0' width='176'><tr><td align='left' valign='top' class='lft-head'>Recently Viewed Items :</td></tr><tr><td align='left' valign='top' class='lft-recent-area'><table border='0' align='center' cellspacing='0' cellpadding='0' width='170'>");
			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("<tr><td align='left' valign='top' class='rp-pro-area-tp-space'><table border='0' align='left' cellspacing='0' cellpadding='0' width='170'><tr><td align='center' valign='middle' width='81' height='63'><a href=" + id + "><img src=" + imageurl + " align='center' border='0'></a></td>");
			
			
			if (price != "" && saleprice != ""){document.write("<td align='left' valign='top'><div class='rp-s-txt'>Regular price: <span class='grey-s-line-through-txt'>" + saleprice + "</span></div><div class='op-s-txt'>Our price: " + price + "</div></td>");}
			
			else if(price != "")
			{
					document.write("<td align='left' valign='top'><div class='op-s-txt'>Our price: " + price + "</div></td>");
			}
			
			else if(saleprice != "")
			{
					document.write("<td align='left' valign='top'><div class='op-s-txt'>Our price: " + saleprice + "</div></td>");
			}
			
			document.write("</tr><tr><td align='left' valign='top' colspan='2' class='rp-pro-name-txt' height='43'><a href=" + id + ">" + name + "</a></td></tr></table></td></tr>");
			
				if(j<2)
				{
					document.write("<tr><td align='center' valign='middle' class='rp-btm-brd'><img src='http://yst.pool-tables-fast.com/images/spacer.gif' alt='' width='1' height='4' border='0' /></td></tr>");
				}
			}
			document.write("</table></td></tr></table>");
		}
	}
}
