<!--
/*
 * cmdatatagutils.js v4.0, 4/18/2005
 *
 * Date			Imp. Eng.			Desc
 * 6/29/06		Hutch White			Add normalPrice() function to remove comma and $ from shop 
 *									action tags price and order tag order total
 */

function normalPrice(price) {

    var pattern = /,/gi;
    var new_price = price;
    new_price = new_price.replace(pattern, "");

    pattern = /\$/gi;
    new_price = new_price.replace(pattern, "");

	return new_price;
}

function cmSetProduction(){
	cm_HOST="www3.macys.com/eluminate?"; 
}

function cmCreateTechPropsTag(pageID, categoryID, cm_ven, cm_cat, cm_pla, cm_ite, linkShareID, custID, refURL) {

	if(pageID == null) {
		pageID = cmGetDefaultPageID();
	}

	var cm=new _cm("tid", "6", "vn2", "e4.0");
	cm.pc="Y";
	cm.pi = pageID;
	cm.cg = categoryID;
	
	if (custID) {
		cm.pv1 = custID;
	}
	if (refURL) {cm.rf = refURL;}
	// 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;
	}
	// checks to see if document.location.href has parameters or ?, then adds mmc to url
	if (cm_ven != null && cm_ite != null) {
		if (cm_ven != "" && cm_ite != "") {
			cm.ul = document.location.href + ((document.location.href.indexOf("?") < 0) ? "?" : "&") + "cm_ven=" + cm_ven;
			if (cm_cat != null) {
			cm.ul = cm.ul + "&" + "cm_cat=" + cm_cat;
			}
			if (cm_pla != null) {
			cm.ul = cm.ul + "&" + "cm_pla=" + cm_pla;
			}
			cm.ul = cm.ul + "&" + "cm_ite=" + cm_ite;
		}
	}
	if (linkShareID) {cm.pv2 = linkShareID;}
	cm.addTP();
	cm.writeImg();
}

function cmCreatePageviewTag(pageID, categoryID, searchString, searchResults, cm_ven, cm_cat, cm_pla, cm_ite, linkShareID, custID, refURL) {
	if (pageID == null) {
		pageID = cmGetDefaultPageID();
	}

	var cm = new _cm("tid", "1", "vn2", "e4.0");
	cm.pi = pageID;
	
	if (custID) {
		cm.pv1 = custID;
	}
	if (searchString) {
		cm.se = searchString;
	}
	cm.sr = searchResults;
	if (categoryID) {
		cm.cg = categoryID;
	}
	if (refURL) {cm.rf = refURL;}
	// 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;
	}
	// checks to see if document.location.href has parameters or ?, then adds mmc to url
	if (cm_ven != null && cm_ite != null) {
		if (cm_ven != "" && cm_ite != "") {
			cm.ul = document.location.href + ((document.location.href.indexOf("?") < 0) ? "?" : "&") + "cm_ven=" + cm_ven;
			if (cm_cat != null) {
			cm.ul = cm.ul + "&" + "cm_cat=" + cm_cat;
			}
			if (cm_pla != null) {
			cm.ul = cm.ul + "&" + "cm_pla=" + cm_pla;
			}
			cm.ul = cm.ul + "&" + "cm_ite=" + cm_ite;
		}
	}
	if (linkShareID) {cm.pv2 = linkShareID;}
	cm.writeImg();
}

function cmCreateDefaultPageviewTag(categoryID) {
	cmCreatePageviewTag(cmGetDefaultPageID(), categoryID, null);
}

