<!--

function cmSetProduction(){
	cm_HOST="data.coremetrics.com/eluminate?"; 
}

function cmCreateTechPropsTag(pageID) {
	var err = cmExtractParameter("cm_err");
	if (err != "") {
		cmCreateErrorTag("Generic Error");
	}
	else {
		var cm=new _cm("tid", "6", "vn2", "e3.1");
		cm.rf = checkReferrer();
		cm.pc="Y";
		cm.pi = pageID;
		cm.ul = resetVCPIVariables(window.location.href);
		cm.addTP();
		cm.writeImg();
	}
}

function cmCreatePageviewTag(pageID, searchString, categoryID) {
	if (pageID == null) {
		pageID = getDefaultPageID();
	}

	var cm = new _cm("tid", "1", "vn2", "e3.1");
	cm.rf = checkReferrer();
	cm.pi = pageID;
	if (searchString) {
		cm.se = searchString;
	}
	if (categoryID) {
		cm.cg = categoryID;
	}
	cm.rf = removeKryptoParameter(document.referrer);
	cm.ul = removeKryptoParameter(window.location.href);
	cm.ul = resetVCPIVariables(cm.ul);
	cm.writeImg();
}

function cmCreateCategoryPageviewTag(catName, catID, pageNo) {
	var cm = new _cm("tid", "1", "vn2", "e3.1");
	cm.pi = "CATEGORY: " + catName + " (" + catID + ")";
	cm.cg = catID;
	cm.cl = catName;
	cm.rf = checkReferrer();
	if (pageNo) {
		if (pageNo != "1") {
			cm.pi = cm.pi + " PAGE: " + pageNo;
		}
	}
	cm.ul = resetVCPIVariables(window.location.href);
	cm.writeImg();
}

function cmCreateDefaultPageviewTag() {
	cmCreatePageviewTag(getDefaultPageID(), null, null);
}

function cmCreateProductviewTag(productID, productName, categoryID) {
	var cm = new _cm("tid", "5", "vn2", "e3.1");

	if (productName == null) {
		productName = "";
	}
	cm.rf = checkReferrer();
	cm.pr = productID
	cm.pm = productName;
	cm.cg = categoryID;

	cm.pc = "Y";
	cm.pi = "PRODUCT: " + productName + " (" + productID + ")";
	cm.ul = resetVCPIVariables(window.location.href);
	cm.writeImg();
}

function cmCreateRegistrationTag(customerEmail, customerCity,
				customerState, customerZIP, newsletterName, 
				subscribe) {
	var cm = new _cm("tid", "2", "vn2", "e3.1");
	cm.rf = removeKryptoParameter(document.referrer);
	cm.ul = removeKryptoParameter(window.location.href);
	cm.em = customerEmail;
	cm.cd = cm.em;
	cm.sa = customerState;
	cm.ct = customerCity;
	cm.zp = customerZIP;

	if (newsletterName && subscribe) {
		cm.nl = newsletterName;
		cm.sd = subscribe;
	}
	cm.ul = resetVCPIVariables(window.location.href);
	cm.writeImg();
}

function cmCreateErrorTag(errorType) {
	var cm=new _cm("tid", "404", "vn2", "e3.1");  //DO NOT CHANGE THESE PARAMETERS
	if (errorType) {
		cm.ul = errorType;
		cm.pi = cm.ul;
		cm.pc = "Y";
	}
	cm.writeImg();
}

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.jsp"; /****************** SET TO DEFAULT DOC NAME */
	}

	while (pageName.indexOf("/") == 0) {
		pageName = pageName.substr(1,pageName.length);
	}

	return(pageName);
}

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 cmOSK = "";

/* Internal, to support aggregation */
function cmGetProductIndex(id){
	var i =0;
	for (i=0; i<cmShopCounter; i++)
	{
		if (id==cmShopIds[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) {
	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++;
	}
}

/* render the aggregated cart lineitems with Shop 5 tags*/
function cmDisplayShop5s(){
	var cm = new _cm("tid", "1", "vn2", "e3.1");
	cm.pi = "Shopping Cart";
	cm.rf = checkReferrer();
	cm.ul = resetVCPIVariables(window.location.href);
	cm.writeImg();

	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;
}

/*
 * 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, categoryID, customerID, orderID,
				orderTotal) {
	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);
		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.rf = removeKryptoParameter(document.referrer);
		cm.ul = removeKryptoParameter(window.location.href);
		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];

		cmOSK += "|" + cm.pr + "|" + cm.bp + "|" + cm.qt + "|";

		cm.pc = "N";
		cm.writeImg();
	}
	cmShopCounter=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, orderShipping, customerID, 
			  customerCity, customerState, customerZIP) {
	var cm = new _cm("tid", "3", "vn2", "e3.1");
	cm.pi = "Order Receipt";
	cm.rf = removeKryptoParameter(document.referrer);
	cm.ul = removeKryptoParameter(window.location.href);
	cm.pc = "Y";
	cm.on = orderID;
	cm.tr = orderTotal;
	cm.osk = cmOSK;
	cm.sg = orderShipping;
	cm.cd = customerID;
	cm.sa = customerState;
	cm.ct = customerCity;
	cm.zp = customerZIP;
	cm.writeImg();

}

function cmIndexOfParameter (parameter) {
	return document.URL.indexOf(parameter);
}

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);
	return s.substring(middle + 1, end);
}

function checkReferrer() {
	var ref = cmExtractParameter("cm_ref");
	if (ref != "") {
		return unescape(ref);
	}
	else {
		return null;
	}
}

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.ritzcamera.com/?URL=" + link.href;
	}
	else {
		cm.pi = "External: " + link;
		cm.ul = "http://www.ritzcamera.com/?URL=" + link;
	}
	cm.rf = document.location.href;
	image1.src = cm.getImgSrc();
	return true;
}

function removeKryptoParameter(inString) {
	return cmRemoveParameter(inString,"krypto");
}

var ampString="%26";
function cmGetParameter(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.length;
			}
		} else {
			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.length;
			}
		}
		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 = cmGetParameter("cm_ven",inS);
	var vCat = cmGetParameter("cm_cat",inS);
	var vPla = cmGetParameter("cm_pla",inS);
	var vItem = cmGetParameter("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) {
	var pattern = /cm%5Fven/ig;
	var new_url = url_string;
	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);
}

//-->
