<!--
/*
 * cmdatatagutils.js
 * Coremetrics Tag v4.0, 12/10/2004
 * COPYRIGHT 1999-2002 COREMETRICS, INC. 
 * ALL RIGHTS RESERVED. U.S.PATENT PENDING
 */

// TAG GENERATING FUNCTIONS ---------------------------------------------------

function cmSetProduction(){
	cm_HOST="bacchus.figleaves.com/eluminate?"; 
}

function cmCreateTechPropsTag(pageID, categoryID, cm_ven, cm_cat, cm_pla, cm_ite) {
	if(pageID == null) {
		pageID = cmGetDefaultPageID();
	}
	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;
		if (cm_ven != null && cm_ite != null) {
			cm.rf = cm.rf + ((cm.rf.indexOf("?") < 0) ? "?" : "&") + "cm_ven=" + cm_ven;
			if (cm_cat != null) {
			cm.rf = cm.rf + "&" + "cm_cat=" + cm_cat;
			}
			if (cm_pla != null) {
			cm.rf = cm.rf + "&" + "cm_pla=" + cm_pla;
			}
			cm.rf = cm.rf + "&" + "cm_ite=" + cm_ite;
		}
	}
	// 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;
	}
	// checks to see if document.location.href has parameters or ?, then adds mmc to url
	if (cm_ven != null && cm_ite != null) {
		cm.ul = document.location.href + ((document.location.href.indexOf("?") < 0) ? "?" : "&") + "cm_ven=" + cm_ven;
		if (cm_cat != null) {
		cm.ul = cm.ul + "&" + "cm_cat=" + cm_cat;
		}
		if (cm_pla != null) {
		cm.ul = cm.ul + "&" + "cm_pla=" + cm_pla;
		}
		cm.ul = cm.ul + "&" + "cm_ite=" + cm_ite;
	}
	cm.addTP();

	cmCheckCookiePageData();

	cm.writeImg();
}

function cmCreatePageviewTag(pageID, categoryID, searchString, numSearchResults, cm_ven, cm_cat, cm_pla, cm_ite) {
	if (pageID == null) {
		pageID = cmGetDefaultPageID();
	}
	var cm = new _cm("tid", "1", "vn2", "e4.0");
	cm.pi = pageID;
	if (searchString) {
		cm.se = searchString;
	}
	if (categoryID) {
		cm.cg = categoryID;
	}
	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;
	}
	// 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;
	}
	// checks to see if document.location.href has parameters or ?, then adds mmc to url
	if (cm_ven != null && cm_ite != null) {
		cm.ul = document.location.href + ((document.location.href.indexOf("?") < 0) ? "?" : "&") + "cm_ven=" + cm_ven;
		if (cm_cat != null) {
		cm.ul = cm.ul + "&" + "cm_cat=" + cm_cat;
		}
		if (cm_pla != null) {
		cm.ul = cm.ul + "&" + "cm_pla=" + cm_pla;
		}
		cm.ul = cm.ul + "&" + "cm_ite=" + cm_ite;
	}

	cmCheckCookiePageData();

	cm.writeImg();
}

function cmCreateDefaultPageviewTag(categoryID) {
	cmCreatePageviewTag(cmGetDefaultPageID(), categoryID, null);
}

function cmCreateProductviewTag(productID, productName, categoryID, cm_ven, cm_cat, cm_pla, cm_ite) {
	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;
	}
	// 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;
	}
	// checks to see if document.location.href has parameters or ?, then adds mmc to url
	if (cm_ven != null && cm_ite != null) {
		cm.ul = document.location.href + ((document.location.href.indexOf("?") < 0) ? "?" : "&") + "cm_ven=" + cm_ven;
		if (cm_cat != null) {
		cm.ul = cm.ul + "&" + "cm_cat=" + cm_cat;
		}
		if (cm_pla != null) {
		cm.ul = cm.ul + "&" + "cm_pla=" + cm_pla;
		}
		cm.ul = cm.ul + "&" + "cm_ite=" + cm_ite;
	}
	cm.pr = productID;
	cm.pm = productName;
	cm.cg = categoryID;
	cm.pc = "Y";
	cm.pi = "PRODUCT: " + productName + " (" + productID + ")";

	cmCheckCookiePageData();

	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 cmShopCounter = 0;
var cmShopOrderIds = new Array();
var cmShopCustomerIds = new Array();
var cmShopOrderPrices = new Array();
var cmShopSKUs = "";

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) {
	productID = productID.toUpperCase();
	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++;
	}
	cmShopSKUs = cmGetOSK();
}

