<!--
/*
 * cmdatatagutils.js
 *
 * $Revision:$
 * $Id:$
 * 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			Imp.Eng.			Desc
 * 071306		Hutch White			Add 1st Party Host Name
 * 071806		Hutch White			Add Flash Tagging Functions
 * 110806		Hutch White			collect MMC parameters from mangled URL
 *
 */
 
function cmParseMMC(url){
	newURL=url;
	if (newURL.toLowerCase().indexOf("cm_ven")>-1){
		if (newURL.indexOf("?")==-1 || (newURL.toLowerCase().indexOf("cm_ven")<newURL.indexOf("?"))){
			ParamStart=newURL.toLowerCase().indexOf("cm_ven");
			if (newURL.indexOf("?")>-1){
				var LGparams=newURL.substring(ParamStart,newURL.indexOf("?"));
			}
			else{
				var LGparams=newURL.substr(ParamStart);
			}
			var params=LGparams.split("/");
			var keepParams=new Array();
			for(var i=0; i<params.length; i++) {
				if (params[i].indexOf("cm_ven__")>-1){
					keepParams[0]=params[i];
				}
				if (params[i].indexOf("cm_cat__")>-1){
					keepParams[1]=params[i];
				}		
				if (params[i].indexOf("cm_ite__")>-1){
					keepParams[2]=params[i];
				}		
				if (params[i].indexOf("cm_pla__")>-1){
					keepParams[3]=params[i];
				}
			}
			var qList=keepParams.join("&");
			pattern = /__/ig;
			qList = qList.replace(pattern, "=");
			if (newURL.indexOf("?")==-1){
				newURL+="?"+qList;
			}
			else {
				newURL+="&"+qList;
			}
			
		}
	}
	return newURL;
}


 // CM_EM Email Capture Code ---------------------------------------------------

function cmIndexOfParameter (parameter) {
	return document.URL.indexOf(parameter);
}

function cmCheckCMEM() {
	if (cmIndexOfParameter("cm_em") != -1){

		var s = document.URL.toLowerCase;
		var begin = s.indexOf("cm_em");
		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];
		}

		cmCreateRegistrationTag(emailAddress,emailAddress);
	}
}

// MANUAL TAGGING FUNCTIONS ---------------------------------------------------
function cmCreateManualLinkClickTag(href,name) {	
	if (cmHandleLinkClick == null && C9 != null) {
		var cmHandleLinkClick = C9;
	}
	if (cmHandleLinkClick != null) {		
		var link = new Object();
		link.tagName = "A";
		link.name = name; 
		link.href = href; 
		cmHandleLinkClick(link);
	}	
}

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();
}

// Global Variable Set
var cm_ClientID="90021491";
var cm_TrackLink="A";
/*
 * Calling this function points tags to the production database
 */
function cmSetProduction(){
	cm_HOST="www9.sharperimage.com/eluminate?"; 
}

// TAG GENERATING FUNCTIONS ---------------------------------------------------
/*
 * Creates a Tech Props tag.
 * pageID		: required. Page ID to set on this Pageview tag
 */
