<!--
/*
 * cmdatatagutils.js
 * $Revision: 95625 $
 * $Id: cmdatatagutils-5667925-90001006-081808.txt 95625 2008-08-18 10:55:02Z wbird $
 *
 * Coremetrics Tag v4.0, 4/18/2004
 * COPYRIGHT 1999-2002 COREMETRICS, INC. 
 * ALL RIGHTS RESERVED. U.S.PATENT PENDING
 *
 * The following functions aid in the creation of Coremetrics data tags.
 * Date		Imp.Eng.		Desc.
 * 12/06/06	Hutch White		Add Normalization to include Member and Join sites.
 * 12/10/07     Kristen Andrews Added conversion events, elements and auto tech props and cm_em parameter.
 * 06/25/08	Will Bird		Expanded Normalization: 15th amendment replatform 5377-5667925; fixed error tag to set cm.pi
 *
 */

// BMG will set cm_ClientID for 90001006, 90001010 as of 8/18/08 - WBIRD
var cm_TrackLink = "A";
var cm_TrackImpressions = "RS";
var cm_JSFEnabled = false;

var cmJv = "1.0";
if (typeof(isNaN) == "function") cmJv = "1.1";
if (typeof(isFinite) == "function") cmJv = "1.2";
if (typeof(NaN) == "number") cmJv = "1.3";
if (typeof(decodeURI) == "function") cmJv = "1.5";
if (typeof(Array.forEach) == "function") cmJv = "1.6";
if (typeof(Iterator) == "object") cmJv = "1.7";

var cmCheckCMEMFlag = true;
// TAG GENERATING FUNCTIONS ---------------------------------------------------


/*
 * Calling this function points tags to the production database
 */
function cmSetProduction(){
	cm_HOST="lego.columbiahouse.com/eluminate?"; 
}

/*
 * Creates a Tech Props tag.
 * pageID		: required. Page ID to set on this Pageview tag
 */
