<!--
/*
 * $Id: includes/coremetrics/cmdatatagutils.js 1.1 2009/05/06 16:25:52PDT Robert Raftery (dv03737) Exp  $
 * $Revision: 1.1 $
 *
 * CMDATATAGUTILS
 *
 * 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			Desc
 * 08/12/08		Hutch White		Add element tags and autotechprops feature
 * 12/04/08		Will Bird		Added Explore Attribute parameters
 * 02/06/09		MOCHOA			Updated/Customized Registration tag parameters for initial use
 *
 *
 */

/*
 ****************************************************************************************
 * TAG GENERATING FUNCTIONS																*
 ****************************************************************************************
 */

var cm_exAttr=new Array;
var cm_ClientID = "90140508";
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";

/*
 * 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="data.coremetrics.com/eluminate?";
}

function cmCreatePageElementTag(elementID, elementCategory, attributes) {
	if (attributes){
		var cm_exAttr=new Array;
		cm_exAttr=attributes.split("-_-");
	}
	cmEncodeCommas(["tid","15","eid",elementID,"ecat",elementCategory,"pflg","0","cm_exAttr",cm_exAttr]);
}

function cmCreateManualImpressionTag(pageID, trackSP, trackRE) {
		// insert code to get pageID from cmTagControl if pageID is null
		cmEncodeCommas(["tid","9","pi",pageID,"cm_sp",trackSP,"cm_re",trackRE,"st",cm_ClientTS]);
}

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);
	}
}

/*
 * 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, attributes) {
	if (pageID == null) { pageID = getDefaultPageID(); }
	if (attributes){
		var cm_exAttr=new Array;
		cm_exAttr=attributes.split("-_-");
	}
	cmEncodeCommas(["tid","1","pi",pageID,"cg",categoryID,"se",searchString,"sr",searchResults,"cm_exAttr",cm_exAttr]);
}


/*
 * 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, attributes){
	if(pageID == null) { pageID = getDefaultPageID(); }	 
	if (attributes){
		var cm_exAttr=new Array;
		cm_exAttr=attributes.split("-_-");
	}	
	cmEncodeCommas(["tid","6","pi",pageID,"cg",categoryID,"pc","Y","cm_exAttr",cm_exAttr]);
}

/*
 * Creates a registration tag
 *
 * pageID		: required. Page ID to set on this tag.  If not populated,
 *				  default page ID is created.
 * CIFNum		: required.  Unique CIFNum for each user.
 * customerType : Optional. Customized Parameter(rg1).
 * customerSubtype	: Optional. Customized Parameter(rg2).
 * userType		: Optional. Customized Parameter(rg3).
 * Page Count = "Y", so this tag also registers as a pageview.  No explicit
					pageview tag is necessary.
 */
function cmCreateRegistrationTag(pageID, CIFNum, customerType, customerSubtype, userType) {
	var cm = new _cm("tid", "2", "vn2", "e4.0");		
	cm.cd = CIFNum;
	if (pageID) {
		cm.pi = pageID;
	} else {
		cm.pi = getDefaultPageID();
	}
	if (customerType) {
		cm.rg1  = customerType;
	}
	if (customerSubtype) {
		cm.rg2  = customerSubtype;
	}
	if (userType) {
		cm.rg3  = userType;
	}
	cm.pc="Y";
	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, toolFlag, errorFlag, firstStepFlag, lastStepFlag, visitorID, transactionID, attributes) {

	if (pageID == null) { 
		pageID = getDefaultPageID()
	} else {
		pageID = getDefaultApplicationPageID(appName, appStepName, appStepNumber );
	}
	if (appName) {
		appName = cmRemoveWhiteSpace(appName);
	}
	if (helpFlag) {
		appUserFlag = "HELP";
	}
	if (toolFlag) {
		appUserFlag = "TOOL";
	}
	if (errorFlag) {
		appUserFlag = "ERROR";
	}
	if (attributes){
		var cm_exAttr=new Array;
		cm_exAttr=attributes.split("-_-");
	}
	cmEncodeCommas(["tid","1","pi",pageID,"cg",categoryID,"pv1", appName,"pv2", appStepNumber,"pv3", appStepName, "pv4", appUserFlag, "pc", "Y","cm_exAttr",cm_exAttr]);

	if (!transactionID) {
		transactionID = cmGetDefaultOrderID();
	}
	if (!visitorID) {
		visitorID = cmGetDefaultCustomerID();
	}
	if ((firstStepFlag)&&(appName)) {
		cmCreateShopAction5Tag(appName, categoryID, attributes);
	}
	if ((lastStepFlag)&&(appName)) {
		cmCreateShopAction9Tag(appName, visitorID, transactionID, categoryID, attributes);
		cmCreateOrderTag(transactionID, appName, visitorID, attributes);
	}
	
	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,categoryID) {
	if(pageID == null) {
		pageID = getDefaultPageID();
	}
	cmEncodeCommas(["tid","404","pi",pageID,"cg",categoryID,"pc","Y"]);

}

