var back_icon = 'https://www.boscovs.com/wcsstore/boscovs/images/store/icon_back.gif'

/**********************************************
ClickHistory class
	number - department/product/assortment number
	name - department/product/assortment name
	type - either 'product', 'assortment' or 'department' (case sensitive)
	enabled - boolean designating if this click history element is active or not (all new objects should be active)
***********************************************/

function ClickHistory(number, name, type, enabled)
{
	this.number = number;
	this.name = name;
	this.type = type;
	this.enabled = enabled;
}

/************************************************
addToClickHisotry(clickHistoryList, resetClickHistory
	clickHistoryList:  array of ClickHistory (1..5)
	
************************************************/

function addToClickHistory(clickHistoryList, isProduct)
{
	handleQueueHistory(clickHistoryList, isProduct);
}

function departmentHistory(number, name, resetClickHistory)
{
	handleHistory(number, name, "department", resetClickHistory);
}

function assortmentHistory(number, name)
{
	handleHistory(number, name, "assortment", "N");
}

function productHistory(number,name,resetClickHistory)
{
	handleHistory(number, name, "product", resetClickHistory);
}

function getIndexLastElement(tree)
{
	for (i = 4; i >= 0; i--)
	{
		if (tree[i].enabled == true)
			return i;
	}
	
	return 0;
}

function printClickHistory(isProduct)
{
	tree = retrieveTreeFromCookie();
	
	lastEnabled = getIndexLastElement(tree);
	
	if (!isProduct)
		tree[lastEnabled].enabled = false;
	
	printTree(tree);
	
	if (!isProduct)
		tree[lastEnabled].enabled = true;
}

function handleHistory(number, name, type, resetClickHistory)
{
	if (resetClickHistory == "Y")
		clearTree();

	tree = retrieveTreeFromCookie();

	cleanTree(tree, type, number);

	if (type == "assortment")
		popLastAssortment(tree);

	printTree(tree);

	if (type != "product")
		pushTree(tree, number, name, type);

	writeTreeToCookie(tree);
}

function handleQueueHistory(clickHistoryList, isProduct)
{
	clearTree();

	tree = retrieveTreeFromCookie();

	for (i = 0; i < clickHistoryList.length; i++)
	{
		clickHistory = clickHistoryList[i];
		
		cleanTree(tree, clickHistory.type, clickHistory.number);

		if (clickHistory.type == "assortment")
			popLastAssortment(tree);

		if (clickHistory.type != "product")
			pushTree(tree, clickHistory.number, clickHistory.name, clickHistory.type);
	}

	if (!isProduct)
		tree[clickHistoryList.length-1].enabled = false;
	
	printTree(tree);
	
	if (!isProduct)
		tree[clickHistoryList.length-1].enabled = true;
	writeTreeToCookie(tree);
}

function clickHistoryCookie(cookieName)
{
	return new cookieObject(cookieName, 1, "/", "number", "name", "type");
}

function retrieveTreeFromCookie()
{
	var tree = new Array(5);

	tree[0] = retrieveCookie("bos_clkhist0");
	tree[1] = retrieveCookie("bos_clkhist1");
	tree[2] = retrieveCookie("bos_clkhist2");
	tree[3] = retrieveCookie("bos_clkhist3");
	tree[4] = retrieveCookie("bos_clkhist4");

	return tree;
}

function retrieveCookie(cookieName)
{
	clckHistCookie = new clickHistoryCookie(cookieName);
	if (clckHistCookie.found)
		return new ClickHistory(clckHistCookie.get("number"), clckHistCookie.get("name"), clckHistCookie.get("type"), true);
	else
		return new ClickHistory(0, "", "", false);
}

function popLastAssortment(tree)
{
	var pop = false;

	for (i = 0; i < 5; i++)
	{
		if (tree[i].type == "assortment")
			pop = true;
		if (pop == true)
		{
			tree[i].enabled = false;
		}
	}
}

function cleanTree(tree, type, number)
{
	var pop = false;

	for (i = 0; i < 5; i++)
	{
		if (pop == false && type == tree[i].type && number == tree[i].number)
			pop = true;

		if (pop == true)
			tree[i].enabled = false;
	}
}

function nextAvailableElement(tree)
{
	for (i = 0; i < 5; i++)
	{
		if (tree[i].enabled == false)
			return i;
	}
	return -1;
}

