/************************************************************************************/
/* $Revision: 115311 $
 * $Id: cmtaggingservices.txt 115311 2009-04-15 21:36:16Z croberts $
 *
 * Author: Coremetrics/PSD 
 * Coremetrics  v2.2, 11/14/2008
 * COPYRIGHT 1999-2008 COREMETRICS, INC. 
 * ALL RIGHTS RESERVED. U.S.PATENT PENDING
 * Disclaimer: Coremetrics is not responsible for hosting or maintenance or this file
 *
 *
 * 04/15/2009	CRoberts	Added tagging for blog pages.
 */
/************************************************************************************/

cmSetProduction();
var psDomain = "proclub.com";
var G_PS_URL_PATH = "" + document.location;
var G_PS_PATHNAME = document.location.pathname.toLowerCase();
var G_PS_QUERYSTRING = document.location.search.toLowerCase();
var G_PS_URL_REFERRER = document.referrer.toLowerCase();
var G_PS_COOKIE_LIFETIME = 259200; // 3*24*60*60 = 3 days

var G_PS_COOKIE_FLAG = "PS_FLAG"; // used as a "session" variable to handle events between pages
var G_PS_CUR_PAGEID = false;
var G_PS_CUR_CATID = psGetValueFromCookie(G_PS_COOKIE_FLAG, "catId");

var G_PS_COOKIE_PROFILE = "PS_PROFILE";
var G_PS_COOKIE_ATTRIBUTES = "PS_ATTRS";
var G_PS_COOKIE_SHOP9 = "PS_SHOP9";
var G_PS_COOKIE_PROD_CATID = "PS_PROD_CATID";

/*=========================== BEGIN NAVIGATION ===============================*/

if (psIsDomainToTag() && document.body != null)
{
	if (document.body.innerHTML.search(/The page cannot be found/gi)>=0)
	{
		// Error tags
		G_PS_CUR_CATID = "HOME"; // Main page
		psCreateErrorTag("The page cannot be found", G_PS_CUR_CATID);
	}
	else if (psIsSearchView())
	{
		// Search results page
		G_PS_CUR_CATID = "SEARCH";
		psPostSearchView();
	}
	else if (psIsRegistration())
	{
		psPostRegistration();
	}
	else if (psIsCreateAccount())
	{
		psPostCreateAccount();
	}
	else if (psIsShoppingCart())
	{
		psPostShoppingCart();
	}
	else if (psIsThankYou())
	{
		psPostThankYou();
	}
	else if (psIsLogin())
	{
		psPostLogin();
	}
	else if (psIsShop())
	{
		psPostShop();
	}
	else if (psIsBlog())
	{
		psPostBlog();
	}
	else
	{
		// Product description
		psPostPageView();
	}

	// Renew catId in cookie
	psSetValueToCookie(G_PS_COOKIE_FLAG, "catId", G_PS_CUR_CATID);
	psSetCookie("psCatId", G_PS_CUR_CATID, G_PS_COOKIE_LIFETIME);
}





function psIsDomainToTag()
{
	var result = false;
	var domain = document.location.hostname.replace("www.", "").toLowerCase();
	if (domain == "tag.coremetrics.com") return true;
	result = (domain.search(psDomain) >= 0);
	return result;
}


// 404 error page

function psCreateErrorTag(pPageID, pCatId) 
{
	pPageID = psCleanPageId(pPageID);
	pCatId = psCleanCatId(pCatId);
	cmCreateErrorTag(pPageID, pCatId);
}


// Search results

function psIsSearchView()
{
	return (G_PS_QUERYSTRING.search(/Search=/gi)>=0);
}

function psGetSearchResult()
{
	if ((document.body != null) && (document.body.innerHTML.search(/No results /gi)>0))
		return "0";
	else
	{
/* Needs to be worked out -------------------------------------------------------------------------- */
		var re = psGetFirstElementByClassName("NormalBold");
		re = re.getElementsByTagName("span")[0];
		
		if (re != null) 
		{
			return re.innerHTML;
		}

		return "0";
	}
}

