<!--
/*
 * cmdatatagutils.js
 *
 * Coremetrics Tag v4.0, 4/18/2004
 * COPYRIGHT 1999-2002 COREMETRICS, INC. 
 * ALL RIGHTS RESERVED. U.S.PATENT PENDING
 *
 * The following functions aid in the creation of Coremetrics data tags.
 *
 */

/*
 ****************************************************************************************
 * TAG GENERATING FUNCTIONS																*
 ****************************************************************************************
 */

var cmDt = new Date();
var cmTimeStamp = cmDt.getTime();
var cmFormFieldsSet = false;
var cmAppName = new Array();
var cmAppStepName = new Array();
var cmAppStepNumber = new Array();
var cmAppCounter = 0;
var cmPageviewThrown = false;

/*
 * 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="www22.tdwaterhouse.com/eluminate?";
}

/*
 * Creates a Pageview tag with the given Page ID
 *
 * pageID		: required. Page ID to set on this Pageview tag.  If not populated,
				  default page ID is created.
 * categoryID	: optional. Category ID to set on this Pageview tag
 * searchString	: optional. Internal search string enterred by user to reach
 *				  this page.
 * helpFlag		: optional.  T/F to indicate if user is seeing a help message.
 * errorFlag	: optional.  T/F to indicate if user is seeing an error message.
 * toolFlag		: optional.  T/F to indicate if user is using a tool, calculator, etc.
 *
 * returns nothing, causes a document.write of an image request for this tag.
 */
function cmCreatePageviewTag(pageID, categoryID, searchString, helpFlag, errorFlag, toolFlag) {
	var cm = new _cm("tid", "1", "vn2", "e3.1");
	if (pageID) {
		cm.pi = pageID;
	} else {
		cm.pi = cmGetDefaultPageID();
	}
	if (searchString) {
		cm.se = searchString;
	}
	if (categoryID) {
		cm.cg = categoryID;
	}	
	if (helpFlag) {
		cm.pv4 = "HELP";
	}
	if (toolFlag) {
		cm.pv4 = "TOOL";
	}
	if (errorFlag) {
		cm.pv4 = "ERROR";
	}
	
	cm.pv15 = cmTimeStamp;

	cm.writeImg();
	cmPageviewThrown = true;
}

/*
 * 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", "e3.1");
	cm.addTP();
	if (pageID) {
		cm.pi = pageID;
	} else {
		cm.pi = cmGetDefaultPageID();
	}
	
	if (categoryID) {
		cm.cg = categoryID;
	} else {
		cm.cg = cmGetDefaultCatID();
	}

	cm.pc = "Y";
	cm.writeImg();
}

/*
 * Creates a registration tag
 * visitorID	: required.  Unique visitor ID for this user.
 *
 */
