/*
	This sets the initial referrer to be used by the getReferrer function when tracking the home page.
	This is constantly being updated by the getReferrer function to store the last url you were at.
*/
var current_ref_url = document.referrer;


/**
	This function returns either a remote URL which the user came from, or the last action tag that was tracked

	@function getReferrer	
	@param actionTag A string that is basically the tracking ID
*/
function getReferrer(actionTag) {
	
	var refUrl = "" ;
	var next_ref_url = "" ;
   
	if( actionTag && actionTag != null && actionTag != "" ) {
		next_ref_url = actionTag ;
	}

	/* If in frameset mode, look at the parent.cm_ref variable */
	if (parent.cm_ref && parent.cm_ref != null) {
		refUrl = parent.cm_ref ;
		parent.cm_ref = document.URL;
	}
   
	/* If no frameset, just look at the document properties */
	else {
		refUrl = current_ref_url ;
		current_ref_url = next_ref_url ;
	}
   

	if ( refUrl && refUrl != null && refUrl != "" ) {

              if (refUrl.indexOf("bball_global_home") >= 0) {
                 return "http://NIKEBASKETBALLGLOBAL.REF?refUrl="+refUrl;
              }
              if (refUrl.indexOf("global_home") >= 0) {
                  return "http://NIKEGLOBAL.REF?refUrl="+refUrl;
              }

		if (refUrl.indexOf("nike.com/nikegoddess") > 0) { return "http://NIKEGODDESS.REF?refUrl="+refUrl;}
		//if (refUrl.indexOf("nike.com/nikebasketball") > 0) { return "http://NIKEBASKETBALL.REF?refUrl="+refUrl;}
		if (refUrl.indexOf("nike.com/jumpman23") > 0) { return "http://JUMPMAN23.REF?refUrl="+refUrl;}
		if (refUrl.indexOf("nike.com/acg") > 0) { return "http://ACG.REF?refUrl="+refUrl;}
		if (refUrl.indexOf("nike.com/nikegolf") > 0) { return "http://NIKEGOLF.REF?refUrl="+refUrl;}
		if (refUrl.indexOf("nike.com/nikehockey") > 0) { return "http://NIKEHOCKEY.REF?refUrl="+refUrl;}
		if (refUrl.indexOf("nike.com/nikebiz") > 0) { return "http://NIKEBIZ.REF?refUrl="+refUrl;}
		if (refUrl.indexOf("nike.com/nikeid") > 0) { return "http://NIKEID.REF?refUrl="+refUrl;}
		if (refUrl.indexOf("nike.com/nikepresto") > 0) { return "http://NIKEPRESTO.REF?refUrl="+refUrl;}
		if (refUrl.indexOf("nike.com/nikefootball") > 0) { return "http://NIKEFOOTBALL.REF?refUrl="+refUrl;}
		if (refUrl.indexOf("nike.com/nikesphere") > 0) { return "http://NIKESPHERE.REF?refUrl="+refUrl;}
		if (refUrl.indexOf("nike.com/usa") > 0) {return "http://NIKEUSA.REF?refUrl="+refUrl;}
		return refUrl;
	}
    
	return null ;

}


/*
	Not sure what cm_search is used for.
	It's probably a feature of coremetrics which we do not implement
*/
var cm_search = "";


/**
	Creates a simple page view tag
	
	@function cmCreatePageViewTag	
	@param actionTag String a keyword passed by flash to identify the section of the site being viewed
*/
function cmCreatePageViewTag(actionTag) {
	
 

	/* If it's the homepage, it needs to do the technical properties 
         save instead of the standard createPageView */
	if( (homePageName && (homePageName != null)) && (actionTag && (actionTag != null) ) &&
(homePageName.toUpperCase() == actionTag.toUpperCase()) ) {

	    var debugStr = cmCreateHomePageTag(actionTag) ;	

	    return debugStr ;	
	}
	
    
	var cm = new _cm("tid", "1", "vn2", "e3.1");
	cm.rf = getReferrer(actionTag);
	if (cm_search == null) cm_search = "";
	cm.se = cm_search;
	cm.pi = actionTag;

	if( dest_url && dest_url != null ) {
		cm.ul = dest_url ;
	}

	cm.loadImg();
	return prepCmPageViewDebugString(cm) ;
	
}


