/************************************************************************************/
/* $Revision: $
 * $Id: $
 *
 * Author: Coremetrics/PSD 
 * Coremetrics  v4.2, 2010/01/15
 * COPYRIGHT 1999-2008 COREMETRICS, INC. 
 * ALL RIGHTS RESERVED. U.S.PATENT PENDING
 * Disclaimer: Coremetrics is not responsible for hosting or maintenance or this file
 *
 */
/************************************************************************************/
//Production data warehouse flag
if (s_account == "mlTTAprod")
{
	cmSetProduction();
}
/*===========================GLOBAL VARIABLES ===============================*/
// options for debug mode when sending tag:
// 1: only alert
// 2: only send tag
// 3: alert & send tag
var G_PS_DEBUG_MODE = 2;

var G_PS_ARR_DOMAIN = new Array("territoryahead.com");

var G_PS_URL_PATH = "" + document.location.href.toLowerCase();
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 = 432000; // 5*24*60*60 = 5 days

var G_PS_COOKIE_CATID = "PS_CATID";
var G_PS_COOKIE_CURRCATID = "PS_CURRCATID";
var G_PS_COOKIE_PROD_CATID = "PS_PROD_CATID";
var G_PS_COOKIE_PROD_NAME = "PS_PROD_NAME";
var G_PS_COOKIE_PROFILE = "PS_PROFILE";
var G_PS_COOKIE_FLAG = "PS_FLAG";		

var G_PS_CK_ALL="PS_ALL";
var G_PS_CK_SHOP5="PS_SHOP5"
var G_PS_SEP="-_-";
var G_PS_CK_SHOP9="PS_SHOP9";
var G_PS_CK_SHOP9_CUSTINFO="PS_SHOP9_CUSTINFO";
var G_PS_CURR_CATID = null;
//var G_PS_VIEW_PRODUCTID = null;

if(G_PS_URL_PATH.indexOf("territoryahead.com")>=0 || G_PS_URL_PATH.indexOf("209.135.45.246")>=0)
{
	
if (psIsSearchView())
{
	psSendPageViewTag_Search();
}
else if (psIsProductView())
{	
	psSendProductViewTag();
	psRegisterListener_Add2Cart();
	psRegisterListener_CrossSell();
	psRegisterListener_PreviouslyViewed();
}
else if (psIsCartView())
{	
	psCreatePageviewTag("Cart", "Cart");
	psSendShop5Tag();
	psRegisterListener_CheckOutBtn_RegistrationTag();
	psRegisterListener_PreviouslyViewed();	
	psRegisterListener_CrossSell();
}
else 
{	
	if(G_PS_PATHNAME.indexOf("reviews.htm") <0 ){
		psSetCookie(G_PS_COOKIE_CURRCATID,"abc","delete");
	}
	
	psSendPageViewTag();	
	psRegisterListener_Register();
	psRegisterListener_Login();
	psRegisterListener_PreviouslyViewed();
	psSendRegistrationTag_Register();
	
	psRegisterListener_ChangeAccount();
	psSendRegistrationTag();
	psSavePr2Shop9();
	psSendShop9ActionTags();
	psRegisterListener_Register_Order();
}

}

function psIsSearchView()
{
	if(G_PS_URL_PATH.indexOf("mercadosearch.do")>=0)
	{
		return true;
	}
	return false;
}

function psIsCartView()
{
	if(G_PS_URL_PATH.indexOf("basket/basketmain.jsp")>=0 || G_PS_URL_PATH.indexOf("/basket/basketemptymain.jsp")>=0)
	{
		return true;
	}
	return false;
}

function psIsOrderView()
{
	return false;
}

function psGetSearchTerm()
{
	var term = null;
	term=psGetValueFromUrl(G_PS_URL_PATH, "freeText");
	return term;
}

function psGetSearchTerm_1()
{
	var term = "";
	var arrTemp=psGetElementsByClassName(document, "span", "mercadoMedium");
	if(psCheckArrayExist(arrTemp)==true)
	{
		var temp=arrTemp[0];
		arrTemp=temp.getElementsByTagName("b");
		if(psCheckArrayExist(arrTemp)==true)
		{
			temp=psGetInnerText(arrTemp[0]);
			if(psCheckElementExist(temp)==true && temp.length>0)
			{
				return temp;
			}
		}
	}
	term=psGetSearchTerm();
	if(psCheckElementExist(term)==true)
	{
		return term;
	}
	return psGetSearchTerm_2();
}

function psGetSearchTerm_2()
{
	var temp=G_PS_URL_PATH.indexOf("%7cfreetext~");	
	if(temp>=0)
	{
		temp=G_PS_URL_PATH.substring(temp+12);
		var arrTemp=temp.split("%5e");
		return unescape(arrTemp[0]).replace("+"," ");
	}
	return null;
}

function psGetSearchResult()
{
	var result = "0";
	var arrTemp=psGetElementsByClassName(document, "span", "mercadoMedium");
	if(psCheckArrayExist(arrTemp)==true)
	{
		var temp=arrTemp[0].innerHTML.toLowerCase();
		arrTemp=temp.split("<br>");
		temp=psGetInnerText(arrTemp[0]).replace(" item(s) matched ","");		
		result=temp;
	}
	return result;
}

function psGetInnerText(pTagOjb){
	var pattern = /<script[\s\S]*?<\/script>/gi;
	if (pTagOjb != null)
	{
		var sT = (typeof(pTagOjb) == "object") ? pTagOjb.innerHTML : pTagOjb;
		while (sT.search(pattern) > -1)
		{
			sT = sT.replace(pattern, "");
		}
		return sT.replace(/\<+.+?\>+/g, "");
	}
	return 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 psTrim(pStr)
{
	if (pStr == null || typeof(pStr) != "string")
		return pStr;
	return (pStr != null) ? pStr.replace(/&nbsp;|\u00A0/gi, ' ').replace(/^\s+|\s+$/g, '') : null;
}

function psGetValueFromUrl(pUrl, pKey)
{
	pUrl = (pUrl != null) ? "?" + psTrim(pUrl.toLowerCase()) : null;
	pKey = (pKey != null) ? psTrim(pKey.toLowerCase()) : null;

	if (pUrl == null || pKey == null || pUrl.indexOf(pKey) == -1) 
		return null;
	
	var start = pUrl.indexOf('&' + pKey + '=');
	start = (start == -1) ? pUrl.indexOf('?' + pKey + '=') : start;
	if (start >= 0)
	{
		start = start + pKey.length;
		var end = pUrl.indexOf("&", start);
		if(end == -1) 
			end = pUrl.length;
		var middle = pUrl.indexOf("=", start);
		return pUrl.substring(middle + 1, end);
	}
	return null;
}

function psGetElementValueById(pTagId, pValueFlag)
{
    var tag = document.getElementById(pTagId);
    return psGetElementValue(tag, pValueFlag);
}

function psGetElementValue(pTagObj, pValueFlag)
{
    var tagValue = null;
    if (pTagObj != null)
    {
        if (pTagObj.tagName.search(/^INPUT$/i) > -1)
            tagValue = pTagObj.value;
        else if (pTagObj.tagName.search(/^SELECT$/i) > -1)
        {
            if (pValueFlag == true)
                tagValue = pTagObj.options[pTagObj.selectedIndex].value;
            else
                tagValue = psHtmlDecode(pTagObj.options[pTagObj.selectedIndex].innerHTML);// return label instead of value
        }
        else
            tagValue = psHtmlDecode(pTagObj.innerHTML);
    }

    return tagValue;
}

function psCheckEmail(pEmail) 
{
    if (pEmail)
    {
        var i = pEmail.search(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);
        return (i > -1);
    }

    return false;
}

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, ">");
		pValue = pValue.replace(/%26/gi, "&");
    }

    return pValue;
}

function psGetMainDomain(pUrl){
	var se = /^https*\:\/\/([^\/\:]+)/gi;
	var domain = (pUrl.search(se) > -1) ? RegExp.$1 : null;
	if(domain != null)
	{
		if(domain.indexOf("www")==0)
		{
			domain = domain.substring(4,domain.length);
		}
		if(G_PS_ARR_DOMAIN != null)
		{
			for(var i =0; i<G_PS_ARR_DOMAIN.length; i++)
			{
				se = new RegExp("[\.]" + G_PS_ARR_DOMAIN[i] + "$","gi");
				if(("." + domain).search(se) > -1)
				{
					domain = G_PS_ARR_DOMAIN[i];
					break;
				}
			}
		}
		domain = "." + domain;
	}
	return domain;
	
}

function psGetDomain(pUrl){
    var se = /^https*\:\/\/([^\/\:]+)/gi;
    return (pUrl.search(se) > -1) ? RegExp.$1 : null;
}

function psCleanPrice(pPrice)
{
	var pattern = /[^0-9\.]/gi;
    return (pPrice != null ? pPrice.toString().replace(pattern, "") : null);
}

