var rpExpDays = 7;
var rpExp = new Date(); 
rpExp.setTime(rpExp.getTime() + (rpExpDays*24*60*60*1000));

rpDiv = 'rpui';
rpCookieName = 'rvproducts';
productURLPrefix = 'http://www.lastminute.com/lmn/pso/catalog/Product.jhtml?';
noOfProducts = 5;

var rpReplacements = new Array(['$P',productURLPrefix],['$A','PRODID'],['$B','CATID'],['$C','POSITION'],['$D','SPOS']);

function getCookieVal (offset) 
	{  
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1) { endstr = document.cookie.length; }
	return unescape(document.cookie.substring(offset, endstr));
	}

function GetCookie (name) 
	{  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) 
		{    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
		}  
	return null;
	}

function SetCookie (name, value) 
	{  
	var expires = false;
	document.cookie = name + "=" + escape (value) + ((expires == true) ? "" : ("; expires=" + rpExp.toGMTString())) + ";path=/" + ";domain=lastminute.com";
	}

function rpclear(){
expiry = new Date();
expiry.setTime(expiry.getTime() - 1000);
document.cookie = rpCookieName + '=;expires=' + expiry.toGMTString() + ';path=/;domain=lastminute.com';
if (document.location.href.indexOf('?')>-1) queryPrefix='&'; else queryPrefix = '?';
document.location.href = document.location.href + queryPrefix + 'set_rvp=false';
}	

function rpadd()
	{
	//get cookie value
	if (document.location.href.indexOf('set_rvp=false') == -1)
		{
		rpCookie = GetCookie(rpCookieName);
		productURL = document.location.href;
		if (productURL.indexOf('PRODID')==-1) return false;
		newProdId = productURL.slice(productURL.indexOf('PRODID') + 7);
		if (newProdId.indexOf('&')>-1) newProdId = newProdId.split('&')[0];
		for (i=0;i<rpReplacements.length;i++)	productURL = productURL.replace(rpReplacements[i][1],rpReplacements[i][0]);
		title = document.title;
		if (title!='' && productURL!='')
			{
			productDetails = productURL + 't#' + title;
			if (rpCookie)
				{
				products = rpCookie.split('#rvp#',noOfProducts-1);
				for (x=0;x<products.length;x++){
					oldProdId = products[x].slice(products[x].indexOf('$A') + 3).split('&')[0];
					if (oldProdId==newProdId)	return false;
				}
				products.unshift(productDetails);
				cookieVal='';
				for (x=0;x<products.length;x++)
					{
					if (x>0) cookieVal+='#rvp#'+products[x];
					else cookieVal+=products[x];
					}
				}
			else cookieVal = productDetails;
			SetCookie(rpCookieName,cookieVal);
			//if it doesn't exist, create cookie
			//add entry
			}
		}
	}

function rpdisplay2(){}
function rpdisplay()
	{
	//Placeholder for Dynamo page
	rpElement = document.getElementById(rpDiv);
	//read cookie
	rpCookie = GetCookie(rpCookieName);
	//display products
	if (rpCookie)
		{
		if (rpCookie!='')
			{
			products = rpCookie.split('#rvp#',noOfProducts);
			html = '<div style="padding-top:10px;"><h3 class="t3" style="margin:0;color:#eb0e8e;float:left;">Recently viewed items</h3><div style="float:right"><a href="javascript:rpclear()"><img src="http://www.lastminute.com/site/ico_secondary_close.gif" style="vertical-align:middle" alt="" border="0" /></a> <a href="javascript:rpclear()"><strong>Clear this list</strong></a></div><div style="clear:both;padding-bottom:3px;"></div><div style="border:1px solid #f09;padding:5px;background-color:#f0f0f0;"><div style="border:1px solid #f09;padding:5px;background:#fff;"><ul style="list-style-image:url(http://www.lastminute.com/site/ico_primary_star.gif);margin:0;">';
			for (i=0;i<products.length;i++)
				{
					product = products[i].split('t#');
					productURL = product[0];
					for (j=0;j<rpReplacements.length;j++)	productURL = productURL.replace(rpReplacements[j][0],rpReplacements[j][1]);
					if (productURL.indexOf('?')>-1) querySymbol = '&'; else querySymbol = '?';
					productURL+=querySymbol+'intcmp=pers.rp'+(i+1);
					productTitle = product[1];
					html += '<li style="margin-left:25px;"><a href="' + productURL + '">' + productTitle + '</a></li>';
				}
			html += '</ul></div></div>';
			rpElement.innerHTML = html;	
			}
		}
		else rpElement.innerHTML = '';
	}
	

