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){
	//alert("in set");
	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(){
//	alert("test");
//ecor4d-250
	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='750' border='0' cellspacing='0' cellpadding='0'><tr><td><table border='0' cellspacing='0' cellpadding='0' width='750' align='center'><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td width='9' align='left' valign='bottom'><img src='https://p4.secure.hostingprod.com/@surveillance-video.com/ssl/new-layout/images/recly_left_new.gif'  width='9' height='24' /></td><td width='200' align='center' class='recentlyMidnew'><span class='orangeText' style='width:140px; display:block;'>Recently Viewed</span></td><td width='9' align='right' valign='bottom'><img src='https://p4.secure.hostingprod.com/@site.surveillance-video.com/ssl/new-layout/images/recly_right_new.gif'  width='9' height='24' /></td><td width='532' class='recentlyHRline' valign='top'>&nbsp;</td></tr></table></td></tr><tr><td style='border:1px solid #e7e7e7;'><table align='center' width='186' border='0' cellspacing='0' cellpadding='0' style='border-left:1px solid #D7D7D7;border-bottom:1px solid #D7D7D7;'><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);
				}


	document.write("<td align='center' valign='top'><table border='0' align='left' cellspacing='0' cellpadding='0' width='170' style='height:175px;'><tr><td height='100' align='center' valign='middle' style='border-top:1px solid #003366; border-left:1px solid #003366; border-bottom:1px solid #003366; border-right:1px solid #003366;'><a href=" + id + "><img src=" + imageurl + " alt='' border='0' /></a></td></tr><tr><td align='center' valign='middle'><a class='recent-link' href=" + id + ">" + name + "</a></td></tr>");
	
if(price == 0)
{
document.write("<tr><td align='center' valign='middle' class='specialPrice'><a href=" + id + ">Click for Special Price</a></td></tr></table></td>");
}
else
{
	document.write("<tr><td align='center' valign='middle' class='mostpopular-price-text'>" + price + "</td></tr></table></td>");	
}

if(j<3){
	document.write("<td style='border-right: 1px solid rgb(215, 215, 215);' width='1'>&nbsp;</td>");
	}
				}
document.write("</tr></table></td></tr></table>");
			
		}		
						
	}
}