function psGetCookie(pCookieName)
{
	var cookies = document.cookie;
	if (!pCookieName || !cookies)
		return null;

	cookies = "; " + cookies.toLowerCase();
	var key = "; " + pCookieName.toLowerCase() + "=";
	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 psCookieBase(pCookieName, pCookieValue, pLifeTime)
{
	var pDomain = psGetMainDomain(G_PS_URL_PATH);
	CC(pCookieName, pDomain);
	if(pLifeTime == "delete") 
	{         
		return true;
	}
	var expire = (pLifeTime) ? (new Date((new Date()).getTime() + (1000 * pLifeTime))).toGMTString() : null;
	
	return CB(pCookieName, escape(pCookieValue), expire, pDomain);
}

function encodeHtml(strValue)
{
	if (strValue!=null)
	{
		strValue = escape(strValue);
		strValue = strValue.replace(/\//g,"%2F");
		strValue = strValue.replace(/\?/g,"%3F");
		strValue = strValue.replace(/=/g,"%3D");
		strValue = strValue.replace(/&/g,"%26");
		strValue = strValue.replace(/@/g,"%40");
	}
	return strValue;
}

function psSetCookie(pCookieName, pCookieValue, pLifeTime)
{
	if (!pCookieName)
	{
		return false;
	}
	
	pCookieValue = (pCookieValue==null)? "null":pCookieValue;
	
	if(pLifeTime != "delete")
	{
		pCookieName = psTrim(pCookieName);
		var oldCookieValue = psGetCookie(pCookieName);
		oldCookieValue = (oldCookieValue==null)? "null":oldCookieValue;
		var totalSize;
		if(document.cookie.indexOf(pCookieName)>-1)
		{
			totalSize =  document.cookie.length + encodeHtml(pCookieValue).length - encodeHtml(oldCookieValue).length;
		}
		else
		{
			totalSize =  document.cookie.length  + encodeHtml(pCookieValue).length + encodeHtml(pCookieName).length;
		}
		if(totalSize > 3500)
		{
			return false;
		}
	}
	psCookieBase(pCookieName,pCookieValue,pLifeTime);
}

function psSetValueToCookie(pCookieName, pKey, pValue)
{
	if (!pCookieName || !pKey)
	{
		return false;
	}

	pCookieName = psTrim(pCookieName);
	pKey = (pKey != null) ? "#" + psTrim(pKey).toLowerCase() + "~" : pKey;
	var catCookie = psGetCookie(pCookieName);
	catCookie = (catCookie == null) ? "" : catCookie;
	pValue = (pValue == null) ? "null" : pValue;
	var oldCatCookie = catCookie;
	
	var start = catCookie.indexOf(pKey);
	
	var totalsize;
	if (start >= 0) 
	{
		var oldValue = psGetValueFromCookie(pCookieName,pKey.replace(/[\~\#]/gi,""));
		oldValue = (oldValue == null) ? "null" : oldValue;
		var end = catCookie.indexOf("#", start + pKey.length);
		if (end == -1)
			end = catCookie.length;
		catCookie = catCookie.replace(catCookie.substring(start, end), "");
		totalsize =  document.cookie.length  + encodeHtml(pKey).length + encodeHtml(pValue).length - encodeHtml(oldValue).length;
	}
	else
	{
		totalsize =  document.cookie.length  + encodeHtml(pKey).length + encodeHtml(pValue).length;  
		if(document.cookie.indexOf(pCookieName)<0)
		{
			totalsize += encodeHtml(pCookieName).length;
		}
	}
	catCookie = pKey + pValue + catCookie;
	var cookieArray = null;   
	
	while (totalsize > 3500)
	{
		var l1 = encodeHtml(catCookie).length;
		cookieArray = catCookie.split("#");
		cookieArray.pop();
		catCookie = cookieArray.join("#");
		var l2 = encodeHtml(catCookie).length;
		totalsize -=  (l1-l2);
	}   
	if(catCookie == null || catCookie == "")
	{
		catCookie = oldCatCookie;
	}	
	psCookieBase(pCookieName, catCookie, G_PS_COOKIE_LIFETIME);
}

function psGetValueFromCookie(pCookieName, pKey)
{	
	pCookieName = psTrim(pCookieName);
	pKey = (pKey != null) ? "#" + psTrim(pKey).toLowerCase() + "~" : pKey;
    var catCookie = psGetCookie(pCookieName);
    if (catCookie != null)
    {
        var start = catCookie.indexOf(pKey);
		if (start >=0 )
		{
			start = start + pKey.length;
			var end = catCookie.indexOf("#", start);
			if (end == -1)
				end = catCookie.length;
			return catCookie.substring(start, end);
		}
		return null;
    }
    return null;
}

function psGenerateRandomValue()
{
	var dtDate = new Date();
	var cusRandom = (dtDate.getTime()%10000000) + (Math.floor(Math.random()*10000));
	return cusRandom;
}

function psShorttenPageID(pLink)
{
	var temp1 = pLink;
	if (temp1 != null)
	{
		temp1 = (temp1.length > 255) ? temp1.substr(0, 255) : temp1;
	}
	return temp1;
}
/********************************************************/
/* WRAPPER FOR COREMETRICS' TAG FUNCTIONS               */
/********************************************************/
function psCreatePageviewTag(pId, pCatId, pSrchTerm, pSrchResult) 
{
	pId = psCleanPageId(pId);
	pId = psShorttenPageID(pId);
	pCatId = psCleanCatId(pCatId);
	
	pId=psHtmlDecode(pId);
	pCatId=psHtmlDecode(pCatId);
    if (pSrchResult != null)
	{
        pSrchResult += "";		
	}
	if(psCheckElementExist(pSrchTerm)==true)
	{
		pSrchTerm=psHtmlDecode(pSrchTerm);
		pSrchTerm=pSrchTerm.replace(/[\+]/," ");
	}
    if (G_PS_DEBUG_MODE == 1 || G_PS_DEBUG_MODE == 3)
        alert("cmCreatePageviewTag(" + pId + ", " + pCatId + ", " + pSrchTerm + ", " + pSrchResult + ")");
    if (G_PS_DEBUG_MODE == 2 || G_PS_DEBUG_MODE == 3)
        cmCreatePageviewTag(pId, pCatId, pSrchTerm, pSrchResult);
}

function psCreateProductviewTag(pId, pName, pCatId) 
{
	pName = psCleanProductName(pName);
	pCatId = psCleanCatId(pCatId);
	
	pName=psHtmlDecode(pName);
	pCatId=psHtmlDecode(pCatId);
    if (G_PS_DEBUG_MODE == 1 || G_PS_DEBUG_MODE == 3)
        alert("cmCreateProductviewTag(" + pId + ", " + pName + ", " + pCatId + ")");
    if (G_PS_DEBUG_MODE == 2 || G_PS_DEBUG_MODE == 3)
        cmCreateProductviewTag(pId, pName, pCatId);
}

function psCreateShopAction5Tag(pId, pName, pQuantity, pPrice, pCatId) 
{
	pName = psCleanProductName(pName);
	pCatId = psCleanCatId(pCatId);
	pQuantity = psCleanPrice(pQuantity);
	pPrice = psCleanPrice(pPrice);
	
	pName=psHtmlDecode(pName);
	pCatId=psHtmlDecode(pCatId);
    if (G_PS_DEBUG_MODE == 1 || G_PS_DEBUG_MODE == 3)
        alert("cmCreateShopAction5Tag(" + pId + ", " + pName + ", " + pQuantity + ", " + pPrice + ", " + pCatId + ")");
    if (G_PS_DEBUG_MODE == 2 || G_PS_DEBUG_MODE == 3)
        cmCreateShopAction5Tag(pId, pName, pQuantity, pPrice, pCatId);    
}

function psCreateShopAction9Tag(pId, pName, pQuantity, pPrice, pCusID, pOrderID, pOrderTotal, pCatId) 
{
	pName = psCleanProductName(pName);
	pCatId = psCleanCatId(pCatId);
	pQuantity = psCleanPrice(pQuantity);
	pPrice = psCleanPrice(pPrice);
	pOrderTotal = psCleanPrice(pOrderTotal);
	
	pName=psHtmlDecode(pName);
	pCatId=psHtmlDecode(pCatId);
    if (G_PS_DEBUG_MODE == 1 || G_PS_DEBUG_MODE == 3)
        alert("cmCreateShopAction9Tag(" + pId + ", " + pName + ", " + pQuantity + ", " + pPrice + ", " + pCusID + ", " + pOrderID + ", " + pOrderTotal + ", " + pCatId + ")");
    if (G_PS_DEBUG_MODE == 2 || G_PS_DEBUG_MODE == 3)
        cmCreateShopAction9Tag(pId, pName, pQuantity, pPrice, pCusID, pOrderID, pOrderTotal, pCatId);
}

function psCreateOrderTag(pId, pOrderTotal, pOrderShipping, pCusID, pCusCity, pCusState, pCusZip) 
{
	pOrderTotal = psCleanPrice(pOrderTotal);
	pOrderShipping = psCleanPrice(pOrderShipping);
    if (G_PS_DEBUG_MODE == 1 || G_PS_DEBUG_MODE == 3)
        alert("cmCreateOrderTag(" + pId + ", " + pOrderTotal + ", " + pOrderShipping + ", " + pCusID + ", " + pCusCity + ", " + pCusState + ", " + pCusZip + ")");
    if (G_PS_DEBUG_MODE == 2 || G_PS_DEBUG_MODE == 3)
        cmCreateOrderTag(pId, pOrderTotal, pOrderShipping, pCusID, pCusCity, pCusState, pCusZip);
}

function psCreateRegistrationTag(pCusID, pCustEmail, pCusCity, pCusState, pCusZip, pNewsletter, pSubscribe) 
{
    if (G_PS_DEBUG_MODE == 1 || G_PS_DEBUG_MODE == 3)
        alert("cmCreateRegistrationTag(" + pCusID + ", " + pCustEmail + ", " + pCusCity + ", " + pCusState + ", " + pCusZip + ", " + pNewsletter + ", " + pSubscribe + ")");
    if (G_PS_DEBUG_MODE == 2 || G_PS_DEBUG_MODE == 3)
        cmCreateRegistrationTag(pCusID, pCustEmail, pCusCity, pCusState, pCusZip, pNewsletter, pSubscribe);
}

function psDisplayShop5s()
{
    if (G_PS_DEBUG_MODE == 1 || G_PS_DEBUG_MODE == 3)
        alert("cmDisplayShop5s()");
    if (G_PS_DEBUG_MODE == 2 || G_PS_DEBUG_MODE == 3)
        cmDisplayShop5s();
}

function psDisplayShop9s()
{
    if (G_PS_DEBUG_MODE == 1 || G_PS_DEBUG_MODE == 3)
        alert("cmDisplayShop9s()");
    if (G_PS_DEBUG_MODE == 2 || G_PS_DEBUG_MODE == 3)
        cmDisplayShop9s();
}

function psGetElementsByClassName(psDocument, psElementTagName, psClassName)
{
    var arrResult = new Array();
    var index = 0;
    var arrInputs = psDocument.getElementsByTagName(psElementTagName);
    if(arrInputs == null)
    {
        return null;
    }
    for(var i = 0; i < arrInputs.length; i ++ )
    {
        if(arrInputs[i].className.toLowerCase() == psClassName.toLowerCase())
        {
            arrResult[index ++ ] = arrInputs[i];
        }
    }
    return arrResult;
}

function psGetElementByName(psDocument, psElementTagName, psElementName, psElementType)
{
    var arrInputs = psDocument.getElementsByTagName(psElementTagName);
    if(arrInputs == null)
    {
        return null;
    }
    for(var i = 0; i < arrInputs.length; i ++ )
    {
        if(arrInputs[i].name.toLowerCase() == psElementName.toLowerCase())
        {
            if(typeof(psElementType) != "undefined" && psElementType != "")
            {
                if(arrInputs[i].type.toLowerCase() == psElementType)
                {
                    return arrInputs[i];
                }
            }
            else
            {
                return arrInputs[i];
            }
        }
    }
    return null;
}

function psGetElementsByName(psDocument, psElementTagName, psElementName, psElementType)
{
    var arrResult = new Array();
    var index = 0;
    var arrInputs = psDocument.getElementsByTagName(psElementTagName);
    if(arrInputs == null)
    {
        return null;
    }
    for(var i = 0; i < arrInputs.length; i ++ )
    {
        if(arrInputs[i].name.toLowerCase() == psElementName.toLowerCase())
        {
            if(typeof(psElementType) != "undefined" && psElementType != "")
            {
                if(arrInputs[i].type.toLowerCase() == psElementType)
                {
                    arrResult[index] = arrInputs[i];
                    index ++ ;
                }
            }
            else
            {
                arrResult[index] = arrInputs[i];
                index ++ ;
            }
        }
    }

    return arrResult;
}

function psCheckArrayExist(pArrElement)
{
    if(typeof(pArrElement) == "undefined" || pArrElement == null || pArrElement.length <= 0)
    {
        return false;
    }
    return true;
}

function psCheckElementExist(pElement)
{
    if(typeof(pElement) == "undefined" || pElement == null)
    {
        return false;
    }
    return true;
}

function psGetParentNode(pElement)
{	
	if(psCheckElementExist(pElement)==false)
	{
		return null;
	}
	var temp=pElement.parentNode;	
	if(psCheckElementExist(temp)==false)
	{
		temp=pElement.parentElement;
		if(psCheckElementExist(temp)==false)
		{
			return null;
		}
	}
	return temp;
}

function psIsBookmark()
{
	if (G_PS_URL_REFERRER!=""){
		if (psGetDomain(G_PS_URL_REFERRER).indexOf("territoryahead.com")>=0)
		{
			return false;
		}
	}
	else
	{
		return false;
	}
	return true;
}

function psIsProductView()
{
	if(G_PS_PATHNAME.indexOf("shopping/tellafriend/previewmailmain.jsp") >= 0){
		var msgspan = psGetElementsByClassName(document,"span","messagesent");
		if(psCheckArrayExist(msgspan)){
			for(var i=0; i<msgspan.length; i++){
				if(psGetInnerText(msgspan[i]).indexOf("Your message has been sent") >= 0){
					return false;
				}
			}
		}
		return true;
	}	
	if(G_PS_PATHNAME.indexOf("/shopping/tellafriend/tellafriendmain.jsp") >= 0 ||
	   G_PS_PATHNAME.indexOf("/submissionmain.jsp") >= 0  ){
		return false;
	}	
	if(G_PS_PATHNAME.indexOf("/backorder.jsp")>=0)
	{
		return false;
	}
	var temp=psGetValueFromUrl(G_PS_URL_PATH, "itemtype");
	if(psCheckElementExist(temp)==true)
	{
		if(temp.toLowerCase()=="product")
		{
			return true;
		}
	}
	return false;
}

function psIsSubPage()
{
	var arrTemp=psGetElementsByClassName(document, "div", "breadcrumb");
	return psCheckArrayExist(arrTemp);
}

function psSendPageViewTag_TopMenu()
{
	var temp=psGetMenuText_TopMenu();
	if(psCheckElementExist(temp)==true)
	{
		var pageId="Category:"+temp;
		if(pageId.indexOf(G_PS_SEP+"1")>=0)
		{
			return false;
		}
		pageId=pageId.replace(G_PS_SEP+"0","");
		var catId=psGetValueFromUrl(G_PS_URL_PATH, "itemid");
		psCreatePageviewTag(pageId, catId);
		psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_CATID, catId);
		return true;
	}
	return false;
}

function psSendPageViewTag_SubPage()
{
	var arrTemp=psGetElementsByClassName(document, "div", "breadcrumb");
	if(psCheckArrayExist(arrTemp)==true)
	{
		var pageId="Home";
		var catId="Home";
		pageId=psGetInnerText(arrTemp[0]).toLowerCase().replace("home&nbsp;&gt;&nbsp;","");
		catId=psGetValueFromUrl(G_PS_URL_PATH, "itemid");
		pageId = "CATEGORY: " + pageId;
		
		var pageNumber = "";		
		if(psGetValueFromUrl(G_PS_URL_PATH,"onePage") == "1" || psGetValueFromUrl(G_PS_URL_PATH,"viewAll") == "1"){
			pageNumber = " VIEW ALL";
		}else{
			var pageTds = psGetElementsByClassName(document,"td","paging");
			if(psCheckArrayExist(pageTds)){
				if(psGetValueFromUrl(G_PS_URL_PATH,"page") != null){
					pageNumber = " Page " + psGetValueFromUrl(G_PS_URL_PATH,"page");
				}else{
					pageNumber = " Page 1";
				}
			}
		}
		pageId = pageId + pageNumber;
		
		psCreatePageviewTag(pageId, catId);
		psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_CATID, catId);
		return true;
	}
	return false;
}

function psSendPageViewTag_Soldout()
{
	var pageId="Some information regarding your selection";
	var catId="Soldout";
	psCreatePageviewTag(pageId, catId);	
}

function psSendPageViewTag()
{
	var pageId="Home";
	var catId="Home";
	
	if(G_PS_PATHNAME.indexOf("/submissionmain.jsp")>= 0){
		catId = "Submit a new review";
		var pName = document.getElementById("BVRRProductDetailsNameID");
		if(psCheckElementExist(pName)){
			pageId = psTrim(psHtmlDecode(unescape(psGetInnerText(pName))));
		}else{
			pageId = catId;
		}
		psCreatePageviewTag(pageId, catId);
		return;
	}
	
	if(G_PS_PATHNAME.indexOf("/shopping/tellafriend/previewmailmain.jsp") >= 0){
		psCreatePageviewTag("tellafriend", "previewmailmain.jsp");
		return;
	}

	
	if(G_PS_PATHNAME=="/" || G_PS_PATHNAME=="/index.jsp" || G_PS_URL_PATH.indexOf("www.territoryahead.com/jump.jsp?itemid=0&itemtype=home_page")>=0)
	{
	}
	else if(G_PS_URL_PATH.indexOf("/backorder.jsp")>=0)
	{
		psSendPageViewTag_Soldout();
		return;
	}
	else if(psSendPageViewTag_TopMenu()==true)
	{	
		return;
	}
	else if(psSendPageViewTag_SubPage()==true)
	{	
		return;
	}	
	else if(G_PS_URL_PATH.indexOf("/account/")>=0 || G_PS_URL_PATH.indexOf("/checkout/")>=0)
	{
		var temp=G_PS_PATHNAME;
		if(temp.indexOf("/")<0)
		{
			psCreatePageviewTag(G_PS_PATHNAME, "ADD URL");
			return null;
		}
		var arrTemp=temp.split("/");	
		pageId=arrTemp[arrTemp.length-2];
		catId=arrTemp[arrTemp.length-3];
		
		if(G_PS_URL_PATH.indexOf("/account/accountmain.jsp")>=0)
		{
			pageId="Myaccount";
			catId="account";
		}
		else if(G_PS_URL_PATH.indexOf("/checkout/billing/billinginformationmain.jsp")>=0)
		{
			pageId="CHECKOUT: Bill to/Ship to";
		}
		else if(G_PS_URL_PATH.indexOf("/checkout/shipping/shippingmethodmain.jsp")>=0)
		{
			pageId="CHECKOUT: Shipping method";
		}
		else if(G_PS_URL_PATH.indexOf("/checkout/invoice/invoicemain.jsp")>=0)
		{
			pageId="CHECKOUT: Review your order";
		}
		else if(G_PS_URL_PATH.indexOf("/checkout/payment/paymentmain.jsp")>=0)
		{
			pageId="CHECKOUT: Payment info";
		}
		else if(G_PS_URL_PATH.indexOf("/checkout/thankyou/thankyoumain.jsp")>=0)
		{
			pageId="CHECKOUT: Order thank you";
		}
	}
	else if(psSendPageViewTag_Special()==true)
	{	
		return;
	}
	else if(G_PS_PATHNAME.indexOf("/subscribemain.jsp")>=0)
	{
		pageId="subscribemain.jsp";
		catId="Home";
	}
	else
	{	
		var divErrors = psGetElementsByClassName(document,"div","catheader");
		if(psCheckArrayExist(divErrors)){
			for(var i=0;i<divErrors.length;i++){
				if(psTrim(psGetInnerText(divErrors[i])).toLowerCase()=="oops!"){
					psCreateErrorTag("ERROR","ERROR");					
					return;
				}
			}
		}
		
		var temp=G_PS_PATHNAME;
		if(temp.indexOf("/")<0)
		{
			psCreatePageviewTag(G_PS_PATHNAME, "ADD URL");
			return null;
		}
		var arrTemp=temp.split("/");
		if(arrTemp.length<=2)
		{
			psCreatePageviewTag(G_PS_PATHNAME, "ADD URL");
			return null;
		}
		pageId=arrTemp[arrTemp.length-1];
		catId=arrTemp[arrTemp.length-2];
	}
	if(G_PS_URL_PATH.indexOf("shopping/catalogquickshop/resultsmain.jsp")>=0)
	{
		psRegisterListener_Add2Cart_QuickShop();
		psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_CATID, "quickshop");
	}
		
	psCreatePageviewTag(pageId, catId);
	
	if(G_PS_URL_PATH.indexOf("/viewwishlistmain.jsp")>=0)
	{
		psRegisterListener_Add2Cart_Wishlist();
		catId="wishlist";
	}
	if(pageId != "reviews.htm"){
		psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_CATID, catId);
	}
}

function psDoesSearchSucceed()
{
	var arrTemp=psGetElementsByClassName(document,"div","noResultsTextInfo");
	if(psCheckArrayExist(arrTemp)==true)
	{
		for(var i=0;i<arrTemp.length;i++)
		{
			var temp=psGetInnerText(arrTemp[i]).toLowerCase();
			if(temp.indexOf("sorry")>=0 && temp.indexOf("returned no results")>=0)
			{
				return false;
			}
		}
	}
	return true;
}

function psGetPage()
{
	var arrTemp=psGetElementsByClassName(document, "span", "pagingOn");
	if(psCheckArrayExist(arrTemp)==true)
	{
		return psTrim(psGetInnerText(arrTemp[0]));
	}
	return "0";
}

function psIsRefinementPage()
{
	var arrTemp=psGetElementsByClassName(document, "div", "breadcrumb");
	if(psCheckArrayExist(arrTemp)==true)
	{
		var temp=psGetInnerText(arrTemp[0]);		
		if(psGetInnerText(temp).search(/Category:|Color:|Price:|Special\s+Size:|Color:|Size:|Search/i)>=0)
		{
			return true;
		}
	}
	return false;
}

function psSendPageViewTag_Search()
{
	var pageId="Home";
	var catId="Search";
	var searchTerm="";
	var searchResult="0";
	if(psDoesSearchSucceed()==false)
	{
		pageId="Search unsuccessful";	
		searchTerm=psGetSearchTerm();
	}
	else
	{
		if(psGetValueFromUrl(G_PS_URL_PATH,"view_all_products") != null ){
			pageId="Search successful VIEW ALL";
			var pageNumber = psGetPage();
			if(pageNumber != "0"){
				pageId="Search successful";
				pageId +=" page " + pageNumber;
			}
			
		}else if(unescape(G_PS_URL_PATH).indexOf("^view_all_products~") >=0){			
			var pageNumber = psGetPage();
			if(pageNumber != "0"){
				pageId="Search successful";
				pageId +=" page " + pageNumber;
			}else{
				pageId="Search successful VIEW ALL";
			}

		}else{	
			pageId="Search successful";
			var pageNumber = psGetPage();
			if(pageNumber != "0"){				
				pageId +=" page " + pageNumber;
			}			
		}
		searchTerm=psGetSearchTerm_1();
		searchResult=psGetSearchResult();
		if(psIsRefinementPage()==true)
		{
			var arrTemp=psGetElementsByClassName(document, "div", "breadcrumb");
			if(psCheckArrayExist(arrTemp)==true)
			{
				var first_searchTerm = searchTerm;				
				var temp=psGetInnerText(arrTemp[0]);				
				var temp1 = psHtmlDecode(unescape(temp));
				var refines = temp1.split(">");
				if(psCheckArrayExist(refines)){
					for(var i=1;i<refines.length;i++){						
						var s1 = refines[i].replace(/Category:|Color:|Price:|Special\s+Size:|Color:|Size:|Search/gi,"");
						s1 = psTrim(s1.replace("'",""));						
						if(s1.toLowerCase() != first_searchTerm.toLowerCase() ){
							searchTerm += ">" + s1;
						}
					}
				}
			}

		}
		else
		{
			if(pageId=="Search successful" && searchResult=="0")
			{
				psSendProductViewTag();
				psRegisterListener_Add2Cart();
				psRegisterListener_CrossSell();
				psRegisterListener_PreviouslyViewed();
				return;
			}
		}
		
	}	
	
	var homeImageDiv = document.getElementById("swfSlotHomeTemplate1");
	if(psCheckElementExist(homeImageDiv)){
		psCreatePageviewTag("Home", "Home");
		psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_CATID, "Home");
	}else{
		psCreatePageviewTag(pageId, catId, searchTerm, searchResult);	
		psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_CATID, catId);
	}
}

