// DR 05-04-09 WebTrends Functions
// rewrote mbLogMetric function


// trims leading and trailing spaces
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}


//----------------------------------------------------------------------------------
// mbLogMetric - parses the name/value pairs input parameters and calls the 
//                     WebTrends dcsMultiTrack() function.
// EXAMPLE:
//	mbLogMetric('WT.cg_n,Resources & Tools, WT.si_n, 
//									ContactUs, WT.si_x, 1, 
//									WT.si_cs, empty');
//
//  dr 05-11-09 - remove escaping from everything but the dcsuri
//-------------------------------------------------------------------------------------
function mbLogMetric(metricsParams){
	
	if(metricsParams == ""){
		alert("Improperly Formed Metrics Parameters.");
		return (0);
	}
	
    // dr 05-12-2009 Temp fix
    // Tridion content contains escape characters in te content group and sub group
    // tags. These characters need to be removed before calling dcsMultiTrack
    // in the mbLogMetric function. decode the params here, the mbLogMetric
    // function will unescape can be removed after Tridion makes a code change to remove the
    // escaping.
    
    // alert(" DAVE WEBTRENDS DEBUG\n" + "metricsParams before decodeURIComponent:\n" + metricsParams);
    var  temp = decodeURIComponent(metricsParams);	// remove after Tridion fix.
    //alert(" DAVE WEBTRENDS DEBUG\n" + "metricsParams after decodeURIComponent:\n" + metricsParams);
    metricsParams = temp;
    
	// init all possible metrics tags
	var dcsqry = "";			
	var dcsuri = "";
	var ti = "";
	var cg_n = "";
	var cg_s = "";
	var dl = "";
	var si_n = "";
	var si_x = "";
	var si_cs = "";
	var srch_oss = "";
	var srch_oss_r = "";
	var srch_product = "";
	var srch_state = "";
	var srch_language = "";
	var hlp_question = "";
	var hlp_vote = "";
	var cu_category = "";
	var cu_subject = "";
	var cu_client_size = "";
	var cu_sales_office = "";
	var cs_customer_name = "";

	var tagName = "";
	var tagValue = "";
	
	var wtparams = metricsParams.split(",");
	for(var i=0; i < wtparams.length; i=(i+2)){
		tagName = wtparams[i].trim();
		tagValue = wtparams[i+1].trim();
		// set the tag values
		if(tagName == "DCS.dcsqry"){					//dcs query param
			dcsqry = tagValue;
			continue;
		}else if(tagName == "DCS.dcsuri"){				// url in report
			dcsuri = encodeURIComponent(tagValue);
			continue;
		}else if(tagName == "WT.ti"){					// title in report
			ti = encodeURIComponent(tagValue);
			continue;
		}else if(tagName == "WT.cg_n"){					// content group name
			cg_n = tagValue;
			continue;
		}else if(tagName == "WT.cg_s"){					// content subgroup name
			cg_s = tagValue;
			continue;
		}else if(tagName == "WT.dl"){					// download file indicator 20=href, 25=right click
			dl = tagValue;
			continue;
		}else if(tagName == "WT.si_n"){					// scenario name
			si_n = tagValue;
			continue;
		}else if(tagName == "WT.si_x"){					// scenario step number
			si_x = tagValue;
			continue;
		}else if(tagName == "WT.si_cs"){				// scenario conversion marker
			si_cs = tagValue;
			continue;
		}else if(tagName == "WT.oss"){					// onsite search term
			srch_oss = tagValue;
			continue;
		}else if(tagName == "WT.oss_r"){				// onsite search result
			srch_oss_r = tagValue;
			continue;
		}else if(tagName == "WT.z_product"){			// onsite advanced search product value
			srch_product = tagValue;
			continue;
		}else if(tagName == "WT.z_state"){				// onsite advanced search state code value
			srch_state = tagValue;
			continue;
		}else if(tagName == "WT.z_language"){			// onsite advanced search language code value		
			srch_language = tagValue;
			continue;
		}else if(tagName == "WT.z_question"){			// Help Question selected
			hlp_question = tagValue;
			continue;
		}else if(tagName == "WT.z_q_vote"){				// Help Question yes/no response
			hlp_vote = tagValue;
			continue;
		}else if(tagName == "WT.z_cu_category"){		// Contact Us page category dropdown selected value
			cu_category = tagValue;
			continue;
		}else if(tagName == "WT.z_cu_subject"){			// Contact Us page subject dropdown selected value
			cu_subject = tagValue;
			continue;
		}else if(tagName == "WT.z_cu_client_size"){		// Contact Us page client size field value
			cu_client_size = tagValue;
			continue;
		}else if(tagName == "WT.z_cu_sales_office"){	// Contact Us page sales office client dropdown selected value	
			cu_sales_office = tagValue;
			continue;
		}else if(tagName == "WT.z_customer_name"){		// Client Specfic Customer name search value
			cs_customer_name = tagValue;
		}	// end if
			
	}	// end for
	
	//Call the dscMultiTrack function.
	
	// if the dscuri and ti are empty then don't include
	// in the parameter list
	if(dcsuri == "" && ti == ""){
		dcsMultiTrack("WT.cg_n", cg_n,
						"WT.cg_s", cg_s,
						"WT.dl", dl,
						"WT.si_n", si_n,
						"WT.si_x", si_x,
						"WT.si_cs", si_cs,
						"WT.oss", srch_oss,
						"WT.oss_r", srch_oss_r,
						"WT.z_product", srch_product,
						"WT.z_state", srch_state,
						"WT.z_language", srch_language,
						"WT.z_question", hlp_question,
						"WT.z_q_vote", hlp_vote,
						"WT.z_cu_category", cu_category,
						"WT.z_cu_subject", cu_subject,
						"WT.z_cu_client_size", cu_client_size,
						"WT.z_cu_sales_office", cu_sales_office,
						"WT.z_customer_name", cs_customer_name,
						"DCS.dcsqry", dcsqry); 
	
	}else if(dcsuri == "" && ti != ""){
		dcsMultiTrack("WT.ti", ti,
						"WT.cg_n", cg_n,
						"WT.cg_s", cg_s,
						"WT.dl", dl,
						"WT.si_n", si_n,
						"WT.si_x", si_x,
						"WT.si_cs", si_cs,
						"WT.oss", srch_oss,
						"WT.oss_r", srch_oss_r,
						"WT.z_product", srch_product,
						"WT.z_state", srch_state,
						"WT.z_language", srch_language,
						"WT.z_question", hlp_question,
						"WT.z_q_vote", hlp_vote,
						"WT.z_cu_category", cu_category,
						"WT.z_cu_subject", cu_subject,
						"WT.z_cu_client_size", cu_client_size,
						"WT.z_cu_sales_office", cu_sales_office,
						"WT.z_customer_name", cs_customer_name,
						"DCS.dcsqry", dcsqry); 
	}else{	
		//Call the dscMultiTrack function.  
		dcsMultiTrack("DCS.dcsuri", dcsuri,
						"WT.ti", ti,
						"WT.cg_n", cg_n,
						"WT.cg_s", cg_s,
						"WT.dl", dl,
						"WT.si_n", si_n,
						"WT.si_x", si_x,
						"WT.si_cs", si_cs,
						"WT.oss", srch_oss,
						"WT.oss_r", srch_oss_r,
						"WT.z_product", srch_product,
						"WT.z_state", srch_state,
						"WT.z_language", srch_language,
						"WT.z_question", hlp_question,
						"WT.z_q_vote", hlp_vote,
						"WT.z_cu_category", cu_category,
						"WT.z_cu_subject", cu_subject,
						"WT.z_cu_client_size", cu_client_size,
						"WT.z_cu_sales_office", cu_sales_office,
						"WT.z_customer_name", cs_customer_name,
						"DCS.dcsqry", dcsqry); 
	}
	
	
    return (1);
}

