<!--
/*
 * cmdatatagutils.js
 *
 * Coremetrics Tag v3.1, 2/28/2002
 * COPYRIGHT 1999-2002 COREMETRICS, INC.
 * ALL RIGHTS RESERVED. U.S.PATENT PENDING
 *
 * Change Log:
 * Added normalization function to strip out Crutchfield Session ID
 * Added code to getDefaultPageId function to strip out Crutchfield Session ID
 * Added category ID to Shop9 tag function to aggregate by Product ID AND Category ID
 * Added optional Virtual Category ID to ProductView tag.
 * 04/28/04 - achen - Added optional searchString to ProductView tag.
 * The following functions aid in the creation of Coremetrics data tags.
 * Date			Modifier		Desc
 *3-29-06		Hutch White		Added code to tags that send page view info
 *								if document.url contains "i-r", take next three
 *								characters and create an mmc argument and append 
 *								document URL and load into cm.ul
 *6-12-06		Hutch White		Added code to tags that create mmc parameter
 *								from pageview / techprops parameter
 */

//
// The following code installs a custom normalization function.
// You must implement this function for link tracking to function properly!
//

// Note: it is necessary to conditionally declare the var
if (defaultNormalize == null) { var defaultNormalize = null; }

function myNormalizeURL(url, isHref) {
    var newURL = url;
    var blackList = ["LP=", "TypeCH=", "VB=", "lp=", "typech=", "vb="];
    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("&");
    }


	// eliminates Crutchfield Session ID
	var tempIndex4 = newURL.indexOf("S-");
	if (tempIndex4 != -1) {
		var tempIndex5 = tempIndex4 + 14;
		newURL = newURL.substr(0,tempIndex4) + newURL.substr(tempIndex5, newURL.length);
	}


    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;
    }
}


// TAG GENERATING FUNCTIONS ---------------------------------------------------


/*
 * Calling this function points tags to the production database
 */
function cmSetProduction(){
	cm_HOST="stereos.crutchfield.com/eluminate?";
}

/*
 * Creates a Tech Props tag.
 * pageID		: required. Page ID to set on this Pageview tag

function cmCreateTechPropsTag(pageID) {
	if (pageID) {
		var cm=new _cm("tid", "6", "vn2", "e3.1");
		cm.pc="Y";
		cm.pi = pageID;
		if (parent.cm_ref != null) {
			cm.rf = parent.cm_ref;
			parent.cm_ref = document.URL;
		}
		cm.addTP();
		cm.writeImg();
	}
}
*/

function cmCreateTechPropsTag(pageID, categoryID,MMCString) {
	if (pageID) {
		var cm=new _cm("tid", "6", "vn2", "e3.1");
		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;
		}
/* This code looks for instances of "i-r" in the destination url and if it is
 * there, appends an MMC tag consisting of the three characters after "i-r" in
 * the url.
 */
		var ckURL=document.URL;
		ckURL=ckURL.toUpperCase();
		if(ckURL.indexOf("I-R")!=-1){
			var ckVen=ckURL.substr(ckURL.indexOf("I-R")+3,3);
			if(ckURL.indexOf("?")!=-1){
				var mmc_string=ckURL+"&cm_mmc="+ckVen+"-_-na-_-na-_-na";}
			else {
				var mmc_string=ckURL+"?cm_mmc="+ckVen+"-_-na-_-na-_-na";}
			cm.ul=mmc_string;
		}

		if (MMCString){
			if (cm.ul){
				ckURL=cm.ul;
			}
			else{
				ckURL=document.URL
			}
			if (ckURL.indexOf("?")>0){
				cm.ul=ckURL+"&"+MMCString;
			}
			else {
				cm.ul=ckURL+"?"+MMCString;
			}
		}	

		cm.addTP();

		cm.writeImg();
	}
}