function psPostSearchView()
{
	// Your logic of throwing pageview tag for search functionalities goes here
	G_PS_CUR_CATID = "SEARCH";
	var pageId = null;
	var sTerm = psGetValueFromUrl(G_PS_QUERYSTRING, "Search");
	var sResult = psGetSearchResult();

	if (document.body != null && document.body.innerHTML.search(/No results /gi)>0)
		pageId = "SEARCH UNSUCCESSFUL";
	else
		pageId = "SEARCH SUCCESSFUL";

	// Throw pageview tag
	if (pageId != null)
		psCreatePageviewTag(pageId, G_PS_CUR_CATID, sTerm, sResult, "");
}


// Login page

function psIsLogin()
{
	return (G_PS_URL_PATH.search(/LoginPage/gi)>=0);
}

function psPostLogin()
{
	G_PS_CUR_PAGEID = "Login Page";
	G_PS_CUR_CATID = "My Account";
	psCreatePageviewTag(G_PS_CUR_PAGEID, G_PS_CUR_CATID);

	if (document.Form != null)
	{
		onsubmitGuestRegisterFormPtr = document.Form.onsubmit; // Back up original pointer
		document.Form.onsubmit = function()
		{
			var inputs = document.Form.getElementsByTagName('input');
			var cusID = null;
			for (r = 0; r < inputs.length; r++) {if (inputs[r].name.search(/Username/gi)>=0) cusID = inputs[r].value;}
	
			if (cusID != null) cmCreateRegistrationTag(cusID, cusID);

			// Invoke original function
			if (onsubmitGuestRegisterFormPtr != null)
				return onsubmitGuestRegisterFormPtr();
		}
	}
}


// Registration

function psIsRegistration()
{
	return (G_PS_QUERYSTRING.search(/tabid=1402/gi)>=0);
}

function psPostRegistration()
{
	G_PS_CUR_PAGEID = "Create Account";
	G_PS_CUR_CATID = "My Account";
	psCreatePageviewTag(G_PS_CUR_PAGEID, G_PS_CUR_CATID);

	if (document.Form != null)
	{
		onsubmitGuestRegisterFormPtr = document.Form.onsubmit; // Back up original pointer
		document.Form.onsubmit = function()
		{
			var inputs = document.Form.getElementsByTagName('input');
			var cusID = null;
			for (r = 0; r < inputs.length; r++) {if (inputs[r].name.search(/Memnum/gi)>=0) cusID = inputs[r].value;}
	
			if (cusID != null) cmCreateRegistrationTag(cusID, cusID);

			// Invoke original function
			if (onsubmitGuestRegisterFormPtr != null)
				return onsubmitGuestRegisterFormPtr();
		}
	}
}


// Shop

function psIsShop()
{
	var result = false;
	var domain = document.location.hostname.replace("www.", "").toLowerCase();
	if (domain == "shop.proclub.com") result = true;
	return result;
}

function psPostShop()
{
	var ch = G_PS_PATHNAME.substr(1);
	if (ch.substr(ch.length-1,1) == "/") ch = ch.substr(0,ch.length-1);
	if (ch.search("aspx") >= 0) ch = ch.substr(0,ch.length-5);
	if (ch == "") ch = "HomePage";
	
	G_PS_CUR_PAGEID = ch;
	G_PS_CUR_CATID = "SHOP";

	var quantity = document.getElementById("Quantity");
	if (quantity != null) {
		G_PS_CUR_PAGEID = "PRODUCT: " + G_PS_CUR_PAGEID;
		psCreateProductviewTag(ch, ch, "SHOP", "-_--_--_--_-");
	}
	else
	{
		psCreatePageviewTag(G_PS_CUR_PAGEID, G_PS_CUR_CATID);
	}
}

// Blog pages

function psIsBlog()
{
	var result = false;
	var domain = document.location.hostname.replace("www.", "").toLowerCase();
	if (domain == "blogs.proclub.com") result = true;
	return result;
}

function psPostBlog()
{
	var ch = G_PS_PATHNAME.substr(1);
	if (ch.substr(ch.length-1,1) == "/") ch = ch.substr(0,ch.length-1);
	if (ch.search("aspx") >= 0) ch = ch.substr(0,ch.length-5);
	if (ch == "") ch = "BlogPages";
	
	G_PS_CUR_PAGEID = ch;
	G_PS_CUR_CATID = "Blogs";

		psCreatePageviewTag(G_PS_CUR_PAGEID, G_PS_CUR_CATID);
}

// Shopping Cart