//-------------------------------------------------------------------
//	mbSetMetricsPageVisitTag(objName, tagName, tagValue)
//  Description: Sets a WebTrends object tag to a value.
//  To reset a tag value send in a string containing the value 'empty'.
//  Params
//		objName: WebTrends object name.
//  	tagName: WebTrends tag to set.
//  	tagValue: value to set in the tagName.
//
//  EXAMPLES:
//	1)	mbSetMetricsPageVisitTag('_pageVisitTag','WT.si_cs', 'empty');
//		Sets the WT.si_cs to an empty string.
//  2)	mbSetMetricsPageVisitTag('_pageVisitTag','WT.si_x', '1'); 
//		Sets the WT.si_x tag to the value of 1.
//-------------------------------------------------------------------

function mbSetMetricsPageVisitTag(objName, tagName, tagValue){
		
	if(tagValue == 'empty'){
		tagValue = '';
	}
	var tmp = objName +"." +  tagName + "= '" +tagValue+ "'";
	eval(tmp);
}

function matchTaggingName(name,nameValuePairs,returnNull) {
	var taggingRegExp = new RegExp(name+":([^;]*)","i");
 	var taggingMatch = taggingRegExp.exec(nameValuePairs);
 	if (taggingMatch != null) {
 		return(taggingMatch[1]);
 	}
 	else {
 		if (returnNull)
 			return("null");
 		else
 			return("");
 	}
}



function doLink (params) {
	// This function is called when external links are clicked	
	
	var nameValuePairs = new String(params);
	
	var linkType = matchTaggingName("TaggingMetaData",nameValuePairs);
	if (linkType == "") {
 		linkType == "link";
	}
	
	/* If no url specified, then can't do link. */
	var url = matchTaggingName("url",nameValuePairs);
	if (url == "") {
		 alert("Improperly Formed Link.");
		 return(0);
	}
	
	var urlType = matchTaggingName("urltype",nameValuePairs);
    if (urlType == "") {
		// assume internal if not specified.
		 urlType="internal";
	}

    var urlTarget = matchTaggingName("urltarget",nameValuePairs);
    if (urlTarget == "") {
        // assume _self (same window)
        urlTarget = "_self";
    }
    
    if (linkType == "link") {
	 // Determine Link Title
	 linkTitle = matchTaggingName("linkTitle",nameValuePairs);
	 if (linkTitle == "") {
		  linkTitle = "UnknownLinkTitle";
	 }
	}
	
	// Determine popup parameters
	// This one is special one-time regex w/ [] around popupParams
	var popupParamsRegex=/popupParams:\[([^\]]+)]/i;
	var popupParamsMatch=nameValuePairs.match(popupParamsRegex);
	if (popupParamsMatch != null) {
		var popupParams=popupParamsMatch[1];
	} else {
		// assume internal if not specified.
		 popupParams="";
	}
	
	// WebTrends tags - name/value pairs
    var  metricsParams = matchTaggingName("metricsLoggingParams",nameValuePairs);

	if (urlType == "external") {
		// call dcsMultiTrack  here.
		mbLogMetric(metricsParams)
	}

	// Call the URL that was extracted from the span tag.

	if (urlTarget=="self" || urlTarget=="_self" || urlTarget==null) {
		 location.href=url;
	} else {
		// get the reference to the window that opened this window
		if(window.opener != null){
			window.opener.name='myparent';
		}	
		window.open(url,'myparent');
		// window.open(url,urlTarget,popupParams);
	}

    return (1);
}