/* Encodes commas in the URL
*
*  Used where page view information is sent
*
*  calls writeImg()
*/
function cmEncodeCommas(__v) {
	var cm = new _cm("vn2", "e4.0");
	var i;
	for (i = 0; i < __v.length; i += 2) {
		var _n = __v[i];
		var _v = __v[i + 1];
		cm[_n] = _v;
	}

	if (cm.tid == "6") {
		cm.addTP();
		document.cookie = "cmTPSet=Y; path=/";		
	}

	if (cm.tid == "1") {
		if (cI("cmTPSet") != 'Y') {
			cm.tid = "6";
			cm.pc = "Y";
			cm.addTP();
			document.cookie = "cmTPSet=Y; path=/";
		}
	}

	if (cm.tid != "4" && typeof(cm.cm_exAttr)!="undefined"){
		switch(cm.tid){
			case "6":
				prefix="pv";
				break;
			case "1":
				prefix="pv";
				break;
			case "5":
				prefix="pr";
				break;
			case "3":
				prefix="o";
				break;
			case "14":
				prefix="c";
				break;
			case "15":
				prefix="e";
				break;
			default:
				break;
		}		
		var attrNum=cm.cm_exAttr.length;
		if (attrNum>15){
			attrNum=15;
		}
		for (i=0;i<attrNum;i++){
			Attval=prefix+"_a"+(i+1);
			cm[Attval]=cm.cm_exAttr[i];
		}
		cm.cm_exAttr=null;
	}	
	if ((cm.pi == null) && (cm.pc == "Y")) {
		cm.pi = getDefaultPageID();
	}
	// if available, override the referrer with the frameset referrer
	try{	
		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;
		}
	}
	catch(err){}	
	if (cm.ul == null) {
		cm.ul = window.location.href;
	}
    
	cm.rf=(cm.rf?cm.rf:document.referrer)
	for(var _n in cm){
	     if(/rf|ul/.test(_n)&&cm[_n]!=null)
	         cm[_n]=cm[_n].replace(/\,|%2C/gi,"%252C");
	}
	
	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, attributes){
	
	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;
	}
	if (attributes){
		var Attr=attributes.split("-_-");
		for (i=0;i<Attr.length;i++){
		Attval="s_a"+(i+1);
		cm[Attval]=Attr[i];
		}
	}
	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, attributes) {
	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;
	}
	if (attributes){
		var Attr=attributes.split("-_-");
		for (i=0;i<Attr.length;i++){
		Attval="s_a"+(i+1);
		cm[Attval]=Attr[i];
		}
	}
	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, attributes) {
	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;
	if (attributes){
			var Attr=attributes.split("-_-");
			var Attval;
			for (i=0;i<Attr.length;i++){
				Attval="o_a"+(i+1);
				cm[Attval]=Attr[i];
			}
		}
	cm.writeImg();
}

/*
 * Creates a Conversion Event tag
 *
 * eventID			: required. Conversion event ID
 * actionType		: required. 1=conversion initiation, 2=conversion completion
 * categoryID		: optional. Category for the event
 * points			: optional. Point value to assign to conversion.
 */
 function cmCreateConversionEventTag(eventID, actionType, categoryID, points, attributes) {
	var cm = new _cm("tid", "14", "vn2", "e4.0");
	cm.cid = eventID;
	cm.cat = actionType;
	cm.ccid = categoryID;
	cm.cpt = points;
	if (attributes){
			var Attr=attributes.split("-_-");
			var Attval;
			for (i=0;i<Attr.length;i++){
				Attval="c_a"+(i+1);
				cm[Attval]=Attr[i];
			}
		}
	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 cmGetDefaultCustomerID(){
	var dt = new Date();
	var randomCustomerID = Math.round(Math.random() * 1000 );
	// alert( dt.getTime()%10000000 + '' + randomCustomerID );
	return dt.getTime()%10000000 + '' + randomCustomerID;
}

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]();
	}
}

if (defaultNormalize == null) { var defaultNormalize = null; }

function myNormalizeURL(url, isHref) {
    	var newURL = url;
    	var blackList = ["webmetro_lid"];
	    var paramString;
	    var paramIndex = newURL.indexOf("?");
        var jsessionIndex = newURL.indexOf(";");
	    var params;
	    var keepParams = new Array();
	    var goodParam;
	
	    if (paramIndex > 0) {
		paramString = newURL.substring(paramIndex+1);

                if (jsessionIndex != -1) {
		        newURL = newURL.substring(0, jsessionIndex-1);
                }
                else {
                        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;
    }
}
//-->