function psIsShoppingCart()
{
	return (G_PS_PATHNAME.search(/ShoppingCart.aspx/gi)>=0);
}

function psPostShoppingCart()
{
	G_PS_CUR_PAGEID = "Shopping Cart";
	G_PS_CUR_CATID = "ORDER";
	psCreatePageviewTag(G_PS_CUR_PAGEID, G_PS_CUR_CATID);

	try
	{
		//redButton redArrow

		if (document.CartForm != null)
		{
			onsubmitGuestRegisterFormPtr = document.CartForm.onsubmit; // Back up original pointer
			document.CartForm.onsubmit = function()
			{
				psThrowShop5Tags();

				// Invoke original function
				if (onsubmitGuestRegisterFormPtr != null)
					return onsubmitGuestRegisterFormPtr();
			}
		}
	}
	catch (ex) {}

}

function psIsThankYou()
{
	return (G_PS_PATHNAME.search(/orderconfirmation.aspx/gi)>=0);
}

function psPostThankYou()
{
	G_PS_CUR_PAGEID = "Thank You";
	G_PS_CUR_CATID = "ORDER";
	psCreatePageviewTag(G_PS_CUR_PAGEID, G_PS_CUR_CATID);

	var attributes = psGetCookie(G_PS_COOKIE_ATTRIBUTES); // Get attributes from cookie
	attributes = (attributes == null ? "" : attributes);

	// Get order info
	var dt = new Date();
	orDid = (dt.getTime()%10000000) + (Math.floor(Math.random()*10000));
	// subtotal
	var cells = psGetElementsByClassName("OrderSummaryValue");
	if (cells != null)
	{
		orDsubtotal = cells[0].getElementsByTagName("span")[0].innerHTML.substr(1);
		orDshipping = cells[1].innerHTML.substr(1);
	}

	// Get user info
	pRid = psGetCookie(G_PS_COOKIE_PROFILE);	
	if (pRid != null)
	{
		var buf = pRid.split('|');
		for (var i=0; i<buf.length; i++)
		{
			var tempVal = buf[i];
			buf[i] = ((tempVal=="null" || tempVal=="NULL")? "" : tempVal); 
		}
		pRid = buf[0];
		pRcity = buf[2];
		pRstate = buf[3];
		pRzip = buf[4];
	}else{
		pRid = pRcity = pRstate = pRzip = null;
	}
	
	// get shop9 tag
	var prd = new psProduct();
	var item9 = psGetCookie(G_PS_COOKIE_SHOP9);
	item9 = (item9 != null ? item9 : "");
	item9 = item9.split("#");

	for (var r = 1; r < item9.length; r++) // item for each row
	{
		if (prd.getItem9(item9[r]))
			psCreateShopAction9Tag(prd.id, prd.name, prd.quantity, prd.price, pRid, orDid, orDsubtotal, prd.catId, attributes);
	}
	// Make sure to have actual postings
	psDisplayShop9s();
	// Post order tag finally
	psCreateOrderTag(orDid, orDsubtotal, orDshipping, pRid, pRcity, pRstate, pRzip, attributes);
	// Delete cookies after checking out successfully
	psSetCookie(G_PS_COOKIE_PROD_CATID, "", "delete");
	psSetCookie(G_PS_COOKIE_SHOP9, "", "delete");
	psSetCookie(G_PS_COOKIE_ATTRIBUTES, "", "delete");
	psSetCookie(G_PS_COOKIE_FLAG, "", "delete");			
}

function psThrowShop5Tags()
{
	// Attribute info
	var attributes = psGetAttributes();

	// Persist attributes to cookie for use in receipt page
	psSetCookie(G_PS_COOKIE_ATTRIBUTES, attributes);
	// Throw shop5 tags
	var cartTbl = document.getElementById("pnlCartSummary").getElementsByTagName("table")[1].getElementsByTagName("table")[0];
		//if (cartTbl == null) return;

	psSetCookie(G_PS_COOKIE_SHOP9, "", "delete"); // Initialize buffer
	var rows = cartTbl.rows;

	var prd = new psProduct();
	for (var r = 2; r < rows.length; r++) // item for each row
	{
		if (prd.getItem5(rows[r], r)) // Get shop5 item successfully?
			psCreateShopAction5Tag(prd.id, prd.name, prd.quantity, prd.price, prd.catId, attributes);
	}
	// Make sure to have actual postings
	psDisplayShop5s();
}