function psGetPrName()
{
	if(G_PS_URL_PATH.indexOf("/product/backorder.jsp")>=0)
	{
		return document.title.toLowerCase().replace(" - the territory ahead","");
	}
	var arrTemp=document.getElementsByTagName("h1");
	if(psCheckArrayExist(arrTemp)==true)
	{
		return psGetInnerText(arrTemp[0]);
	}
	return null;
}

function psGetPrCat_PrV()
{
	if(psIsBookmark()==true)	
	{
		return "Bookmark";
	}
	else if(G_PS_URL_PATH.indexOf("/mercadosearch.do")>=0|| (G_PS_URL_REFERRER.indexOf("mercadosearch.do")>=0 && psIsGiftCardPage()==false) || G_PS_URL_PATH.indexOf("fromnewsearch=true")>=0)
	{
		return "search";
	}
	else if(G_PS_URL_PATH.indexOf("/detailmain.jsp")>=0)
	{
		return "RViewed";
	}
	else if(G_PS_URL_REFERRER.indexOf("/viewwishlistmain.jsp")>=0)
	{
		return "wishlist";
	}	
	else if(G_PS_PATHNAME.indexOf("shopping/tellafriend/previewmailmain.jsp")>=0)
	{
		return "email friend";
	}
	else if(G_PS_URL_REFERRER.indexOf("/jump.jsp")>=0 || G_PS_URL_REFERRER.indexOf("/detailmain.jsp")>=0 || G_PS_URL_REFERRER.indexOf("/basket/basketmain.jsp")>=0)
	{
		var temp=psGetCookie(G_PS_COOKIE_CURRCATID);		
		if(psCheckElementExist(temp)==true && temp.toLowerCase()=="cross sell")
		{
			psSetCookie(G_PS_COOKIE_CURRCATID,"abc","delete");
			return "cross sell";
		}
		if(psCheckElementExist(temp)==true&& temp.toLowerCase()=="rviewed")
		{
			psSetCookie(G_PS_COOKIE_CURRCATID,"abc","delete");
			return "rviewed";
		}
	}
	var temp=psGetValueFromUrl(G_PS_URL_PATH, "path");
	if(psCheckElementExist(temp)==true)
	{
		var arrTemp=temp.split("%2c");
		if(arrTemp.length>1)
		{
			return arrTemp[arrTemp.length-1];
		}
		else if(temp.indexOf(",")>=0)
		{
			arrTemp=temp.split(",");
			return arrTemp[arrTemp.length-1];
		}
	}	
	return psGetValueFromCookie(G_PS_CK_ALL, G_PS_COOKIE_CATID);
}

