<!--
/*
 * cmdatatagutils.js 
 * $Id: cmdatatagutils - 5147298 - 90032588 - 031908 .txt 82383 2008-03-19 15:01:21Z mochoa $
 * $Revision: 82383 $
 *
 * Version 4.1.0
 *
 * Coremetrics Tag v4.0, 8/7/2006
 * COPYRIGHT 1999-2002 COREMETRICS, INC. 
 * ALL RIGHTS RESERVED. U.S.PATENT PENDING
 *
 * The following functions aid in the creation of Coremetrics data tags.
 * Date				Imp. Eng.			Desc
 * 01/16/08			Hutch White			Upgrade cmdatatagutils and eluminate to newest feature set.
 * 03/19/08			MOCHOA				Enable Conversion Event & Element Tagging Function
 *
 */
 
var cm_ClientID = "90032588";
var cm_TrackLink = "A";
var cm_TrackImpressions = "";
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";

function getABString() {
  var is_abtest = document.URL.indexOf('abtest=');
  var abtest_string = '';
  if(is_abtest >= 0) {
    abtest_string = document.URL.substring(is_abtest + 7, document.URL.length);
    if(abtest_string.indexOf('&') != -1) {
      abtest_string = ' ab(' + unescape(abtest_string.substring(0, abtest_string.indexOf('&')).replace(/\+/g, " ")) + ')';
    } else {
      abtest_string = ' ab(' + unescape(abtest_string.replace(/\+/g, " ")) + ')';
    }
  }
  return abtest_string;

}

var cmdatautilsVersion = "1.7";
var cmDt = new Date();
var cmTimeStamp = cmDt.getTime();
var cmRandom;
var cmOnChangeCounter = 0;
var cmOnChangeTextBoxName = new Array();
var cmOnChangePointer = new Array();
var cmOnChangeFirst = new Array();
var cmAppName;
var cmAppStepNumber;
var cmAppStepName;

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();
}
 
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();
}

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 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);
	}
}

function cmCreateManualImpressionTag(pageID, trackSP, trackRE) {
		var cm = new _cm("tid","9","vn2","4.0");
		cm.pi = pageID;
		if (trackSP){
			cm.cm_sp = trackSP;
		}
		if (trackRE){
			cm.cm_re = trackRE;
		}		
		cm.st = cm_ClientTS;
        cm.writeImg();
}

/* manual PageviewTag for off site page tagging.  Allows client to supply URL and Referring URL
*/
function cmCreateManualPageviewTag(pageID, categoryID,DestinationURL,ReferringURL) {
	if (pageID == null) {
		pageID = cmGetDefaultPageID();
	}

	var cm = new _cm("tid", "1", "vn2", "e4.0");
	cm.pi = pageID;
	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 (ReferringURL) {
		cm.rf = ReferringURL;
	}
	// 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;
	}
	if (DestinationURL) {
		cm.ul = DestinationURL;
	}
	cm.writeImg();
}

function cmErrorTag(errmsg,fieldID) {
	var cm = new _cm("tid", "7", "vn2", "e4.0");
	cm.li = 10;
	cm.ps1 = document.URL;
	if (errmsg){
			cm.ps2 = errmsg;
	}
	if (fieldID){
		cm.ps3=fieldID
	}
	cm.ul=document.URL;
	cm.writeImg();
}

function cmSetProduction(){	
	cm_HOST="www3.rei.com/eluminate?";
	//	cm_JSFPCookieDomain = "somedomain.com";
}

function cmCreateTechPropsTag(pageID, categoryID, cookieID) {	
	if (pageID) {	
		var cm=new _cm("tid", "6", "vn2", "e4.0");
		cm.pc="Y";
		cm.pi = pageID + getABString();
		cm.cg = categoryID;
		cm.pv1 = cookieID;
		if (parent.cm_ref != null) {
			cm.rf = parent.cm_ref;
			parent.cm_ref = document.URL;
		}
	cm.ul=document.URL;
	cm.addTP();
	cm.writeImg();
	}
}

function cmCreatePageviewTag(pageID, searchString, categoryID, searchResults, cookieID) {
	if (pageID == null) {
		pageID = getDefaultPageID();
	}	
	var cm = new _cm("tid", "1", "vn2", "e4.0");
	
	if (cm.tid == "1") {
		if (cI("cmTPSet") != 'Y') {
			cm.tid = "6";
			cm.pc = "Y";
			cm.addTP();
			document.cookie = "cmTPSet=Y; path=/";
		}
	}
		
	cm.pi = pageID + getABString();
	cm.se = searchString;
	cm.cg = categoryID;
	cm.pv1 = cookieID;
	cm.sr = searchResults;
	if (parent.cm_ref != null) {	cm.rf = parent.cm_ref;
	parent.cm_ref = document.URL;
	}
	cm.ul=document.URL;
	cm.writeImg();
}