function cmCreateTechPropsTag(pageID) {
	if (pageID) {
		var cm=new _cm("tid", "6", "vn2", "e3.1");
		cm.pc="Y";
		cm.pi = pageID;
		cm.pn = cmScrubName(pageID);
		cm.ul=resetVCPIVariables(window.location.href);
		// 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.ul=cmParseMMC(document.URL);
		cmCheckCMEM();

		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, pageName, searchString, categoryID, ATC_location, numSearchResults) {
	if (pageID == null) {
		pageID = getDefaultPageID();
	}

	var cm = new _cm("tid", "1", "vn2", "e3.1");
	cm.pi = pageID;
	cm.ul=resetVCPIVariables(window.location.href);
	if (pageName) {
		cm.pn = cmScrubName(pageName);
	} else {
		cm.pn = cmScrubName(pageID);
	}
	if (searchString) {
		cm.se = searchString;
	}
	if (categoryID) {
		cm.cg = categoryID;
	}
	if (ATC_location) {
		cm.pv2 = ATC_location;
	}
	if (numSearchResults)
	{
		cm.sr = numSearchResults;
	}

	// 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.ul=cmParseMMC(document.URL);
	cmCheckCMEM();

	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.
 */
 
/* edm -- this is the real func */
function cmCreateProductviewTag(productID, productName, categoryID, SKU,prefix) {
	var cm = new _cm("tid", "5", "vn2", "e3.1");

	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 = "Y";
	cm.pn = cmScrubName(prefix + ": " + productName + " (" + productID + ")");
	cm.pi = prefix + ": (" + productID + ")";
	cm.pn = cmScrubName(cm.pi);
	cm.ul=resetVCPIVariables(window.location.href);

	if (SKU) {
		cm.pv1 = SKU;
	}
	cm.ul=cmParseMMC(document.URL);
	cmCheckCMEM();

	cm.writeImg();
}

/* edm 8/20/04 -- A temp stub to handle probs deploying cuz of akamai cache */
function cmCreateProductviewTag(productID, productName, categoryID, SKU) {
	var cm = new _cm("tid", "5", "vn2", "e3.1");

	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 = "Y";
	cm.pn = cmScrubName("PIP: " + productName + " (" + productID + ")");
	cm.pi = "PIP: (" + productID + ")";
	cm.pn = cmScrubName(cm.pi);
	cm.ul=resetVCPIVariables(window.location.href);

	if (SKU) {
		cm.pv1 = SKU;
	}
	cm.ul=cmParseMMC(document.URL);
	cmCheckCMEM();

	cm.writeImg();
}


/* edm 8/20/04 -- A temp stub to handle probs deploying cuz of akamai cache */
function cmCreateNewProductviewTag(productID, productName, categoryID, SKU) {
	var cm = new _cm("tid", "5", "vn2", "e3.1");

	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 = "Y";
	cm.pn = cmScrubName("PIPNEW: " + productName + " (" + productID + ")");
	cm.pi = "PIPNEW: (" + productID + ")";
	cm.pn = cmScrubName(cm.pi);
	cm.ul=resetVCPIVariables(window.location.href);

	if (SKU) {
		cm.pv1 = SKU;
	}				
	cmCheckCMEM();
	cm.ul=cmParseMMC(document.URL);
	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();

// these are for aggregating 8-digit SKU custom tags
var cmBySKUShopProducts = new Array();
var cmBySKUShopIds = new Array();
var cmBySKUShopSKUs = new Array();
var cmBySKUShopQtys = new Array();
var cmBySKUShopCounter = 0;
var cmBySKUShopOrderIds = new Array();
var cmBySKUShopCustomerIds = new Array();
var cmBySKUShopOrderPrices = new Array();

// these are for aggregating price custom tags
var cmByPriceShopProducts = new Array();
var cmByPriceShopIds = new Array();
var cmByPriceShopPrices = new Array();
var cmByPriceShopQtys = new Array();
var cmByPriceShopCounter = 0;
var cmByPriceShopOrderIds = new Array();
var cmByPriceShopCustomerIds = new Array();
var cmByPriceShopOrderPrices = 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;
}

/* Internal, to support price aggregation */
function cmGetPriceIndex(id, price){
	var i =0;
	for (i=0; i<cmByPriceShopCounter; i++)
	{
		if (id==cmByPriceShopIds[i] && parseFloat(price)==cmByPriceShopPrices[i])
		{
			return i;
		}
	}
	return -1;
}

/* Internal, to support SKU aggregation */
function cmGetSKUIndex(SKU){
	var i =0;
	for (i=0; i<cmBySKUShopCounter; i++)
	{
		if (SKU==cmBySKUShopSKUs[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, SKU) {
	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++;
	}
	/* aggregation code for custom tags for 8-digit SKU's */
	var index = cmGetSKUIndex(SKU);
	if (index != -1) {
		var oldQty = cmBySKUShopQtys[index];
		var newQty = oldQty + parseInt(productQuantity);
		cmBySKUShopQtys[index] = newQty;
	} else {
		if (!SKU) {
			SKU = "";
		}
		cmBySKUShopProducts[cmBySKUShopCounter] = productName;
		cmBySKUShopIds[cmBySKUShopCounter] = productID;
		cmBySKUShopSKUs[cmBySKUShopCounter] = SKU;
		cmBySKUShopQtys[cmBySKUShopCounter] = parseInt(productQuantity);
		cmBySKUShopCounter++;
	}
	/* aggregation code for custom tags for prices */
	var index = cmGetPriceIndex(productID, productPrice);
	if (index != -1) {
		var oldQty = cmByPriceShopQtys[index];
		var newQty = oldQty + parseInt(productQuantity);
		cmByPriceShopQtys[index] = newQty;
	} else {
		cmByPriceShopProducts[cmByPriceShopCounter] = productName;
		cmByPriceShopIds[cmByPriceShopCounter] = productID;
		cmByPriceShopPrices[cmByPriceShopCounter] = parseFloat(productPrice);
		cmByPriceShopQtys[cmByPriceShopCounter] = parseInt(productQuantity);
		cmByPriceShopCounter++;
	}
}

/* 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", "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;
	/* custom tags to capture 8-digit SKU's */
	for(i=0; i<cmBySKUShopCounter; i++) {
		var cm = new _cm("tid", "7", "vn2", "e3.1");
		cm.li = "1";
		cm.ps1 = "5";
		cm.ps2 = cmBySKUShopIds[i];
		cm.ps3 = cmBySKUShopProducts[i];
		cm.ps4 = cmBySKUShopSKUs[i];
		cm.ps5 = cmBySKUShopQtys[i];
		cm.pc = "N";
		cm.writeImg();
	}
	cmBySKUShopCounter=0;
	/* custom tags to capture prices */
	for(i=0; i<cmByPriceShopCounter; i++) {
		var cm = new _cm("tid", "7", "vn2", "e3.1");
		cm.li = "2";
		cm.ps1 = "5";
		cm.ps2 = cmByPriceShopIds[i];
		cm.ps3 = cmByPriceShopProducts[i];
		cm.ps5 = cmByPriceShopQtys[i];
		cm.ps6 = cmByPriceShopPrices[i];
		cm.pc = "N";
		cm.writeImg();
	}
	cmByPriceShopCounter=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, productQuantity,
				productPrice, customerID, orderID,
				orderTotal, categoryID, SKU) {
	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++;
	}
	/* aggregation code for custom tags for 8-digit SKU's */
	var index = cmGetSKUIndex(SKU);
	if (index != -1) {
		var oldQty = cmBySKUShopQtys[index];
		var newQty = oldQty + parseInt(productQuantity);
		cmBySKUShopQtys[index] = newQty;
	} else {
		if (!SKU) {
			SKU = "";
		}
		cmBySKUShopProducts[cmBySKUShopCounter] = productName;
		cmBySKUShopIds[cmBySKUShopCounter] = productID;
		cmBySKUShopSKUs[cmBySKUShopCounter] = SKU;
		cmBySKUShopQtys[cmBySKUShopCounter] = parseInt(productQuantity);
		cmBySKUShopOrderIds[cmBySKUShopCounter] = orderID;
		cmBySKUShopOrderPrices[cmBySKUShopCounter] = orderTotal;
		cmBySKUShopCustomerIds[cmBySKUShopCounter] = customerID;
		cmBySKUShopCounter++;
	}
	var index = cmGetPriceIndex(productID, productPrice);
	if(index!=-1){
		var oldQty = cmByPriceShopQtys[index];
		var newQty = oldQty + parseInt(productQuantity);
		cmByPriceShopQtys[index] = newQty;
	} else {
		cmByPriceShopProducts[cmByPriceShopCounter] = productName;
		cmByPriceShopIds[cmByPriceShopCounter] = productID;			
		cmByPriceShopQtys[cmByPriceShopCounter] = parseInt(productQuantity);
		cmByPriceShopPrices[cmByPriceShopCounter] = parseFloat(productPrice);
		cmByPriceShopOrderIds[cmByPriceShopCounter] = orderID;
		cmByPriceShopOrderPrices[cmByPriceShopCounter] = orderTotal;
		cmByPriceShopCustomerIds[cmByPriceShopCounter] = customerID;
		cmByPriceShopCounter++;
	}
}


/* 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.pc = "N";
		cm.writeImg();
	}
	cmShopCounter=0;
	/* custom tags to capture 8-digit SKU's */
	for(i=0; i<cmBySKUShopCounter; i++) {
		var cm = new _cm("tid", "7", "vn2", "e3.1");
		cm.li = "1";
		cm.ps1 = "9";
		cm.ps2 = cmBySKUShopIds[i];
		cm.ps3 = cmBySKUShopProducts[i];
		cm.ps4 = cmBySKUShopSKUs[i];
		cm.ps5 = cmBySKUShopQtys[i];
		cm.ps7 = cmBySKUShopCustomerIds[i];
		cm.ps8 = cmBySKUShopOrderIds[i];
		cm.ps9 = cmBySKUShopOrderPrices[i];
		cm.pc = "N";
		cm.writeImg();
	}
	cmBySKUShopCounter=0;
	/* custom tags to capture prices */
	for(i=0; i<cmByPriceShopCounter; i++) {
		var cm = new _cm("tid", "7", "vn2", "e3.1");
		cm.li = "2";
		cm.ps1 = "9";
		cm.ps2 = cmByPriceShopIds[i];
		cm.ps3 = cmByPriceShopProducts[i];
		cm.ps5 = cmByPriceShopQtys[i];
		cm.ps6 = cmByPriceShopPrices[i];
		cm.ps7 = cmByPriceShopCustomerIds[i];
		cm.ps8 = cmByPriceShopOrderIds[i];
		cm.ps9 = cmByPriceShopOrderPrices[i];
		cm.pc = "N";
		cm.writeImg();
	}
	cmByPriceShopCounter=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, orderSKUdata, orderShipping, TSIOrderNumber, 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;
		if (TSIOrderNumber) {
			cm.or1 = TSIOrderNumber;
		}

		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, customerEmail, customerCity,
				customerState, customerZIP, newsletterName, 
				subscribe) {
	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;
	}
	
	cm.writeImg();
}

