<!--
/*
 * 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.
 *
------------------
Library Amendments
------------------

DATE: 08/04/09
TICKET: 5377-8384278
MADE BY: Andrew Stockton
 
LIBRARY CHANGES:	
Added var cm_TrackLink = "A"; to turn on link click tracking

SENT TO: alan.boyd@alliance-leicester.co.uk

 */

/*
 ****************************************************************************************
 * TAG GENERATING FUNCTIONS																*
 ****************************************************************************************
 */

cm_ClientID="90097389";
var cm_TrackLink = "A";
/*
 * 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="server1.alliance-leicester.co.uk/eluminate?";
}

/*
 * 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, PID, 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 (PID)
	{
		cm.pv15 = PID;
	}

	// 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;
	}

	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();
}


//Manual link click tag to work with Flash
function cmCreateManualLinkClickTag(href, linkName) {
	if (cmHandleLinkClick == null && C9 != null) {					var cmHandleLinkClick = C9;		
	}
	if (cmHandleLinkClick != null) {
		var link = new Object();
		link.tagName = "A";
		link.name = linkName;
		link.href = href;
		cmHandleLinkClick(link);
	}
}


/*
 * Creates a registration 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.
 * visitorID	: required.  Unique visitor ID for this user.
 *
 * Page Count = "Y", so this tag also registers as a pageview.  No explicit
					pageview tag is necessary.
 */
function cmCreateRegistrationTag(pageID, categoryID, visitorID) {
	var cm = new _cm("tid", "2", "vn2", "e4.0");		
	cm.cd = visitorID;
	if (pageID) {
		cm.pi = pageID;
	} else {
		cm.pi = getDefaultPageID();
	}
	if (categoryID) {
		cm.cg  = categoryID;
	}
	
	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, errorFlag, 
	toolFlag, firstStepFlag, lastStepFlag, visitorID, transactionID,PID) {

	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 (PID)
	{
		cm.pv15 = PID;
	}

	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);
				}
			}
		}
	}
}



//Manual link click tag to work with Flash
function cmCreateManualLinkClickTag(href, linkName, pageID) {
		var link = new Object();
		link.tagName = "A";
		link.name = linkName;
		link.href = href;
		var dt = new Date();
		var ti = dt.getTime();
		var t1 = cm_ClientTS;
		cM(t1,ti, linkName, href, false, pageID);
}
/*
 ****************************************************************************************
 * 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++;
}
 
//Noramlizing URL function. This will strip out unwanted parameters for LIVEview
function myNormalizeURL(url, isHref) { 
     var newURL = url; 
     var blackList = ["borrowamount=","loanterm=","protection=","loanamount=","loanrepaymentperiod=","PrePopLoanProtection="]; 
     var paramString; 
     var paramIndex = newURL.indexOf("?"); 
     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; 
 }


//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; }

// install normalization
if (document.cmTagCtl != null) {
    var func = "" + document.cmTagCtl.normalizeURL;
    if (func.indexOf('myNormalizeURL') == -1) {
        defaultNormalize = document.cmTagCtl.normalizeURL;
        document.cmTagCtl.normalizeURL = myNormalizeURL;
    }
}
//-->

