// $Header: /export/atg_uep/UEP_ATG/modules/uepb2cstore/j2ee-apps/Web/Web.war/coremetrics/cmdatatagutils.js,v 1.5.66.1 2008/07/09 15:31:38 vtran Exp $

<!--
/*
 * cmdatatagutils.js
 *
 * Coremetrics Tag v3.1, 2/28/2002
 * COPYRIGHT 1999-2002 COREMETRICS, INC. 
 * ALL RIGHTS RESERVED. U.S.PATENT PENDING
 *
 * The following functions aid in the creation of Coremetrics data tags.
 *
 *
 * Update History
 * Date          Engineer      Changes Made
 * 28-FEB-06    Hutch White   	Added SearchResults parameter to	
 *                             	cmCreatePageviewTag
 * 080606	 	Hutch White   	Set for 1st Party
 * 062807		MOCHOA			Added Conversion Event & Element Tag
 * 072307		Hutch White		Add manual tagging
 * 010708		Hutch White		Add normalization for jsessionid
 * 031408		MOCHOA			Added searchString and searchResults to Productview Tag 
 * 031708		Hutch White		Add autotechprops and cmJv code
 * 053008		Eliot Towb		Fixed Shop5 and Shop9 functions to handle zero-value prices
*/

// TAG GENERATING FUNCTIONS ---------------------------------------------------

var cm_ClientID = "90018888";
var cm_TrackLink = "A";
var cm_TrackImpressions = "";

// code to determine javascript version
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";

/*
 * Calling this function points tags to the production database
 */
function cmSetProduction(){
	cm_HOST="www7.officemax.com/eluminate?"; 
}

function cmSetQuixtarMMC(){

	if (this.omx_quixtar != null) {
		var newURL = window.location.href + ((document.location.href.indexOf("?") < 0) ? "?" : "&") + "cm_mmc=" + omx_quixtar;
	}

	return newURL;
}

var rot13map; 

function rot13init() 
{ 
  var map = new Array(); 
  var s = "abcdefghijklmnopqrstuvwxyz"; 
   
  for (i=0; i<s.length; i++) 
    map[s.charAt(i)] = s.charAt((i+13)%26); 
  for (i=0; i<s.length; i++) 
    map[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase(); 
  return map; 
} 

function rot13(a) 
{ 
  if (!rot13map) 
    rot13map=rot13init(); 
  s = ""; 
  for (i=0; i<a.length; i++) 
    { 
      var b = a.charAt(i); 

      s += (b>='A' && b<='Z' || b>='a' && b<='z' ? rot13map[b] : b); 
    } 
  return s; 
} 

function cmIndexOfParameter (parameter) {
	return document.URL.indexOf(parameter);
}

if (cmIndexOfParameter("cm_lm") != -1){
	
	var s = document.URL;
	var begin = s.indexOf("cm_lm");
	var end = s.indexOf("&", begin);
	if (end == -1) {
		end = s.length;
	}
	var middle = s.indexOf("=", begin);
	var encodedEmail = s.substring(middle + 1, end);

	var unencodedEmail = rot13(encodedEmail);

	cmSetProduction();
	cmCreateRegistrationTag(unencodedEmail,unencodedEmail);
}


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();

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.ul=document.URL;	
	cm.writeImg();
}
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.ul=document.URL;	
	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.ul=document.URL;	
	cm.writeImg();
}

function cmCreateManualImpressionTag(pageID, trackSP, trackRE) {
		var cm = new _cm("tid","9","vn2","e4.0");
		cm.pi = pageID;
		cm.cm_sp = trackSP;
		cm.cm_re = trackRE;
		cm.st = cm_ClientTS;
		cm.ul=document.URL;		
		cm.writeImg();
}

function cmCreateManualLinkClickTag(href,name,pageID) {	
	if (cmCreateLinkTag == null && cM != null) {
		var cmCreateLinkTag = cM;
	}
	if (cmCreateLinkTag != null) {		
		var dt = new Date();
		cmLnkT3 = dt.getTime();
		cmCreateLinkTag(cm_ClientTS, cmLnkT3, name, href, false, pageID);
	}
}

/* manual PageviewTag for off site page tagging.  Allows client to supply URL and Referring URL
*/
function cmCreateManualPageviewTag(pageID, categoryID,DestinationURL,ReferringURL,searchTerm) {
	var cm = new _cm("tid","1","vn2","e4.0");
	cm.pi = pageID;
	cm.cg = categoryID;
	cm.ul = DestinationURL;
	cm.rf = ReferringURL;
	if (searchTerm) {
		cm.se=searchTerm;
	}
	cm.writeImg();
}

