<!--
/*
 * cmdatatagutils.js
 *
 * Coremetrics Tag v3.1, 2/28/2002
 * COPYRIGHT 1999-2003 COREMETRICS, INC.
 * ALL RIGHTS RESERVED. U.S.PATENT PENDING
 *
 * The following functions aid in the creation of Coremetrics data tags.
 *
 */

// TAG GENERATING FUNCTIONS ---------------------------------------------------


/*
 * Calling this function points tags to the production database
 */
function cmSetProduction(){
	cm_HOST="iacg.cabelasgames.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();
	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, categoryID, categoryName, searchString, pageNo, pv1, pv2, pv3, pv4, pv5, pv6, pv7, pv8, pv9, pv10) {
	var cm = new _cm("tid", "1", "vn2", "e3.1");
	if (pageID == null) {
		cm.pi = getDefaultPageID();
	}
	else {
		cm.pi = pageID;
	}
	if (pageName) {
		cm.pn = pageName;
	}
	if (searchString) {
		cm.se = searchString;
	}
	if (categoryID) {
		cm.cg = categoryID;
		cm.cl = categoryName;
		cm.pi = "CATEGORY: " + categoryName + " (" + categoryID + ")";
	}
	if (pageNo) {
		if ((pageNo != "") && (pageNo != "1")) {
			cm.pi = cm.pi + " PG: " + pageNo;
		}
	}
	if (pv1) { cm.pv1 = pv1;}
	if (pv2) { cm.pv2 = pv2;}
	if (pv3) { cm.pv3 = pv3;}
	if (pv4) { cm.pv4 = pv4;}
	if (pv5) { cm.pv5 = pv5;}
	if (pv6) { cm.pv6 = pv6;}
	if (pv7) { cm.pv7 = pv7;}
	if (pv8) { cm.pv8 = pv8;}
	if (pv9) { cm.pv9 = pv9;}
	if (pv10) { cm.pv10 = pv10;}
	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());
}

/*
 * Creates a Pageview tag appropriate for category pages.
 * Format of Page ID is "CATEGORY: <category Name> (<category ID>)"
 *
 * 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.
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreateCategoryPageviewTag(categoryID, categoryName) {
	cmCreatePageviewTag(null, null, categoryID, categoryName);
}

/*
 * 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) {
	var cm = new _cm("tid", "5", "vn2", "e3.1");

	cm.pr = productID
	cm.pm = productName;
	cm.cg = categoryID;

	var overrideCategory = cmExtractParameter("cmCat");
	if (overrideCategory != "") {
		cm.cg = overrideCategory;
	}
	cm.pc = "Y";
	cm.pi = "PRODUCT: " + productName + " (" + productID + ")";

	cm.writeImg();
}

/*
 * Variables and Arrays to support Lineitem Aggregation
 */

var cmShopCounter = 0;

var cmShopCustID = "";
var cmShopOrderID = "";
var cmShopOrderTotal = "";
var cmTempOrderID = "";

var cmShopProductID = new Array();
var cmShopProductQuan = new Array();
var cmShopProductPrice = new Array();
var cmShopProductCat = new Array();
var cmShopCustomSKUData = new Array();
var cmShopBackordered = new Array();

/* Internal, sets a session cookie with a random number for orderID in case AS/400 is down */
function cmCheckOrderID(orderID) {
	if (orderID != "0") {
		return orderID;
	}
	else if (cmTempOrderID != "") {
		return cmTempOrderID;
	}
	else {
		var cookieData = document.cookie;
		var pos = cookieData.indexOf("cmtemporderid=");
		if (pos != -1) {
			var start = pos + 14;
			var end = cookieData.indexOf(";", start);
			if (end == -1) {
				end = cookieData.length;
			}
			var value = cookieData.substring(start, end);
			value = unescape(value);
			cmTempOrderID = value;
			return cmTempOrderID;
		}
		else {
			// create the cookie
			var dateValue = new Date();
			cmTempOrderID = dateValue.getTime();
			document.cookie = "cmtemporderid=" + cmTempOrderID + ";secure";
			return cmTempOrderID;
		}
	}
}