function psSendProductViewTag()
{
	var pCatId=psGetPrCat_PrV();	
	if(pCatId == null) 
		pCatId = "Bookmark";
	G_PS_CURR_CATID = pCatId;
	var pName=psGetPrName();
	if(psCheckElementExist(pName)==false && psIsGiftCardPage()==true)
	{
		pName="gift card";
	}
	var tableitem_divs = psGetElementsByClassName(document,"div","tableitem");
	if(psCheckArrayExist(tableitem_divs)){
		for(var i=0; i<tableitem_divs.length; i++){
			var temp = psHtmlDecode(unescape(psGetInnerText(tableitem_divs[i])));			
			if(temp.search(/(Regular|Tall|Petite)*\s+\-\s+(\w+)\s+/) >= 0){
				var pId = RegExp.$2;				
				psCreateProductviewTag(pId, pName, pCatId);
				//G_PS_VIEW_PRODUCTID = pId;
				break;
			}
		}		
	}
	
	psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_CATID, pCatId);
}

function psSavePr2Ck()
{
	var pId = "";	
	var RadioTags = psGetElementsByName(document,"input","rProduct_0","radio");
	if(psCheckArrayExist(RadioTags)){
		for(var i=0; i<RadioTags.length; i++){			
			if(RadioTags[i].checked == true){				
				var prdRow = psGetParentNode(psGetParentNode(psGetParentNode(RadioTags[i])));
				if(psCheckElementExist(prdRow)){
					var tableitem_divs = psGetElementsByClassName(prdRow,"div","tableitem");
					if(psCheckArrayExist(tableitem_divs)){
						for(var i=0; i<tableitem_divs.length; i++){
							var temp = psHtmlDecode(unescape(psGetInnerText(tableitem_divs[i])));							
							if(temp.search(/(Regular|Tall|Petite)*\s+\-\s+(\w+)\s+/) >= 0){
								pId = RegExp.$2;								
							}
						}
					}					
				}
			}			
		}
	}
	
	var pCatId = "";
	if(G_PS_CURR_CATID != null){
		pCatId=G_PS_CURR_CATID;
	}else{
		pCatId=psGetPrCat_PrV();
	}

//	if(G_PS_VIEW_PRODUCTID != null && pId.toLowerCase() != G_PS_VIEW_PRODUCTID.toLowerCase()){
//		var pName=psGetPrName();
//		if(psCheckElementExist(pName)==false && psIsGiftCardPage()==true){
//			pName="gift card";
//		}
//		psCreateProductviewTag(pId, pName, pCatId);
//	}

	psSetValueToCookie(G_PS_COOKIE_PROD_CATID, pId, pCatId);
}

