//***************************************************
//******* CODE COPYRIGHT AMERILIFEVITAMIN.COM *******
//******* USE WITHOUT PERMISSION IS PROHIBITED ******
//***************************************************
function product()
{
	this.productId;
	this.name;
	this.url;
	this.imageThumbnailUrl;
}

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 exp = new Date(); 
    exp.setTime (exp.getTime()+60*(24*60*60*1000));   // expire after two months
    document.cookie = name + "=" + value + "; expires=" + exp.toGMTString();
}

function delCookie(name)
{
	var exp = new Date();
	exp.setTime (exp.getTime()-1);
	var cval = getCookie(name);
	document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
}

function onLoad()
{
	var i;
	var nItem = 5;
	var strSiteTitle = "_www.amerilifevitamin.com_";
	var old_info = getCookie("history_info");
	if(old_info != null)
	{
		var old_item = old_info.split(strSiteTitle, nItem);			
		var wlink, content="";
		content +=("<div class='addbgimg'><img src='/lib/yhst-15658733819255/bodybg-top.gif' width='1000' height='15'></div>");
		content +=("<div id='recentview'>");
		content +=("<div class='recentviewHeader'>Recently Viewed Products</div>");
		content +=("<table width='100%' class='contentsTable'><tr>");
		for(i = 0; i < nItem; i++)
		{
			if(old_item[i] != null && old_item[i] != "" && old_item[i] != "undefined" )
			{
			  wlink=old_item[i].split("|");
			  content +=("<td valign='top'>");
			  content +=("<a href='"+wlink[2]+"'>"+"<img src='" + wlink[3]+"'border='0'><br>"+wlink[1]+"</a><br>");
			  content +=("</td>");
			}
		}
		content +=("</tr></table>");
		content +=("<div class='recentviewClear'><a href='javascript:history.go(0)' onClick=javascript:delCookie('history_info');>click here to clear recently viewed items</a></div>");
		content +=("</div>");
		content +=("<div class='addbgcolor'><img src='/lib/yhst-15658733819255/bodybg-bottom.gif' width='1000' height='28'></div>");
		document.getElementById("history").innerHTML=content;
	}
	else
	{
		//document.getElementById("history").innerHTML="Welcome to AmeriLifeVitamin.com";
	}
	
	if (product != null && product.productId != undefined && product.name != undefined && product.url != undefined && product.imageThumbnailUrl != undefined)
	{	
		var history_info_str = product.productId + "|" + product.name + "|" + product.url + "|" + product.imageThumbnailUrl + strSiteTitle;
		if(old_info != null)
		{
			var insert = true;			
			for(i = 0; i < nItem; i++)
			{
				if(old_item[i] != null)
				{
					if(old_item[i].indexOf(product.productId) != -1)
					{
						insert = false;
						break;
				    }
				}
			}
			
			if(insert)
			{
				for (i = 0; i < old_item.length; i++)
				{
					if (old_item[i] != null && old_item[i] != "")
					{
						history_info_str += old_item[i] + strSiteTitle;	
					}
				}
				
				setCookie("history_info",history_info_str);
			}
		}
		else
		{
			setCookie("history_info",history_info_str);			
		}	
	}
}

