// JavaScript Document

function setCookie(Name, Value, Expires)
{
 	var CookieText =  escape(Name) + '=' + escape(Value);
	CookieText +=	  (Expires ? '; EXPIRES=' + Expires.toGMTString() : '');
	document.cookie = CookieText;
}

function getCookie(Name)
{
 	var Value =	  null;
	if(document.cookie)	   //only if exists
	{
       	var arr = 	  document.cookie.split((escape(Name) + '=')); 
		if(2 <= arr.length)
       	{
           	var arr2 = arr[1].split(';');
       		Value  =   unescape(arr2[0]);
       	}
	}
	return Value;
}

function deleteCookie(Name)
{
	setCookie(Name,null,(new Date(1))); 
	
}


function setLastPageEKM() 
{
   var expire = new Date();
   expire.setHours(expire.getHours()+1);
   setCookie("ekmlastpage",location.href, expire);
}

function getLastPageEKM()
{
	var lastpage=getCookie("ekmlastpage");
	if(lastpage != null) {
		return lastpage;
	}
	else {
		return '/';
	}
}

function returnLastPageEKM()
{
	window.location.href=getLastPageEKM();	
}

// This is the beginning of the script for showing the last five items viewed


var lastprod = new Array(15);
var lastprod_html_before_img="";
var lastprod_html_after_img="";
var lastprod_html_before_txt="";
var lastprod_html_after_txt="";

function InitLastProductsViewedEKM()
{
	var lastprodtxt=getCookie("lastProdNameEKM");

	for(i=0;i<15;i++) {
		lastprod[i]="";
	}
	
	if(lastprodtxt) {
		lastprod=lastprodtxt.split(";;;");
	}
}

function SetLastProductViewedStartHTML(code)
{
	if(lastprod[0]!="") {	
		document.write(code);
	}
}

function SetLastProductViewedEndHTML(code)
{
	if(lastprod[0]!="") {	
		document.write(code);
	}
}


function SetLastProductViewedImageHTML(before,after)
{
	lastprod_html_before_img = before;
	lastprod_html_after_img = after;
}

function SetLastProductViewedTextHTML(before,after)
{
	lastprod_html_before_txt = before;
	lastprod_html_after_txt = after;
}


function ShowLastProductImageViewedLargeEKM(index)
{
	index *= 3;
	if(lastprod[index]!="") {
		document.write(lastprod_html_before_img+'<a href="'+lastprod[index+2]+'"><img src='+lastprod[index]+' border="0" width="100%" /></a><br/>'+lastprod_html_after_img);
	}
}

function ShowLastProductImageViewedEKM(index,dx,dy)
{
	index *= 3;
	if(lastprod[index]!="") {
		document.write(lastprod_html_before_img+'<a href="'+lastprod[index+2]+'"><img src='+lastprod[index]+' border="0" width="'+dx+'" height="'+dy+'" /></a><br/>'+lastprod_html_after_img);
	}
}

function GetLastProductNameViewedEKM(index)
{
	index *= 3;
	if(lastprod[index]!="") {
		document.write(lastprod_html_before_txt+'<a href="'+lastprod[index+2]+'">'+lastprod[index+1]+'</a><br/>'+lastprod_html_after_txt);
	}
}

function setLastProductViewedEKM(ImagePath, ProdName, ProdLink) 
{
    var expire = new Date();
	var i;
	var cookietext;
	var lastprodtxt=getCookie("lastProdNameEKM");

	expire.setHours(expire.getHours()+24);


	for(i=0;i<15;i++) {
		lastprod[i]="";
	}
	
	if(lastprodtxt) {
		lastprod=lastprodtxt.split(";;;");
	}
	
	// check to see if already in our list
	for(i=0;i<15;i+=3) {
		if(ImagePath==lastprod[i]) {
			return;  // if in our list, exit without updating the cookie
		}
	}
	
	// shuffle the product down so newest is at the top
	for(i=14;i>=3;i--) {
		lastprod[i]=lastprod[i-3];
	}
	lastprod[0]=ImagePath;
	lastprod[1]=ProdName;
	lastprod[2]=ProdLink;
	
	cookietext="";
	for(i=0;i<15;i++) {
		cookietext+=lastprod[i];
		if(i<14) {
			cookietext+=";;;";
		}
	}
	setCookie("lastProdNameEKM",cookietext, expire);
}