function cmCreateTechPropsTag(pageID, categoryID) {

	if(pageID == null) {
		pageID = cmGetDefaultPageID();
	}

	var cm=new _cm("tid", "6", "vn2", "e4.0");
	cm.pc="Y";
	cm.pi = pageID;
	cm.cg = categoryID;

	// if available, override the referrer with the frameset referrer
	if (parent.cm_ref != null) {
		cm.rf = parent.cm_ref;
		parent.cm_ref = document.URL;
	}

	// if parent had mmc variables and this is the first pageview, add mmc to this url
	if(parent.cm_set_mmc) {
		cm.ul = document.location.href + 
				((document.location.href.indexOf("?") < 0) ? "?" : "&") + 
				parent.cm_mmc_params; 
		parent.cm_ref = cm.ul;
		parent.cm_set_mmc = false;
	}

	// if the referral variable is in the page, overwrite the referring URL
	if (this.cmReferURL != null) {
		var referralString = this.cmReferURL;
		cm.rf = referralString;
		this.cmReferURL = null;
	}

	cm.addTP();
	cm.writeImg();
	
}
/*
 * Creates a Pageview tag with the given Page ID
 *
 * pageID	: required. Page ID to set on this Pageview tag
 * categoryID	: optional. Category ID to set on this Pageview tag
 * searchString	: optional. Internal search string enterred by user to reach
 *				  this page.
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreatePageviewTag(pageID, categoryID, searchString, searchResults) {
	if (pageID == null) {
		pageID = cmGetDefaultPageID();
	}

	var cm = new _cm("tid", "1", "vn2", "e4.0");
	cm.pi = pageID;
	if (searchString) {
		cm.se = searchString;
	}
	if (searchResults)
	{
		cm.sr = searchResults;
	}
	if (categoryID) {
		cm.cg = categoryID;
	}

	// if available, override the referrer with the frameset referrer
	if (parent.cm_ref != null) {
		cm.rf = parent.cm_ref;
		parent.cm_ref = document.URL;
	}

	// if parent had mmc variables and this is the first pageview, add mmc to this url
	if(parent.cm_set_mmc) {
		cm.ul = document.location.href + 
				((document.location.href.indexOf("?") < 0) ? "?" : "&") + 
				parent.cm_mmc_params; 
		parent.cm_ref = cm.ul;
		parent.cm_set_mmc = false;
	}

	// if the referral variable is in the page, overwrite the referring URL
	if (this.cmReferURL != null) {
		var referralString = this.cmReferURL;
		cm.rf = referralString;
		this.cmReferURL = null;
	}

	if (cm.tid == "1") {
		if (cI("cmTPSet") != 'Y') {
			cm.tid = "6";
			cm.addTP();
			document.cookie = "cmTPSet=Y";
		}
	}
	cm.writeImg();
}

/*
 * Creates a Pageview tag with the default value for Page ID. 
 * Format of Page ID is "x/y/z/MyPage.asp"
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreateDefaultPageviewTag(categoryID) {
	cmCreatePageviewTag(cmGetDefaultPageID(), categoryID, null);
}

function cmCreatePageElementTag(elementID, elementCategory, pageID, pageCategoryID, elementLocation) {
	var cm=new _cm("tid", "15", "vn2", "e4.0");
	
	cm.eid=elementID;
	cm.ecat=elementCategory;
	cm.pflg=0;
	cm.pid=pageID;
	cm.pcat=pageCategoryID;
	cm.eloc=elementLocation;
	
	cm.writeImg();
}

function cmCreateProductElementTag(elementID, elementCategory, productID, productCategoryID, elementLocation) {
	var cm=new _cm("tid", "15", "vn2", "e4.0");

	cm.eid=elementID;
	cm.ecat=elementCategory;
	cm.pflg=1;
	cm.pid=productID;	
	cm.pcat=productCategoryID;
	cm.eloc=elementLocation;
	
	cm.writeImg();
}

function cmCreateConversionEventTag(eventID, actionType, categoryID, points) {
	var cm = new _cm("tid", "14", "vn2", "e4.0");
	cm.cid = eventID;
	cm.cat = actionType;
	cm.ccid = categoryID;
	cm.cpt = points;
	cm.writeImg();
 }

/*
 * Creates a Productview Tag
 * Also creates a Pageview Tag by setting pc="Y"
 * Format of Page ID is "PRODUCT: <Product Name> (<Product ID>)"
 *
 * productID	: required. Product ID to set on this Productview tag
 * productName	: required. Product Name to set on this Productview tag
 * categoryID	: optional. Category ID to set on this Productview tag 
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreateProductviewTag(productID, productName, categoryID, pageID, basePrice,
								artistInfo, directorInfo, productNo, genre) {
	var cm = new _cm("tid", "5", "vn2", "e4.0");

	if (productName == null) {
		productName = "";
	}

	// if available, override the referrer with the frameset referrer
	if (parent.cm_ref != null) {
		cm.rf = parent.cm_ref;
		parent.cm_ref = document.URL;
	}

	// if parent had mmc variables and this is the first pageview, add mmc to this url
	if(parent.cm_set_mmc) {
		cm.ul = document.location.href + 
				((document.location.href.indexOf("?") < 0) ? "?" : "&") + 
				parent.cm_mmc_params; 
		parent.cm_ref = cm.ul;
		parent.cm_set_mmc = false;
	}

	// if the referral variable is in the page, overwrite the referring URL
	if (this.cmReferURL != null) {
		var referralString = this.cmReferURL;
		cm.rf = referralString;
		this.cmReferURL = null;
	}

	cm.pr = productID;
	cm.pm = productName;
	cm.cg = categoryID;

	cm.pc = "Y";
	cm.pi = pageID;
	cm.bp = basePrice;

	// was: cm.s1 = artistInfo;
	cm.pr1 = artistInfo;

	// was: cm.s2 = directorInfo;
	cm.pr2 = directorInfo;

	cm.pa1 = productNo;

	// added line number to associate with in custom table
	cm.li = 1;
	cm.ps1 = genre;

	cm.writeImg();
}

/*
 * Variables and Arrays to support Lineitem Aggregation
 */