/* Creates an Error Tag
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreateErrorTag(pageID) {
	var cm=new _cm("tid", "404", "vn2", "e3.1");  //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";
	if (pageID) {
		cm.pi = pageID;
	} else {
		cm.pi = getDefaultPageID();
	}
	cm.pn = cmScrubName(cm.pi);
	cm.ul=resetVCPIVariables(window.location.href);
		
	cmCheckCMEM();

	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); 
} 

/* MMC parameter reordering code */
var ampString="%26";
function cmExtractParameter(parameter, s) {
	var begin = s.indexOf(parameter);
	if (begin ==-1) {
		return "";
	} else {
		var end = s.indexOf("&", begin);
		if (end == -1) {
			end = s.indexOf("%26", begin);
			if (end==-1) {
				end = s.indexOf("?", begin);
				if (end==-1) {
					end = s.length;
				}
			} else {
				if (s.indexOf("?", begin)>-1) {
					end = Math.min(end, s.indexOf("?", begin));
				}
			}
		} else {
			if (s.indexOf("?", begin)>-1) {
				end = Math.min(end, s.indexOf("?", begin));
			}
			ampString="&";
		}
		var middle = s.indexOf("=", begin);
		return s.substring(middle + 1, end);
	}
}

function cmRemoveParameter(inS, value) {
	var begin = inS.indexOf(value);
	if (begin == -1) {
		return inS;
	} else {
		var end = inS.indexOf("&", begin);
		if (end == -1) {
			end = inS.indexOf("%26", begin);
			if (end==-1) {
				end = inS.indexOf("?", begin);
				if (end==-1) {
					end = inS.length;
				}
			} else {
				if (inS.indexOf("?", begin)>-1) {
					end = Math.min(end, inS.indexOf("?", begin));
				}
			}
		} else {
			if (inS.indexOf("?", begin)>-1) {
				end = Math.min(end, inS.indexOf("?", begin));
			}
		}
		if ((end<inS.length) && (inS.charAt(end) == "&")){
			end = end +1;
		} else {
			if ((end+2<inS.length) && (inS.charAt(end) == "%") && (inS.charAt(end+1) == "2") && (inS.charAt(end+2) == "6")) 
				end = end + 3;
		}

		return inS.substring(0, begin) + inS.substring(end, inS.length);
	}
}