function psRegisterListener_Add2Cart()
{
	var arrTemp=psGetElementsByName(document,"input","add to basket","image");
	if(psCheckArrayExist(arrTemp)==false)
	{
		return;
	}
	var temp=arrTemp[0];
	temp.oldFunc=temp.onclick;
	temp.onclick=function()
	{
		psSavePr2Ck();
		if(psCheckElementExist(this.oldFunc)==true)
		{
			return this.oldFunc();
		}
	}
}

function psGetCartTable()
{
	var arrTemp=document.getElementsByTagName("table");
	if(psCheckArrayExist(arrTemp)==false)
	{
		return null;
	}
	for(var i=0;i<arrTemp.length;i++)
	{
		var temp=arrTemp[i];
		try{
			if(psCheckElementExist(temp.rows[2])==true)
			{
				var row=temp.rows[2];
				if(psCheckElementExist(row.cells[1])==true)
				{
					var cell=row.cells[1];
					if(psGetInnerText(cell).toLowerCase().indexOf("item#")>=0)
					{
						return temp;
					}
				}
			}
		}
		catch(err){
		}
	}
}

function psSendShop5ActionTag(arrPrID, arrPrName, arrPrQuan, arrPrPrice, arrPrCatID)
{
	psSetCookie(G_PS_CK_SHOP5, "delete", "delete");
	for(var i=0;i<arrPrCatID.length;i++)
	{
		if(psCheckElementExist(arrPrName[i])==true)
		{
			arrPrName[i]=psHtmlDecode(arrPrName[i]).toLowerCase();
		}
		psCreateShopAction5Tag(arrPrID[i], arrPrName[i], arrPrQuan[i], arrPrPrice[i], arrPrCatID[i]);
		psSetValueToCookie(G_PS_CK_SHOP5,arrPrID[i],arrPrCatID[i]);
	}	
	psDisplayShop5s();
}

function psGetItemSku(pCell)
{
	var temp=psGetInnerText(pCell);
	return psTrim(temp);
}

function psGetProductQuantity_1(pCell)
{
	var temp= pCell.getElementsByTagName("input");
	if(psCheckArrayExist(temp)==true)
	{
		for(var i=0;i<temp.length;i++)
		{
			if(temp[i].name.indexOf("qty")>=0)
			{
				return temp[i].value;
			}
		}
	}
	return null;
}

function psGetProductName_1(pCell)
{
	var temp=pCell.getElementsByTagName("a");
	if(psCheckElementExist(temp)==true)
	{
		return psGetInnerText(temp[0]);
	}
	return null;
}

function psGetProductBasePrice_1(pCell)
{
	var price = psGetInnerText(pCell);
	if(price.indexOf("$") > -1)
		return psCleanPrice(psTrim(psGetInnerText(pCell)));
	else 
		return 0;
}

function psGetPrCatFromPrId(pPrId)
{
	var temp=psGetValueFromCookie(G_PS_COOKIE_PROD_CATID, pPrId);
	if(psCheckElementExist(temp)==false)
	{
		temp = "unknown";
	}
	return temp;
}

function psRegisterListener_ProductLink_Cart(pCell, catId)
{
	var arrTemp=pCell.getElementsByTagName("a");
	if(psCheckArrayExist(arrTemp)==false)
	{
		return;
	}
	for(var i=0;i<arrTemp.length;i++)
	{
		arrTemp[i].oldFunc=arrTemp[i].onclick;
		arrTemp[i].onclick=function()
		{
			psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_CATID, catId);
			if(psCheckElementExist(this.oldFunc)==true)
			{
				return this.oldFunc();
			}
		}
	}
}

function psSendShop5Tag()
{
	var arrPrID=new Array();
	var arrPrName=new Array();
	var arrPrQuan=new Array();
	var arrPrPrice=new Array();
	var arrPrCatID=new Array();
	
	var index=0;
	var row_index=5;
	var temp=psGetCartTable();
	var con=true;
	if(psCheckElementExist(temp)==false)
	{		
		return;
	}
	var rows = temp.rows;
	for (var r = 0; r < rows.length; r++)
	{
		var rContent = psGetInnerText(rows[r]);
		if(rows[r].cells.length < 8)
			continue;
		if(rows[r].innerHTML.toLowerCase().indexOf("remove") > -1)
		{
			var itemCol = rows[r].cells[2].innerHTML;		
			arrPrID[index] = psTrim(psGetInnerText(rows[r].cells[1]));
			arrPrName[index] = psTrim(psGetInnerText(rows[r].cells[2]));				

			if(arrPrID[index].search(/GFTCARD/i) >=0 ){
				arrPrCatID[index] = "Gift card";
				psSetValueToCookie(G_PS_COOKIE_PROD_CATID, arrPrID[index], arrPrCatID[index]);
			}
			else
			{
				arrPrCatID[index]=psGetPrCatFromPrId(arrPrID[index]);
			}
			
			arrPrQuan[index]=psGetProductQuantity_1(rows[r].cells[5]);
			arrPrPrice[index]=psGetProductBasePrice_1(rows[r].cells[6]);
			if(arrPrPrice[index] == 0 || arrPrPrice[index] == "0")
			{
					arrPrCatID[index]=arrPrName[index];
			}
			
			psRegisterListener_ProductLink_Cart(rows[r].cells[2], arrPrCatID[index]);
			psRegisterListener_ProductLink_Cart(rows[r].cells[8], arrPrCatID[index]);		
			
			index++;	
		}
		
	}
	psSendShop5ActionTag(arrPrID, arrPrName, arrPrQuan, arrPrPrice, arrPrCatID);
}

function psSaveCusInfo2Ck_Register()
{
	var temp=psGetElementByName(document, "input", "New User E-mail", "text");
	if(psCheckElementExist(temp)==false)
	{
		temp=psGetElementByName(document, "input", "Email", "text");		
		if(psCheckElementExist(temp)==false)
		{
			return;
		}
	}
	psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_PROFILE, temp.value+G_PS_SEP+temp.value);
}

