<!--
/*
 * cmdatatagutils.js
 *
 * 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.
 *
 * MODIFICATION HISTORY:
 * DATE        PERSON      NOTES
 * ----------- ----------- ----------------------------------------------------
 * 14-OCT-2004 BILL DELONG Added additional parameters to the Registration Tag.
 *                         Temp fix to force "0" to "0.001" for all price values
 *                         to avoid null price passed to DB via eluminate.js.
 *                         Decrypt CustID per specs.
 * 030107		MOCHOA		Added First Party Sub-Domain
 */

// TAG GENERATING FUNCTIONS ---------------------------------------------------


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 emailAddress = s.substring(middle + 1, end);

	if (emailAddress.indexOf(":") != -1){
		var tempArray = emailAddress.split(":");
		emailAddress = tempArray[1];
	}

	cmSetProduction();
	cmCreateRegistrationTag(emailAddress,emailAddress);
}


/*
 * Calling this function points tags to the production database
 */
function cmSetProduction(){
	cm_HOST="c.esteelauder.com/eluminate?"; 
}

/*
 * Creates a Tech Props tag.
 * pageID		: required. Page ID to set on this Pageview tag
 */
function cmCreateTechPropsTag(pageID, categoryID) {
	if (pageID) {
		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;
		}
		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", "e4.0");
	cm.pi = pageID;
	if (searchString) {
		cm.se = searchString;
	}
	if (categoryID) {
		cm.cg = categoryID;
	}
    if (searchResults)
    {
		cm.sr = searchResults;
    }

	// 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.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 
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreateProductviewTag(productID, productName, categoryID) {
	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;
	}

	cm.pr = productID;
	cm.pm = productName;
	cm.cg = categoryID;

	cm.pc = "N";
	cm.pi = "PRODUCT: " + productName + " (" + productID + ")";

	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();

var cmSKUCounter      = 0;
var cmServerSKUCount  = 0;
var cmSKUIds          = new Array();
var cmSKUProducts     = new Array();
var cmSKUProductIDs   = new Array();
var cmSKUCats         = new Array();
var cmSKUQtys         = new Array();
var cmSKUPrices       = new Array();
var cmSKUOrderIds     = new Array();

/* Internal, to support Product aggregation */
function cmGetProductIndex(id){
	var i =0;
	for (i=0; i<cmShopCounter; i++)
	{
		if (id==cmShopIds[i])
		{
			return i;
		}
	}
	return -1;
}

/* Internal, to support SKU aggregation */
function cmGetSKUIndex(id){
	var i =0;
	for (i=0; i<cmSKUCounter; i++)
	{
		if (id==cmSKUIds[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
							   , sku
							   , skuName
							   , productQuantity
							   , productPrice
							   , categoryID
							   ) {
	// Product Aggregation
	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++;
	}

    // SKU Aggregation
	var index = cmGetSKUIndex(sku);
//	cmServerSKUCount = counter;
	if(index!=-1){
		var oldPrice = cmSKUPrices[index];
		var oldQty = cmSKUQtys[index];
		var newQty = oldQty + parseInt(productQuantity);
		var newPrice = (oldPrice*oldQty + parseInt(productQuantity)*parseFloat(productPrice))/(newQty);

		cmSKUPrices[index] = newPrice;
		cmSKUQtys[index] = newQty;

	} else {
		if (!categoryID) {
			categoryID = "";
		}

		cmSKUProducts[cmSKUCounter] = skuName;
		cmSKUIds[cmSKUCounter] = sku;
		cmSKUProductIDs[cmSKUCounter] = productID;
		cmSKUCats[cmSKUCounter] = categoryID;
		cmSKUQtys[cmSKUCounter] = parseInt(productQuantity);
		cmSKUPrices[cmSKUCounter] = parseFloat(productPrice);
		cmSKUCounter++;
	}

}

/* render the aggregated cart lineitems with Shop 5 tags*/
function cmDisplayShop5s(){
	var rt=new Date();
	var connector = rt.getTime()%10000000;

	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] ;
		
		if (cmShopPrices[i] == "0")
		{ cm.bp = "0.001";
		}
		else
		{ cm.bp = cmShopPrices[i];
		}

		cm.pc = "N";
		cm.sx1 = connector;
		cm.writeImg();
	}
	cmShopCounter=0;

	var i;
	for(i=0; i<cmSKUCounter;i++){
		var cm = new _cm("tid", "7", "vn2", "e4.0");
		cm.li  = 5;
		cm.ps1 = cmSKUIds[i];
		cm.ps2 = cmSKUProducts[i];
		cm.ps3 = cmSKUQtys[i];

		if (cmSKUPrices[i] == "0")
		{ cm.ps4 = "0.001";
		}
		else
		{ cm.ps4 = cmSKUPrices[i];
		}

		cm.ps5 = cmSKUCats[i];	
		cm.ps6 = null; // no Order ID yet
		cm.ps7 = cmSKUProductIDs[i];
		cm.ps8 = connector;
		cm.writeImg();
	}
	cmSKUCounter=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
							   , sku
							   , skuName
							   , 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++;
	}

// SKU AGGREGATION
	var index = cmGetSKUIndex(sku);
	if(index!=-1){
		var oldPrice = cmSKUPrices[index];
		var oldQty = cmSKUQtys[index];
		var newQty = oldQty + parseInt(productQuantity);
		var newPrice = (oldPrice*oldQty + parseInt(productQuantity)*parseFloat(productPrice))/(newQty);

		cmSKUPrices[index] = newPrice;
		cmSKUQtys[index] = newQty;

	} else {
		if (!categoryID) {
			categoryID = "";
		}

		cmSKUProducts[cmSKUCounter] = skuName;
		cmSKUIds[cmSKUCounter] = sku;
		cmSKUProductIDs[cmSKUCounter] = productID;
		cmSKUCats[cmSKUCounter] = categoryID;
		cmSKUQtys[cmSKUCounter] = parseInt(productQuantity);
		cmSKUPrices[cmSKUCounter] = parseFloat(productPrice);
		cmSKUOrderIds[cmSKUCounter] = orderID;
		cmSKUCounter++;
	}


}