/* Internal, to support aggregation */
function cmGetProductIndex(id){
	var i =0;
	for (i=0; i<cmShopCounter; i++)
	{
		if (id==cmShopProductID[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, productQuantity, productPrice, categoryID, backOrdered, sku) {
	var index = cmGetProductIndex(productID);
	if(index!=-1){
		cmShopProductQuan[index] += parseInt(productQuantity);
		cmShopCustomSKUData[index] += "|" + sku + "|" + productQuantity + "|" + productPrice + "|";
	} else {
		cmShopProductID[cmShopCounter] = productID;
		cmShopProductCat[cmShopCounter] = categoryID;
		cmShopProductQuan[cmShopCounter] = parseInt(productQuantity);
		cmShopProductPrice[cmShopCounter] = parseFloat(productPrice);
		cmShopBackordered[cmShopCounter] = backOrdered;
		cmShopCustomSKUData[cmShopCounter] = "|" + sku + "|" + productQuantity + "|" + productPrice + "|";
		cmShopCounter++;
	}
}

/* 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 = cmShopProductID[i];
		cm.cg = cmShopProductCat[i];
		cm.qt = cmShopProductQuan[i] ;
		cm.bp = cmShopProductPrice[i];
		cm.sx1 = cmShopBackordered[i];
		cm.sx2 = cmShopCustomSKUData[i];
		cm.pc = "N";
		cm.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, productQuantity,
				productPrice, customerID, orderID,
				orderTotal, categoryID,backOrdered,sku) {
	var index = cmGetProductIndex(productID);
	if(index!=-1){
		cmShopProductQuan[index] += parseInt(productQuantity);
		cmShopCustomSKUData[index] += "|" + sku + "|" + productQuantity + "|" + productPrice + "|";
	} else {
		cmShopProductID[cmShopCounter] = productID;
		cmShopProductCat[cmShopCounter] = categoryID;
		cmShopProductQuan[cmShopCounter] = parseInt(productQuantity);
		cmShopProductPrice[cmShopCounter] = parseFloat(productPrice);
		cmShopBackordered[cmShopCounter] = backOrdered;
		cmShopCustomSKUData[cmShopCounter] = "|" + sku + "|" + productQuantity + "|" + productPrice + "|";
		cmShopCustID = customerID;
		cmShopOrderID = cmCheckOrderID(orderID);
		cmShopOrderTotal = orderTotal;
		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 = cmShopProductID[i];
		cm.cg = cmShopProductCat[i];
		cm.qt = cmShopProductQuan[i] ;
		cm.bp = cmShopProductPrice[i];
		cm.cd = cmShopCustID;
		cm.on = cmShopOrderID;
		cm.tr = cmShopOrderTotal;
		cm.sx1 = cmShopBackordered[i];
		cm.sx2 = cmShopCustomSKUData[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, email,
			  newsletter ,subscribed , basketID) {

	var cm = new _cm("tid", "3", "vn2", "e3.1");
	cm.on = cmCheckOrderID(orderID);
	cm.tr = orderTotal;
	cm.osk = getOSK();
	cm.sg = orderShipping;
	cm.cd = customerID;
	cm.sa = customerState;
	cm.ct = customerCity;
	cm.zp = customerZIP;
	cm.or1 = basketID;
	cm.pi = getDefaultPageID();
	cm.pc = "Y";
	cm.writeImg();

	cm = new _cm("tid", "2", "vn2", "e3.1");
	cm.cd = customerID;
	cm.em = email;
	cm.sa = customerState;
	cm.ct = customerCity;
	cm.zp = customerZIP;
	if (newsletter && subscribed) {
		cm.nl = newsletter;
		if (subscribed == "true") {
			cm.sd = "Y";
		}
		else {
			cm.sd = "N";
		}
	}
	cm.writeImg();

}

function getOSK() {
	var i =0;
	var result = "";
	for (i=0; i<cmShopCounter; i++)
	{
		result += "|" + cmShopProductID[i] + "|" + cmShopProductPrice[i] + "|" + cmShopProductQuan[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;
		if (subscribe == "true") {
			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() {
	var cm=new _cm("tid", "404", "vn2", "e3.1");
	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);
}

/*
 * 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 "";
    }
	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);
}

function cmExternalClick(link) {
	var image1 = new Image();
	var cm = new _cm("tid", "1", "vn2", "e3.1");
	var vartype = typeof link;
	if (vartype == "Link") {
		cm.pi = "External: " + link.href;
		cm.ul = "http://www.cabelas.com/?URL=" + link.href;
	}
	else {
		cm.pi = "External: " + link;
		cm.ul = "http://www.cabelas.com/?URL=" + link;
	}
	cm.rf = document.location.href;
	image1.src = cm.getImgSrc();
	return true;
}
//-->