<!--
/*
 * cmdatatagutils.js
 *
 * Coremetrics Tag v4.0, 4/18/2003
 * COPYRIGHT 1999-2002 COREMETRICS, INC. 
 * ALL RIGHTS RESERVED. U.S.PATENT PENDING
 *
 * The following functions aid in the creation of Coremetrics data tags.
 * DATE			NAME			DESCRIPTION
 * 030207		MOCHOA			Added Country Parameter to Registration Tag
 * 010408 		HWHITE			Add Manual Tagging
 * 010808		HWHITE			Add client variables, auto tech prop
 * 19DEC08		ABRINK			Add Manual mmc
 * 14JAN09		ABRINK			Add Element tag
 *
 */

var cm_ClientID = "90104102";
var cm_TrackLink = "A";
var cm_TrackImpressions = "RS";

var cmJv = "1.0";
if (typeof(isNaN) == "function") cmJv = "1.1";
if (typeof(isFinite) == "function") cmJv = "1.2";
if (typeof(NaN) == "number") cmJv = "1.3";
if (typeof(decodeURI) == "function") cmJv = "1.5";
if (typeof(Array.forEach) == "function") cmJv = "1.6";
if (typeof(Iterator) == "object") cmJv = "1.7";

/*
 ****************************************************************************************
 * TAG GENERATING FUNCTIONS																*
 ****************************************************************************************
 *
 * Redirects tag traffic (by default, aimed at test servers) to Coremetrics production servers.
 * Should be called from and only from production environments.
 */
function cmSetProduction(){
	cm_HOST="ncm.neteller.com/eluminate?";
}

function cmCreateManualImpressionTag(pageID, trackSP, trackRE) {
		var cm = new _cm("tid","9","vn2","e4.0");
		cm.pi = pageID;
		cm.cm_sp = trackSP;
		cm.cm_re = trackRE;
		cm.st = cm_ClientTS;
		cm.writeImg();
}

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);
	}
}

/* manual PageviewTag for off site page tagging.  Allows client to supply URL and Referring URL
*/
function cmCreateManualPageviewTag(pageID, categoryID,DestinationURL,ReferringURL,searchTerm) {
	var cm = new _cm("tid","1","vn2","e4.0");
	cm.pi = pageID;
	cm.cg = categoryID;
	cm.ul = DestinationURL;
	cm.rf = ReferringURL;
	if (searchTerm) {
		cm.se=searchTerm;
	}

	//check for manual_cm_mmc parameter;
	if (this.manual_cm_mmc != null) {
		cm.ul = cm.ul + ((cm.ul.indexOf("&") == -1) ? ((cm.ul.indexOf("?") == -1) ? "?" : "&") : "&") + "cm_mmc=" + this.manual_cm_mmc;
	}

	cm.writeImg();
}

function cmCreatePageElementTag(elementID, elementCategory) {
	var cm = new _cm("tid", "15", "vn2", "e4.0");
	
	cm.eid=elementID;
	cm.ecat=elementCategory;
	cm.pflg=0;
	cm.writeImg();
}

/*
 * Creates a Pageview tag with the given Page ID
 *
 * pageID	: required. Page ID to set on this Pageview tag
 * categoryID	: optional. Category ID to set on this Pageview tag
 * searchString	: optional. Internal search string enterred by user to reach
 *				  this page.
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreatePageviewTag(pageID, categoryID, searchString, searchResults) {
	if (pageID == null) {
		pageID = cmGetDefaultPageID();
	}

	var cm = new _cm("tid", "1", "vn2", "e4.0");
	cm.pi = pageID;
	if (searchString) {
		cm.se = searchString;
	}
	cm.sr = searchResults;
	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 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;
	}
		//check for manual_cm_mmc parameter;
	if (this.manual_cm_mmc != null) {
		cm.ul = cm.ul + ((cm.ul.indexOf("&") == -1) ? ((cm.ul.indexOf("?") == -1) ? "?" : "&") : "&") + "cm_mmc=" + this.manual_cm_mmc;
	}

	if (cm.tid == "1") {
		if (cI("cmTPSet") != 'Y') {
			cm.tid = "6";
			cm.pc = "Y";
			cm.addTP();
			document.cookie = "cmTPSet=Y; path=/";
		}
	}
	cm.writeImg();
}


/*
 * Creates a technical properties tag
 *
 * pageID		: required. Page ID to set on this tag.  If not populated,
				  default page ID is created.
 * categoryID	: optional. Category ID to set on this tag
 *
 * Page Count = "Y", so this tag also registers as a pageview.  No explicit
					pageview tag is necessary.
 */
function cmCreateTechPropsTag(pageID, categoryID){
	var cm = new _cm("tid", "6", "vn2", "e4.0");
	cm.addTP();
	if (pageID) {
		cm.pi = pageID;
	} else {
		cm.pi = getDefaultPageID();
	}
	
	if (categoryID) {
		cm.cg = categoryID;
	}

	cm.pc = "Y";
	cm.writeImg();
}

