<!--
/*
 * cmdatatagutils.js 
 * $Id: $
 *
 * Date		    Imp Eng.	    Desc
 * 12/19/07     Orest Ivasiv    Added cmCreateConversionEventTag,
 *                              cmCreatePageElementTag and cmCreateProductElementTag functions 
 * 01/22/08     Orest Ivasiv    Added customized myNormalizeURL function.        
 *
 * 05/22/2009	Arthur Blattman	added manual_cm_mmc logic to every tag that functions as a page view,
 *								also added declarations for cm.ul in every tag after new _cm object, before manual_cm_mmc
 */

var cm_exAttr=new Array;  
var cm_ClientID = "90029163";
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;


/* 
 * Creates a Conversion Event tag.
 */
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 Page Element tag.
 */
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();
}

/* 
 * Creates a Product Element tag.
 */
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 cmSetProduction(serverType){
    if (serverType == "Production") {
        cm_HOST = "www89.childrensplace.com/eluminate?";
    }
};
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.ul = window.location.href;
	
			//check for manual_cm_mmc parameter;
		if (this.manual_cm_mmc != null) {
		cm.ul = cm.ul + ((cm.ul.indexOf("&") == -1) ? ((cm.ul.indexOf("?") == -1) ? "?" : "&") : "&") + "cm_mmc=" + this.manual_cm_mmc;
		}

        cm.writeImg();
    }
};


function cmCreatePageviewTag(pageID, searchString, categoryID, prefixStr, suffixStr, numSearchResults){

	// fix to remove 10001 at the end of the pageID
	if (suffixStr == '10001')
		suffixStr = '';
	
    if (pageID == null) {
        pageID = getDefaultPageID();
    }
    var cm = new _cm("tid", "1", "vn2", "e3.1");
    cm.pi = pageID;
    if (prefixStr) {
        cm.pi = prefixStr + cm.pi;
    }
    if (suffixStr) {
        cm.pi = cm.pi + suffixStr;
    }
    if (searchString) {
        cm.se = searchString;
    }
    if (categoryID) {
        cm.cg = categoryID;
    }
    if (parent.cm_ref != null) {
        cm.rf = parent.cm_ref;
        parent.cm_ref = document.URL;
    }
    cm.sr = numSearchResults;

	cm.ul = window.location.href;

		//check for manual_cm_mmc parameter;
	if (this.manual_cm_mmc != null) {
		cm.ul = cm.ul + ((cm.ul.indexOf("&") == -1) ? ((cm.ul.indexOf("?") == -1) ? "?" : "&") : "&") + "cm_mmc=" + this.manual_cm_mmc;
	}

    cm.writeImg();

};
function cmCreateDefaultPageviewTag(){
    cmCreatePageviewTag(getDefaultPageID(), null, null);
};