var cmShopProducts = new Array();
var cmShopIds = new Array();
var cmShopCats = new Array();
var cmShopQtys = new Array();
var cmShopPrices = new Array();
var cmShopCounter = 0;
var cmShopOrderIds = new Array();
var cmShopCustomerIds = new Array();
var cmShopOrderPrices = new Array();
var cmShopProductNos = new Array();
var cmShopMemberID = new Array ();
var cmShopSKUs = "";

/* Internal, to support aggregation */
function cmGetProductIndex(id){
	var i =0;
	for (i=0; i<cmShopCounter; i++)
	{
		if (id==cmShopIds[i])
		{
			return i;
		}
	}
	return -1;
}

/*
 * Creates a Shop tag with Action 5 (Shopping Cart)
 *
 * productID	: required. Product ID to set on this Shop tag
 * quantity	: required. Quantity to set on this Shop tag
 * productPrice	: required. Price of one unit of this product
 * categoryID	: optional. Category to set on this Shop tag
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreateShopAction5Tag(productID, productName, productQuantity, productPrice, categoryID, productNo) {

	productID = productID.toUpperCase();

	var index = cmGetProductIndex(productID);
	if(index!=-1){
		var oldPrice = cmShopPrices[index];
		var oldQty = cmShopQtys[index];
		var newQty = oldQty + parseInt(productQuantity);
		var newPrice = (oldPrice*oldQty + parseInt(productQuantity)*parseFloat(productPrice))/(newQty);

		cmShopPrices[index] = newPrice;
		cmShopQtys[index] = newQty;
	} else {
		if (!categoryID) {
			categoryID = "";
		}

		cmShopProducts[cmShopCounter] = productName;
		cmShopIds[cmShopCounter] = productID;
		cmShopCats[cmShopCounter] = categoryID;
		cmShopQtys[cmShopCounter] = parseInt(productQuantity);
		cmShopPrices[cmShopCounter] = parseFloat(productPrice);
		cmShopProductNos[cmShopCounter] = productNo;
		cmShopCounter++;

	}

	cmShopSKUs = cmGetOSK();

}

/* render the aggregated cart lineitems with Shop 5 tags*/
function cmDisplayShop5s(){
	var i;
	for(i=0; i<cmShopCounter;i++){
		var cm = new _cm("tid", "4", "vn2", "e4.0");
		cm.at = "5";
		cm.pr = cmShopIds[i]; 
		cm.pm = cmShopProducts[i];
		cm.cg = cmShopCats[i];
		cm.qt = cmShopQtys[i] ;
		cm.bp = cmShopPrices[i];
		cm.pa1 = cmShopProductNos[i];
		cm.pc = "N";
		cm.writeImg();
	}
	cmShopSKUs = cmGetOSK();

	cmShopCounter=0;
}