/*
 * 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, cardHolderID, vipID, newsletterName, subscribe, customerCountry) {
	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.rg11 = cardHolderID;
	cm.rg12 = vipID;
	

	if (newsletterName && subscribe) {
		cm.nl = newsletterName;
		cm.sd = subscribe;
	}
	
	cm.writeImg();
}

/*
 * Creates application-related tags: 
 *		Pageview tag, application tag, any form field tags.
 *
 * pageID			: required.  Page ID to set on the embedded Pageview tag.  If not populated,
					  default application page ID is created.
 * categoryID		: optional.  Category ID to set on this tag.
 * appName			: required.  Name of application for this tag.
 * appStepNumber	: required.  Number of step in application.
 * appStepName		: required.  Name of step in application.
 * helpFlag			: optional.  T/F to indicate if user is seeing a help message in application flow.
 * errorFlag		: optional.  T/F to indicate if user is seeing an error message in application flow.
 * toolFlag			: optional.  T/F to indicate if user is using a tool, calculator, etc. in application flow.
 * firstStepFlag	: optional.  T/F to indicate if step is first step in application flow.
 * lastStepFlag		: optional.  T/F to indicate if step is last step in application flow.
 * visitorID		: required for last step in application.  Unique visitor ID for this user.
 * transactionID	: required for last step in application.  Unique transaction ID or order ID.
					  If not populated, unique ID is created.
 */
function cmCreateApplicationTags(pageID, categoryID, appName, appStepNumber, appStepName, helpFlag, errorFlag, 
	toolFlag, firstStepFlag, lastStepFlag, visitorID, transactionID) {
	
	if( visitorID == null || visitorID == "" ){ visitorID = myRNG(); }
	if( transactionID == null || transactionID == "" ){ transactionID = myRNG(); }

	var cm = new _cm("tid", "1", "vn2", "e4.0");
	if (pageID) {
		cm.pi = pageID;
	} else {
		cm.pi = getDefaultApplicationPageID(appName, appStepName, appStepNumber );
	}
	
	if (categoryID){
		cm.cg = categoryID;
	}
	
	if (appName) {
		appName = cmRemoveWhiteSpace(appName);
		cm.pv1 = appName;
	}
	if (appStepNumber) {
		cm.pv2 = appStepNumber;
	}	
	if (appStepName) {
		cm.pv3 = appStepName;
	}
	if (helpFlag) {
		cm.pv4 = "HELP";
	}
	if (toolFlag) {
		cm.pv4 = "TOOL";
	}
	if (errorFlag) {
		cm.pv4 = "ERROR";
	}

	if (!transactionID) {
		transactionID = cmGetDefaultOrderID();
	}
	if ((firstStepFlag)&&(appName)) {
		cmCreateShopAction5Tag(appName, categoryID);
	}
	if ((lastStepFlag)&&(appName)) {
		cmCreateShopAction9Tag(appName, visitorID, transactionID, categoryID);
		cmCreateOrderTag(transactionID, appName, visitorID);
	}
	
	// if available, override the referrer with the frameset referrer
	if (parent.cm_ref != null) {
		cm.rf = parent.cm_ref;
		parent.cm_ref = document.URL;
	}

	cm.writeImg();
	cmAppName = appName;
	cmAppStepName = appStepName;
	cmAppStepNumber = appStepNumber;
	cmSetupApplicationTextBoxTags(categoryID, firstStepFlag, lastStepFlag);
}

/* Creates an Error Tag
 *
 * pageID	: required.  If not populated, default page ID is created.
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreateErrorTag(pageID) {
	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;
	}

	cm.pc = "Y";
	if (pageID) {
		cm.pi = pageID;
	} else {
		cm.pi = getDefaultPageID();
	}
	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(), null, null);
}

/*
 ****************************************************************************************
 * INTERNAL TAG-CREATING FUNCTIONS														*
 ****************************************************************************************
 */

/*
 * Creates a Shop tag with Action 5 (First step in application)
 *
 * productID		: required. Product ID to set on this Shop tag
 * 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, categoryID){
	
	var cm = new _cm("tid", "4", "vn2", "e4.0");
	cm.at = "5";
	cm.pr = productID;
	cm.pm = productID;
	cm.qt = "1";
	cm.bp = "1";
	if (categoryID) {
		cm.cg = categoryID;
	}

	cm.writeImg();
}

/*
 * Creates a Shop tag with Action 9 (Application submitted page)
 *
 * productID		: required. Product ID to set on this Shop tag
 * cust_id			: required. ID of customer making the purchase
 * orderID			: required. ID 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, cust_id, orderID, categoryID) {
	var cm = new _cm("tid", "4", "vn2", "e4.0");
	cm.at = "9";
	cm.pr = productID;
	cm.pm = productID;
	cm.qt = "1";
	cm.bp = "1";
	cm.cd = cust_id;
	cm.on = orderID;
	cm.tr = "1";
	if (categoryID) {
		cm.cg = categoryID;
	}

	cm.writeImg();
}

/*
 * Creates an Order tag
 *
 * orderID			: required. Order ID of this order
 * appName			: required. Product applied for in this order
 * customerID		: required. Customer ID that placed this order
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreateOrderTag(orderID, appName, customerID) {
	var cm = new _cm("tid", "3", "vn2", "e4.0");
	cm.on = orderID;
	cm.tr = "1";
	cm.osk = '|' + appName + '|1|1|';
	cm.sg = "0";
	cm.cd = customerID;

	cm.writeImg();
}

/*
 * Creates Form Field activity tags
 */