//function cmCreateProductviewTag(productID, productName, categoryID){
function cmCreateProductviewTag(pageID, productID, productName, categoryID, storeID, pageCount, masterItemCategory, catIDoverrid) {
    var cm = new _cm("tid", "5", "vn2", "e3.1");
    if (productName == null) {
        productName = "";
    }
    if (parent.cm_ref != null) {
        cm.rf = parent.cm_ref;
        parent.cm_ref = document.URL;
    }
    
    if (pageID == null) {
    	if (cmStyle != null)
    		productID = cmStyle;
    }
    	
    cm.pr = productID;
    cm.pm = productName;
    if (masterItemCategory == null || masterItemCategory == '')
    	cm.cg = categoryID;
	else
		cm.dg = masterItemCategory;    	
    cm.pc = "Y";
    cm.pi = "PRODUCT: " + productName + " (" + productID + ")";

	cm.ul = window.location.href;

		//check for manual_cm_mmc parameter;
	if (this.manual_cm_mmc != null) {
		cm.ul = cm.ul + ((cm.ul.indexOf("&") == -1) ? ((cm.ul.indexOf("?") == -1) ? "?" : "&") : "&") + "cm_mmc=" + this.manual_cm_mmc;
	}

    cm.writeImg();
};

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 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){
	
	var productIDs = productID.split("_");
	productID = productIDs[0];
    categoryID = cmHandleLineItemCategory(productID, categoryID, COOKIE_CART, PERSISTENT_CART_CAT_ID);
    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++;
    }
};
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;
};
function cmCreateShopAction9Tag(productID, productName, productQuantity, productPrice, customerID, orderID, orderTotal, categoryID){
	
	var productIDs = productID.split("_");
	productID = productIDs[0];
    categoryID = cmHandleLineItemCategory(productID, categoryID, COOKIE_CART, PERSISTENT_CART_CAT_ID);
    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++;
    }
};
function cmDisplayShop9s(){
    var i;
    for (i = 0; i < cmShopCounter; i++) {
        deleteProductFromCookie(COOKIE_CART, cmShopIds[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;
};
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;
};
function cmCreateRegistrationTag(customerID, customerEmail, customerCity, customerState, customerZIP, newsletterName, subscribe, pageID){
    var cm = new _cm("tid", "2", "vn2", "e3.1");
    cm.cd = customerID;
    cm.em = customerEmail;
    cm.sa = customerState;
    cm.ct = customerCity;
    cm.zp = customerZIP;
    if (newsletterName && subscribe) {
        cm.nl = newsletterName;
        cm.sd = subscribe;
    }
    if (pageID) {
        cm.pc = "Y";
        cm.pi = pageID;
    }

		cm.ul = window.location.href;


		//check for manual_cm_mmc parameter;
	if (this.manual_cm_mmc != null) {
		cm.ul = cm.ul + ((cm.ul.indexOf("&") == -1) ? ((cm.ul.indexOf("?") == -1) ? "?" : "&") : "&") + "cm_mmc=" + this.manual_cm_mmc;
	}

    cm.writeImg();
};
function cmCreateErrorTag(){
    var cm = new _cm("tid", "404", "vn2", "e3.1");
    if (parent.cm_ref != null) {
        cm.rf = parent.cm_ref;
        parent.cm_ref = document.URL;
    }
    cm.pc = "Y";
    cm.pi = getDefaultPageID();

	cm.ul = window.location.href;

		//check for manual_cm_mmc parameter;
	if (this.manual_cm_mmc != null) {
		cm.ul = cm.ul + ((cm.ul.indexOf("&") == -1) ? ((cm.ul.indexOf("?") == -1) ? "?" : "&") : "&") + "cm_mmc=" + this.manual_cm_mmc;
	}

    cm.writeImg();
};
function getDefaultPageID(){
    pageName = window.location.pathname;
    var tempIndex1 = pageName.indexOf("?");
    if (tempIndex1 != -1) {
        pageName = pageName.substr(0, tempIndex1);
    }
    var tempIndex2 = pageName.indexOf("#");
    if (tempIndex2 != -1) {
        pageName = pageName.substr(0, tempIndex2);
    }
    var tempIndex3 = pageName.indexOf(";");
    if (tempIndex3 != -1) {
        pageName = pageName.substr(0, tempIndex3);
    }
    var slashPos = pageName.lastIndexOf("/");
    if (slashPos == pageName.length - 1) {
        pageName = pageName + "index.jsp";
    }
    while (pageName.indexOf("/") == 0) {
        pageName = pageName.substr(1, pageName.length);
    }
    pageName = pageName.replace("webapp/wcs/stores/servlet/", "");
    pageName = cleanWebSphereView(pageName);
    return (pageName);
};
function cmCreateCategoryPageviewTag(categoryID, categoryName, pageNumber){
    if (categoryID && categoryName) {
        if (pageNumber && (pageNumber != "") && (pageNumber != "1")) {
            cmCreatePageviewTag("CAT: " + categoryName + " (" + categoryID + ")" + " PAGE " + pageNumber, null, categoryID);
        }
        else {
            cmCreatePageviewTag("CAT: " + categoryName + " (" + categoryID + ")", null, categoryID);
        }
    }
};
function cmCreateProductviewNoPVTag(productID, productName, categoryID){
    var cm = new _cm("tid", "5", "vn2", "e3.1");
    if (productName == null) {
        productName = "";
    }
    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.ul = window.location.href;

		//check for manual_cm_mmc parameter;
	if (this.manual_cm_mmc != null) {
		cm.ul = cm.ul + ((cm.ul.indexOf("&") == -1) ? ((cm.ul.indexOf("?") == -1) ? "?" : "&") : "&") + "cm_mmc=" + this.manual_cm_mmc;
	}

    cm.writeImg();
};
function cmCreateWishlistLineItemTag(productID, productName){
    var categoryID = cmHandleLineItemCategory(productID, "", COOKIE_WISH, WISHLIST_CAT_ID);
    if (categoryID == WISHLIST_CAT_ID) {
        var cm = new _cm("tid", "5", "vn2", "e3.1");
        if (productName == null) {
            productName = "";
        }
        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.ul = window.location.href;

			//check for manual_cm_mmc parameter;
	if (this.manual_cm_mmc != null) {
		cm.ul = cm.ul + ((cm.ul.indexOf("&") == -1) ? ((cm.ul.indexOf("?") == -1) ? "?" : "&") : "&") + "cm_mmc=" + this.manual_cm_mmc;
	}


        cm.writeImg();
    }
};
function cmIndexOfParameter(parameter){
    return document.URL.indexOf(parameter);
};
function cmExtractParameter(parameter){
    if (cmIndexOfParameter(parameter) == -1) {
        return "";
    }
    var s = location.search;
    var begin = s.indexOf(parameter);
    var end = s.indexOf("&", begin);
    if (end == -1) {
        end = s.length;
    }
    var middle = s.indexOf("=", begin);
    return s.substring(middle + 1, end);
};
function trimCurrency(dollarValue){
    var decimalPos = dollarValue.indexOf(".");
    if (dollarValue.substr(decimalPos, dollarValue.length - decimalPos - 1).length > 2) {
        dollarValue = dollarValue.substr(0, decimalPos + 3);
    }
    return dollarValue.replace("$", "");
};
function cleanWebSphereView(pageName){
    re = /(\w+)([a-z])([A-Z])(\w+)/;
    while (pageName.search(re) != -1) {
        pageName = pageName.replace(re, "$1$2 $3$4");
    }
    return pageName.replace(" View", "");
};
var COOKIE_FUTURE_DATE = new Date("January 1, 2038 11:59:59");
var COOKIE_CART = "CART";
var COOKIE_WISH = "WISH";
var WISHLIST_CAT_ID = "99998";
var PERSISTENT_CART_CAT_ID = "persistentcart";
var QUERY_STRING_PROD_ID = "addProdID";
var QUERY_STRING_CAT_ID = "addCatID";
var DELIMITER_LIST = ",";
var DELIMITER_LINE_ITEM = "#";
var DELIMITER_PROD_CAT = "^";
function cmHandleLineItemCategory(productID, categoryID, cookieType, defaultCatID, doNotExpireCookie){
    var catIDResult = "";
    var cookieDate = (doNotExpireCookie) ? COOKIE_FUTURE_DATE : null;
    categoryID = (categoryID) ? categoryID : "";
    var catIDCookieValue = getCategoryIDFromCookie(productID, cookieType);
    catIDResult = (catIDCookieValue) ? catIDCookieValue : "";
    if (categoryID != "") {
        addModifyProductInCookie(cookieType, productID, categoryID, cookieDate);
        catIDCookieValue = categoryID;
        catIDResult = categoryID;
    }
    if (typeof addProdID == "undefined") {
        return (catIDResult == "" ? defaultCatID : catIDResult);
    }
    var prodArray = parseList(addProdID, DELIMITER_LIST);
    var catArray = parseList(addCatID, DELIMITER_LIST);
    var i;
    for (i = 0; i < prodArray.length; i++) {
        if (i <= catArray.length) {
            if (prodArray[i] == productID) {
                addModifyProductInCookie(cookieType, productID, catArray[i], cookieDate);
                catIDResult = catArray[i];
            }
        }
    }
    return (catIDResult == "" ? defaultCatID : catIDResult);
};
function cmSetCookie(name, value, expires, path, domain, secure){
    var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
    document.cookie = curCookie;
};
function cmGetCookie(name){
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) 
            return (null);
    }
    else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
};
function cmDeleteCookie(name, path, domain){
    if (cmGetCookie(name)) {
        document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
};
function parseList(theList, delimiter){
    var listArray = new Array();
    var arrayPos = 0;
    var endPos;
    while ((theList) && (theList.length > 0)) {
        var delimPos = theList.indexOf(delimiter);
        if (delimPos == -1) {
            listArray[arrayPos] = theList;
            theList = "";
        }
        else {
            var listElement = theList.substr(0, delimPos);
            listArray[arrayPos] = listElement;
            theList = theList.substr(delimPos + 1, theList.length - delimPos);
            arrayPos++;
        }
    }
    return listArray;
};
function getCategoryIDFromCookie(productID, cookieType){
    var lineItemArray = parseList(cmGetCookie(cookieType), DELIMITER_LINE_ITEM);
    var i;
    for (i = 0; i < lineItemArray.length; i++) {
        var nameValuePair = lineItemArray[i];
        var tempProdID = getNameFromPair(nameValuePair, DELIMITER_PROD_CAT);
        if ((tempProdID) && (tempProdID == productID)) {
            return (getValueFromPair(nameValuePair, DELIMITER_PROD_CAT));
        }
    }
    return (null);
};
function deleteProductFromCookie(cookieType, productID, cookieDate){
    var curCookieValue = cmGetCookie(cookieType);
    if (curCookieValue) {
        var lineItemArray = parseList(curCookieValue, DELIMITER_LINE_ITEM);
        var newCookieValue = "";
        var i;
        for (i = 0; i < lineItemArray.length; i++) {
            var nameValuePair = lineItemArray[i];
            var tempProdID = getNameFromPair(nameValuePair, DELIMITER_PROD_CAT);
            if (tempProdID != productID) {
                newCookieValue = (newCookieValue == "") ? nameValuePair : newCookieValue + DELIMITER_LINE_ITEM + nameValuePair;
            }
        }
        cmSetCookie(cookieType, newCookieValue, cookieDate);
    }
};
function addModifyProductInCookie(cookieType, productID, categoryID, cookieDate){
    deleteProductFromCookie(cookieType, productID, cookieDate);
    var curCookie = cmGetCookie(cookieType);
    curCookie = (curCookie) ? curCookie + DELIMITER_LINE_ITEM : "";
    cmSetCookie(cookieType, curCookie + productID + DELIMITER_PROD_CAT + categoryID, cookieDate);
};
var PAIR_NAME = 0;
var PAIR_VALUE = 1;
function getNameFromPair(nameValuePairString, delim){
    return (getNameORValueFromPair(nameValuePairString, delim, PAIR_NAME));
};
function getValueFromPair(nameValuePairString, delim){
    return (getNameORValueFromPair(nameValuePairString, delim, PAIR_VALUE));
};
function getNameORValueFromPair(nameValuePairString, delim, nameORValue){
    var pairArray = parseList(nameValuePairString, delim);
    if ((pairArray) && (pairArray.length == 2)) {
        return pairArray[nameORValue];
    }
    else {
        return (null);
    }
};
function cmExternalClick(link, pageId){
    var image1 = new Image();
    var cm = new _cm("tid", "1", "vn2", "e3.1");
    if (pageId) {
        cm.pi = pageId;
    }
    else {
        cm.pi = "External: " + link.href;
    }
    cm.ul = "http://www.childrensplace.com/?URL=" + link.href;
    cm.rf = document.location.href;
    image1.src = cm.getImgSrc();
    return true;
};

// Added by oivasiv
// Begin block
if (defaultNormalize == null) { var defaultNormalize = null; }

function myNormalizeURL(url, isHref){
    var newURL = url;
    
    var param = "krypto=";
    //This match is case insensitive.  Remove .toLowerCase() to add case sensitivity
    var paramIndex = newURL.toLowerCase().indexOf(param);
    if (paramIndex > 0) {
        var leadCharacter = newURL.charAt(paramIndex - 1);
        if (leadCharacter == '?' || leadCharacter == '&') {
            var end = newURL.indexOf("&", paramIndex + param.length);
            if (end == -1) 
                paramIndex--;
            var u = newURL.substring(0, paramIndex);
            if (end != -1) 
                u += newURL.substring(end + 1);
            newURL = u;
        }
    }
    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;
    }
}
// End block

//-->