/*
 * Creates a Pageview tag with the given Page ID
 *
 * pageID		: required. Page ID to set on this Pageview tag
 * searchString	: optional. Internal search string enterred by user to reach
 *				  this page.
 * categoryID	: optional. Category ID to set on this Pageview tag
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
 
function cmCreatePageviewTag(pageID, searchString, categoryID, numberOfSearchResults,MMCString) {
	if (pageID == null) {
		pageID = getDefaultPageID();
	}

	var cm = new _cm("tid", "1", "vn2", "e3.1");
	cm.pi = pageID;
	if (searchString) {
		cm.se = searchString;
	}
	if (categoryID) {
		cm.cg = categoryID;
	}
	if (numberOfSearchResults)
	{
		cm.sr = numberOfSearchResults;
	}

	// if available, override the referrer with the frameset referrer
	if (parent.cm_ref != null) {
		cm.rf = parent.cm_ref;
		parent.cm_ref = document.URL;
	}

/* This code looks for instances of "i-r" in the destination url and if it is
 * there, appends an MMC tag consisting of the three characters after "i-r" in
 * the url.
 */
		var ckURL=document.URL;
		if(ckURL.indexOf("i-r")!=-1){
			var ckVen=ckURL.substr(ckURL.indexOf("i-r")+3,3);
			if(ckURL.indexOf("?")!=-1){
				var mmc_string=ckURL+"&cm_mmc="+ckVen+"-_-na-_-na-_-na";}
			else {
				var mmc_string=ckURL+"?cm_mmc="+ckVen+"-_-na-_-na-_-na";}
			cm.ul=mmc_string;
		}

		if (MMCString){
			if (cm.ul){
				ckURL=cm.ul;
			}
			else{
				ckURL=document.URL
			}
			if (ckURL.indexOf("?")>0){
				cm.ul=ckURL+"&"+MMCString;
			}
			else {
				cm.ul=ckURL+"?"+MMCString;
			}
		}	

	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() {
	cmCreatePageviewTag(getDefaultPageID(), null, null);
}

/*
 * 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
 * virtualCategoryID : optional. Virtual Category ID to set on this Productview tag
 * searchString : optional. Pass this in if a user gets to a product view directly from typing in a search term (i.e. no search results page)
 * numberOfSearchResults : optional.
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreateProductviewTag(productID, productName, categoryID, virtualCategoryID, searchString, numberOfSearchResults) {
	var cm = new _cm("tid", "5", "vn2", "e3.1");

	if (productName == null) {
		productName = "";
	}

	if (searchString) {
		cm.se = searchString;
	}


	// if available, override the referrer with the frameset referrer
	if (parent.cm_ref != null) {
		cm.rf = parent.cm_ref;
		parent.cm_ref = document.URL;
	}

	cm.pr = productID;
	cm.pm = productName;
	cm.cg = categoryID;
	cm.pr1 = virtualCategoryID;

	cm.pc = "Y";
	cm.pi = "PRODUCT: " + productName + " (" + productID + ")";

	if (numberOfSearchResults)
	{
		cm.sr = numberOfSearchResults;
	}

	/* This code looks for instances of "i-r" in the destination url and if it is
 * there, appends an MMC tag consisting of the three characters after "i-r" in
 * the url.
 */
		var ckURL=document.URL;
		if(ckURL.indexOf("i-r")!=-1){
			var ckVen=ckURL.substr(ckURL.indexOf("i-r")+3,3);
			if(ckURL.indexOf("?")!=-1){
				var mmc_string=ckURL+"&cm_mmc="+ckVen+"-_-na-_-na-_-na";}
			else {
				var mmc_string=ckURL+"?cm_mmc="+ckVen+"-_-na-_-na-_-na";}
			cm.ul=mmc_string;
		}
			
	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 cmShopSKUs = new Array();
var cmShopCounter = 0;
var cmShopOrderIds = new Array();
var cmShopCustomerIds = new Array();
var cmShopOrderPrices = new Array();