/*
 * Creates a Shop tag with Action 9 (Order Receipt / Confirmed)
 *
 * productID	: required. Product ID to set on this Shop tag
 * productName	: required. Product Name to set on this Shop tag
 * quantity	: required. Quantity to set on this Shop tag
 * productPrice	: required. Price of one unit of this product
 * customerID	: required. ID of customer making the purchase
 * orderID	: required. ID of order this lineitem belongs to
 * orderTotal	: required. Total price of order this lineitem belongs to
 * categoryID	: optional. Category to set on this Shop tag
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreateShopAction9Tag(productID, productName, productQuantity,
				productPrice, customerID, orderID,
				orderTotal, categoryID, productNo, memberID) {

	productID = productID.toUpperCase();

	var index = cmGetProductIndex(productID);
	if(index!=-1){
		var oldPrice = cmShopPrices[index];
		var oldQty = cmShopQtys[index];
		var newQty = oldQty + parseInt(productQuantity);
		var newPrice = (oldPrice*oldQty + parseInt(productQuantity)*parseFloat(productPrice))/(newQty);

		cmShopPrices[index] = newPrice;
		cmShopQtys[index] = newQty;
	} else {
		if (!categoryID) {
			categoryID = "";
		}
		cmShopProducts[cmShopCounter] = productName;
		cmShopIds[cmShopCounter] = productID;			
		cmShopOrderIds[cmShopCounter] = orderID;
		cmShopOrderPrices[cmShopCounter] = orderTotal;
		cmShopCustomerIds[cmShopCounter] = customerID;
		cmShopCats[cmShopCounter] = categoryID;
		cmShopQtys[cmShopCounter] = parseInt(productQuantity);
		cmShopPrices[cmShopCounter] = parseFloat(productPrice);
		cmShopProductNos[cmShopCounter] = productNo;
		cmShopMemberID[cmShopCounter] = memberID;
		cmShopQtys[index] = newQty;
		cmShopCounter++;
	}
	cmShopSKUs = cmGetOSK();

}


/* render the aggregated order lineitems with Shop 9 tags*/
function cmDisplayShop9s(){
	var i;
	for(i=0; i<cmShopCounter;i++){
		var cm = new _cm("tid", "4", "vn2", "e4.0");
		cm.at = "9";
		cm.pr = cmShopIds[i]; 
		cm.pm = cmShopProducts[i];
		cm.cg = cmShopCats[i];
		cm.qt = cmShopQtys[i] ;
		cm.bp = cmShopPrices[i];
		cm.cd = cmShopCustomerIds[i];
		cm.on = cmShopOrderIds[i];
		cm.tr = cmShopOrderPrices[i];
		cm.pa1 = cmShopProductNos[i];

		// was: cm.s1 = cmShopMemberID[i];
		cm.sx1 = cmShopMemberID[i];

		cm.pc = "N";
		cm.writeImg();

	}
	cmShopSKUs = cmGetOSK();

	cmShopCounter=0;
}

