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();
}

/*
 * Takes the URL of the current page and removes its extension, query string
 * and web site/directory.  For example, this function will return the string
 * "MyPage" for the URL http://www.mysite.com/directory1/directory2/MyPage.jsp"
 * and removeExtension == true, removePath == true
 */
function getFileNameFromURL(removeExtension, removePath) {
    var pageName = window.location.pathname;

    if (removeExtension) {
        var extensionPos = pageName.lastIndexOf(".");
        if (extensionPos != -1) {
            pageName = pageName.substr(0, extensionPos);
        }
    }

    if (removePath) {
        var parentDirPos = pageName.lastIndexOf("/");
        if (parentDirPos != -1) {
            pageName = pageName.substr(parentDirPos + 1, pageName.length - parentDirPos - 1);
        }
    } else {
        var rootDirPos = pageName.indexOf("/");
        if (rootDirPos != -1) {
            pageName = pageName.substr(rootDirPos + 1, pageName.length - rootDirPos - 1);
        }
    }
    if (pageName == "") {
        pageName = "default.asp";
    }
    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 indexOfParameter (parameter) {
    return document.URL.indexOf(parameter);
}

/*
 * Extracts the value of a name-value pair in the request search string.
 */
function extractParameter (parameter) {
    if (indexOfParameter(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);
    return s.substring(middle + 1, end);
}

/*
 * Creates a simple page view tag
 */
function cmCreatePageViewTag(search_term, pv1) {

    var checkEmailParam = extractParameter("cm_cmem");
   checkEmailParam = unescape(checkEmailParam);
    if (checkEmailParam == "") {
        var cm = new _cm("tid", "1", "vn2", "e4.0");

        if (search_term != null) {
            cm.se = search_term;
        }
        if (pv1 != null) {
            cm.pv1 = pv1;
        }
        cm.pi = getFileNameFromURL(false,false);
        cm.writeImg();
    }
    else {
        var cm = new _cm("tid", "2", "vn2", "e4.0");
        cm.pi = getFileNameFromURL(false,false);
        cm.em = checkEmailParam;
        cm.cd = "CM:" + cm.em;
        cm.pc = "Y";
        if (pv1 != null) {
            cm.pv1 = pv1;
        }
        cm.writeImg();
    }
}

/*
 * Creates a category tag with the following format:
 *
 * Page ID = CATEGORY: <cat_name> (<cat_id>)
 * Category = <cat_name> (<cat_id>)
 *
 * page_num: The current page number for this category page.  Page numbers begin with
 *           "1" and only the first page will not display the page number in the page ID.
 *
 * Page count = Y
 */
function cmCreateCategoryTag(cat_name, cat_id) {

    var checkEmailParam = extractParameter("cm_cmem");
    if (checkEmailParam == "") {
        var cm = new _cm("tid", "1", "vn2", "e4.0");
        cm.pi = "CATEGORY: " + cat_name + " (" + cat_id + ")";
        cm.cg = cat_id;
        cm.cl = cat_name + " (" + cat_id + ")";

        cm.writeImg();
    }
    else {
        var cm = new _cm("tid", "2", "vn2", "e4.0");
        cm.pi = "CATEGORY: " + cat_name + " (" + cat_id + ")";
        cm.em = checkEmailParam;
        cm.cd = "CM:" + cm.em;
        cm.pc = "Y";

        cm.writeImg();
    }
}

/*
 * Creates a Showcase tag with the following format:
 *
 * Page ID = Showcase: <sc_name> (<mfg_id>)
 *
 * page_num: The current page number for this category page.  Page numbers begin with
 *           "1" and only the first page will not display the page number in the page ID.
 *
 * Page count = Y
 */
function cmCreateShowcaseTag(sc_name, mfg_id) {

    var checkEmailParam = extractParameter("cm_cmem");
    if (checkEmailParam == "") {
        var cm = new _cm("tid", "1", "vn2", "e4.0");
        cm.pi = "Showcase: " + sc_name + " (" + mfg_id + ")";

        cm.writeImg();
    }
    else {
        var cm = new _cm("tid", "1", "vn2", "e4.0");
        cm.pi = "Showcase: " + sc_name + " (" + mfg_id + ")";
        cm.em = checkEmailParam;
        cm.cd = "CM:" + cm.em;
        cm.pc = "Y";

        cm.writeImg();
    }
}

/*
 * Creates a product tag in the following format:
 *
 * Page ID = PRODUCT: <cat_name> (<cat_id>):<prod_name> (<prod_id>)
 * Product ID = <prod_id>
 * Product Name = <prod_name>
 * Category ID = <cat_id>
 * Category Name = <cat_name>
 * placement = HOMEPAGE, SEARCH, etc
 * MAP = is this a MAP product, "Y" or "N"
 * rebate = M:mail-in I:instant
 * availability = 48 Hours, 3-6 Days,...
 * pennyship = Y/N
 *
 * Page count = Y
 *      NOTE: Since this tag is not being created as a page view,
 *            you must have another tag on the same page (like a Page
 *            View, for example) with pc = "Y"
 */
function cmCreateProductTag(cat_name, cat_id, prod_name, prod_id, prod_sku, placement, MAP, rebate, availability, pennyship,totalReviewCount,avgRating) {

    var checkEmailParam = extractParameter("cm_cmem");
    if (checkEmailParam == "") {
        // normal productview
        var cm = new _cm("tid", "5", "vn2", "e4.0");
        cm.pi = "PRODUCT: " + prod_name + " (" + prod_id + ")";
        cm.pr = prod_id;
        cm.pm = prod_name + " (" + prod_sku + ")";
        cm.cg = cat_id;
        cm.cl = cat_name + " (" + cat_id + ")";
        cm.pc = "Y";
        cm.writeImg();

        // custom tag
        var cm = new _cm("tid", "7", "vn2", "e4.0");
        cm.li = 1;
        cm.ps1 = prod_id;
        cm.ps2 = placement;
        cm.ps3 = MAP;
        cm.ps4 = rebate;
        cm.ps5 = availability;
        cm.ps6 = pennyship;
        cm.ps7 = "";
        cm.writeImg();
    }
    else {
        // registration tag
        var cm = new _cm("tid", "2", "vn2", "e4.0");
        cm.pi = "PRODUCT: " + prod_name + " (" + prod_id + ")";
        cm.em = checkEmailParam;
        cm.cd = "CM:" + cm.em;
        cm.pc = "Y";

        cm.writeImg();

        // custom tag
        var cm = new _cm("tid", "7", "vn2", "e4.0");
        cm.li = 1;
        cm.ps1 = prod_id;
        cm.ps2 = placement;
        cm.ps3 = MAP;
        cm.ps4 = rebate;
        cm.ps5 = availability;
        cm.ps6 = pennyship;
        cm.ps7 = "";
        cm.writeImg();
    }
   if (totalReviewCount || avgRating){
      cm.li=10300;
      cm.ps1 = prod_id;
      cm.ps2 = prod_name;
      cm.ps3 = cat_id;
      if (totalReviewCount){
         cm.ps4=totalReviewCount;
      }
      if (avgRating){
         cm.ps5=avgRating;
      }
      cm.ps6 = "";
        cm.ps7 = "";
      cm.writeImg();
   }
}

function cmCreateBazaarViewTag(productID, productName,categoryID) {
      var cm = new _cm("tid", "7", "vn2", "e4.0");
      cm.li = 10301;
      cm.ps1 = productID;
      cm.ps2 = productName;
      cm.ps3 = categoryID;
      cm.writeImg();
}

/*
 * Creates a shop action 5 tag in the following format:
 *
 * Product ID = <prod_id>
 * Quantity = <prod_qty>
 * Base Price = <prod_price>
 *
 * Page count = N
 *      NOTE: Since this tag is not being created as a page view,
 *            you must have another tag on the same page (like a Page
 *            View, for example) with pc = "Y"
 */
function cmCreateShopAction5Tag(prod_id, prod_name, prod_sku, prod_qty, prod_price, cat_id, rebate, placement, MAP, availability, pennyship, addtocart) {
    var cm = new _cm("tid", "4", "vn2", "e4.0");
    cm.at = "5";
    cm.pr = prod_id;
    cm.pm = prod_name + " (" + prod_sku + ")";
    cm.qt = prod_qty;
    cm.bp = prod_price;
    cm.cg = cat_id;
    cm.pc = "N";
    cm.sx1 = "";
    if (placement) {
        cm.sx2 = placement;
    }
    if (MAP) {
        cm.sx3 = MAP;
    }
    if (rebate) {
        cm.sx4 = rebate;
    }
    if (availability) {
        cm.sx5 = availability;
    }
    if (pennyship) {
        cm.sx6 = pennyship;
    }
    if (addtocart) {
        cm.sx7 = addtocart;
    }

    cm.writeImg();
}

/*
 * Creates a shop action 9 tag in the following format:
 *
 * Product ID = <prod_id>
 * Product Name = <prod_name>
 * Quantity = <prod_qty>
 * Base Price = <prod_base_price>
 * Customer ID = <cust_id>
 * Order ID = <order_id>
 * Order Subtotal = <order_price>
 *
 * Page count = N
 *      NOTE: Since this tag is not being created as a page view,
 *            you must have another tag on the same page (like a Page
 *            View, for example) with pc = "Y"
 */
function cmCreateShopAction9Tag(order_id, cust_id, order_price,
                                prod_id, prod_name, prod_sku, prod_qty, prod_base_price, cat_id, rebate, placement, MAP, availability, pennyship, addtocart) {
    var cm = new _cm("tid", "4", "vn2", "e4.0");
    cm.at = "9";
    cm.pr = prod_id;
    cm.pm = prod_name + " (" + prod_sku + ")";
    cm.qt = prod_qty;
    cm.bp = prod_base_price;
    cm.cd = cust_id;
    cm.on = order_id;
    cm.tr = order_price;
    cm.cg = cat_id;
    cm.pc = "N";
    cm.sx1 = "";
    if (placement) {
        cm.sx2 = placement;
    }
    if (MAP) {
        cm.sx3 = MAP;
    }
    if (rebate) {
        cm.sx4 = rebate;
    }
    if (availability) {
        cm.sx5 = availability;
    }
    if (pennyship) {
        cm.sx6 = pennyship;
    }
    if (addtocart) {
        cm.sx7 = addtocart;
    }

    cm.writeImg();
}

/*
 * Creates an order tag in the following format:
 *
 * Customer ID = <cust_id>
 * Order ID = <order_id>
 * Order subtotal = <subtotal>
 * Order SKU data = <sku_data>
 * Shipping cost = <shipping>
 * State = <state>
 * City = <city>
 * Zip = <zip>
 *
 * Page count = N
 *      NOTE: Since this tag is not being created as a page view,
 *            you must have another tag on the same page (like a Page
 *            View, for example) with pc = "Y"
 */
function cmCreateOrderTag(order_id, cust_id, subtotal, shipping, sku_data,
                            city, state, zip, logged_in, gf_type) {
    var cm = new _cm("tid", "3", "vn2", "e4.0");
    cm.cd = cust_id;
    cm.on = order_id;
    cm.tr = subtotal;
    cm.osk = sku_data;
    cm.sg = shipping;
    cm.sa = state;
    cm.ct = city;
    cm.zp = zip;
    cm.pc = "N";
    cm.or1 = logged_in;
    cm.or2 = gf_type;

    cm.writeImg();
}

/*
 * Creates a registration tag in the following format:
 *
 * Newsletter Name = <newsletter_name> (Only required if subscribed = "Y")
 * Customer ID = <cust_id>
 * E-mail = <email>
 * Subscribed Flag = <subscribed>
 *      NOTE: subscribed MUST be either the string "Y" or "N"  If subscribed = "Y", you must
 *             set newsletter_name
 * Page count = <countPage>
 *      NOTE: countPage MUST be either the string "Y" or "N"
 *      NOTE: If this tag is not being created as a page view,
 *            you must have another tag on the same page (like a Page
 *            View, for example) with pc = "Y"
 */
function cmCreateRegistrationTag(newsletter_name, subscribed, email, city, state, zip) {
    var cm = new _cm("tid", "2", "vn2", "e4.0");
    cm.pi = getFileNameFromURL(false, false);
    cm.cd = email;
    cm.em = email;
    cm.pc = "Y";
    cm.sd = subscribed;
    cm.nl = newsletter_name;

    if (city != null) {
        cm.ct = city;
        cm.sa = state;
        cm.zp = zip;
    }

    cm.writeImg();
}

/*
 * Creates an error tag
 */
function cmCreateErrorTag(ul) {
    var cm=new _cm("tid", "404", "vn2", "e4.0");  //DO NOT CHANGE THESE PARAMETERS
    cm.ul = ul;

    cm.writeImg();
}

/*
 * New function for search tag.
 * This is required as filename sent to CM is changed.
 */
function cmCreateSearchTag(pi, search_term, pv1, num_search_results) {
    var cm = new _cm("tid", "1", "vn2", "e4.0");

    if (search_term != null) {
        cm.se = search_term;
    }
    if (pv1 != null) {
        cm.pv1 = pv1;
    }
    if (pi == "") {
        pi = getFileNameFromURL(false,false);
    }
   cm.sr = num_search_results;
    cm.pi = pi;
    cm.writeImg();
}

/*
 * New function for power search tag.
 * This is required as filename sent to CM is changed.
 */
function cmCreatePowerSearchTag(pi, search_term, num_search_results) {
    var cm = new _cm("tid", "1", "vn2", "e4.0");

    if (search_term != null) {
        cm.se = search_term;
    }

    if (pi == "") {
        pi = getFileNameFromURL(false,false);
    }

   cm.sr = num_search_results;
    cm.pi = pi;
    cm.writeImg();
}

/** BELOW WAS ADDED TO SUPPORT EXTERNAL LINK TRACKING **/

/*
 * Throw a pageview when user clicks a link that will leave the site.
 * This function serves as an onclick handler for external links.,
 */
function cmPageviewOnClick(pageID, theURL) {
    var cm = new _cm("tid", "1", "vn2", "e4.0");
    cm.pi = pageID;

    if (theURL)
    {
        cm.ul = theURL;
    }
   cm.writeImg();
}

/* Support for the onclick handler above.  Returns URL of a pageview tag
 * rather than directly requesting the tag.
 */
function cmGetPageviewTagSrc(pageID, theURL){
    if (pageID) {
        var cm = new _cm("tid", "1", "vn2", "e4.0");
        cm.pi = pageID;

        if (theURL)
        {
            cm.ul = theURL;
        }
        return cm.getImgSrc();
    } else {
        return "";
    }
}

/* Normalization code for new 4.0 functionality
 *
 * DO NOT MODIFY
 *
 */
if (defaultNormalize == null) { var defaultNormalize = null; }

/* This normalization function takes a list of parameters and parses out
   all url parameters that ARE in that list.  This only handles the simple case of
   basic url parameters in the query string.  */
function myNormalizeURL(url, isHref) {

    var newURL = url;

    if (isHref) {
       var blackList = ["ref=", "errors="];
       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;
    }
}

/*
 * Creates a simple page view tag
 */
function cmCreateInstantServiceTag(agent_name, department) {

     var cm = new _cm("tid", "1", "vn2", "e4.0");

     if (agent_name != null) {
         cm.pi = agent_name;
     }
     else {
        cm.pi = 'InstantService';
     }
     if (department != null) {
         cm.cg = department;
     }
     cm.writeImg();
}


/**
 * code moved from default.asp
 *  */
function cmHomePageTag(homePage)
{
   var checkEmailParam = extractParameter("cm_cmem");
   if (checkEmailParam == "") {
     var cm = new _cm("tid", "6", "vn2", "e3.1");
     cm.pi=homePage;
     cm.pc = "Y";
     cm.addTP();
   }
   else {
     var cm = new _cm("tid", "2", "vn2", "e3.1");
     cm.pi=homePage;
     cm.em = checkEmailParam;
     cm.cd = "CM:" + cm.em;
     cm.pc = "Y";
   }
   cm.writeImg();
}
//-->