function cmCreateProductviewTag(productID, productName, categoryID, cm_ven, cm_cat, cm_pla, cm_ite, 
				linkShareID, custID, refURL, cmCrossSell) {
	var cm = new _cm("tid", "5", "vn2", "e4.0");
	
	if (productName == null) {
		productName = "";
	}
	if (custID) {
		cm.pv1 = custID;
	}
	// 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 (refURL) {cm.rf = refURL;}
	// 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;
	}

	cm.pr = productID;
	cm.pm = productName;
	cm.cg = categoryID;

	cm.pc = "Y";
	cm.pi = "PRODUCT: " + productName + " (" + productID + ")";
	// checks to see if document.location.href has parameters or ?, then adds mmc to url
	if (cm_ven != null && cm_ite != null) {
		if (cm_ven != "" && cm_ite != "") {
			cm.ul = document.location.href + ((document.location.href.indexOf("?") < 0) ? "?" : "&") + "cm_ven=" + cm_ven;
			if (cm_cat != null) {
			cm.ul = cm.ul + "&" + "cm_cat=" + cm_cat;
			}
			if (cm_pla != null) {
			cm.ul = cm.ul + "&" + "cm_pla=" + cm_pla;
			}
			cm.ul = cm.ul + "&" + "cm_ite=" + cm_ite;
		}
	}
	if (linkShareID) {cm.pv2 = linkShareID;}
	if (cmCrossSell) {cm.pv13 = cmCrossSell;}
	cm.writeImg();
}

function cmCreateMasterMemberTag(MasterProductID, MasterProductName, MasterCatID, IsMaster, custID) {
	var cm=new _cm("tid", "7", "vn2", "e4.0");  //DO NOT CHANGE THESE PARAMETERS
	
	cm.pc = "N";
	cm.li = "55"
	if (MasterProductID) {cm.ps2 = MasterProductID;}
	if (MasterProductName) {cm.ps3 = MasterProductName;}
	if (MasterCatID) {cm.ps4 = MasterCatID;}
	if (IsMaster) {cm.ps5 = IsMaster;}
	if (custID) {cm.ps6 = custID;}

	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();
//THESE ARE WACK VARIABLES THAT DO NOT WORK, WE FIXED IT! JONATHAN/PATRICK 081005
var mMasterID = new Array();
var mMasterName = new Array();
var crossSellParam = new Array();
var mMasterCatID = new Array();
var mIsMasterFlag = 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;
}

function cmCreateShopAction5Tag(productID, productName, productQuantity, productPrice, categoryID, 
				MasterProductID, MasterProductName, MasterCatID, IsMaster, cmCrossSell) {

	productID = productID.toUpperCase();
	productPrice=normalPrice(productPrice);

	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);
		mMasterID[cmShopCounter] = MasterProductID;
		mMasterName[cmShopCounter] = MasterProductName;
		crossSellParam[cmShopCounter] = cmCrossSell;
		mMasterCatID[cmShopCounter] = MasterCatID;
		mIsMasterFlag[cmShopCounter] = IsMaster;
		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.sx1 = mMasterID[i];
		cm.sx2 = mMasterName[i];
		cm.sx3 = crossSellParam[i];
		cm.sx4 = mMasterCatID[i];
		cm.sx5 = mIsMasterFlag[i];
		cm.pc = "N";
		cm.writeImg();
	}
	cmShopSKUs = cmGetOSK();
	cmShopCounter=0;
}

function cmCreateShopAction9Tag(productID, productName, productQuantity,
				productPrice, customerID, orderID,
				orderTotal, categoryID, MasterProductID, MasterProductName, 
				MasterCatID, IsMaster, cmCrossSell) {

	productID = productID.toUpperCase();
	productPrice=normalPrice(productPrice);

	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);
		cmShopQtys[index] = newQty;
		mMasterID[cmShopCounter] = MasterProductID;
		mMasterName[cmShopCounter] = MasterProductName;
		crossSellParam[cmShopCounter] = cmCrossSell;
		mMasterCatID[cmShopCounter] = MasterCatID;
		mIsMasterFlag[cmShopCounter] = IsMaster;
		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.sx1 = mMasterID[i];
		cm.sx2 = mMasterName[i];
		cm.sx3 = crossSellParam[i];
		cm.sx4 = mMasterCatID[i];
		cm.sx5 = mIsMasterFlag[i];
		cm.pc = "N";
		cm.writeImg();

	}
	cmShopSKUs = cmGetOSK();

	cmShopCounter=0;
}