function psGetAttributes()
{
	return "-_--_--_--_-";
}

// Create Account

function psIsCreateAccount()
{
	return (G_PS_PATHNAME.search(/createaccount.aspx/gi)>=0);
}

function psPostCreateAccount()
{
	G_PS_CUR_PAGEID = "Create Account";
	G_PS_CUR_CATID = "ORDER";
	psCreatePageviewTag(G_PS_CUR_PAGEID, G_PS_CUR_CATID);

	if (document.frmCreateAccount != null)
	{
		onsubmitGuestRegisterFormPtr = document.frmCreateAccount.onsubmit; // Back up original pointer
		document.frmCreateAccount.onsubmit = function()
		{
			pSid = getField(document.frmCreateAccount.BillingFirstName);
			pSemail = null;
			if (getField(document.frmCreateAccount.BillingLastName) != null) pSid += " " + getField(document.frmCreateAccount.BillingLastName);
			pScity = getField(document.frmCreateAccount.BillingCity);
			pSstate = getSelect(document.frmCreateAccount.BillingState);
			pSzip = getField(document.frmCreateAccount.BillingZip);
			pScountry = getSelect(document.frmCreateAccount.BillingCountry);

			cmCreateRegistrationTag(pSid, null, pScity, pSstate, pSzip, null, null);
			
			var data = pSid + '|' + pSemail + '|' + pScity + '|' + pSstate + '|' + pSzip +	'|' + pScountry;
			psSetCookie(G_PS_COOKIE_PROFILE, data);
			
			if (onsubmitGuestRegisterFormPtr != null)	return onsubmitGuestRegisterFormPtr();
		}
	}	
}

// Pages view

function psPostPageView() 
{
	G_PS_CUR_PAGEID = psGetPageTitle();
	G_PS_CUR_CATID = psGetTopCategory();
	psHardCode();
	psCreatePageviewTag(G_PS_CUR_PAGEID, G_PS_CUR_CATID);
}


// Get info

function getField(field)
{
	result = null;
	if (field != null && field != "" && field.value != null)
	{
		result = psTrim(field.value);
	}
	
	return result;
}

function getSelect(field)
{
	result = null;
	if (field != null && field != "")
	{
		return psTrim(field.options[field.selectedIndex].text);
	}
}

function psGetTopCategory() 
{
	var node = psGetFirstElementByClassName('NavMainMember_selected');
	var psTopCategory = "Home";
	if (node != null) psTopCategory = psTrim(psHtmlDecode(node.innerHTML));
	
	return psTopCategory;
}

function psGetPageTitle() 
{
	var node = document.getElementById('dnn_TabTitle1_tabName');
	var psPageTitle = "HomePage";
	if (node != null) psPageTitle = psTrim(psHtmlDecode(node.innerHTML));
	
	var subdomain = document.location.hostname.replace("www.", "").toLowerCase();
	if (subdomain == "thespa.proclub.com" || subdomain == "spa.proclub.com")
	{
		var path = document.location.pathname.substr(1).split("/");
		psPageTitle = path[0];
		for (p = 1; p < path.length; p++)
		{
			if (path[p] == "tabid") break;
			psPageTitle += " - " + path[p];
		}
	}
	
	return psPageTitle;
}

function psHardCode()
{
	if (G_PS_URL_PATH.search(/AthleticsFitness/gi)>=0) G_PS_CUR_CATID = "Athletics & Fitness";
	if (G_PS_URL_PATH.search(/MedicalWellness/gi)>=0) G_PS_CUR_CATID = "Medical & Wellness";
	if (G_PS_URL_PATH.search(/SpasSalon/gi)>=0 || document.location.hostname.replace("www.", "").toLowerCase() == "thespa.proclub.com" || document.location.hostname.replace("www.", "").toLowerCase() == "spa.proclub.com") G_PS_CUR_CATID = "Spas & Salon";
	if (G_PS_URL_PATH.search(/YouthFamily/gi)>=0) G_PS_CUR_CATID = "Youth & Family";
	if (G_PS_URL_PATH.search(/Amenities/gi)>=0) G_PS_CUR_CATID = "Amenities";
	if (G_PS_URL_PATH.search(/blogs/gi)>=0) G_PS_CUR_CATID = "Blogs";
	if (G_PS_URL_PATH.search(/checkoutshipping.aspx/gi)>=0) {G_PS_CUR_CATID = "ORDER"; G_PS_CUR_PAGEID = "Shipping Method Select";}
	if (G_PS_URL_PATH.search(/checkoutpayment.aspx/gi)>=0) {G_PS_CUR_CATID = "ORDER"; G_PS_CUR_PAGEID = "Checkout Payment";}
	if (G_PS_URL_PATH.search(/checkoutreview.aspx/gi)>=0) {G_PS_CUR_CATID = "ORDER"; G_PS_CUR_PAGEID = "Checkout Review";}
	if (G_PS_URL_PATH.search(/Home\/MyInfo/gi)>=0) {G_PS_CUR_CATID = "Home"; G_PS_CUR_PAGEID = "My Info";}
}