function cmCorrectMMCParameters(inS) {
	// indeces
	// alert(inS);
	// alert(window.location.href);
	var iVen = inS.indexOf("cm_ven");
	var iCat = inS.indexOf("cm_cat");
	var iPla = inS.indexOf("cm_pla");
	var iItem = inS.indexOf("cm_ite");

	// values
	var vVen = cmExtractParameter("cm_ven",inS);
	var vCat = cmExtractParameter("cm_cat",inS);
	var vPla = cmExtractParameter("cm_pla",inS);
	var vItem = cmExtractParameter("cm_ite",inS);

	if ((iVen == -1) || (iItem == -1)) {
		// alert(1);
		return inS;
	}
	if ((!vVen) || (!vItem)) {
		// alert(vPla);
		return inS;
	}
	if ((iVen < iCat) && (iCat < iPla) && (iPla < iItem)) {
		// alert(3);
		return inS;
	}
	else {
		var out;
		out = cmRemoveParameter(inS, "cm_ven");
		out = cmRemoveParameter(out, "cm_cat");
		out = cmRemoveParameter(out, "cm_pla");
		out = cmRemoveParameter(out, "cm_ite");

		var MMCString = "cm_ven=" + vVen + ampString;
		if (vCat) {
			MMCString += "cm_cat=" + vCat + ampString;
		}
		if (vPla) {
			MMCString += "cm_pla=" + vPla + ampString;
		}
		MMCString += "cm_ite=" + vItem;

		if (out.charAt(out.length - 1) != "?") {
			MMCString = ampString + MMCString;
		}
		out = out + MMCString;
		// alert(out);
		return out;
	}
}

function resetVCPIVariables(url_string) {
	if (url_string.indexOf("cm_ven")<url_string.indexOf("?")){
		return url_string;
	}
	var pattern = /cm%5Fven/ig;
	var new_url = url_string;
	if (new_url.indexOf("?")<new_url.indexOf("&")){
		return url_string;
	}
	else {	
	new_url = new_url.replace(pattern, "cm_ven");

	pattern = /cm%5Fcat/ig;
	new_url = new_url.replace(pattern, "cm_cat");
	
	pattern = /cm%5Fpla/ig;
	new_url = new_url.replace(pattern, "cm_pla");
	
	pattern = /cm%5Fite/ig;
	new_url = new_url.replace(pattern, "cm_ite");
	return cmCorrectMMCParameters(new_url);
	}
}

function cmScrubName(name) {
	var illegalChars  = ['"','&'];

	for (i=0;i<illegalChars.length;i++) {           
		name = name.split(illegalChars[i]).join(' ');    
	}        
	return name;
}

if (defaultNormalize == null) { var defaultNormalize = null; }

function myNormalizeURL(url, isHref) {

    var newURL = url;
    var blackList = ["sessionid=", "requestid="];
    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;
    }
}
//-->