/*
 * 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 available, override the referrer with the frameset referrer
		if (parent.cm_ref != null) {
			cm.rf = parent.cm_ref;
			parent.cm_ref = document.URL;
		}

		if (document.location.href.indexOf("omx_quixtar") == -1)
			{
			if (this.omx_quixtar != null) {
					cm.ul = window.location.href + ((document.location.href.indexOf("?") < 0) ? "?" : "&") + "cm_mmc=" + omx_quixtar;
			}
		}
		cm.ul=document.URL;		
		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,searchResults) {
	if (pageID == null) {
		pageID = getDefaultPageID();
	}

	var cm = new _cm("tid", "1", "vn2", "e3.1");
	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 (document.location.href.indexOf("omx_quixtar") == -1)
	{
		if (this.omx_quixtar != null) {
				cm.ul = window.location.href + ((document.location.href.indexOf("?") < 0) ? "?" : "&") + "cm_mmc=" + omx_quixtar;
			}
	}
	cm.ul=document.URL;	
    if (cm.tid == "1") {
		if (cI("cmTPSet") != 'Y') {
			cm.tid = "6";
			cm.pc = 'Y';
			cm.addTP();
			document.cookie = "cmTPSet=Y; path=/";
		}
	}
	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
 * pageName     : required. Name of page
 * 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, pageName, categoryID, searchString, searchResults) {
	var cm = new _cm("tid", "5", "vn2", "e3.1");

	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;
	}

	cm.pr = productID;
	cm.pm = productName;
	cm.cg = categoryID;
	cm.se= searchString;
	cm.sr = searchResults;

	cm.pc = "Y";
	cm.pi = pageName;

	if (document.location.href.indexOf("omx_quixtar") == -1)
	{
		if (this.omx_quixtar != null) {
				cm.ul = window.location.href + ((document.location.href.indexOf("?") < 0) ? "?" : "&") + "cm_mmc=" + omx_quixtar;
			}
	}
	cm.ul=document.URL;
	cm.writeImg();
}

/* 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) {
	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);
		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];
		if (cm.bp == 0) { cm.bp = "0"; }
		cm.pc = "N";
		cm.ul=document.URL;		
		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);
	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];
		if (cm.bp == 0) { cm.bp = "0"; }
		cm.cd = cmShopCustomerIds[i];
		cm.on = cmShopOrderIds[i];
		cm.tr = cmShopOrderPrices[i];

		cm.pc = "N";
		cm.ul=document.URL;		
		cm.writeImg();
	}
	cmShopCounter=0;
}

function getOSK() {
	var i =0;
	var result = "";
	for (i=0; i<cmShopCounter; i++)
	{
		result += cmShopSKUs[i];
	}
	return result;
}


/*
 * 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) {
	
	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.ul=document.URL;
	cm.writeImg();
	
}


/*
 * 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
 * maxPerksNum		: customer's MaxPerks number
 * 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, customerCity,
				customerState, customerZIP, maxPerksNum, newsletterName, 
				subscribe, edid) {
	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.rg1 = maxPerksNum;
	
	if(maxPerksNum) {
	  cm.rg11 = "Y";
	}
	
	if(edid) {
	  cm.rg2 = edid;
	}

	if (newsletterName && subscribe) {
		cm.nl = newsletterName;
		cm.sd = subscribe;
	}
	cm.ul=document.URL;	
	cm.writeImg();
}


/*
 * Creates a Newsletter tag
 *
 * customerEmail	: Required. Email address for customer.
 * newsletterName	: Required. The name of the Newsletter.
 * subscribe		: Required. Either "Y" or "N"
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreateNewsletterTag(customerEmail, newsletterName, subscribe) {
	var cm = new _cm("tid", "2", "vn2", "e3.1");

	cm.em = customerEmail;
	cm.nl = newsletterName;
	cm.sd = subscribe;
	cm.ul=document.URL;	
	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();
	cm.ul=document.URL;	
	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; 

	// 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);
	}

	while (pageName.indexOf("/") == 0) {
		pageName = pageName.substr(1,pageName.length);
	}

	// eliminates "max/solutions/"
	var replaceString = "max/solutions/";
	var tempIndex4 = pageName.indexOf(replaceString);
	if(tempIndex4 == 0) {
	   	pageName = pageName.substr(replaceString.length, pageName.length);
	}

	var slashPos = pageName.lastIndexOf("/");
	if (slashPos == pageName.length - 1) {
		pageName = pageName + "home.jsp"; /****************** SET TO DEFAULT DOC NAME */
	}

	return(pageName); 
} 

if (defaultNormalize == null) { var defaultNormalize = null; }

function myNormalizeURL(url, isHref) {

    var newURL = url;
	if ((newURL.indexOf(";jsessionid=")<newURL.indexOf("?") && newURL.indexOf(";jsessionid=")>1)|| (newURL.indexOf(";jsessionid=")>-1 && newURL.indexOf("?")==-1)){
		startArg=newURL.indexOf(";jsessionid=");
		if (newURL.indexOf("?")>-1){
			newURL=newURL.substring(0,startArg)+newURL.substring(newURL.indexOf("?"));
		} else {
			newURL=newURL.substring(0,startArg);
		}
	}

    var blackList = ["bvc=", "BV_UseBVCookie=", "BV_SessionID=", "BV_EngineID=", "userId=", "email=", "question=", "lName=", "fName=", "autoLogin=", "response=", "maxMail=", "pwdOld=", "onumIndex=", "jsessionid="];
    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].toLowerCase().indexOf(blackList[j].toLowerCase()) == 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;
    }
}

//-->