// Product

function psProduct()
{
    this.id = null;
    this.name = null;
    this.catId = "SHOP";
    this.price = null;
    this.quantity = null;

	this.reset = function()
	{
		this.id = null;
		this.name = null;
		this.catId = null;
		this.price = null;
		this.quantity = null;
	}
	/*
	 * Extracting product info from source code specified by the "current" row
	 * of items table in the shopping cart
	 */
	this.getItem5 = function(itemRow, rowIndex)
	{
		try
		{
			this.id = null;
			this.name = null;
			this.catId = "SHOP";
			this.price = null;
			// Extract product info
				this.id = this.name = itemRow.cells[0].getElementsByTagName("a")[0].innerHTML;
				this.id = (this.id.length > 0 ? this.id : null);
				if (this.id != null)
				{
					this.price = itemRow.cells[2].innerHTML.substr(1);
					this.quantity = psGetFirstElementByClassName("quantity_box",itemRow).value;
				}
				else
					return false;

			//
			// Persist shop9 items in pair (id, "quantity|price")
			var item9 = this.quantity + "|" + this.price;
			//
			// to differentiate extended protection plan items, suffixed by "_" + price
			var itemId = this.id;
			psSetValueToCookie(G_PS_COOKIE_SHOP9, itemId, item9);

			return true;
		}
		catch (ex) { return false; }
	}

	/*
	 * Extract product info from item (get back from cookie)
	 */
	this.getItem9 = function(item)
	{
		try
		{
			this.reset();
			//
			var data = item.split("~");
			if (data.length >= 2)
			{
				this.id = data[0];
				data = data[1].split("|");
				this.name = null;
				this.quantity = data[0];
				this.price = data[1];
				this.catId = "SHOP";
			}
			return true;
		}
		catch (ex) {return false;}
	}
}

function psGetAttributes()
{
	var attributes = "";
	var card = psGetFirstElementByClassName("vcard");
	if (card != null)
	{
		// organization
		var org = psGetFirstElementByClassName("org");
		org = (org != null ? psGetInnerText(org) : "");
		if (org.indexOf(",")>=0)
		{
			org = org.split(",");
			attributes = psTrim(org[0]) + "-_-"; // first attribute
			org = org[1];
		}
		else
			attributes = "-_-";
		// region
		var region = psGetFirstElementByClassName("region");
		attributes += (region != null ? psGetInnerText(region) : "") + "-_-";
		// third attribute
		if (org.indexOf("#")>=0)
		{
			org = org.split("#");
			attributes += psTrim(org[1]) + "-_-";
		}
		else
			attributes += "-_-";
		// fourth attribute (postal-code)
		var postalCode = psGetFirstElementByClassName("postal-code");
		attributes += (postalCode != null ? psGetInnerText(postalCode) : "") + "-_-";
		// fifth attribute (locality)
		var locality = psGetFirstElementByClassName("locality");
		attributes += (locality != null ? psGetInnerText(locality).replace(",", "") : "") + "-_-";
	}
	else
		attributes = "-_--_--_--_-";

	return attributes;
}

// Info utils

function psGetCookie(pCookieName)
{
	var cookies = document.cookie;
	if (!pCookieName || !cookies)
		return null;

	cookies = "; " + cookies.toUpperCase();
	var key = "; " + pCookieName.toUpperCase() + "=";
	var start = cookies.lastIndexOf(key);
	if (start >= 0)
	{
		start = start + key.length;
		var end = cookies.indexOf(";", start);
		if (end == -1)
			end = cookies.length;

		return unescape(cookies.substring(start, end));
	}

    return null;
}