/**
	Creates home page tag
	
	@function cmCreateHomePageTag
	
	@param actionTag String a keyword passed by flash to identify the section of the site being viewed
*/
function cmCreateHomePageTag(actionTag) {

	
	var test = new _cm();
	
	var cm = new _cm("tid", "6", "vn2", "e3.1");

	cm.pi = actionTag;
	
	cm.pc = "Y";
	cm.rf = getReferrer(actionTag) ;
	cm.addTP();

	if( dest_url && dest_url != null ) {
		cm.ul = dest_url ;
	}

	cm.loadImg();

	return prepCmHomeViewDebugString(cm) ;

}

/**
	Creates a registration tag in the following format: 
 
	Newsletter Name = <newsletter_name> (Only required if subscribed = "Y")
	Subscribed Flag = <subscribed>
NOTE: subscribed MUST be either the string "Y" or "N"  If subscribed = "Y", you must
 		set newsletter_name
	Customer ID = <cust_id>
Zip Code = <zip>
@function cmCreateRegistrationTag
	
	@param newsletter_name String
	@param subscribed String
	@param cust_id String
	@param zip String
	
*/
function cmCreateRegistrationTag(newsletter_name, subscribed, cust_id, zip) {

	var cm = new _cm("tid", "2", "vn2", "e3.1");

	cm.rf = getReferrer("registration");

	if (subscribed == "Y") {
		cm.sd = "Y";
		cm.nl = newsletter_name;
	} else {
		cm.sd = "N";
	}

	cm.cd = cust_id;
	cm.zp = zip;
	
	cm.pc = "Y";

	cm.loadImg();

	
	return(prepCmRegistrationDebugString(cm)) ;

}



/**
	Creates a product tag in the following format:
	
	Page ID = PRODUCT: <cat_name> (<cat_id>):<prod_name> (<prod_id>)
	Product ID = <prod_id>
Product Name = <prod_name>
Category ID = <cat_id>
Category Name = <cat_name>
Page count = "Y"
	Category name should be removed if a CDF is provided.
 
 	Category name and id should be added to the page id if the customer
 	determines they want to see that in MarketForce pages
 	
 	@function cmCreateProductTag
 	
 	@param cat_name
 	@param cat_id
 	@param prod_name
 	@param prod_id
 	
 */
function cmCreateProductTag(cat_name, cat_id, prod_name, prod_id) {
	
	var cm = new _cm("tid", "5", "vn2", "e3.1");  //DO NOT CHANGE THESE PARAMETERS
	
	cm.rf = getReferrer(prod_name);
	cm.pi = "PRODUCT: " + prod_name + " (" + prod_id + ")";
	cm.pr = prod_id;
	cm.pm = prod_name;
	cm.cg = cat_id;
	cm.cl = cat_name;
	cm.pc = "Y";


	cm.loadImg();

	return prepCmProductViewDebugString(cm) ;

}


/**
	Creates an error tag
	
	@function cmCreateErrorTag	
*/
function cmCreateErrorTag() {
	
	var cm = new _cm("tid", "404", "vn2", "e3.1");

	cm.rf = getReferrer();

	cm.pi = getFileNameFromURL(false,false);

	cm.loadImg();
}


/**
	Creates the debug display string for coremetrics
      home page view.
	
	@function prepCmHomeViewDebugString
	
	@param cmRef A reference to the Coremetrics object
*/

function prepCmHomeViewDebugString( cmRef ) {

	var cm_debug_string = "" ;
	cm_debug_string = "<b>TechProperties</b>" + "<br />" +
	"<i>ClientPageID (pi):</i> " + cmRef.pi + "<br />" +
	"<i>Dest URL (ul):</i> " + cmRef.ul + "<br />" +
	"<i>Referrer (rf):</i> " + cmRef.rf + "<br />" +
	"<i>ClientID (ci):</i> " + cmRef.ci + "<br />" +
	"<i>TagID (tid)</i>: " + cmRef.tid + " (TechProperties)<br />" +
	//"<i>JavaScriptVersion (jv):</i> " + cmRef.jv + "<br />" +
	//"<i>JavaScriptEnabled (je):</i> " + cmRef.je + "<br />" +
	//"<i>CookiesEnabled (ce):</i> " + cmRef.ce + "<br />" +
	//"<i>CPUClass (cp):</i> " + cmRef.cp + "<br />" +
	//"<i>ScreenWidth (sw):</i> " + cmRef.sw + "<br />" +
	//"<i>ScreenHeight (sh):</i> " + cmRef.sh + "<br />" +
	//"<i>PixelDepth (pd):</i> " + cmRef.pd + "<br />" +
	//"<i>TimeZone (tz):</i> " + cmRef.tz + "<br />" +
	"<i>Random (rnd):</i> " + cmRef.rnd + "<br />" +
	"<i>TestSession (ts):</i> " + cmRef.ts ;
	
	return cm_debug_string ;
}