/* Internal, to support aggregation */
function cmGetProductIndex(id,catid){
	var i =0;
	for (i=0; i<cmShopCounter; i++)
	{
		if (id==cmShopIds[i] && catid==cmShopCats[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) {
	var index = cmGetProductIndex(productID,categoryID);
	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);
		cmShopCounter++;
	}
}

/* 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", "e3.1");
		cm.at = "5";
		cm.pr = cmShopIds[i];
		cm.pm = cmShopProducts[i];
		cm.cg = cmShopCats[i];
		cm.qt = cmShopQtys[i] ;
		cm.bp = cmShopPrices[i];
		cm.pc = "N";
		cm.writeImg();
	}
	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) {
	var index = cmGetProductIndex(productID,categoryID);
	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;
		cmShopSKUs[index] = "|" + productID + "|" + newPrice + "|" + 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);
		cmShopSKUs[cmShopCounter] = "|" + productID + "|" + productPrice + "|" + productQuantity + "|";
		cmShopCounter++;
	}
}


/* 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", "e3.1");
		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.pc = "N";
		cm.writeImg();
	}
	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,
			  customerCity, customerState, customerZIP) {
	if (orderID && orderTotal && orderShipping && customerID) {
		var cm = new _cm("tid", "3", "vn2", "e3.1");
		cm.on = orderID;
		cm.tr = orderTotal;
		cm.osk = getOSK();
		cm.sg = orderShipping;
		cm.cd = customerID;
		cm.sa = customerState;
		cm.ct = customerCity;
		cm.zp = customerZIP;

		cm.writeImg();
	}
}

function getOSK() {
	var i =0;
	var result = "";
	for (i=0; i<cmShopCounter; i++)
	{
		result += cmShopSKUs[i];
	}
	return result;
}

/*
 * Creates a Registration tag
 *
 * customerID		: required for Registration. ID of Customer to register.
 * customerEmail	: optional for registration, required for email Subscription.
 * 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
 * catalogSubscribe	: optional. Either "Y" or "N"
 * emailSubscribe	: optional. Either "Y" or "N"
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreateRegistrationTag(customerID, customerEmail, customerCity,
				customerState, customerZIP, catalogSubscribe, emailSubscribe) {
	var cm = new _cm("tid", "2", "vn2", "e3.1");
	cm.cd = customerID;
	cm.em = customerEmail;
	cm.sa = customerState;
	cm.ct = customerCity;
	cm.zp = customerZIP;
	cm.rg11 = catalogSubscribe;
	cm.rg12 = emailSubscribe;

	cm.writeImg();
}

/* Creates an Error Tag
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreateErrorTag() {
	var cm=new _cm("tid", "404", "vn2", "e3.1");  //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;
	}

	cm.pc = "Y";
	cm.pi = getDefaultPageID();

	/* This code looks for instances of "i-r" in the destination url and if it is
 * there, appends an MMC tag consisting of the three characters after "i-r" in
 * the url.
 */
		var ckURL=document.URL;
		if(ckURL.indexOf("i-r")!=-1){
			var ckVen=ckURL.substr(ckURL.indexOf("i-r")+3,3);
			if(ckURL.indexOf("?")!=-1){
				var mmc_string=ckURL+"&cm_mmc="+ckVen+"-_-na-_-na-_-na";}
			else {
				var mmc_string=ckURL+"?cm_mmc="+ckVen+"-_-na-_-na-_-na";}
			cm.ul=mmc_string;
		}
			
	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 getDefaultPageID() {
	var pageName = window.location.pathname;

//alert(window.location.href);

	// 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);
	}

	// eliminates Crutchfield Session ID
	var tempIndex4 = pageName.indexOf("S-");
	if (tempIndex4 != -1) {
		var tempIndex5 = tempIndex4 + 14;
		pageName = pageName.substr(0,tempIndex4) + pageName.substr(tempIndex5, pageName.length);
	}

	var slashPos = pageName.lastIndexOf("/");
	if (slashPos == pageName.length - 1) {
		pageName = pageName + "index.html"; /****************** SET TO DEFAULT DOC NAME */
	}

	while (pageName.indexOf("/") == 0) {
		pageName = pageName.substr(1,pageName.length);
	}

	return(pageName);
}
//-->