function cmCreateDefaultPageviewTag() {	
	cmCreatePageviewTag(getDefaultPageID(), null, null, null);
}

function cmCreateProductviewTag(productID, productName, brand, categoryID, cookieID) {
	var cm = new _cm("tid", "5", "vn2", "e4.0");
	if (productName == null) {
		productName = "";
	}	
	if (parent.cm_ref != null) {
		cm.rf = parent.cm_ref;
		parent.cm_ref = document.URL;
	}	
	cm.pv1 = cookieID;
	cm.pr = productID;
	cm.pm = productName;
	cm.cg = categoryID;
	cm.pc = "Y";
	var storeName="";
	if(store_id_tag_value==8001) {
		storeName = "OUTLET ";
	} else {
		storeName="REI ";
	}
	cm.pi = storeName + "PRODUCT: " + productName + " " + brand + " (" + productID + ")" + getABString();
	cm.ul=document.URL;
	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 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", "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.pc = "N";
		if (parent.cm_ref != null) {	
			cm.rf = parent.cm_ref;
			parent.cm_ref = document.URL;
		}	
		cm.ul=document.URL;		
		cm.writeImg();
	}	
	cmShopCounter=0;
}

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++;
	}
}

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];
		if (parent.cm_ref != null) {
			cm.rf = parent.cm_ref;
			parent.cm_ref = document.URL;
		}	
		cm.pc = "N";
		cm.ul=document.URL;		
		cm.writeImg();
	}cmShopCounter=0;
}

function cmCreateOrderTag(orderID, orderTotal, orderShipping, customerID, customerZIP) {	
	var cm = new _cm("tid", "3", "vn2", "e4.0");
	cm.on = orderID;
	cm.tr = orderTotal;
	cm.osk = getOSK();
	cm.sg = orderShipping;
	cm.cd = customerID;
	cm.zp = customerZIP;
	if (parent.cm_ref != null) {
		cm.rf = parent.cm_ref;
		parent.cm_ref = document.URL;
	}	
	cm.ul=document.URL;	
	cm.writeImg();
}

function getOSK() {	
	var i =0;
	var result = "";
	for (i=0; i<cmShopCounter; i++){	
		result += cmShopSKUs[i];
	}	
	return result;
}

function cmCreateRegistrationTag(customerID, customerEmail, customerZIP, coopNum, reiCookie, userCookie, newsletterName, subscribe) {
	var cm = new _cm("tid", "2", "vn2", "e4.0");
	cm.cd = customerID;
	cm.em = customerEmail;
	cm.zp = customerZIP;
	cm.rg1 = coopNum;
	cm.rg2 = reiCookie;
	cm.rg3 = userCookie;
	if (newsletterName && subscribe) {
		cm.nl = newsletterName;
		cm.sd = subscribe;
	}	
	if (parent.cm_ref != null) {
		cm.rf = parent.cm_ref;
		parent.cm_ref = document.URL;
	}	
	cm.ul=document.URL;	
	cm.writeImg();
}

function cmCreateErrorTag() {
	var cm=new _cm("tid", "404", "vn2", "e4.0");
	if (parent.cm_ref != null) {
		cm.rf = parent.cm_ref;
		parent.cm_ref = document.URL;
	}
	cm.pc = "Y";
	cm.pi = getDefaultPageID() + getABString();
	cm.ul=document.URL;	
	cm.writeImg();
}

function cmReportLoadTime(pageID, categoryID) {	
	var dt = new Date();
	if (pageID == null) {	
		pageID = getDefaultPageID();
	}	
	var cm = new _cm("tid", "7", "vn2", "e4.0");
	cm.li = "12358";
	cm.ps1 = pageID;
	cm.ps2 = categoryID;
	cm.ps3 = cm_date.getTime();
	cm.ps4 = cmT2;
	cm.ul=document.URL;	
	cm.writeImg();
}

function cmCreateFormFieldTag(appName, appStepNumber, appStepName, categoryID) {
	cmAppName = appName;
	cmAppStepNumber = appStepNumber;
	cmAppStepName = appStepName;
	cmCreateApplicationStepTag(appName, appStepNumber, appStepName, categoryID);
	cmSetupFormFieldTags();
}

function getDefaultPageID() {	
	var 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 + "default.asp";
	}	
	while (pageName.indexOf("/") == 0) {
		pageName = pageName.substr(1,pageName.length);
	}	
	return(pageName);
}

function cmCreateApplicationStepTag(appName, appStepNumber, appStepName, category){	var dt=new Date();
	cmRandom = dt.getTime()%10000000;
	var cm = new _cm("tid", "7", "vn2", "4.0");
	cm.li = 210000;
	cm.ps1= appName;
	cm.ps2 = appStepNumber;
	cm.ps3 = appStepName;
	cm.ps4 = cmRandom;
	if(category) {	cm.ps5= category.toUpperCase();
	}
	cm.ps15 = cmTimeStamp;
	cm.ul=document.URL;	
	cm.writeImg();
}

