/*
 * We declare a new object for QL tracking so that we can control the scope of 
 * any functions or variables declared that are to do with tracking.
 * All functions and objects specific to tracking must be declared as 
 * properties of this object.
 */
var QLTracking = new Object();
QLTracking.events;
/*
 * The QLTracking.currentProducts is populated with product 'objects' 
 * that have the following fields: productCode, quantity and price.
 */
QLTracking.currentProducts;
QLTracking.orderId = "";
QLTracking.searchString = "";
/*
 * QLTracking.userId is a field to hold the userId (if available).
 * This must be set in the JSP of the AnalyticsComponent.
 */
QLTracking.userId = "";
/* 
 * QLTracking.navigationMode is the way in which a user navigates to a product 
 * e.g. browse, search, cross-sell, internal campaign.
 */
QLTracking.navigationMode = "";
/*
 * QLTracking.crossSellParent is populated with the product the user 
 * has navigated from by clicking on a cross-sell link.
 */
QLTracking.crossSellParent = "";
/*
 * QLTracking.breadcrumbItems is a JavaScript array containing each 
 * breadcrumb item (excluding the home link). It is used for generating 
 * site navigation position information in the format specific to each
 * particular tagging implementation.
 */
QLTracking.breadcrumbItems = new Array();
QLTracking.pageName = "";
QLTracking.channel = "";
QLTracking.category = "";
QLTracking.subcategory = "";
QLTracking.pageType = "";
QLTracking.paymentMethod = "";
QLTracking.state = "";
QLTracking.zip = "";
QLTracking.shippingMethod= "";
QLTracking.hier1 = "";

/*
 * QLTracking.pageLabel is a field to hold the page label. This must be 
 * set in the JSP of the analytics component.
 */
QLTracking.pageLabel = "";


QLTracking.addTrackingEvent = function(event) {
	if (isEmpty(QLTracking.events)) {
		QLTracking.events = new Array(event);
	} else {
		QLTracking.events.push(event);
	}
}

/*
 * This function adds a product to the array QLTracking.currentProducts.
 * A product has the following fields: productCode, quantity and price.
 */
QLTracking.addTrackingProduct = function(productCode, qty, price) {
	if (isEmpty(qty)) {
		qty = "";
	}
	if (isEmpty(price)) {
		price = "";
	}
	var newProductEntry = new Object();
	newProductEntry.productCode = productCode;
	newProductEntry.quantity = qty;
	newProductEntry.price = price;
	
	if (isEmpty(QLTracking.currentProducts)) {
		QLTracking.currentProducts = new Array(newProductEntry);
	} else {
		QLTracking.currentProducts.push(newProductEntry);
	}
}

QLTracking.changeTrackingProduct = function(productCode) {
	var newProductEntry = new Object();
	newProductEntry.productCode = productCode;
	newProductEntry.quantity = "";
	newProductEntry.price = "";
	QLTracking.currentProducts = new Array(newProductEntry);
}

QLTracking.addTrackingBreadcrumbItem = function(item) {
	QLTracking.breadcrumbItems.push(item);
}

QLTracking.getPathFromBreadcrumb = function(delimiter) {
	var path = "";
	for (var i in QLTracking.breadcrumbItems) {
		if (i == 0) {
			path = QLTracking.breadcrumbItems[i];
		} else {
			path = path + delimiter + QLTracking.breadcrumbItems[i];
		}
	}
	return path;
}

/*
 * All implementations should create a local copy of tracking values.
 * We must treat QLTracking values as read only so that we can have
 * more than one tracking implementation on the same site without
 * them interfering with each other.
 * This function facilitates the creation of these local vars.
 */
QLTracking.populateLocalVariables = function() {
	var localVars = new Object();
	localVars.events = QLTracking.events;
	localVars.currentProducts = QLTracking.currentProducts;
	localVars.orderId = QLTracking.orderId;
	localVars.searchString = QLTracking.searchString;
	localVars.userId = QLTracking.userId;
	localVars.navigationMode = QLTracking.navigationMode;
	localVars.crossSellParent = QLTracking.crossSellParent;
	localVars.pageName = QLTracking.pageName;
	localVars.channel = QLTracking.channel;
	localVars.category = QLTracking.category;
	localVars.subcategory = QLTracking.subcategory;
	localVars.pageType = QLTracking.pageType;
	localVars.pageLabel = QLTracking.pageLabel;
	
	localVars.paymentMethod = QLTracking.paymentMethod;
	localVars.shippingMethod = QLTracking.shippingMethod;
	localVars.state = QLTracking.state;
	localVars.zip = QLTracking.zip;
	localVars.hier1 = QLTracking.hier1;
	return localVars;
}