function pushTree(tree, number, name, type)
{
	nextElement = nextAvailableElement(tree);
	
	if (nextElement != -1)
	{
		tree[nextElement].name = name;
		tree[nextElement].number = number;
		tree[nextElement].type = type;
		tree[nextElement].enabled = true;
		
	}
}

function writeTreeToCookie(tree)
{
		writeCookie(tree[0], "bos_clkhist0");
		writeCookie(tree[1], "bos_clkhist1");
		writeCookie(tree[2], "bos_clkhist2");
		writeCookie(tree[3], "bos_clkhist3");
		writeCookie(tree[4], "bos_clkhist4");
}

function writeCookie(element, cookieName)
{
	var cookie;
	
	if (element.enabled == false)
	{
		cookie = new clickHistoryCookie(cookieName);
		cookie.remove();
	}
	else
	{
		cookie = new clickHistoryCookie(cookieName);
		cookie.put("number", element.number);
		cookie.put("name", element.name);
		cookie.put("type", element.type);
		
		cookie.write();
	}
}

function clearTree()
{
	var cookie;
	
	cookie = new clickHistoryCookie("bos_clkhist0");
	cookie.remove();
	cookie = new clickHistoryCookie("bos_clkhist1");
	cookie.remove();
	cookie = new clickHistoryCookie("bos_clkhist2");
	cookie.remove();
	cookie = new clickHistoryCookie("bos_clkhist3");
	cookie.remove();
	cookie = new clickHistoryCookie("bos_clkhist4");
	cookie.remove();
}

function printTree(tree)
{
	for (i = 0; i < 5; i++)
	{
		if (tree[i].enabled && i == 0) //first element
		{
			//<![CDATA[
			document.write('<img src="' + back_icon + '" alt="Back" border="0"> ');
			//]]>
		}
		if (tree[i].enabled == true)
		{
			if(i!=0)
				printElement(tree[i],tree[i-1]);
			else
				printElement(tree[i],tree[i]);
		}
	}
}

function printElement(element,previousElement)
{
	//<![CDATA[
	document.write('<a href="DepartmentSelect.bos?departmentNumber=' + element.number + '&type=Department&pdn='+ previousElement.number + '">' + element.name+'</a> | ');
	//]]>
}

/*

DISCLAIMER: THESE JAVASCRIPT FUNCTIONS ARE SUPPLIED 'AS IS', WITH
NO WARRANTY EXPRESSED OR IMPLIED. YOU USE THEM AT YOUR OWN RISK.
PAUL STEPHENS DOES NOT ACCEPT ANY LIABILITY FOR
ANY LOSS OR DAMAGE RESULTING FROM THEIR USE, HOWEVER CAUSED.

Paul Stephens' cookie-handling object library

version 2.0

www.paulspages.co.uk

(C) Paul Stephens, 2001-2003. Feel free to use this code, but please leave this comment block in. This code must not be sold, either alone or as part of an application, without the consent of the author.

*/

function cookieObject(name, expires, accessPath) {
var i, j
this.name = name
this.fieldSeparator = "#"
this.found = false
this.expires = expires
this.accessPath = accessPath
this.rawValue = ""
this.fields = new Array()
this.fieldnames = new Array()
if (arguments.length > 3) { // field name(s) specified
  j = 0
  for (i = 3; i < arguments.length; i++) {
    this.fieldnames[j] = arguments[i]   
    j++
  }
}
this.read = ucRead

this.write = ucWrite

this.remove = ucDelete
this.get = ucFieldGet
this.put = ucFieldPut
this.namepos = ucNamePos
this.read()
}


function ucFieldGet(fieldname) {
var i = this.namepos(fieldname)
if (i >=0) {
  return this.fields[i]
} else {
  return "BadFieldName!"
}
}

function ucFieldPut (fieldname, fieldval) {
var i = this.namepos(fieldname)
if (i >=0) {
  this.fields[i] = fieldval
  return true
} else {
  return false
}
}

function ucNamePos(fieldname) {
var i
for (i = 0; i < this.fieldnames.length; i++) {
  if (fieldname == this.fieldnames[i]) {
    return i
  }
}
return -1
}