function cmCreateRegistrationTag(visitorID, tier, platform, brokerRetire) {

    var cm = new _cm("tid", "2", "vn2", "e3.1");
    cm.cd = visitorID;
	cm.rg11 = tier;
	cm.rg12 = platform;
	cm.rg13 = brokerRetire;
    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 cmCreateApplicationTag(pageID, categoryID, appName, appStepNumber, appStepName, helpFlag, errorFlag, 
	toolFlag, firstStepFlag, lastStepFlag, visitorID, transactionID) {

	if(!cmPageviewThrown) {
		if(!pageID) {
			pageID = cmGetDefaultPageID();
		}
		cmCreatePageviewTag(pageID, categoryID, null, helpFlag, errorFlag, toolFlag);
	}
	
	if (!transactionID) {
		transactionID = cmGetDefaultOrderID();
	}
	if ((firstStepFlag)&&(appName)) {
		cmCreateShopAction5Tag(appName, categoryID);
	}
	if ((lastStepFlag)&&(appName)) {
		cmCreateShopAction9Tag(appName, visitorID, transactionID, categoryID);
		cmCreateOrderTag(transactionID, appName, visitorID);
	}
	
	cmCreateApplicationStepTag(appName, appStepNumber, appStepName, categoryID, firstStepFlag, lastStepFlag);

	if(!cmFormFieldsSet) {
		cmSetupFormFieldTags();
		cmFormFieldsSet = true;
	}
	
}

/* 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", "e3.1");  //DO NOT CHANGE THESE PARAMETERS
	
	cm.pc = "Y";
	if (pageID) {
		cm.pi = pageID;
	} else {
		cm.pi = cmGetDefaultPageID();
	}
	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(cmGetDefaultPageID(), cmGetDefaultCatID(), null);
}

/*
 ****************************************************************************************
 * INTERNAL TAG-CREATING FUNCTIONS														*
 ****************************************************************************************
 */

var cmRandom;
var cmOnChangeCounter = 0;
var cmOnChangeTextBoxName = new Array();
var cmOnChangePointer = new Array();
var cmOnChangeFirst = new Array();

/*
 * 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", "e3.1");
	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", "e3.1");
	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", "e3.1");
	cm.on = orderID;
	cm.tr = "1";
	cm.osk = '|' + appName + '|1|1|';
	cm.sg = "0";
	cm.cd = customerID;

	cm.writeImg();
}

/*
 * Creates Application Step tag
 */
function cmCreateApplicationStepTag(appName, appStepNumber, appStepName, category, first, last){
	
	var dt=new Date();
	cmRandom = dt.getTime()%10000000;
	
	// add application step to global arrays
	cmAppName[cmAppCounter] = appName;
	cmAppStepNumber[cmAppCounter] = appStepNumber;
	cmAppStepName[cmAppCounter] = appStepName;
	cmAppCounter++;
	
	// throw application step tag
	var cm = new _cm("tid", "7", "vn2", "e3.1");
	cm.li = 1;
	cm.ps1= appName;
	cm.ps2 = appStepNumber;
	cm.ps3 = appStepName;
	cm.ps4 = cmRandom;
	cm.ps5= category;
	if (first){
		cm.ps6="FIRST";
	}
	if (last){
		cm.ps7="LAST";
	}
	cm.ps15 = cmTimeStamp;
	
	cm.writeImg();
}

/*
 * Creates Form Field activity tags
 */
function cmSetupFormFieldTags(){
	var dt=new Date();
	cmRandom = dt.getTime()%10000000;

	for (var i=0;i<document.forms.length; i++){
		for (var j=0;j<document.forms[i].elements.length; j++)
		{
			var typeList = "checkbox|radio|select|text|textarea|select-one";
			if (typeList.indexOf(document.forms[i].elements[j].type.toLowerCase()) > -1)
			{
				

				/*if(document.forms[i].elements[j].value==""){
					cmCheckForOnChange(document.forms[i].elements[j]);
				} else {
					cmSendFormFieldTag(document.forms[i].elements[j].name, true);
				}*/
				cmCheckForOnChange(document.forms[i].elements[j]);
			}
		}
	}
}

/*
 ****************************************************************************************
 * INTERNAL HELPER FUNCTIONS															*
 ****************************************************************************************
 */



// 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 cmGetDefaultPageID(){
	var pageName=cmExtractParameter("cm_page");
	// check to see if "cm_pageID" exists, and use if so
	if (pageName == "") {
		if (this.cm_pageID) {
			pageName = this.cm_pageID;
		}
	}
	var docURL=document.URL.toLowerCase();
	docURL = docURL.toLowerCase();
	if(pageName==""){
		var wbNEPage_index=docURL.indexOf("wbnepage");
		if(wbNEPage_index>-1){
			var wbNEPage_close=docURL.indexOf("?",wbNEPage_index + 9);
			if(wbNEPage_close>-1){
				pageName=docURL.substr(wbNEPage_index + 9,wbNEPage_close - (wbNEPage_index + 9));
			} else {
				pageName=docURL.substr(wbNEPage_index + 9);
			}
			do {
				pageName=pageName.replace("+","/");
			} while(pageName.indexOf("+")>-1);
		}
	}
	if(pageName==""){
		var wbPage_index=docURL.indexOf("wbpage");
		if(wbPage_index>-1){
			var wbPage_close=docURL.indexOf("?",wbPage_index + 7);
			if(wbPage_close>-1){
				pageName=docURL.substr(wbPage_index + 7,wbPage_close - (wbPage_index + 7));
			} else {
				pageName=docURL.substr(wbPage_index + 7);
			}
			do {
				pageName=pageName.replace("+","/");
			} while(pageName.indexOf("+")>-1);
		}
	}
	if(pageName==""){
		var wbSelect_index=docURL.indexOf("wbselect");
		if(wbSelect_index>-1){
			var wbSelect_close=docURL.indexOf("?",wbSelect_index + 9);
			if(wbSelect_close>-1){
				pageName=docURL.substr(wbSelect_index + 9,wbSelect_close - (wbSelect_index + 9));
			} else {
				pageName=docURL.substr(wbSelect_index + 9);
			}
			do {
				pageName=pageName.replace("+","/");
			} while(pageName.indexOf("+")>-1);
		}
	}
	if(pageName==""){
		pageName=window.location.pathname;
		var tempIndex1=pageName.indexOf("?");
		if(tempIndex1 != -1){
			pageName=pageName.substr(0,tempIndex1);
		}
		var tempIndex2=pageName.indexOf("#");
		if(tempIndex2 != -1){
			pageName=pageName.substr(0,tempIndex2);
		}
		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";
		}
		while(pageName.indexOf("/")==0){
			pageName=pageName.substr(1,pageName.length);
		}	
	}
	var pathname=window.location.protocol.toLowerCase();
	if(pathname.indexOf("https:")>-1){
		pageName="PRI: " + pageName;
	} else {
		pageName="PUB: " + pageName;
	}
	return(pageName);
}