function psIsRegisterSuccessfull()
{
	var arrTemp=psGetElementsByClassName(document, "span","messageerror");
	if(psCheckArrayExist(arrTemp)==true)
	{
		for(var i=0;i<arrTemp.length;i++)
		{
			var temp=psGetInnerText(arrTemp[i]);
			if(psCheckElementExist(temp)==true && temp.toLowerCase().indexOf("your information has been saved")<0)
			{
				return false;
			}
		}		
	}
	return true;
}

function psRegisterListener_Register()
{
	if(G_PS_PATHNAME.indexOf("account/newaccount/newaccountmain.jsp")>=0)
	{
		if(psIsRegisterSuccessfull()==false)
		{
			psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_FLAG,"true");
		}
		var temp=document.getElementById("form1");
		if(psCheckElementExist(temp)==true)
		{
			temp.oldFunc=temp.onsubmit;
			temp.onsubmit=function()
			{
				psSaveCusInfo2Ck_Register();
				psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_FLAG,"false");
				if(psCheckElementExist(this.oldFunc)==true)
				{
					return this.oldFunc();
				}
			}
		}
	}
}

function psIsLoginSuccessful()
{
	var accLink = document.getElementsByName("top_myaccount");
	if(psCheckArrayExist(accLink))
		return true;
	return false;
}

function psSendRegistrationTag()
{
	var temp=psGetValueFromCookie(G_PS_CK_ALL, G_PS_COOKIE_FLAG);
	if(psCheckElementExist(temp)==true && temp=="false" && psIsLoginSuccessful())
	{
		temp=psGetValueFromCookie(G_PS_CK_ALL, G_PS_COOKIE_PROFILE);
		if(psCheckElementExist(temp)==true)
		{
			psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_FLAG, "true");
			if(temp.indexOf(G_PS_SEP)<0)
			{
				if(psCheckElementExist(temp)==true && temp.length>0)
				{
					psCreateRegistrationTag(temp, temp);				
				}
			}
			else
			{
				var arrTemp=temp.split(G_PS_SEP);
				if(psCheckElementExist(arrTemp[0])==true && arrTemp[0].length>0)
				{
					if(arrTemp.length==2)
					{
						psCreateRegistrationTag(arrTemp[0], arrTemp[1]);
					}
					else if(arrTemp.length>=6)
					{
						psCreateRegistrationTag(arrTemp[0], arrTemp[1],arrTemp[2], arrTemp[3],arrTemp[4], arrTemp[5]);
					}
				}
			}
		}
		psSetValueToCookie(G_PS_CK_ALL,G_PS_COOKIE_FLAG,"delete");
	}
}

function psSaveCusInfo2Ck_ChangeAccount()
{
	var temp=psGetElementByName(document,"input","city");
	if(psCheckElementExist(temp)==false)
	{		
		return;
	}
	var city=temp.value;
	
	temp=psGetElementByName(document,"input","zip");
	if(psCheckElementExist(temp)==false)
	{		
		return;
	}
	var zip=temp.value;
	
	var state=null;
	temp=psGetElementByName(document, "select", "state");
	if(psCheckElementExist(temp)==true)
	{
		var temp1=temp.options[temp.selectedIndex].text;
		state=temp1.split("-")[0];
	}
	else
	{
		return;
	}
	
	var country=null;	
	temp=psGetElementByName(document, "select", "country");
	if(psCheckElementExist(temp)==true)
	{
		var temp1=temp.options[temp.selectedIndex].text;
		country=temp1.split("-")[0];
	}
	else
	{
		return;
	}
	
	if(G_PS_URL_PATH.indexOf("/checkout/billing/billinginformationmain.jsp")>=0)
	{
		temp=psGetElementByName(document,"input","EMail");
		if(psCheckElementExist(temp)==false)
		{		
			return;
		}
		temp=temp.value+G_PS_SEP+temp.value;
	}
	else
	{
		temp=psGetElementByName(document,"input","your email");
		if(psCheckElementExist(temp)==true)
		{
			temp=temp.value+G_PS_SEP+temp.value;
		}
		else
		{
			temp=psGetValueFromCookie(G_PS_CK_ALL, G_PS_COOKIE_PROFILE);
			if(psCheckElementExist(temp)==false)
			{
				temp="testaccount@yahoo.com"+G_PS_SEP+"testaccount@yahoo.com";
			}
			else
			{
				var arrTemp=temp.split(G_PS_SEP);
				temp=arrTemp[0]+G_PS_SEP+arrTemp[0];
			}
		}
	}
	temp=temp+G_PS_SEP+city+G_PS_SEP+state+G_PS_SEP+zip+G_PS_SEP+country;
	psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_PROFILE, temp);
}

function psRegisterListener_ChangeAccount()
{
	if(G_PS_URL_PATH.indexOf("account/changeaccount/changeaccountmain.jsp")>=0 || G_PS_URL_PATH.indexOf("/checkout/billing/billinginformationmain.jsp")>=0)
	{
		if(psIsRegisterSuccessfull()==false)
		{
			psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_FLAG,"true");
		}
		var temp=document.getElementById("form1");
		if(psCheckElementExist(temp)==true)
		{
			temp.oldFunc=temp.onsubmit;
			temp.onsubmit=function()
			{
				psSaveCusInfo2Ck_ChangeAccount();
				psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_FLAG,"false");
				if(psCheckElementExist(this.oldFunc)==true)
				{
					return this.oldFunc();
				}
			}
		}
	}
}

function psRegisterListener_Login()
{
	if(G_PS_PATHNAME.indexOf("account/login/loginmain.jsp")>=0 || G_PS_PATHNAME.indexOf("/checkout/login/checkoutmain.jsp")>=0)
	{   
		if(psIsRegisterSuccessfull()==false)
		{
			psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_FLAG,"true");
		}
		var temp=document.getElementById("form1");
		if(psCheckElementExist(temp)==true)
		{
			temp.oldFunc=temp.onsubmit;
			temp.onsubmit=function()
			{
				psSaveCusInfo2Ck_Register();
				psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_FLAG,"false");
				if(psCheckElementExist(this.oldFunc)==true)
				{
					return this.oldFunc();
				}
			}
		}
		else
		{
			var loginButtons = psGetElementsByName(document,"input","login","image");
			if(psCheckArrayExist(loginButtons))
			{				
				var loginButton = loginButtons[0];
				loginButton.oldFunc = loginButton.onclick;
				loginButton.onclick = function ()
				{					
					psSaveCusInfo2Ck_Register();
					psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_FLAG,"false");
					if(psCheckElementExist(this.oldFunc)==true)
					{
						return this.oldFunc();
					}				
				}
			}
		}		
	}
}

function psSendRegistrationTag_Register()
{
	if(G_PS_URL_PATH.indexOf("account/changeaccount/changeaccountmain.jsp")>=0 && G_PS_URL_REFERRER.indexOf("/account/newaccount/newaccountmain.jsp")>=0)
	{
		psSendRegistrationTag();
	}
}

function psSendPageViewTag_Special()
{
	if(G_PS_URL_PATH.indexOf("jump.jsp")>=0 && G_PS_URL_PATH.indexOf("itemtype=category")>=0)
	{
		var temp=psGetMenuText_TopMenu();
		var pageId="";
		if(psCheckElementExist(temp)==true)
		{
			pageId=temp.replace(G_PS_SEP+"1", "");		
			temp=psGetSubText();
			if(psCheckElementExist(temp)==false)
			{
				return false;
			}
			pageId+=" > "+temp;
			pageId = "CATEGORY: " + pageId;
		}
		else
		{
			pageId=document.title.split("-")[0];
		}		
		var catId=psGetValueFromUrl(G_PS_URL_PATH, "itemid");
		psCreatePageviewTag(pageId, catId);
		
		psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_CATID, catId);
		
		return true;
	}
	return false;
}

function psGetMenuText_TopMenu()
{
	var arrTemp=psGetElementsByClassName(document, "td","navheaderbg");
	if(psCheckArrayExist(arrTemp)==true)
	{
		var temp=arrTemp[0];
		arrTemp=temp.getElementsByTagName("a");
		if(psCheckArrayExist(temp)==true)
		{
			for(var i=0;i<arrTemp.length;i++)
			{
				if(psCheckElementExist(arrTemp[i].href)==true && arrTemp[i].href.toLowerCase().indexOf("jump.jsp?itemtype=category")>=0)
				{
					temp=psGetValueFromUrl(arrTemp[i].href,"path");
					var temp1=psGetValueFromUrl(G_PS_URL_PATH,"path");
					if(psCheckElementExist(temp1)==false)
					{
						return null;
					}
					if(temp1.indexOf(temp)>=0)
					{
						var temp2=arrTemp[i];
						arrTemp=temp2.getElementsByTagName("img");
						if(psCheckArrayExist(arrTemp)==false)
						{
							return null;
						}
						temp=(temp1==temp)? "0" : "1";
						return arrTemp[0].alt+G_PS_SEP+temp;
					}
				}
			}
		}
	}	
	return null;
}