function ucWrite() {     
  var cookietext = this.name + "="

// concatenate array elements into cookie string

// Special case - single-field cookie, so write without # terminator
if (this.fields.length == 1) {
  cookietext += escape(this.fields[0])
  } else { // multi-field cookie
    for (i in this.fields) {
      cookietext += escape(this.fields[i]) + this.fieldSeparator }
  }


// Set expiry parameter, if specified
    if (this.expires != null) { 
      if (typeof(this.expires) == "number") { // Expiry period in days specified 
        var today=new Date()    
        var expiredate = new Date()     
        expiredate.setTime(today.getTime() + 1000*60*60*24*this.expires)
        cookietext += "; expires=" + expiredate.toGMTString()
      } else { // assume it's a date object
        cookietext +=  "; expires=" + this.expires.toGMTString()
      } // end of typeof(this.expires) if
    } // end of this.expires != null if
  
// add path, if specified
   if (this.accessPath != null) {
   cookietext += "; PATH="+this.accessPath }

// write cookie
   // alert("writing "+cookietext)
   document.cookie = cookietext
   return null 
}


function ucRead() {
  var search = this.name + "="                      
  var CookieString = document.cookie           
  this.rawValue = null
  this.found = false    
  if (CookieString.length > 0) {               
    offset = CookieString.indexOf(search)      
    if (offset != -1) {                        
      offset += search.length                  
      end = CookieString.indexOf(";", offset)  
      if (end == -1) {  // cookie is last item in the string, so no terminator                       
       end = CookieString.length }             
      this.rawValue = CookieString.substring(offset, end)                                  
      this.found = true
      }
    }
  
if (this.rawValue != null) { // unpack into fields

  var sl = this.rawValue.length
  var startidx = 0
  var endidx = 0
  var i = 0

// Special case - single-field cookies written by other functions,
// so without a '#' terminator

if (this.rawValue.substr(sl-1, 1) != this.fieldSeparator) {
  this.fields[0] = unescape(this.rawValue)
  } else { // separate fields

  do 
  {
   endidx = this.rawValue.indexOf(this.fieldSeparator, startidx)
   if (endidx !=-1) {
     this.fields[i] = unescape(this.rawValue.substring(startidx, endidx))
     i++
     startidx = endidx + 1}
  }
  while (endidx !=-1 & endidx != (this.rawValue.length -1));
}
} // end of unpack into fields if block
  return this.found
} // end of function


function ucDelete() {
  this.expires = -10
  this.write()
  return this.read()
}


// IT'S OK TO REMOVE THE CODE BELOW HERE IF YOUR PAGE
// DOESN'T USE cookieList() OBJECTS OR THE findCookieObject() FUNCTION.


function findCookieObject(cName, cObjArray) {
/*
This function finds a named cookie among the objects
pointed to by a cookieList array (see below).

Parameters are the cookie name to search for (a string), and an array created with
the new cookieList() constructor (see below)

NOTE - if you're only dealing with a specific, named cookie, then it's
more efficient to ceate a single cookieObject directly with that name,
and check its .found property to see if it already exists on this client.

This function is for when you've created an all-cookies array anyway,
and now want to check whether a specific cookie is present.

It returns a pointer to the cookieObject if found, or null if not found.
*/

var cpointer = null, i
for (i in cObjArray) {
  if (cName == cObjArray[i].name) {
    cpointer = cObjArray[i]
  }
}
return cpointer
}


function cookieList() {
/*
This constructor function creates a cookieObject object (see below)
for each cookie in document.cookie,
and returns an array of pointers to the objects.

You can use it to load all the cookies available to a page, then walk through them.

Example usage:

cookList = new cookieList()
for (i in cookList) {
 document.write(cookList[i].name + " " + cookList[i].fields[0] + "
")
}

*/

var i = 0, rawstring, offset = 0, start, newname
cpointers = new Array()
rawstring = document.cookie
if (rawstring.length > 0) {
  do {
   start = rawstring.indexOf("=", offset)
   if (start != -1) { // another cookie found in string
     // get cookie string up to end of current cookie name
     newname = rawstring.substring(0, start)
     if (offset > 0) {
       // if not first cookie in string, remove previous cookie data from substring
       // subsequent cookie names have a space before them (just a little browser foible!)
       newname = newname.substring(newname.lastIndexOf(";")+2, start)
     }    
     cpointers[i] = new cookieObject(newname)
     offset = start + 1
     i++
   }
  } while (start != -1)
} // end rawstring.length > 0
return cpointers
} //end function