/* render the aggregated order lineitems with Shop 9 tags*/
function cmDisplayShop9s(){
	var rt=new Date();
	var connector = rt.getTime()%10000000;
	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] ;
		
		if (cmShopPrices[i] == "0")
		{ cm.bp = "0.001";
		}
		else
		{ cm.bp = cmShopPrices[i];
		}

		cm.cd = decodeCustomerID(cmShopCustomerIds[i]);
		cm.on = cmShopOrderIds[i];
		cm.tr = cmShopOrderPrices[i];
		cm.sx1 = connector;

		cm.pc = "N";
		cm.writeImg();
	}
	cmShopCounter=0;

	for(i=0; i<cmSKUCounter;i++){
		var cm = new _cm("tid", "7", "vn2", "e4.0");
		cm.li  = 9;
		cm.ps1 = cmSKUIds[i];
		cm.ps2 = cmSKUProducts[i];
		cm.ps3 = cmSKUQtys[i];

        if (cmSKUPrices[i] == "0")
		{ cm.ps4 = "0.001";
		}
		else
		{ cm.ps4 = cmSKUPrices[i];
		}

		cm.ps5 = cmSKUCats[i];
		cm.ps6 = cmSKUOrderIds[i];
		cm.ps7 = cmSKUProductIDs[i];
		cm.ps8 = connector;
		cm.writeImg();
	}
	cmSKUCounter=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) {
	
		var cm = new _cm("tid", "3", "vn2", "e4.0");
		cm.on = orderID;
		cm.tr = orderTotal;
		cm.osk = getOSK();
		cm.sg = orderShipping;
		cm.cd = decodeCustomerID(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 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		// Required VARCHAR2(256) 
                                , customerEmail		// Optional VARCHAR2(256)
								, customerCity		// Optional VARCHAR2(256)
								, customerState		// Optional VARCHAR2(256)
								, customerZIP		// Optional VARCHAR2(256)
								, newsletterName	// Optional VARCHAR2(256) 
								, subscribe			// Optional CHAR(1)	--> Y or N
								, country			// Optional VARCHAR2(256)
								, firstName			// Optional VARCHAR2(256)
								, age				// Optional NUMBER(3)
								, gender			// Optional CHAR(1) --> M or F
								, minIncome			// Optional NUMBER(14,2)
								, maxIncome			// Optional NUMBER(14,2)
								, educationLevel	// Optional VARCHAR2(256)
								, extraField1		// Optional VARCHAR2(100)
								, extraField2		// Optional VARCHAR2(100)
								, extraField3		// Optional VARCHAR2(100)
								, extraField4		// Optional VARCHAR2(100)
								, extraField5		// Optional VARCHAR2(100)
								) {
	var cm = new _cm("tid", "2", "vn2", "e4.0");
	cm.cd = decodeCustomerID(customerID);
	cm.em = decodeCustomerID(customerEmail); // CustomerID is being passed in lieu of Email
	cm.sa = customerState;
	cm.ct = customerCity;
	cm.zp = customerZIP;
	cm.cy = country;
	cm.fn = firstName;
	cm.ag = age;
	cm.gd = gender;
	cm.ml = minIncome;
	cm.xl = maxIncome;
	cm.ed = educationLevel;
	cm.rg11 = extraField1; 
	cm.rg12 = extraField2;
	cm.rg13 = extraField3;
	cm.rg14 = extraField4;
	cm.rg15 = extraField5;

	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() {
	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;
	}

	cm.pc = "Y";
	cm.pi = getDefaultPageID();
	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);
	}

	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) {
	    // ... transform newURL here ...
	}
    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;
    }
}

function decodeCustomerID(customerID) 
{
  return parseInt("0x"+customerID)-100000000;
}

//-->