function cmGetDefaultCatID(defPage){

	var catName = "";
	var pathname=window.location.protocol.toLowerCase();
	
	var winloc = window.location.hostname.toLowerCase();
	
	if (pathname.indexOf("https:")>-1){
		catName="PRI";
	} else {
		catName="PUB";
	}
	
	if ((typeof wbSID != "undefined") && (winloc.indexOf("banking")>-1 || winloc.indexOf("bonddesk")>-1) ) {
		if (wbSID.length == 0) {
			catName="PUB";
		}
	}

	if (typeof  cm_catID == "undefined") {
		if (typeof  navID1 != "undefined" && navID1 != "") { 
			catName = catName + '_' + navID1;
		}
		if (typeof  navID2 != "undefined" && navID2 != "") {
			catName = catName + '_' + navID2;
		}
		if (typeof navID3 != "undefined" && navID3 != "") {
			if (typeof nav3Present == "undefined" || nav3Present==true) {
				catName = catName + '_' + navID3;
				if (typeof navID4 != "undefined" && navID4 != "") {
					catName = catName + '_' + navID4;
					if (typeof navID5 != "undefined" && navID5 != "") {
						catName = catName + '_' + navID5;
					}
				}
			}
		}
	} else {
		catName = catName + '_' + cm_catID;
	}
	
	return(catName); 
}

/*
 * Creates an acceptable default Page ID value to use for Pageview tags.
 */
function cmGetDefaultApplicationPageID(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){
	
	for(var i = 0; i < cmAppCounter; i++) {
		//alert("sending: " + cmAppName[i] + " " + name);
		var cm = new _cm("tid", "7", "vn2", "e3.1");
		cm.li  = 2;
		cm.ps1 = cmAppName[i];
		cm.ps2 = cmAppStepNumber[i];
		cm.ps3 = cmAppStepName[i];
		cm.ps4 = cmRandom;
		cm.ps5 = name;
		cm.ps15 = cmTimeStamp;
		
		cm.writeImg();
		
	}
}

function cmMultipleOnChange(id){
	if (cmOnChangeFirst[id]){
		cmSendFormFieldTag(cmOnChangeTextBoxName[id]);
		cmOnChangeFirst[id] = false;
	}
	if (cmOnChangePointer[id]!=null){
		cmOnChangePointer[id]();
	}
}

/*
 * Extracts the value of a name-value pair in the request search string.
 */
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);
}
/*
 * 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 cmIndexOfParameter (parameter) {
	return document.URL.indexOf(parameter);
}
if (defaultNormalize == null) { var defaultNormalize = null; }

function myNormalizeURL(url, isHref) {

    var newURL = url;
    
    if (isHref) {
	    var blackList = ["RetVal=", "PageID=", "Brand=", "Flag=", "FirstPage=", 
						 "TLEAccess=", "NavID1=", "NavID2", "SID=", "AccountNo="];
	    var paramString;
	    var paramIndex = newURL.lastIndexOf("?");
	    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;
    }
}
//-->