function psGetSubText()
{
	var arrTemp=psGetElementsByClassName(document, "td","leftNavOn");
	if(psCheckArrayExist(arrTemp)==true)
	{
		return psGetInnerText(arrTemp[0]);
	}
	return null;
}

function psRegisterListener_CrossSell()
{
	var temp = null;
	var arrTemp=psGetElementsByClassName(document, "table", "crosscelltable");
	if(psCheckArrayExist(arrTemp)==false){
		temp=document.getElementById("youMayAlsoLikeTable");
	}else{
		temp=arrTemp[0];
	}
	if(psCheckElementExist(temp)==true)
	{
		arrTemp=temp.getElementsByTagName("a");
		if(psCheckArrayExist(arrTemp)==true)
		{
			for(var i=0;i<arrTemp.length;i++)
			{
				if(psCheckElementExist(arrTemp[i].href)==true && arrTemp[i].href.toLowerCase().indexOf("/jump.jsp?")>=0)
				{
					arrTemp[i].oldFunc=arrTemp[i].onclick;
					arrTemp[i].onclick=function()
					{
						psSetCookie(G_PS_COOKIE_CURRCATID, "cross sell");
						if(psCheckElementExist(this.oldFunc)==true)
						{
							return this.oldFunc();
						}
					}
				}
			}
		}
	}
}

function psGetPreviouslyViewedTable()
{
	var arrTemp=document.getElementsByTagName("table");
	if(psCheckArrayExist(arrTemp)==true)
	{
		for(var i=0;i<arrTemp.length;i++)
		{			
			var temp=arrTemp[i];
			try
			{
				if(psCheckElementExist(temp.rows[2])==true)
				{
					var row=temp.rows[2];
					var cell=row.cells[0];
					if(psCheckElementExist(cell)==true && psCheckElementExist(cell.className))
					{
						if(cell.className.toLowerCase()=="previouslyviewedhdr")
						{
							return temp;
						}
					}
				}
			}
			catch(err)
			{
			}
			if(psCheckElementExist(temp.id)==true && temp.id.toLowerCase()=="recentlyviewedproducttable")
			{
				return temp;
			}
		}
	}
	return null;
}

function psRegisterListener_PreviouslyViewed()
{
	var temp=psGetPreviouslyViewedTable();
	if(psCheckElementExist(temp)==true)
	{
		var arrTemp=temp.getElementsByTagName("a");
		if(psCheckArrayExist(arrTemp)==true)
		{
			for(var i=0;i<arrTemp.length;i++)
			{
				if(psCheckElementExist(arrTemp[i].href)==true && arrTemp[i].href.toLowerCase().indexOf("/jump.jsp?")>=0)
				{
					arrTemp[i].oldFunc=arrTemp[i].onclick;
					arrTemp[i].onclick=function()
					{
						psSetCookie(G_PS_COOKIE_CURRCATID, "RViewed");
						if(psCheckElementExist(this.oldFunc)==true)
						{
							return this.oldFunc();
						}
					}
				}
			}
		}
	}
}

function psRegisterListener_CheckOutBtn_RegistrationTag()
{
	var temp=psGetElementByName(document, "input","Checkout", "image");
	if(psCheckElementExist(temp)==true)
	{
		temp.oldFunc=temp.onclick;
		temp.onclick=function()
		{
			psSendShop5Tag();
			if(psCheckElementExist(this.oldFunc)==true)
			{
				return this.oldFunc();
			}
		}
	}
}

function psSavePr2Ck_QuickShop()
{
	var tables = document.getElementsByTagName("table");
	var prdTable = null;
	if(psCheckArrayExist(tables)){
		for(var i=0; i<tables.length; i++){
			if(tables[i].rows.length >2 && tables[i].rows[1].cells.length > 2 && psGetInnerText(tables[i].rows[1].cells[2]).indexOf("Item #") >= 0){
				prdTable = tables[i];
			}
		}
	}
	if(prdTable == null) 
		return;
	var rows = prdTable.rows;
	for(var i=0; i<rows.length; i++){
		if(rows[i].cells.length < 2)
			continue;
		if(rows[i].cells[1].innerHTML.indexOf("<table") < 0)
			continue;
		var prdId = psTrim(psGetInnerText(rows[i].cells[2]));		
		psSetValueToCookie(G_PS_COOKIE_PROD_CATID, prdId,"quickshop");
	}
}

function psRegisterListener_Add2Cart_QuickShop()
{	
	var temp=psGetElementByName(document, "form", "ProductDetail");
	if(psCheckElementExist(temp)==true)
	{
		temp.oldFunc=temp.onsubmit;
		temp.onsubmit=function()
		{
			psSavePr2Ck_QuickShop();
			if(psCheckElementExist(this.oldFunc)==true)
			{
				return this.oldFunc();
			}
		}
	}
}

function psSavePr2Shop9()
{
	if(G_PS_URL_PATH.indexOf("/checkout/invoice/invoicemain.jsp")<0)
	{
		return;
	}
	var arrPrID=new Array();
	var arrPrName=new Array();
	var arrPrCatID=new Array();
	var arrPrQuan=new Array();
	var arrPrPrice=new Array();
	var arrIndex=0;
	
	var cont=true;
	var index=0;
	var temp1="abc";
	var arrTemp=document.getElementsByTagName("tr");
	if(psCheckArrayExist(arrTemp)==true)
	{
		for(var i=0;i<arrTemp.length;i++)
		{
			var temp=arrTemp[i];
			if(arrTemp[i].className.toLowerCase().indexOf("tableitem1bg")>=0 ||arrTemp[i].className.toLowerCase().indexOf("tableitem2bg")>=0)
			{
				if(psCheckElementExist(temp.cells[1])==true)
				{
					var cell=temp.cells[1];
					temp1=psGetItemSku(cell);
					if(psCheckElementExist(temp1)==true && temp1.length>0)
					{						
						cell=temp.cells[2];
						arrPrName[index]=psGetItemSku(cell).replace(/-->/gi,"");
						cell=temp.cells[5];
						arrPrQuan[index]=psGetItemSku(cell);
						cell=temp.cells[6];
						arrPrPrice[index]=psGetProductBasePrice_1(cell);
						if(temp1.toLowerCase()=="gift box")
						{
							arrPrCatID[index]="gift";
							arrPrID[index]="giftbox";
						}
						else
						{
							arrPrID[index]= temp1;
							arrPrCatID[index] = psGetValueFromCookie(G_PS_CK_SHOP5,temp1);
						}
						index++;
					}
				}
			}
		}
		psSendShop5ActionTag(arrPrID, arrPrName, arrPrQuan, arrPrPrice, arrPrCatID);		
		psSaveShop92Ck(arrPrID, arrPrName, arrPrQuan, arrPrPrice, arrPrCatID);		
		psSaveCustInfo2Ck_Shop9();
	}
}

function psSaveShop9(prID, prName, prQuantity, prPrice, catID)
{
    var strVal =psHtmlDecode(prName) + G_PS_SEP + prQuantity + G_PS_SEP + prPrice + G_PS_SEP + catID;
    psSetValueToCookie(G_PS_CK_SHOP9, prID, strVal);
}

function psSaveShop92Ck(pArrPrID, pArrPrName, pArrPrQuan, pArrPrPrice, pArrPrCatID)
{
	var temp=0;	
    for(var i=0;i<pArrPrID.length;i++)
	{
		if(pArrPrID[i]!="PS_BEDELETED")
		{
			for(var j=i+1;j<pArrPrID.length;j++)
			{
				if(pArrPrID[j]!="PS_BEDELETED")
				{
					if(pArrPrID[i]==pArrPrID[j])
					{
						temp=parseInt(pArrPrQuan[i])+parseInt(pArrPrQuan[j]);
						pArrPrQuan[i]=temp;
						pArrPrID[j]="PS_BEDELETED";
					}
				}
			}
		}
	}
	psSetCookie(G_PS_CK_SHOP9, "temp","delete");
	for(var i=0;i<pArrPrID.length;i++)
	{
		if(pArrPrID[i]!="PS_BEDELETED")
		{
			psSaveShop9(pArrPrID[i], pArrPrName[i], pArrPrQuan[i], pArrPrPrice[i], pArrPrCatID[i]);
		}
	}
}

function psGetOrderID()
{
	if(typeof(tid)!="undefined" && tid != null && tid != "")
	{
		return tid;
	}
	
	var arrTemp=document.getElementsByTagName("div");
	if(psCheckArrayExist(arrTemp)==true)
	{
		for(var i=0;i<arrTemp.length;i++)
		{
			if(psGetInnerText(arrTemp[i]).toLowerCase().indexOf("your order number is")>=0)
			{
				arrTemp=arrTemp[i].getElementsByTagName("b");
				if(psCheckArrayExist(arrTemp)==true)
				{
					return psGetInnerText(arrTemp[0]);
				}
			}		
		}
	}
	return psGenerateRandomValue();
}

