/*
 * cmdtemplateutils.js
 *
 * Coremetrics Tag v3.1, 2/28/2002
 * COPYRIGHT 1999-2002 COREMETRICS, INC. ALL RIGHTS RESERVED. U.S.PATENT PENDING
 *
 * Authors: Eliot Towb and Leo A. Ramirez, Jr.
 *
 * The following functions aid in the creation of Coremetrics data tags.
 *
 * All the tags created are frameset safe as long as the parent.cm_ref
 * property is set on the first occurrance of the frameset.  Otherwise,
 * tags will be created assuming frames do not exist on pages.
 */

/*
 * Takes the URL of the current page and removes its extension, query string
 * and web site/directory.  For example, this function will return the string
 * "MyPage" for the URL http://www.mysite.com/directory1/directory2/MyPage.jsp"
 */
 
 function getFileNameFromURL(removeExtension, removePath) { 
	var pageName = window.location.pathname; 

	if (removeExtension) {
	var extensionPos = pageName.lastIndexOf("."); 
		if (extensionPos != -1) { 
			pageName = pageName.substr(0, extensionPos); 
		} 
	}

	if (removePath) {
		var parentDirPos = pageName.lastIndexOf("/"); 
		if (parentDirPos != -1) { 
			pageName = pageName.substr(parentDirPos + 1, pageName.length - parentDirPos - 1); 
		} 
	} else {
		var rootDirPos = pageName.indexOf("/");
		if (rootDirPos != -1) { 
			pageName = pageName.substr(rootDirPos + 1, pageName.length - rootDirPos - 1); 
		} 
	}

 // Remove the "nikegolf/" string from the ID
	var extraString = "nikegolf/";
	var extraStringPos = pageName.toLowerCase().indexOf(extraString);
	if (extraStringPos != -1) {
		pageName = pageName.substr(extraStringPos + extraString.length, pageName.length - extraString.length - extraStringPos);
	}

 // If this is a flash page, remove the "clubs/swf/swf_avea/" and ".html" strings from the ID
	var flashString = "tracking/";
	var flashStringPos = pageName.toLowerCase().indexOf(flashString);
	if (flashStringPos != -1) {
		pageName = pageName.substr(flashStringPos + flashString.length, pageName.length - flashString.length - flashStringPos);
		if (pageName.toLowerCase().indexOf(".html") != -1) {
			pageName = pageName.substr(0, pageName.toLowerCase().indexOf(".html"));
		}
		pageName = "FLASH: " + pageName;
	}

	return(pageName); 
}

// Reg---
/*
* Creates a registration tag in the following format: 
*
* Newsletter Name = <newsletter_name> (Only required if subscribed =
"Y" or
"N")
* Subscribed Flag = <subscribed>
*      NOTE: If subscribed = "Y" or "N", you must set newsletter_name
* Customer ID = <cust_id>
* Zip Code = <zip>
*/
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
/*
function track(actionTag, pageType){
	if(pageType == "homePage"){
		cmCreateHomePageTage(actionTag);
	}else{
	cmCreatePageViewTag(actionTag);
	}
}

function cmCreateHomePageTag(actionTag){
	var cm = new _cm("tid", "6", "vn2", "e3.1");
	//cm.pi= getFileNameFromURL(false,false);
	cm.pi = actionTag;
	
	cm.pc="y";
	cm.rf=getReferrer();
	cm.addTP();
	
	//cm.writeImg();
	cm.loadImg();
	}
*/
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////

function cmCreateRegistrationTag(newsletter_name, subscribed, cust_id, zip)
{
	var cm = new _cm("tid", "2", "vn2", "e3.1");

	if ((subscribed != null) && (subscribed == "Y") || (subscribed=="N")) {
		cm.sd = subscribed;
		cm.nl = newsletter_name;
	}

	cm.cd = cust_id;
	cm.em = cust_id;
	cm.zp = zip;

	cm.pc = "Y";
 
 	cm.writeImg();
 }

function cmFlashCreatePageViewTag(page_id, cm_search) {
	var cm = new _cm("tid", "1", "vn2", "e3.1");

/*	// get the referrer from the frameset
	if (parent.cm_ref != null) {
		cm.rf = parent.cm_ref;
		parent.cm_ref = document.URL;
	}*/
	
	if (cm_search == null)
		cm_search = "";
	cm.se = cm_search;


	if ((page_id != null) && (page_id != "")) {
		cm.pi = page_id;

	} else {
		cm.pi = getFileNameFromURL(false,false);

	}

	cm.writeImg();//*/
}

//Creates a simple page view tag
function cmCreatePageViewTag() {
	var pageID = getFileNameFromURL(false,false);

	// Make sure the page which spawns the Flash animation
	// never throws a Coremetrics tag
	if (pageID.toUpperCase().indexOf("CLUBS/NG_MICROSITE") < 0) {
		var cm = new _cm("tid", "1", "vn2", "e3.1");

		// get the referrer from the frameset
		if (parent.cm_ref != null) {
			cm.rf = parent.cm_ref;
			parent.cm_ref = document.URL;
		}

		if (cm_search == null) {
			cm_search = "";
		}
		cm.se = cm_search;
		cm.pi = pageID;
		cm.writeImg();
	}
}

/* function cmCreatePageViewTag() {
	var cm = new _cm("tid", "1", "vn2", "e3.1");

	// get the referrer from the frameset
	if (parent.cm_ref != null) {
		cm.rf = parent.cm_ref;
		parent.cm_ref = document.URL;
	}

	if (cm_search == null)
		cm_search = "";
	cm.se = cm_search;

	cm.pi = getFileNameFromURL(false,false);

	cm.writeImg();
}*/

/*
 * Creates an error tag
 */
function cmCreateErrorTag() {
	var cm=new _cm("tid", "404", "vn2", "e3.1");
	// get the referrer from the frameset
	if (parent.cm_ref != null) {
		cm.rf = parent.cm_ref;
		parent.cm_ref = document.URL;
	}

	cm.pi = getFileNameFromURL(false,false);

	cm.writeImg();
}
