/*
 * Coremetrics: Page View Tag
 */
function cmCreatePageViewTag() {
  var cm = new _cm("tid", "1", "vn2", "e3.1");
  cm.pi = getFileNameFromURL();
  cm.writeImg();
}

/*
 * Coremetrics: 404 Error Page Tag
 */
function cmCreateErrorTag() {
  var cm = new _cm("tid", "404", "vn2", "e3.1");
  cm.pi = getFileNameFromURL();
  cm.pc = "Y";
  cm.writeImg();
}

/*
 * Coremetrics: Shop Action 5
 *
 * Note that productSku is the SKU of the parent if the product is a child.
 * The only exception to this rule occurs when dealing with a Gift Card.  In
 * this case we want the product SKU to be the SKU of the child Gift Card.
 *
 * productSku - SKU of the product
 * quantity - The quantity of this product for this line item
 * unitPrice - The price (AFTER discounts) of the product
 *
 * Page Count is set to "N"
 */
function cmCreateShopAction5Tag(productSku, quantity, unitPrice, category) {
  var cm = new _cm("tid", "4", "vn2", "e3.1");
  cm.pr = productSku;
  cm.at = "5";
  cm.qt = quantity;
  cm.bp = unitPrice;
  cm.pc = "N";
  cm.cg = category;
  cm.writeImg();
}


/*
 * Coremetrics: Shop Action 9
 *
 * Note that productSku is the Sku of the parent if the product is a child.
 * The only exception to this rule occurs when dealing with a Gift Card.  In
 * this case we want the product SKU to be the SKU of the child Gift Card.
 *
 * productSku - SKU of the product
 * shopperId - The unique shopper ID assigned to this registered user
 * quantity - The quantity of this product for this line item
 * unitPrice - The price (AFTER discounts) of the product
 * slotNumber - A UNIQUE value (for the quote) for this line item.  Usually
 *              set to the line item number for this product
 * orderId - Unique order ID for the order containing this product
 * subTotal - Order total (BEFORE taxes and shipping are applied)
 * productName - The name assigned to the product for this line item
 * category - The category assigned to this product
 *
 * Page Count is set to "N"
 */
function cmCreateShopAction9Tag(productSku, shopperId, quantity, unitPrice,
    slotNumber, orderId, subTotal, productName, category) {

  var cm = new _cm("tid", "4", "vn2", "e3.1");
  cm.at = "9";
  cm.pc = "N";
  cm.rf = "http://fakeurl.com";

  cm.pr = productSku;
  cm.cd = shopperId;
  cm.qt = quantity;
  cm.bp = unitPrice;

  // Leo from Coremetrics said to do this.
//  cm.sn = slotNumber;
  cm.sn = 1;

  cm.on = orderId;
  cm.tr = subTotal;
  cm.pm = productName;
  cm.cg = category;

  cm.writeImg();
}


/*
 * Coremetrics: Order Tag
 *
 * orderId - Unique order ID for the order containing this product
 * shopperId - The unique shopper ID assigned to this registered user
 * subTotal - Order total (BEFORE taxes and shipping are applied)
 * skuData - A string containing all line items contained in the order.  This
 *           string MUST be in the form of: |<SKU>|<quantity>|<unit price>|
 * city - The billing city
 * state - The billing state
 * zip - The billing zip
 * country - The billing country
 *
 * Page Count is set to "N"
 */
function cmCreateOrderTag(orderId, shopperId, subTotal, shippingTotal, skuData, city,
    state, zip, country) {

  var cm = new _cm("tid", "3", "vn2", "e3.1");
  cm.pi = "Order Confirmation";
  cm.pc = "N";
  cm.rf = "http://michaels.com";

  cm.on = orderId;
  cm.cd = shopperId;
  cm.tr = subTotal;
  cm.sg = shippingTotal;
  cm.osk = skuData;
  cm.ct = city;
  cm.sa = state;
  cm.zp = zip;
  cm.cy = country;

  cm.writeImg();
}


/*
 * Coremetrics: Registration Tag
 *
 * shopperId - The unique shopper ID assigned to this registered user
 * email - The e-mail address of the registered user
 * suscribe - A ONE character string that lists whethe someone is subscribed ("Y")
 *            or not subscribed ("N")
 * newsLetterName - The name given to the subscribed newsletter/e-mail campaign
 * countPage - A ONE character string that determines whether this tag should count
 *             as a page view.  Valid values are "Y" or "N"
 * pageID - If countPage="Y", a pageID MUST be supplied
 */
function cmCreateRegistrationTag(shopperId, email, subscribe, newsLetterName,
                                 countPage, pageID) {

  var cm = new _cm("tid", "2", "vn2", "e3.1");
  cm.pc = countPage;
  if (countPage == "Y")
	  cm.pi = pageID;
  cm.rf = "http://www.michaels.com";

  cm.cd = shopperId;
  cm.em = email;
  if(subscribe == "") {
    subscribe = "Y";
  }
  cm.sd = subscribe;
  cm.nl = newsLetterName;

  cm.writeImg();
}

/*
 * Coremetrics: Product Tag
 *
 * productName - The name assigned to the product for this line item
 * productSku - SKU of the product
 *
 * Page Count is set to "Y"
 */
function cmCreateProductTag(productName, productSku, categoryId, categoryName) {
  var cm = new _cm("tid", "5", "vn2", "e3.1");
  cm.pc = "Y";
  cm.pi = "PRODUCT: "+productName+" ("+productSku+")";
  cm.pr = productSku;
  cm.pm = productName;
  cm.cg = categoryId;
  cm.cl = categoryName;

  cm.writeImg();
}


/*
 * Retrieves a generically created page name for the current URL.  This
 * correctly handles files located in the ncommerce3 directory.  Other
 * URLs will simply return the portion of the URL after the server name
 * and will not include GET parameters.
 */
function getFileNameFromURL() {
  var pageName = window.location.pathname;
  var paramPos = pageName.indexOf("?");
  if (paramPos == -1)
  	paramPos = pageName.length;

  var removeStr = "ncommerce3/";
  var removeStrLen = removeStr.length;
  var afterCgiBinPos = pageName.indexOf(removeStr) + removeStrLen;
  // If removeStr does not exist in the pathname, the value of
  // afterCgiBinPos will be -1 + removeStrLen (because the indexOf
  // function will return -1).  Therefore, check for this value
  // and set afterCgiBinPos to 0 so as not to change the resulting
  // page name
  if (afterCgiBinPos == (removeStrLen - 1))
  	afterCgiBinPos = 0;

  pageName = pageName.substr(afterCgiBinPos, paramPos - afterCgiBinPos);
  if (pageName.charAt(0) == "/")
  	pageName = pageName.substr(1, pageName.length - 1);

  return pageName;
}