/*
 * Creates an Order tag
 *
 * orderID			: required. Order ID of this order
 * orderTotal		: required. Total of this order (minus tax and shipping)
 * orderShipping	: required. Shipping charge for this order
 * customerID		: required. Customer ID that placed this order
 * customerCity		: optional. City of Customer that placed this order
 * customerState	: optional. State of Customer that placed this order
 * customerZIP		: optional. Zipcode of Customer that placed this order
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreateOrderTag(orderID, orderTotal, orderShipping, customerID, 
			  firstName, customerCity, customerState, customerZIP, country,
			  GR, orderKeywords, memberID) {
	
		var cm = new _cm("tid", "3", "vn2", "e4.0");
		cm.on = orderID;
		cm.tr = orderTotal;
		cm.osk = cmShopSKUs;
		cm.sg = orderShipping;
		cm.cd = customerID;
		cm.sa = customerState;
		cm.ct = customerCity;
		cm.zp = customerZIP;
		cm.fn = firstName;
		cm.cy = country;
		
		// was: cm.hs = GR;
		cm.or2 = GR;
		cm.osk = orderKeywords;

		// was: cm.s1 = memberID;
		cm.rg1 = memberID;
		cm.writeImg();
}

function cmGetOSK() {
	var i =0;
	var result = "";
	for (i=0; i<cmShopCounter; i++)
	{
		result += "|" + cmShopIds[i] + "|" + cmShopPrices[i] + "|" + cmShopQtys[i] + "|";
	}
	return result;
}

/*
 * Creates a Registration tag and/or a Newsletter tag
 *
 * customerID		: required for Registration. ID of Customer to register.
 * customerEmail	: required for Newsletters. Optional for Registration.
 * customerCity		: optional. City of Customer that placed this order
 * customerState	: optional. State of Customer that placed this order
 * customerZIP		: optional. Zipcode of Customer that placed this order
 * newsletterName	: required for Newsletters. The name of the Newsletter.
 * subscribe		: required for Newsletters. Either "Y" or "N"
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreateRegistrationTag(customerID, customerEmail, firstName, age, gender,
								customerCity, customerState, customerZIP, country, 
								memberID, offerCode, commitStatus, eprimary,
								pocNoc, deptKey, discount, newsletterName, subscribe, sessionTrackingCode) {
	var cm = new _cm("tid", "2", "vn2", "e4.0");
	cm.cd = customerID;
	cm.em = customerEmail;
	cm.sa = customerState;
	cm.ct = customerCity;
	cm.cy = country;
	cm.zp = customerZIP;
	cm.fn = firstName;
	cm.ag = age;
	cm.gd = gender;

	// was:
	// cm.bt = customerCity; -- above
	// cm.bs = customerState; -- above
	// cm.by = country; -- above
	// cm.s1 = memberID;
	// cm.s11 = offerCode;
	// cm.s12 = commitStatus;
	// cm.s13 = listeningPrefs;
	// cm.s14 = pocNoc;
	// cm.s15 = deptKey;
	// cm.s6 = discount;

	cm.rg1 = memberID;
	if (sessionTrackingCode){
		rg2=sessionTrackingCode;
	}
	cm.rg11 = offerCode;
	cm.rg12 = commitStatus;
	cm.rg13 = eprimary;
	cm.rg14 = pocNoc;
	cm.rg15 = deptKey;
	cm.rg6 = discount;

	if (newsletterName && subscribe) {
		cm.nl = newsletterName;
		cm.sd = subscribe;
	}
	
	cm.writeImg();
}

/* Creates an Error Tag
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreateErrorTag(pageID, categoryID) {
	var cm=new _cm("tid", "404", "vn2", "e4.0");  //DO NOT CHANGE THESE PARAMETERS
	
	// get the referrer from the frameset
	if (parent.cm_ref != null) {
		cm.rf = parent.cm_ref;
		parent.cm_ref = document.URL;
	}

	// if parent had mmc variables and this is the first pageview, add mmc to this url
	if(parent.cm_set_mmc) {
		cm.ul = document.location.href + 
				((document.location.href.indexOf("?") < 0) ? "?" : "&") + 
				parent.cm_mmc_params; 
		parent.cm_ref = cm.ul;
		parent.cm_set_mmc = false;
	}

	// if the referral variable is in the page, overwrite the referring URL
	if (this.cmReferURL != null) {
		var referralString = this.cmReferURL;
		cm.rf = referralString;
		this.cmReferURL = null;
	}

	cm.pc = "Y";
	cm.pi = pageID;
	if(cm.pi == null) {
		cm.pi = cmGetDefaultPageID();
	}
	cm.cg = categoryID;
	cm.writeImg();
}

// HELPER FUNCTIONS -----------------------------------------------------------
/* These functions are used by the tag-generating functions and/or may be used
 * in in general as convenience functions
 */

/*
 * Creates an acceptable default Page ID value to use for Pageview tags.
 * The default Page ID is based on the URL, and consists of the path and
 * filename (without the protocol, domain and query string).
 * 
 * example:
 * returns "x/y/MyPage.asp" for the URL http://www.mysite.com/x/y/MyPage.asp
 */
function cmGetDefaultPageID() { 
	var pageName = window.location.pathname; 

	// eliminates everything after "?" (for Opera browswers)
	var tempIndex1 = pageName.indexOf("?");
	if (tempIndex1 != -1) {
		pageName = pageName.substr(0, tempIndex1);
	}
	// eliminates everything after "#" (for Opera browswers)
	var tempIndex2 = pageName.indexOf("#");
	if (tempIndex2 != -1) {
		pageName = pageName.substr(0, tempIndex2);
	}
	// eliminates everything after ";"
	var tempIndex3 = pageName.indexOf(";");
	if (tempIndex3 != -1) {
		pageName = pageName.substr(0, tempIndex3);
	}

	var slashPos = pageName.lastIndexOf("/");
	if (slashPos == pageName.length - 1) {
		pageName = pageName + "default.asp"; /****************** SET TO DEFAULT DOC NAME */
	}

	while (pageName.indexOf("/") == 0) {
		pageName = pageName.substr(1,pageName.length);
	}

	return(pageName); 
} 

