// 4-Tell Cookie Data

// Global Data ////////////////////
var SID = "";
var CID = "";
var CIDExists = false;
var ViewItems = "";
var MaxViewItems = 10;
var NumViewItems = 0;
var CookieOpen = false;
var CookieName = "4-TellData";
var Tags = ["SID", "CID", "View"];
var TagSID = 0; var TagCID = 1; var TagView = 2;

// Private Base Functions ///////////////////
// GetCookie //
function GetCookie(cname) {
	var i, x, y, value, ARRcookies = document.cookie.split(";");
	for (i = 0; i < ARRcookies.length; i++) {
		x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
		y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
		x = x.replace(/^\s+|\s+$/g, "");
		if (x == cname) {
			value = unescape(y);
			ParseValue(value);
			CookieOpen = true;
			break;
		}
	}
	if (!CookieOpen) {
		rand = Math.round(Math.random() * 10000000000);
		SID = "4T" + rand;
		SetCookie(CookieName);
	}
}

function ParseValue(value) {
	var index, pos, i;
	var subTag, subVal;
	// return if value is empty
	if (value == "")
		return;
	// parse non-empty value
	pos = 0;
	while (true) {
		// subTag
		index = value.indexOf("=", pos);
		if (index < 0)
			break;
		subTag = value.substring(pos, index);
		// subVal
		pos = index + 1;
		index = value.indexOf("&", pos);
		if (index < 0)
			index = value.length; // It is end of string
		subVal = value.substring(pos, index);
		// set pos
		pos = index + 1;
		if (pos > value.length)
			pos = value.length;
		// set value
		if (subTag == Tags[TagSID]) {
			SID = subVal;
		}
		else if (subTag == Tags[TagCID]) {
			CID = subVal;
			if (CID != "")
				CIDExists = true;
		}
		else if (subTag == Tags[TagView]) {
			ViewItems = subVal;
			SetNumViewItems();
		}
	}
	return;
}

function SetNumViewItems() {
	var index = 0;
	NumViewItems = 0;
	if (ViewItems == "")
		return;
	else
		NumViewItems = 1; // last item has no comma, and this covers if only one item
	while (true) {
		index = ViewItems.indexOf(",", index+1);
		if (index < 0) break;
		NumViewItems++;
	}
	return;
}

// SetCookie //
function SetCookie(cname) 
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + 365);
	// Need path to be /, otherwise new cookie for each path
	var c_value = escape(SetValue()) + "; expires=" + exdate.toUTCString() + "; path=/";
	document.cookie = cname + "=" + c_value;
}

function SetValue()
{
	var i;
	value = Tags[TagSID] + "=" + SID + "&" + Tags[TagCID] + "=" + CID + "&" + Tags[TagView] + "=" + ViewItems;
	return value;
}

// Public Functions /////////////////////////////////////
// ID Stuff //
// Get anonymous but persistent shopper ID
function GetSID() {
	if (!CookieOpen)
		GetCookie(CookieName);
	return SID;
}

// Get same customer ID as used with sales data
function GetCID() {
	if (!CookieOpen)
		GetCookie(CookieName);
	return CID;
}

// Get CID if exist, otherwise get SID
function GetID() {
	if (!CookieOpen)
		GetCookie(CookieName);
	if (CID != "")
		return CID;
	return SID;
}

// Set customer ID from logon
function SetCID(id) {
	if (!CookieOpen)
		GetCookie(CookieName);
	CID = id;
	SetCookie(CookieName);
	return;
}

// View Data //
// Set current viewed item, one at a time with no spaces in ID
function SetViewItem(id) {
	var index;
	if (id == "" || id == null) 
		return;
	if (!CookieOpen)
		GetCookie(CookieName);
	// KLL NEED TO CHECK IF ITEM IS ALREADY IN ARRAY, and remove if so

	// Assumes that id is one item ID and spaces don't need to be trimmed
	if (ViewItems == "")
		ViewItems = id;
	else
		ViewItems = id + "," + ViewItems;
	NumViewItems++;
	// remove last item - can only be one larger
	if (NumViewItems > MaxViewItems) {
		index = ViewItems.lastIndexOf(",");
		ViewItems = ViewItems.slice(0, index);
	}
	SetCookie(CookieName);
	return;
}

// Get whole CSV list of recently viewed items
function GetViewItems() {
	if (!CookieOpen)
		GetCookie(CookieName);
	return ViewItems;
}