function cmSetupApplicationTextBoxTags(category, first, last){
	var dt=new Date();
	cmRandom = dt.getTime()%10000000;

	var cm = new _cm("tid", "7", "vn2", "e4.0");
	cm.li = 1;
	cm.ps1= cmAppName;
	cm.ps2 = cmAppStepNumber;
	cm.ps3 = cmAppStepName;
	cm.ps4 = cmRandom;
	cm.ps5= category;
	if (first){
		cm.ps6="FIRST";
	}
	if (last){
		cm.ps7="LAST";
	}
	cm.writeImg();

	for (var i=0;i<document.forms.length; i++){
		for (var j=0;j<document.forms[i].elements.length; j++)
		{
			if (document.forms[i].elements[j].type=="text")
			{
				if(document.forms[i].elements[j].value==""){
					cmCheckForOnChange(document.forms[i].elements[j]);
				} else {
					cmSendFormFieldTag(document.forms[i].elements[j].name, true);
				}
			}
		}
	}
}

/*
 ****************************************************************************************
 * INTERNAL HELPER FUNCTIONS															*
 ****************************************************************************************
 */

var cmRandom;
var cmAppName;
var cmAppStepName;
var cmAppStepNumber;
var cmOnChangeCounter = 0;
var cmOnChangeTextBoxName = new Array();
var cmOnChangePointer = new Array();
var cmOnChangeFirst = new Array();

// Removes beginning, ending, and double spaces from strings
function cmRemoveWhiteSpace(str){
	while (str.substring(0,1) == ' ') str = str.substring(1);
    while (str.substring(str.length-1,str.length) == ' ') str = str.substring(0,str.length-1);
	var check = true;
	while (check) {
		var pos = str.indexOf('  ');
		if (pos>-1){
			str = str.substring(0,pos) + str.substring(pos,str.length);
    	} else {
			check = false;
		}
	}
    return(str);
}

/*
 * 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() { 
	if (document.title) 
	{
		var doctitle = document.title;

		if (doctitle.length >= 100)
		{
			doctitle = doctitle.substring(0,90);
		}

		return doctitle;
	}

	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); 
}

/*
 * Creates an acceptable default Page ID value to use for Pageview tags.
 */
function getDefaultApplicationPageID(appName, appStepName, appStepNumber){
	var	cmPageID = "Application: " + appName + " Step: " + appStepNumber + " (" + appStepName + ")";
	return(cmPageID);
}

function cmGetDefaultOrderID(){
	var dt = new Date();
	var randomOrderID = Math.round(Math.random() * 1000 );
	// alert( dt.getTime()%10000000 + '' + randomOrderID );
	return dt.getTime()%10000000 + '' + randomOrderID;
}

function cmCheckForOnChange(textbox){
	cmOnChangeTextBoxName[cmOnChangeCounter] = textbox.name;
	cmOnChangePointer[cmOnChangeCounter] = textbox.onchange;
	cmOnChangeFirst[cmOnChangeCounter] = true;
	textbox.onchange = new Function("cmMultipleOnChange(" + cmOnChangeCounter + ");");
	cmOnChangeCounter++;
}
 
//Even though the event parameter is not used, it must remain since Netscape will 
//automatically send it as the first parameter, the rest of the time it will be null
function cmSendFormFieldTag(name, write){
	var cm = new _cm("tid", "7", "vn2", "e4.0");
	cm.li  = 2;
	cm.ps1 = cmAppName;
	cm.ps2 = cmAppStepNumber;
	cm.ps3 = cmAppStepName;
	cm.ps4 = cmRandom;
	cm.ps5 = name;
	
	if (write)
	{
		cm.writeImg();
	} else {
		var image1 = new Image();
		image1.src = cm.getImgSrc();
	}
}

function cmMultipleOnChange(id){
	if (cmOnChangeFirst[id]){
		cmSendFormFieldTag(cmOnChangeTextBoxName[id], false);
		cmOnChangeFirst[id] = false;
	}
	if (cmOnChangePointer[id]!=null){
		cmOnChangePointer[id]();
	}
}

// a 16 digit random number generator
function myRNG(){
	var foreverDate = new Date();
	var seed = foreverDate.getTime() % 1000;
	var rnd = Math.round( Math.random() * 9999999999999999 );
	return seed + rnd;
}

/* 
 * cmOnSubmit function that overwrites what is in eluminate
 * as long as cmdatatagutils.js is included after eluminate.js
 * this should work
 */
function cP(e){

            CI();
            if (typeof e == typeof e123_x) {
                        var e = this;
            }
            else {
                        var e=CG(e);
            }
            cO(e?e.cM1:-1,"S");
            CA(1);
            CJ(1);
            CE();
}

//-->