function psSetCookie(pCookieName, pCookieValue, pLifeTime, pDomain)
{
    if (!pCookieName)
		return false;

	if(pLifeTime == "delete") 
    {         
        CC(pCookieName, pDomain);//delete cookie by calling coremetrics's cookie function
        return true;
    }
    // set cookie by calling coremetrics's cookie function
    var expire = (pLifeTime) ? (new Date((new Date()).getTime() + (1000 * pLifeTime))).toGMTString() : null;
    
    return CB(pCookieName, escape(pCookieValue), expire, pDomain);
}

function psSetValueToCookie(pCookieName, pKey, pValue)
{
	// "normalize" input parameters
	pCookieName = psTrim(pCookieName);
	pKey = (pKey != null ? psTrim(pKey).toUpperCase() : pKey);
	// 
	var catCookie = psGetCookie(pCookieName);
	if (catCookie == null)
		catCookie = '';

	if (catCookie.indexOf(pKey) >=0) // Store before -> remove the old value
	{
        var reg = new RegExp("#" + pKey + "~([^#]*)", "gi");
        catCookie = catCookie.replace(reg, "");
	}
	// remove the last items (eldest items) until cookie size < 3500	
	if (pValue != null && pValue != '')
	{
		catCookie = "#" + pKey + "~" + pValue + catCookie;
		var cookieArray = null;
		while (catCookie.length > 3500)
		{
			cookieArray = catCookie.split("#");
			cookieArray.pop();
			catCookie = cookieArray.join("#");
		}
	}
	// Save to cookie
	psSetCookie(pCookieName, catCookie, G_PS_COOKIE_LIFETIME);
}

function psGetValueFromCookie(pCookieName, pKey)
{
	// "normalize" input parameters
	pCookieName = psTrim(pCookieName);
	pKey = psTrim(pKey);
	// extract catId associated with the specified key (pKey)
    var catCookie = psGetCookie(pCookieName);
    if (catCookie != null)
    {
        var re = new RegExp("#" + pKey + "~([^#$]+)", "i");
		if (catCookie.search(re) == -1)
			return null;

        return RegExp.$1;
    }
    return null;
}

function psGetValueFromUrl(pUrl, pKey)
{
    var re = new RegExp("[?&amp;&]" + pKey + "=([^&$]*)", "i");
    if (pUrl.search(re) == -1)
		return null;
    return unescape(RegExp.$1);
}

function psGetInnerText(node, ignorewhitespace)
{
	if (node == null)
		return "";

	var text = "";
	if (typeof(node) == "object")
	{
		if (node.hasChildNodes())
		{
			var children = node.childNodes;
			for(var i=0; i<children.length; i++)
			{
				if(children[i].nodeName == "#text")
				{
					if(ignorewhitespace)
					{
						if(!/^\s+$/.test(children[i].nodeValue))
							text = text.concat(children[i].nodeValue);
					}
					else
						text = text.concat(children[i].nodeValue);
				}
				else if(children[i].nodeName.search(/BR/i)>=0)
					text = text.concat(" "); // a break line for a <br> tag
				else
					text = text.concat(psGetInnerText(children[i])); // recursively get text
			}
		}
		else
		{
			if(node.nodeName == "#text")
				text = text.concat(node.nodeValue);
			else if(node.nodeName.search(/BR/i)>=0)
				text = text.concat("\n"); // a break line for a <br> tag
		}
	}
	else
		text =  node.replace(/\<+.+?\>+/g, "");

	return text;
}

function psGetElementsByClassName(pClassName, node)
{
	if (node == null)
		node = document;

	result = Array(); var p = 0;

	var allPageTags = node.getElementsByTagName("*");	
	for (var k = 0; k < allPageTags.length; k++)
	{
		if (allPageTags[k].className.toLowerCase() == pClassName.toLowerCase())
		{
			result[p] = allPageTags[k];
			p++;
		}
	}
	
	if (result.length > 0) return result;
	return null
}

function psGetFirstElementByClassName(pClassName, node)
{
	if (node == null)
		node = document;
	var allPageTags = node.getElementsByTagName("*");	
	for (var k = 0; k < allPageTags.length; k++)
	{
		if (allPageTags[k].className.toLowerCase() == pClassName.toLowerCase())
			return  allPageTags[k];
	}
	return null;
}

