// GLOBALS
var cookieDuration = 180;			// Duration of cookie in days.
var delimiter = ",";				// Gear List product delimiter.
var siteScope = "http://www.thenorthface.com/";				// Cookie site scope.
var regionCookie="TNF_Region";			// Region Cookie.
var conceptOnCookiePrefix = "TNF_ConceptOn_";	// Prefix for "Concept On" tracking cookie
var conceptOffCookiePrefix = "TNF_ConceptOff_";	// Prefix for "Concept Off" tracking cookie

// Calculate expiration time for Cookie
var milliExpiraton = 1000 * 60 * 60 * 24 * cookieDuration;
var milliToday = new Date().getTime();
var milliExpirationDate = milliToday + milliExpiraton;
var dateExpiration = new Date(milliExpirationDate);

// Value of region cookie.
var userRegion = getCookie(regionCookie);

// Extrapolated name of Concept Tracking cookies
var conceptOnCookie = conceptOnCookiePrefix + userRegion;
var conceptOffCookie = conceptOffCookiePrefix + userRegion;

// Function: bool addProduct(string SKU, bool usingConcept)
//
// Add a single SKU to the appropriate Concept Tracking cookie.
// Returns true on success, false on failure
function addProduct(SKU, usingConcept)
{
	// Boolean to indicate whether SKU is added to the list.
	var boolSuccess = false;

	var arrayProducts = new Array();
	var stringProducts = new String();

	arrayProducts = getProductList(usingConcept);

	// Check to see whether we need to split apart the array.
	if(arrayProducts.length > 0)
	{
		var boolDuplicateItem = false;

		// Check to see if they already have this SKU.
		for(var item in arrayProducts)
		{
			if(arrayProducts[item] == SKU)
				boolDuplicateItem = true;
		}

		// If not, add the SKU to the array.
		if(! boolDuplicateItem)
		{
			arrayProducts.push(SKU);
			boolSuccess = true;
		}

		// Merge the array into a CSV string.
		stringProducts = arrayProducts.join(delimiter);
	}
	else
	{
		stringProducts = SKU;	
		boolSuccess = true;
	}

	// Generate our tracking cookie.
	setCookie(usingConcept ? conceptOnCookie : conceptOffCookie,
		stringProducts, dateExpiration, siteScope);

	return boolSuccess;
}

// Function: array getProductList(bool)
//
// Parse the appropriate Concept tracking cookie and return an array of products.
// Returns array containing SKUs or an empty array.
function getProductList(usingConcept)
{
	var arrayProducts = new Array();
	var stringProducts = getCookie(usingConcept ? conceptOnCookie : conceptOffCookie);

	if(stringProducts)
		arrayProducts = stringProducts.split(delimiter);

	return arrayProducts;
}

// Function: bool productExists(bool)
//
// Determine if the given SKU exists in the appropriate Concept tracking cookie
function productExists(SKU, usingConcept)
{
	var exists = false;
	var products = getProductList(usingConcept);
	
	// Check to see whether we need to split apart the array.
	if(products.length > 0)
	{
		for(var item in products)
		{
			if(products[item] == SKU)
			{
				exists = true;
				break;
			}
		}
	}
	
	return exists;
}