function cmSetupFormFieldTags(){
	var dt=new Date();
	cmRandom = dt.getTime()%10000000;
	for (var i=0; i<document.forms.length; i++){
		for (var j=0;j<document.forms[i].elements.length; j++){
			if (document.forms[i].elements[j].type=="text"){
				if(document.forms[i].elements[j].value==""){
					cmCheckForOnChange(document.forms[i].elements[j]);
				} else {
					cmSendFormFieldTag(document.forms[i].elements[j].name);
				}	
			}	
		}	
	}
}

function cmCheckForOnChange(textbox){
	cmOnChangeTextBoxName[cmOnChangeCounter] = textbox.name;
	cmOnChangePointer[cmOnChangeCounter] = textbox.onchange;
	cmOnChangeFirst[cmOnChangeCounter] = true;
	textbox.onchange = new Function("cmMultipleOnChange(" + cmOnChangeCounter + ");");
	cmOnChangeCounter++;
}

function cmSendFormFieldTag(name){
	var cm = new _cm("tid", "7", "vn2", "4.0");
	cm.li = 210001;
	cm.ps1 = cmAppName;
	cm.ps2 = cmAppStepNumber;
	cm.ps3 = cmAppStepName;
	cm.ps4 = cmRandom;
	cm.ps5 = name;
	cm.ps15 = cmTimeStamp;
	cm.ul=document.URL;	
	cm.writeImg();
}

function cmMultipleOnChange(id){
	if (cmOnChangeFirst[id]){
		cmSendFormFieldTag(cmOnChangeTextBoxName[id]);
		cmOnChangeFirst[id] = false;
	}	
	if (cmOnChangePointer[id]!=null){
		cmOnChangePointer[id]();
	}
}

//if (!cmHandleLinkClick) { var cmHandleLinkClick = C9;}

if (defaultNormalize == null) { var defaultNormalize = null;}

function myNormalizeURL(url, isHref) {
	 var newURL = url;
	 if (url.toLowerCase().indexOf("/reicheck")>-1 || url.toLowerCase().indexOf("/search")>-1 || url.toLowerCase().indexOf("/shopping basket")>-1 || url.toLowerCase().indexOf("/gift registry")>-1 || url.toLowerCase().indexOf("/your account")>-1 || url.toLowerCase().indexOf("/order")>-1 || url.toLowerCase().indexOf("/payment")>-1){
		 if (!isHref) {
		 	var WhitelistURL=1;
		 }
	 }
	 if(isHref) {	
		if(url.indexOf("http://") == 0 || url.indexOf("https://") == 0) {
			newURL = newURL.substring(newURL.indexOf("://")+3);
			newURL = newURL.substring(newURL.indexOf("/"));
		} 
	 }
	 if (WhitelistURL==1){
	    var whiteList = ["cm_re=", "cm_re_o=", "cm_sp=", "cm_sp_o=","cm_mmc=","cm_mmc_o="];
	    var paramString;
	    var paramIndex = newURL.indexOf("?");
	    var params;
	    var keepParams = new Array();
	    if (paramIndex > 0) {
			paramString = newURL.substring(paramIndex+1);
			newURL = newURL.substring(0, paramIndex);
			params = paramString.split("&");
			for(var i=0; i<params.length; i++) {
				for(var j=0; j<whiteList.length; j++) {
					//This match is case insensitive.  Remove .toLowerCase() to add case sensitivity
					if (params[i].toLowerCase().indexOf(whiteList[j].toLowerCase()) == 0) {
						keepParams[keepParams.length] = params[i];
					}
				}
			}

			newURL += "?" + keepParams.join("&");
		}
   } else {
		 var blackList = ["krypto=","orderId=","currency=","productid","parent_category_rn=","crumb=","cat="];
 		 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].toUpperCase().indexOf(blackList[j].toUpperCase()) == 0) {	
						 goodParam = false;
					 }	
				 }	
				 if(params[i].indexOf("query=") == 0) {
					 params[i] = params[i].substring(0, 26);
				 }	
				 if(goodParam == true) {
					 keepParams[keepParams.length] = params[i];
				 }	
			}	
			newURL += "?" + keepParams.join("&");
		} 
   }
   if (defaultNormalize != null) {
		newURL = defaultNormalize(newURL, isHref);
   } 
   return newURL;
}

if (document.cmTagCtl != null) {
	var func = "" + document.cmTagCtl.normalizeURL;
	if (func.indexOf('myNormalizeURL') == -1) { 
		defaultNormalize = document.cmTagCtl.normalizeURL;
		document.cmTagCtl.normalizeURL = myNormalizeURL;
	}
}
//-->