// Cleaning info

function psTrim(pStr)
{
	if (pStr == null || typeof(pStr) == "undefined")
		return pStr;

	return pStr.replace(/&nbsp;|\u00A0/gi, ' ').replace(/^\s+|\s+$/g, '');
}

function psHtmlDecode(pValue)
{
    if (pValue)
    {
        pValue = pValue.replace(/&nbsp;/gi, " ");
        pValue = pValue.replace(/&quot;/gi, '"');
        pValue = pValue.replace(/&amp;/gi, "&");
        pValue = pValue.replace(/&lt;/gi, "<");
        pValue = pValue.replace(/&gt;/gi, ">");
    }

    return pValue;
}

function psCleanPrice(pPrice)
{
	var pattern = /[^0-9\.]/gi;
    return (pPrice != null ? pPrice.toString().replace(pattern, "") : null);
}

function psCleanCatId(pCatId)
{
    return (pCatId != null) ? pCatId.replace(/[\'\":,]/g, "") : null;
}

function psCleanPageId(pPageId)
{
	return (pPageId != null) ? pPageId.replace(/[\n\t\v\r?\'\"]/gi, "") : null; 
}

function psCleanProductName(pProductName)
{
	return (pProductName != null) ? pProductName.replace(/[\n\t\v\r?\'\"]/gi, "") : null; 
}

function psStrReplace ( search, replace, subject ) 
{
    if(!(replace instanceof Array)){
        replace=new Array(replace);
        if(search instanceof Array){//If search    is an array and replace    is a string, then this replacement string is used for every value of search
            while(search.length>replace.length){
                replace[replace.length]=replace[0];
            }
        }
    }
 
    if(!(search instanceof Array))search=new Array(search);
    while(search.length>replace.length){//If replace    has fewer values than search , then an empty string is used for the rest of replacement values
        replace[replace.length]='';
    }
 
    if(subject instanceof Array){//If subject is an array, then the search and replace is performed with every entry of subject , and the return value is an array as well.
        for(k in subject){
            subject[k]=str_replace(search,replace,subject[k]);
        }
        return subject;
    }
 
    for(var k=0; k<search.length; k++){
        var i = subject.indexOf(search[k]);
        while(i>-1){
            subject = subject.replace(search[k], replace[k]);
            i = subject.indexOf(search[k],i);
        }
    }
 
    return subject;
}


// Tagging functions

function psCreatePageviewTag(pId, pCatId, pSrchTerm, pSrchResult, pAttributes)
{
	pId = psCleanPageId(pId);
	pCatId = psCleanCatId(pCatId);
    if (pSrchResult != null)
        pSrchResult += "";
        cmCreatePageviewTag(pId, pCatId, pSrchTerm, pSrchResult, pAttributes);
}

function psCreateProductviewTag(pId, pName, pCatId, pAttributes)
{
	pName = psCleanProductName(pName);
	pCatId = psCleanCatId(pCatId);
	cmCreateProductviewTag(pId, pName, pCatId, pAttributes);
}

function psCreateShopAction5Tag(pId, pName, pQuantity, pPrice, pCatId, pAttributes)
{
	pName = psCleanProductName(pName);
	pCatId = psCleanCatId(pCatId);
  cmCreateShopAction5Tag(pId, pName, pQuantity, pPrice, pCatId, pAttributes);
}

function psDisplayShop5s()
{
	cmDisplayShop5s();
}

function psCreateShopAction9Tag(pId, pName, pQuantity, pPrice, pCusID, pOrderID, pOrderTotal, pCatId, pAttributes)
{
	pName = psCleanProductName(pName);
	pCatId = psCleanCatId(pCatId);
  cmCreateShopAction9Tag(pId, pName, pQuantity, pPrice, pCusID, pOrderID, pOrderTotal, pCatId, pAttributes);
}

function psDisplayShop9s()
{
	cmDisplayShop9s();
}

function psCreateOrderTag(pId, pOrderTotal, pOrderShipping, pCusID, pCusCity, pCusState, pCusZip, pAttributes)
{
	cmCreateOrderTag(pId, pOrderTotal, pOrderShipping, pCusID, pCusCity, pCusState, pCusZip, pAttributes);
}