/**
	Creates the debug display string for coremetrics
      standard page view.
	
	@function prepCmPageViewDebugString
	
	@param cmRef A reference to the Coremetrics object
*/
function prepCmPageViewDebugString( cmRef ) {

	var cm_debug_string = "" ;
	cm_debug_string = "<b>CM Pageview tag</b>" + "<br />" +
		"<i>ClientPageID (pi):</i> " + cmRef.pi + "<br />" +
		"<i>Dest URL (ul):</i> " + cmRef.ul + "<br />" +
		"<i>Referrer (rf):</i> " + cmRef.rf + "<br />" +
		"<i>ClientID (ci):</i> " + cmRef.ci + "<br />" +
		"<i>TagID (tid)</i>: " + cmRef.tid + "<br />" +
		"<i>Random (rnd):</i> " + cmRef.rnd + "<br />" +
		"<i>TestSession (ts):</i> " + cmRef.ts ;

	return cm_debug_string ;

}


/**
	Creates the debug display string for coremetrics
      registration page view.
	
	@function prepCmRegistrationDebugString
	
	@param cmRef A reference to the Coremetrics object
*/
function prepCmRegistrationDebugString( cmRef ) {

	var cm_debug_string = "" ;
	cm_debug_string = "<b>CM Registration tag</b>" + "<br />" +
		"<i>ClientPageID (pi):</i> " + cmRef.pi + "<br />" +
		"<i>Subscribed (sd):</i> " + cmRef.sd + "<br />" +
		"<i>NewsletterName (nl):</i> " + cmRef.nl + "<br />" +
		"<i>CustomerID (cd):</i> " + cmRef.cd + "<br />" +
		"<i>Zip (zp):</i> " + cmRef.zp + "<br />" +
		"<i>Dest URL (ul):</i> " + cmRef.ul + "<br />" +
		"<i>Referrer (rf):</i> " + cmRef.rf + "<br />" +
		"<i>ClientID (ci):</i> " + cmRef.ci + "<br />" +
		"<i>TagID (tid)</i>: " + cmRef.tid + "<br />" +
		"<i>Random (rnd):</i> " + cmRef.rnd + "<br />" +
		"<i>TestSession (ts):</i> " + cmRef.ts ;

	return cm_debug_string ;

}


/**
	Creates the debug display string for coremetrics
      product page view.
	
	@function prepCmProductViewDebugString
	
	@param cmRef A reference to the Coremetrics object
*/
function prepCmProductViewDebugString( cmRef ) {

	var cm_debug_string = "" ;
	cm_debug_string = "<b>CM Productag</b>" + "<br />" +
		"<i>ClientPageID (pi):</i> " + cmRef.pi + "<br />" +
		"<i>Dest URL (ul):</i> " + cmRef.ul + "<br />" +
		"<i>Referrer (rf):</i> " + cmRef.rf + "<br />" +
		"<i>ClientID (ci):</i> " + cmRef.ci + "<br />" +
		"<i>ProductID (pr):</i> " + cmRef.pr + "<br />" +
		"<i>ProductName (pm):</i> " + cmRef.pm + "<br />" +
		//"<i>CategoryID (cg):</i> " + cmRef.cg + "<br />" +
		//"<i>CategoryName (cl):</i> " + cmRef.cl + "<br />" +
		//"<i>PageCount (pc):</i> " + cmRef.pc + "<br />" +
		//"<i>TagID (tid)</i>: " + cmRef.tid + " (product pageview)<br />" +
		"<i>Random (rnd):</i> " + cmRef.rnd + "<br />" +
		"<i>TestSession (ts):</i> " + cmRef.ts ;
		
	return cm_debug_string ;
}









