<!--
/*
 * 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.
 *date			name			desc
 *5/12/06		hutch white		change host id for 1st party migration
 *
 */

// global variables for new 2k4.1 reports

var cm_TrackLink = "A";
var cm_TrackImpressions = "R";




// TAG GENERATING FUNCTIONS ---------------------------------------------------

/*
 * Calling this function points tags to the production database
 */
function cmSetProduction(){
  cm_HOST="www1.horchow.com/eluminate?";
}

/*
 * Creates a Tech Props tag.
 * pageID   : required. Page ID to set on this Pageview tag
 */
function cmCreateTechPropsTag() {
  var cm=new _cm("tid", "6", "vn2", "e3.1");
  cm.pc="Y";
  cm.pi = getDefaultPageID();
  cm.addTP();

  var tempRef = cmExtractParameter("CM_REF");
  if (tempRef != null) {
		cm.rf = tempRef;
  }
  if (this.static_mmc != null) {
		cm.ul = window.location.href + "?" + static_mmc;
  }
  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, categoryID, searchString, searchResults, pageNo) {
  if (pageID == null) {
    pageID = getDefaultPageID();
  }

  var cm = new _cm("tid", "1", "vn2", "e3.1");

  if ((window.location.href.indexOf("cm_ven") == -1) && (window.location.href.indexOf("cm_mmc") == -1)) {
	  if (pageNo == "1")
	  {
		  cm.se = searchString;
		  cm.sr = searchResults;
		  cm.pv15 = searchResults;
	  }
  }

  if (categoryID) {
    cm.cg = categoryID;
  }
  if (pageNo) {
    if ((pageNo != "") && (pageNo != "1")) {
      pageID = pageID + " PG " + pageNo;
    }
  }
  cm.pi = pageID;

  var tempRef = cmExtractParameter("CM_REF");
	if (tempRef != null) {
		cm.rf = tempRef;
	}
  if (this.static_mmc != null) {
		cm.ul = window.location.href + "?" + static_mmc;
  }
  cm.writeImg();
}

/*
 * Creates a Pageview tag appropriate for category pages.
 * Format of Page ID is "CATEGORY: <category Name> (<category ID>) <PAGE x>"
 *
 * categoryID : required. Category ID to set on this Pageview tag and in
 *          the Page ID.
 * categoryName : required. Category Name to set on this Pageview tag and in
 *          the Page ID.
 * pageNumber : optional. If appropriate, the current page number for this
 *          category.  Only page numbers greater than "1" will be
 *          added to the Page ID.
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreateCategoryPageviewTag(categoryID, categoryName, pageNumber) {
  if (!(categoryID)) {
    categoryID = "Default";
  }
  if (!(categoryName)) {
    categoryName = "Default";
  }
  var pageIDcategoryID = categoryID;
  var indexOfLastCat = pageIDcategoryID.lastIndexOf("cat");
  if (indexOfLastCat > -1) {
	  pageIDcategoryID = pageIDcategoryID.substring(indexOfLastCat);
  }
  else {
	  pageIDcategoryID = "NO_CAT_ID";
  }

  var pageID = "CATEGORY: " + categoryName + " (" + pageIDcategoryID + ")";
  cmCreatePageviewTag(pageID, categoryID, null, null, pageNumber);
}


/*
 * 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, 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, pageCount, clientPageId, masterID) {
  var cm = new _cm("tid", "5", "vn2", "e3.1");

  cm.pr = productID;
  cm.pm = productName;
  cm.cg = categoryID;

  cm.pc = pageCount;
  cm.pi = clientPageId;

  if (masterID) {
	  if (masterID.toLowerCase() != "default") {
		cm.li = 100;
		cm.ps1 = productID;
		cm.ps2 = masterID;
		cm.ps3 = categoryID;
	  }
  }
  var tempRef = cmExtractParameter("CM_REF");
	if (tempRef != null) {
		cm.rf = tempRef;
	}
  if (this.static_mmc != null) {
		cm.ul = window.location.href + "?" + static_mmc;
  }
  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
 * 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, customerCity,
        customerState, customerZIP, newsletterName,
        subscribe) {
  var cm = new _cm("tid", "2", "vn2", "e3.1");
  cm.cd = customerID;
  cm.em = customerID;
  cm.sa = customerState;
  cm.ct = customerCity;
  cm.zp = customerZIP;

  if (newsletterName && subscribe) {
    cm.nl = newsletterName;
    if (subscribe.toLowerCase() == "yes") {
      cm.sd = "Y";
    }
    else {
      cm.sd = "N";
    }
  }

  cm.pi = getDefaultPageID();
  cm.pc = "Y";

  cm.writeImg();
}

/* Creates an Error Tag
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreateErrorTag(errorName) {
  var cm=new _cm("tid", "404", "vn2", "e3.1");
  if (errorName) {
    cm.ul = errorName;
  }
  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 + "index.jhtml"; /****************** SET TO DEFAULT DOC NAME */
  }

  while (pageName.indexOf("/") == 0) {
    pageName = pageName.substr(1,pageName.length);
  }

  return(pageName);
}