function psGetBillingArea()
{
	var arrTemp=document.getElementsByTagName("table");
	for(var i=0;i<arrTemp.length;i++)
	{
		var row=arrTemp[i].rows[0];
		var cell=row.cells[0];
		if(psGetInnerText(cell).toLowerCase().indexOf("billing info")>=0)
		{
			cell=arrTemp[i].rows[2].cells[0];
			return cell.getElementsByTagName("div")[0].innerHTML;
		}
	}
}

function psGetSubTotalShipping()
{
	var arrTemp=psGetElementsByClassName(document, "div", "tableitem");
	var subTotal="";
	var shipping=0;
	if(psCheckArrayExist(arrTemp)==true)
	{
		for(var i=0;i<arrTemp.length;i++)
		{
			if((psGetInnerText(arrTemp[i])).toLowerCase().indexOf("subtotal")>=0)
			{
				var temp1=psGetParentNode(psGetParentNode(arrTemp[i]));
				temp1=temp1.cells[1];
				subTotal=psGetInnerText(temp1);
			}
			else
			if((psGetInnerText(arrTemp[i])).toLowerCase().indexOf("shipping:")>=0)
			{
				var temp1=psGetParentNode(psGetParentNode(arrTemp[i]));
				temp1=temp1.cells[1];
				shipping+=parseFloat(psCleanPrice(psGetInnerText(temp1)));
				shipping=parseFloat(shipping);
			}
			else
			if((psGetInnerText(arrTemp[i])).toLowerCase().indexOf("handling:")>=0)
			{
				var temp1=psGetParentNode(psGetParentNode(arrTemp[i]));
				temp1=temp1.cells[1];
				shipping+=parseFloat(psCleanPrice(psGetInnerText(temp1)));
				shipping=parseFloat(shipping);
			}
		}
		return subTotal+G_PS_SEP+shipping;
	}
	return null;
}

function psSaveCustInfo2Ck_Shop9()
{
	var temp=psGetBillingArea();
	if(psCheckElementExist(temp)==false)
	{
		return;
	}
	var arrTemp=temp.toLowerCase().split("<br>");
	var email=arrTemp[arrTemp.length-1];
	var country=arrTemp[3];
	temp=psTrim(arrTemp[2]);
	
	arrTemp=temp.split(",");
	var city=arrTemp[0];
	var state=null;
	var zip=null;
	
	temp=psTrim(arrTemp[1]);
	arrTemp=temp.split(" ");
	if(arrTemp.length==1)
	{
		zip=arrTemp[0];
	}
	else
	{
		state=arrTemp[0];
		zip=arrTemp[1];
	}	
	var subTotalShipping=psGetSubTotalShipping();
	psSetCookie(G_PS_CK_SHOP9_CUSTINFO,email+G_PS_SEP+city+G_PS_SEP+state+G_PS_SEP+zip+G_PS_SEP+subTotalShipping);		
}

function psGetCustID()
{
	var temp=psGetCookie(G_PS_CK_SHOP9_CUSTINFO);
	if(psCheckElementExist(temp)==false)
	{
		temp=psGetValueFromCookie(G_PS_CK_ALL, G_PS_COOKIE_PROFILE);
		if(psCheckElementExist(temp)==false)
		{
			return psGenerateRandomValue();
		}
	}
	if(temp.indexOf(G_PS_SEP)>=0)
	{
		var arrTemp=temp.split(G_PS_SEP);
		return arrTemp[0];
	}
	else
	{
		return temp;
	}
	return null;
}

function psSendShop9ActionTags()
{
	if(G_PS_PATHNAME.indexOf("/thankyoumain.jsp")<0)
	{
		return;
	}
	var orderID=psGetOrderID();
	var cusID=psGetCustID();
	var temp=psGetCookie(G_PS_CK_SHOP9_CUSTINFO);
	if(psCheckElementExist(temp)==false)
	{
		return;
	}
	var arrTemp=temp.split(G_PS_SEP);
	var city=arrTemp[1];
	var state=arrTemp[2];
	var zip=arrTemp[3];
	var subTotal=arrTemp[4];
	var shipping=arrTemp[5];
	var country=arrTemp[6];
	var prID = null;
    var prName = null;
    var prQuan = null;
    var prPrice = null;
    var prCatID = null;

    var temp = psGetCookie(G_PS_CK_SHOP9);
    if(psCheckElementExist(temp) == false)
    {
        return;
    }
    var arrTemp = temp.split(";");
    if(psCheckArrayExist(arrTemp) == false)
    {
        return null;
    }
    arrTemp = arrTemp[0].split("#");

    if(psCheckArrayExist(arrTemp) == false)
    {
        return null;
    }
    var arrTemp1 = null;
    var arrTemp2 = null;
    for(var i = 1; i < arrTemp.length; i ++ )
    {
        arrTemp1 = arrTemp[i].split("~");
        if(psCheckArrayExist(arrTemp1) == true)
        {
            prID = arrTemp1[0];
            arrTemp2 = arrTemp1[1].split(G_PS_SEP);
            if(psCheckArrayExist(arrTemp2) == true)
            {
                prName = psCleanCatId(arrTemp2[0]);
                prQuan = arrTemp2[1];
                prPrice = arrTemp2[2];
                prCatID = arrTemp2[3];
            }
            else
            {
                prName = null;
                prQuan = null;
                prPrice = null;
                prCatID = null;
            }
            psCreateShopAction9Tag(prID, prName, prQuan, prPrice, cusID, orderID, subTotal, prCatID);
        }
    }
    psDisplayShop9s();
    psCreateOrderTag(orderID, subTotal, shipping, cusID, city, state, zip);	

	psSetCookie(G_PS_CK_SHOP9, "abc", "delete");
	psSetCookie(G_PS_CK_SHOP9_CUSTINFO, "abc", "delete");
}

function psRegisterListener_Register_Order()
{
	if(G_PS_PATHNAME.indexOf("/thankyoumain.jsp")>=0)
	{
		if(psIsRegisterSuccessfull()==false)
		{
			psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_FLAG,"true");
		}
		var temp=document.getElementById("frmGuestAccountSave");
		if(psCheckElementExist(temp)==true)
		{
			temp.oldFunc=temp.onsubmit;
			temp.onsubmit=function()
			{
				psSaveCusInfo2Ck_Register_Order();
				if(psCheckElementExist(this.oldFunc)==true)
				{
					return this.oldFunc();
				}
			}
		}
	}	
}

function psSaveCusInfo2Ck_Register_Order()
{
	var temp=psGetElementByName(document, "input", "guestemail", "text");
	if(psCheckElementExist(temp)==true && temp.value.length>0)
	{
		var temp1=psGetElementByName(document, "input", "re-enter guestemail", "text");
		if(psCheckElementExist(temp)==false || temp.value.length==0)
		{
			return;
		}
		temp1=psGetElementByName(document, "input", "Create a password", "password");
		if(psCheckElementExist(temp)==false || temp.value.length==0)
		{
			return;
		}
		temp1=psGetElementByName(document, "input", "Re-enter your password", "password");
		if(psCheckElementExist(temp)==false || temp.value.length==0)
		{
			return;
		}
		psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_FLAG,"false");
		psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_PROFILE, temp.value+G_PS_SEP+temp.value);
	}	
}

function psIsGiftCardPage()
{
	var arrTemp=psGetElementsByClassName(document, "div", "detailheader");
	if(psCheckArrayExist(arrTemp)==true)
	{
		for(var i=0;i<arrTemp.length;i++)
		{
			if(psGetInnerText(arrTemp[i]).toLowerCase().indexOf("gift card")>=0)
			{
				return true;
			}
		}
	}
	return false;
}

function psSavePr2Ck_Wishlist()
{
	var arrTemp=document.getElementsByTagName("input");
	if(psCheckArrayExist(arrTemp)==true)
	{
		for(var i=0;i<arrTemp.length;i++)
		{
			if(arrTemp[i].type=="hidden" && arrTemp[i].name.toLowerCase().indexOf("wliproductid")>=0)
			{
				var temp=arrTemp[i].value;
				psSetValueToCookie(G_PS_COOKIE_PROD_CATID, temp,"wishlist");
			}
		}
	}
}

function psRegisterListener_Add2Cart_Wishlist()
{
	var temp=psGetElementByName(document, "form", "viewwishlist");
	if(psCheckElementExist(temp)==true)
	{
		temp.oldFunc=temp.onsubmit;
		temp.onsubmit=function()
		{
			psSavePr2Ck_Wishlist();
			if(psCheckElementExist(this.oldFunc)==true)
			{
				return this.oldFunc();
			}
		}
	}
}
function psCreateErrorTag(pPageID, pCatId) 
{
	pPageID = psCleanPageId(pPageID);
	pCatId = psCleanCatId(pCatId);
    if (G_PS_DEBUG_MODE == 1 || G_PS_DEBUG_MODE == 3)
        alert("cmCreateErrorTag(" + pPageID + ", " + pCatId + ")");
    if (G_PS_DEBUG_MODE == 2 || G_PS_DEBUG_MODE == 3)
        cmCreateErrorTag(pPageID, pCatId);
}