function cmCreateOrderTag(orderID, orderTotal, orderShipping, customerID, 
			  customerCity, customerState, customerZIP, custID) {

		orderTotal=normalPrice(orderTotal);

		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;
	if (custID) {
		cm.or1 = custID;
	}
		cm.writeImg();
}

function cmGetOSK() {
	var i =0;
	var result = "";
	for (i=0; i<cmShopCounter; i++)
	{
		result += "|" + cmShopIds[i] + "|" + cmShopPrices[i] + "|" + cmShopQtys[i] + "|";
	}
	return result;
}

function cmCreateRegistrationTag(customerID, customerEmail, customerCity,
				customerState, customerZIP, customerGender, newsletterName, 
				subscribe) {
	var cm = new _cm("tid", "2", "vn2", "e4.0");
	cm.cd = customerID;
	cm.em = customerEmail;
	cm.sa = customerState;
	cm.ct = customerCity;
	cm.zp = customerZIP;
	cm.gd = customerGender;

	if (newsletterName && subscribe) {
		cm.nl = newsletterName;
		cm.sd = subscribe;
	}
	
	cm.writeImg();
}

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;
	}

	cm.pc = "Y";
	if(pageID == null) {
		cm.pi = cmGetDefaultPageID();
	} else {
		cm.pi = pageID;
	}
	cm.cg = categoryID;
	cm.writeImg();
}

function cmCreateUserErrorTag(pageID, categoryID, mMsgCode, mMsgType, mMsgClass, mMsgDesc, mServerName, mCloneName) {
	var cm=new _cm("tid", "7", "vn2", "e4.0");  //DO NOT CHANGE THESE PARAMETERS
	
	cm.pc = "N";
	cm.li = "52"
	if (pageID) {cm.ps2 = pageID;}
	if (categoryID) {cm.ps3 = categoryID;}
	if (mMsgCode) {cm.ps4 = mMsgCode;}
	if (mMsgType) {cm.ps5 = mMsgType;}
	if (mMsgClass) {cm.ps6 = mMsgClass;}
	if (mMsgDesc) {cm.ps7 = mMsgDesc;}
	if (mServerName) {cm.ps8 = mServerName;}
	if (mCloneName) {cm.ps9 = mCloneName;}
	cm.writeImg();
}

function cmCreateDelayedShopTag(productID, productName, productQuantity, productPrice, categoryID, cmReason, cmShipDays) {
	var cm=new _cm("tid", "7", "vn2", "e4.0");  //DO NOT CHANGE THESE PARAMETERS
	
	cm.pc = "N";
	cm.li = "50"
	if (productID) {cm.ps2 = productID;}
	if (productName) {cm.ps3 = productName;}
	if (productQuantity) {cm.ps4 = productQuantity;}
	if (productPrice) {cm.ps5 = productPrice;}
	if (categoryID) {cm.ps6 = categoryID;}
	if (cmReason) {cm.ps7 = cmReason;}
	if (cmShipDays) {cm.ps8 = cmShipDays;}

	cm.writeImg();
}

function cmCreateDivisionTag(cmDivisionID, cmZipCode) {
	var cm=new _cm("tid", "7", "vn2", "e4.0");  //DO NOT CHANGE THESE PARAMETERS
	
	cm.pc = "N";
	cm.li = "51"
	if (cmDivisionID) {cm.ps2 = cmDivisionID;}
	if (cmZipCode) {cm.ps3 = cmZipCode;}
	cm.writeImg();
}

// HELPER FUNCTIONS -----------------------------------------------------------
/* These functions are used by the tag-generating functions and/or may be used
 * in in general as convenience functions
 */

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; }

function myNormalizeURL(url, isHref) {
    var newURL = url;
    if (isHref) {
	    var blackList = ["LinkShareID=", "LinkshareID=", "PartnerID=", "BannerID="];
	    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;
    }
}


//-->
