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 setCookie(name,id,imageurl,price,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 + "|p:" + price + "|saleprice:" + 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 writeCookie(){
	var name,id,imageurl,price,saleprice,Lname,Lid,Limageurl,Lprice,Lsaleprice,cookieVal;
	Lname = "productName:";
	Lid = "id:";
	Limageurl = "imageurl:";
	Lprice = "p:";
	Lsaleprice = "saleprice:";
	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='710' border='0' align='left' cellpadding='0' cellspacing='0'><tr><td align='left' valign='top' class='sub-head-area'><img src='http://yst.equestrian-tack.com/images/sub-head-rgt-crv.jpg' width='5' height='31' class='sub-head-rgt-crv'>Recently Viewed Products</td></tr><tr><td align='left' valign='top' class='rvp-pro-tp-space'><table width='155' border='0' align='left' 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("p:") > -1)
						price = nvp[k].substr(Lprice.length);
					if(nvp[k].indexOf("saleprice:") > -1)
						saleprice = nvp[k].substr(Lsaleprice.length);
				}
				//alert(price);

document.write("<td align='left' valign='top'><div class='fp-pro-box'><table align='center' width='136' border='0' cellspacing='0' cellpadding='0'><tr><td align='center' valign='middle' height='116' width='136'><a href=" + id + "><img src=" + imageurl + " alt='' border='0'></a></td></tr></table></div><div class='pro-name'><a href=" + id + ">" + name + "</a></div><div class='rp-txt'>" + priceText + " " + price + "<br><span class='black-bold-txt'>" + salePriceText + "</span> <span class='red-dark-bold-txt'>"  + saleprice + "</span></div></td>");
if(j<3){
	document.write("<td align='left' valign='top' width='30'><img src='http://yst.equestrian-tack.com/images/spacer.gif' width='30' height='1' border='0'></td>");
	}
				}
document.write("</tr></table></td></tr></table>");
			
		}		
						
	}
}