if (defaultNormalize == null) { var defaultNormalize = null; }

/* This normalization function takes a list of parameters and parses out
   all url parameters that ARE in that list.  This only handles the simple case of 
   basic url parameters in the query string.  */
function myNormalizeURL(url, isHref) {
    var newURL = url;
    if (isHref) {
		var ckURL=url.toLowerCase();
		if(ckURL.indexOf("/sa/")!=-1 && ckURL.indexOf("dvd-movies.")<0 ){
			var startString=ckURL.indexOf("/sa/");
			var endString=ckURL.indexOf("/ch/");
			var startURL=ckURL.substring(0,startString+3);
			var endURL=ckURL.substring(endString,ckURL.length);
			newURL=startURL+endURL;
		}
		else {
			if (newURL.toLowerCase().indexOf("-dvd")>-1 && newURL.toLowerCase().indexOf("-dvds")<0){
				newURL=newURL.substring(0,newURL.indexOf(".com")+4);
				return newURL;
			}
			else {
				
				if (newURL.toLowerCase().indexOf(";jsessionid=")>-1){
					var semiParams=newURL.split(";");
					var goodURL="";
					for (var k=0;k<semiParams.length;k++ ){
						if (semiParams[k].toLowerCase().indexOf("jsessionid=")<0){
							goodURL=goodURL+semiParams[k];
						}
					}
					if (goodURL.length>1){
						newURL=goodURL;
					}
				}
				
				var blackList = ["dt=", "pin=", "bak=", "mode=","jsessionid=","catid=","type=","cid=","club=","stype=","x=","y=","itemid=","positionid=","action=","upsell_code=","ptype=","catname=","pageid=","countas=","method=","index=","startat=","searchtype=","searchfor=","maxitems=","pagenavparamname=","indiv_page=","_flowExecutionKey=","_eventId=","itemID="];
				var paramString;
				var paramIndex = newURL.indexOf("?");
				var params;
				var keepParams = new Array();
				var goodParam;
			
				if (paramIndex > 0) {
					paramString = newURL.substring(paramIndex+1);
					newURL = newURL.substring(0, paramIndex);
					params = paramString.split("&");
				
					for(var i=0; i<params.length; i++) {
						goodParam = true;
						for(var j=0; j<blackList.length; j++) {
							if (params[i].indexOf(blackList[j]) == 0) {
								goodParam = false;
							}
						}
						if(goodParam == true) {
							keepParams[keepParams.length] = params[i];
						}
					}
					
					newURL += "?" + keepParams.join("&");
				
				}
			}
		 }
		if (defaultNormalize != null) {
			newURL = defaultNormalize(newURL, isHref);
		}
	}	
    return newURL;
}


// install normalization
if (document.cmTagCtl != null) {
    var func = "" + document.cmTagCtl.normalizeURL;
    if (func.indexOf('myNormalizeURL') == -1) {
        defaultNormalize = document.cmTagCtl.normalizeURL;
        document.cmTagCtl.normalizeURL = myNormalizeURL;
    }
}

//This function picks up cm_em or cm_lm from the URL and sends a registration tag

function cmCheckEmParameter() {
	var emParam
	if (cmIndexOfParameter("cm_em") != -1){
		var emParam="cm_em";
	}
	if (cmIndexOfParameter("cm_lm") != -1){
		var emParam="cm_lm";
	}
	if (emParam){
		var s = document.location.href;
		var begin = s.indexOf("cm_em");
		var end = s.indexOf("&", begin);
		if (end == -1) {
			end = s.length;
		}
		var middle = s.indexOf("=", begin);
	
		var emailAddress = s.substring(middle + 1, end);

		if (emailAddress.indexOf(":") != -1){
			var tempArray = emailAddress.split(":");
			emailAddress = tempArray[1];
		}

		cmCreateRegistrationTag(emailAddress,emailAddress);
	}
}
//Mandatory helper function, do not leave out!
function cmIndexOfParameter (parameter) {
	return document.location.href.toLowerCase().indexOf(parameter.toLowerCase());
}




//-->