/*
 * 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 cmShopImages = new Array();
var cmShopCustomSKUs = new Array();
var cmShopPromoCodes = new Array();
var cmShopMasterIDs = new Array();

/* 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, customSKU, masterID) {
  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);
    cmShopCustomSKUs[cmShopCounter] = customSKU;
	cmShopMasterIDs[cmShopCounter] = masterID;
    cmShopCounter++;
  }
}

function cmDisplayShop5s(){

  var cmPv = new _cm("tid", "1", "vn2", "e3.1");
  cmPv.pi = getDefaultPageID();

  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.cl = cmShopCats[i];
    cm.qt = cmShopQtys[i] ;
    cm.bp = cmShopPrices[i];
    cm.sx1 = cmShopCustomSKUs[i];
	cm.sx3 = cmShopMasterIDs[i];
    cm.pc = "N";
    cm.writeImg();
  }
  cmPv.writeImg();
}

/*
 * 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, customSKU, promoCode, masterID) {
  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 + "|";
    cmShopCustomSKUs[cmShopCounter] = customSKU;
    cmShopPromoCodes[cmShopCounter] = promoCode;
	cmShopMasterIDs[cmShopCounter] = masterID;
    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];
    cm.cd = cmShopCustomerIds[i];
    cm.on = cmShopOrderIds[i];
    cm.tr = cmShopOrderPrices[i];
    cm.sx1 = cmShopCustomSKUs[i];
    cm.sx2 = cmShopPromoCodes[i];
	cm.sx3 = cmShopMasterIDs[i];

    cm.pc = "N";
    cm.writeImg();
  }
}

/*
 * 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, newsletter, subscribed, 
	promoCode, lostShipping, tenderType) {
  if (orderID && orderTotal && orderShipping && customerID) {
    var cm = new _cm("tid", "3", "vn2", "e3.1");
    cm.pi = getDefaultPageID();
    cm.pc = "Y";
    cm.on = orderID;
    cm.tr = orderTotal;
    cm.osk = getOSK();
    cm.sg = orderShipping;
    cm.cd = customerID;
    cm.sa = customerState;
    cm.ct = customerCity;
    cm.zp = customerZIP;
    if (promoCode) {
	cm.or1 = promoCode;
    }
    if (lostShipping) {
	cm.or2 = lostShipping;
    }
    if (tenderType) {
    cm.or3 = tenderType;
    }
    
    cm.writeImg();
    cmDisplayShop9s();
    
    var cmReg = new _cm("tid", "2", "vn2", "e3.1");
    cmReg.cd = customerID;
    cmReg.em = customerID;
    cmReg.sa = customerState;
    cmReg.ct = customerCity;
    cmReg.zp = customerZIP;

    if (newsletter && subscribed) {
      cmReg.nl = newsletter;
      if (subscribed.toLowerCase() == "yes") {
        cmReg.sd = "Y";
      }
      else {
        cmReg.sd = "N";
      }
    }

    cmReg.writeImg();    
  }
}

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

/* Code for generating a tag when user clicks a link to an external site. */

function cmExternalClick(link) {
  var image1 = new Image();
  //Build the appropriate tag
  var cm = new _cm("tid", "1", "vn2", "e3.1");
  //Use the appropriate naming convention for pi
  cm.pi = "External Link: " + link.href;
  cm.ul = "http://www.horchow.com/?ExternalURL=" + link.href;
  cm.writeImg();
  return true;
}

//link normalization function
if (defaultNormalize == null) { 
	var defaultNormalize = null; 
}

function myNormalizeURL(url, isHref) {
	var newURL = url;

	// remove the jsessionid parameter
	var param = ';jsessionid=';
    var paramIndex = newURL.indexOf(param);

    if (paramIndex > 0) {
        var end = newURL.indexOf("?",paramIndex+param.length);
		var u = newURL.substring(0, paramIndex);

        if (end != -1) {
            u += newURL.substring(end);
		}
		newURL = u;
    }

	// remove the _requestid parameter
	param = "_requestid";
	paramIndex = newURL.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;
	}
}

/*
 * Checks to see if a parameter exists in the request search string.
 * Returns -1 if not found or the index of the first character of the 
 * name in the name-value pair if found.
 */
function cmIndexOfParameter (parameter) {
	return document.URL.indexOf(parameter);
}

/*
 * Extracts the value of a name-value pair in the request search string.
 */
function cmExtractParameter (parameter) {
    if (cmIndexOfParameter(parameter) == -1) {
        return null;
    }
	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);
}

//-->
