// CKIM: renamed global variables to use ALL_CAPS
var RECENT_PRODUCT_COOKIE_DURATION_DAYS = 90; // CKIM: extended the time
var RECENT_PRODUCT_SEPARATOR = '~';

// CKIM: added a variable to replace multiple strings
var RECENT_PRODUCT_COOKIE_NAME = 'recent_products';

function storeViewedProduct(productId, productName)
{
	// get cookie
	var recent_products = readCookie(RECENT_PRODUCT_COOKIE_NAME);
		
	var products_current;
	var products_new = new Array();
	
	if (recent_products != null)
	{
		
		products_current = recent_products.split(RECENT_PRODUCT_SEPARATOR);
				
		for (var i=0; i<products_current.length; i++)
		{
			var product = products_current[i].split(':');				
			if (product[0] == productId) continue;
			products_new[products_new.length] = products_current[i];
		}
	}
	
	var cookie_value = productId + ':' + productName;
	
	for (var i=0; i<products_new.length; i++)
		cookie_value += RECENT_PRODUCT_SEPARATOR + products_new[i];
	
	createCookie(RECENT_PRODUCT_COOKIE_NAME, cookie_value, RECENT_PRODUCT_COOKIE_DURATION_DAYS);
		
}

function getProductId(index)
{
	// get cookie
	var recent_products = readCookie(RECENT_PRODUCT_COOKIE_NAME);
				
	if (recent_products != null)
	{
		
		var products_current = recent_products.split(RECENT_PRODUCT_SEPARATOR);
		
		if (index > products_current.length - 1)
			return null;
		
		var product = products_current[index].split(':');
		
		return product[0];
				
	}
	else
		return null;

}

function getProductName(index)
{
	// get cookie
	var recent_products = readCookie(RECENT_PRODUCT_COOKIE_NAME);
				
	if (recent_products != null)
	{
		var products_current = recent_products.split(RECENT_PRODUCT_SEPARATOR);
		
		if (index > products_current.length - 1)
			return null;
			
		var product = products_current[index].split(':');
		
		return product[1];
				
	}
	else
		return null;

}

// trims the cookie to the maximum number of products
function trimRecentProducts(maximumNumberOfProducts)
{
	// get cookie
	var recent_products = readCookie(RECENT_PRODUCT_COOKIE_NAME);
		
	var products_current;
	var products_new = new Array();
	
	// trim the cookie
	if (recent_products != null)
	{
		
		products_current = recent_products.split(RECENT_PRODUCT_SEPARATOR);
		
		var i_to = products_current.length < maximumNumberOfProducts ? products_current.length : maximumNumberOfProducts;
				
		for (var i=0; i<i_to; i++)
		{	
			var product = products_current[i].split(':');				
			products_new[products_new.length] = products_current[i];
		}
	}
	
	// compile the cookie value string
	var cookie_value = '';
	
	for (var i=0; i<products_new.length; i++)
	{
		if (cookie_value != '')
			cookie_value += RECENT_PRODUCT_SEPARATOR;
			
		cookie_value += products_new[i];
	
	}
	
	// save new cookie
	createCookie(RECENT_PRODUCT_COOKIE_NAME, cookie_value, RECENT_PRODUCT_COOKIE_DURATION_DAYS);

}

function createCookie(name,value,days)
{
  // CKIM: moved the scope of this var because it was declared locally in the
  //        if() block, meaning the value is lost when written
  var expires;
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+escape(value)+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length,c.length));
	}
	return null;
}

// are cookies enabled?
if (navigator.cookieEnabled == 0) {
  alert("You need to enable cookies for this site to load properly!");
}