/* 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", "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";
		cm.writeImg();
	}
	cmShopSKUs = cmGetOSK();
	cmShopCounter=0;
}

function cmCreateShopAction9Tag(productID, productName, productQuantity,
				productPrice, customerID, orderID,
				orderTotal, categoryID) {
	productID = productID.toUpperCase();
	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;			
		cmShopOrderIds[cmShopCounter] = orderID;
		cmShopOrderPrices[cmShopCounter] = orderTotal;
		cmShopCustomerIds[cmShopCounter] = customerID;
		cmShopCats[cmShopCounter] = categoryID;
		cmShopQtys[cmShopCounter] = parseInt(productQuantity);
		cmShopPrices[cmShopCounter] = parseFloat(productPrice);
		cmShopQtys[index] = newQty;
		cmShopCounter++;
	}
	cmShopSKUs = cmGetOSK();

}


/* 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", "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];
		cm.pc = "N";
		cm.writeImg();
	}
	cmShopSKUs = cmGetOSK();
	cmShopCounter=0;
}

function cmCreateOrderTag(orderID, orderTotal, orderShipping, customerID, 
			  customerCity, customerState, customerZIP, customerCountry, voucherCode, voucherAmt, minimumSpent) {
		var cm = new _cm("tid", "3", "vn2", "e4.0");
		cm.on = orderID;
		cm.tr = orderTotal;
		cm.osk = cmShopSKUs;
		cm.sg = orderShipping;
		cm.cd = customerID;
		cm.sa = customerState;
		cm.ct = customerCity;
		cm.zp = customerZIP;
		cm.cy = customerCountry;
		cm.or1 = voucherCode;
		cm.or2 = voucherAmt;
		cm.or3 = minimumSpent;
		cm.writeImg();
}

function cmGetOSK() {
	var i =0;
	var result = "";
	for (i=0; i<cmShopCounter; i++)
	{
		result += "|" + cmShopIds[i] + "|" + cmShopPrices[i] + "|" + cmShopQtys[i] + "|";
	}
	return result;
}

function cmCreateRegistrationTag(customerID, customerEmail, customerCity, customerState, customerZIP,
				customerCountry, newsletterName, subscribe, customerGender) {
	var cm = new _cm("tid", "2", "vn2", "e4.0");
	cm.cd = customerID;
	cm.em = customerEmail;
	cm.sa = customerState;
	cm.ct = customerCity;
	cm.zp = customerZIP;
	cm.cy = customerCountry;
	cm.gd = customerGender;
	if (newsletterName && subscribe) {
		cm.nl = newsletterName;
		cm.sd = subscribe;
	}
	cm.writeImg();
}

function cmCreateErrorTag(pageID, categoryID) {
	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;
	}
	// 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;
	}
	cm.pc = "Y";
	if(pageID == null) {
		cm.pi = cmGetDefaultPageID();
	}
	cm.cg = categoryID;
	cm.writeImg();
}

// HELPER FUNCTIONS -----------------------------------------------------------
function cmCheckCookiePageData() {

	if (document.cookie.indexOf("cmdata") > -1) {
		var tempStart = document.cookie.indexOf("cmdata");
		var tempEnd = document.cookie.indexOf(";",tempStart);
		if (tempEnd == -1) {
			tempEnd = document.cookie.length;
		}
		var tempData = document.cookie.substring(tempStart + 7, tempEnd);
		if (tempData != "") {
			tempData = tempData.split("||");
			if (document.location.href != unescape(tempData[2])) {	
				var cm2 = new _cm("tid", "1", "vn2", "e4.0");
				cm2.pi = unescape(tempData[0]);
				cm2.cg = unescape(tempData[1]);
				cm2.ul = unescape(tempData[2]);
				cm2.rf = unescape(tempData[3]);

				mmc_temp_value = unescape(tempData[4]);
				if ((mmc_temp_value != null) && (mmc_temp_value != "") && (mmc_temp_value != "null")) {

					var mmc_String = "cm_ven=" + unescape(tempData[4]);
					mmc_temp_value = unescape(tempData[5]);
					if ((mmc_temp_value != null) && (mmc_temp_value != "") && (mmc_temp_value != "null")) {
						mmc_String += "&cm_cat=" + mmc_temp_value;
					}
					mmc_temp_value = unescape(tempData[6]);
					if ((mmc_temp_value != null) && (mmc_temp_value != "") && (mmc_temp_value != "null")) {
						mmc_String += "&cm_pla=" + mmc_temp_value;
					}
					mmc_String += "&cm_ite=" + unescape(tempData[7]);
					cm2.ul += ((cm2.ul.indexOf("?") < 0) ? "?" : "&") + mmc_String;

				}

				cm2.writeImg();
			}
		}
		document.cookie="cmdata=;domain=.figleaves.com;expires=Fri,02-Jan-1970 00:00:00 GMT";
	}

}

function cmGetDefaultPageID() { 
	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) {
	    var blackList = ["src=", "cookied=", "forced=", "on_sale=", "attribute=", "match=", "sequence=", "cookied=", "size=", "range_name=", "sale=", "address_id=", "calling=", "sel_method=", "q=", "addresstype=", "ShowXSell=", "t=", "lsize=", "calling_page=", "p_faqid=", "p_cat_lvl1=", "colour=", "u=", "vp_id=", "max_price=", "min_price=", "original=", "format=", "ss=", "mci="];
	    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;
    }
}
//-->
