/************************************************************************************/
/* $Revision: 153663 $
 * $Id: cmtaggingservices.txt 153663 2010-08-12 14:50:17Z yhunt $
 *
 * Author: Coremetrics/PSD 
 * Coremetrics  v1.0, 2010/08/12
 * COPYRIGHT 1999-2008 COREMETRICS, INC. 
 * ALL RIGHTS RESERVED. U.S.PATENT PENDING
 * Disclaimer: Coremetrics is not responsible for hosting or maintenance or this file
 *
 */
/************************************************************************************/
/*===========================GLOBAL VARIABLES ===============================*/
var G_PS_DEBUG_MODE 		= 2;

var G_PS_ARR_DOMAIN 		= new Array("windwardreports.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_PROFILE 	= "PS_PROFILE";
var G_PS_COOKIE_FLAG 		= "PS_FLAG";		
//G_PS_COOKIE_FLAG : 
var G_PS_SEARCH_STATUS		= 0;	// 0: begin | 1: loading | 2: loaded
/*========================= END GLOBAL VARIABLES =============================*/

/*=========================== BEGIN NAVIGATION ===============================*/
var G_PS_PREFIX = "";
if(psGetDomain(G_PS_URL_PATH).search(/(blogs|helpdesk|wiki|ideas)/gi)>-1)
	G_PS_PREFIX = RegExp.$1;
var pPageId = psGetValueFromTitle();
if(psIsLoginSuccessful() && psCheckArrayExist(psGetValueFromCookie(G_PS_COOKIE_FLAG,"$Log")))
	psPostRegistration();
if(psIsRegisterSuccessful() && psCheckArrayExist(psGetValueFromCookie(G_PS_COOKIE_FLAG,"$Reg")))
	psPostRegistration();
if(psGetDomain(G_PS_URL_PATH).search(/windwardreports\.com/gi)>-1)
{	
	if(G_PS_PATHNAME.search(/(^\/?|^\/home\/?|\/index\.htm)$/gi)>-1)
	{
		psCreatePageviewTag("Home",psCheckArrayExist(G_PS_PREFIX) ? G_PS_PREFIX : "Home",null,null);
		window.setInterval("psDoSearch();","1000");
		setTimeout("psHijackSearchBlogs();","2000");
	}
	else if(psCheckElementExist(pForm = document.getElementById("form_content_left")) && pForm.innerHTML.search(/The\s*page\s*you\s*are\s*looking\s*for\s*cannot\s*be\s*found/)>-1)
		psCreatePageviewTag("Page Not Found","Error",null,null);	
	else if(G_PS_PATHNAME.search(/^\/cubiclewars\d*\.htm/gi)>-1)
		psCreatePageviewTag(pPageId,"Cubicle Wars",null,null);
	else if(G_PS_PATHNAME.search(/^\/tutorials\.htm/gi)>-1)
		psCreatePageviewTag(pPageId,"tutorials",null,null);
	else if(G_PS_PATHNAME.search(/^\/apps\/consult\.aspx/gi)>-1)
		psCreatePageviewTag(pPageId,"Consult",null,null);	
	else if(G_PS_PATHNAME.search(/^\/(java|dot_net|arrow|autotag)\.htm/gi)>-1)
	{
		psPostProductView();
	}
	else if(G_PS_PATHNAME.search(/^\/apps\/(login|user|register|password|invoices|statement)\.aspx/gi)>-1)
	{
		psPostAccountView();
	}
	else if(psIsSearchView())
	{		
		psPostSearchView();
	}
	else if(G_PS_PATHNAME.search(/^\/apps\/cart\.aspx/gi)>-1)
	{
		psPostCartView();
	}	
	else if(psGetDomain(G_PS_URL_PATH).search(/helpdesk/gi)>-1)
	{
		if(G_PS_PATHNAME.search(/^\/(registration)(\/[^\/\.]+)?/gi)>-1)
			psCreatePageviewTag((psCheckArrayExist(RegExp.$2) ? "Sign up complete" : "Sign up"),"helpdesk",null,null);	
		else if(G_PS_PATHNAME.search(/^\/(forums|entries)\//gi)>-1)
			psCreatePageviewTag(pPageId,"helpdesk "+RegExp.$1,null,null);	
		else	
			psCreatePageviewTag(pPageId,G_PS_PREFIX,null,null);			
	}
	else if(psGetDomain(G_PS_URL_PATH).search(/blogs/gi)>-1)
	{
		pCatId = "Blogs";
		if(G_PS_PATHNAME.search(/^\/windward\-reports\/?$/gi)>-1)
			pPageId = "Home";
		else if(G_PS_PATHNAME.search(/^\/([^\/\.]+)\/?$/gi)>-1)
			pPageId = psGetValueFromTitle()+" - "+RegExp.$1;
		else if(G_PS_PATHNAME.search(/^\/([^\/\.]+)\/(\d+)\/(\d+)/gi)>-1)
			pPageId = G_PS_PATHNAME.substring(0,G_PS_PATHNAME.indexOf("."));
		psCreatePageviewTag(pPageId,pCatId,null,null);	
		setTimeout("psHijackSearchBlogs();","2000");
	}
	else if(psGetDomain(G_PS_URL_PATH).search(/wiki/gi)>-1)
	{
		if(G_PS_PATHNAME.search(/^\/Wiki.*Pages\/home\.aspx$/gi)>-1)
			pCatId = pPageId = "Documentation";
		else if(G_PS_PATHNAME.search(/\/\_layouts\/([^\/\.]+)\.aspx/gi)>-1)
			pCatId = pPageId = RegExp.$1;
		else if(G_PS_PATHNAME.search(/^\/Wiki.*Pages\/([^\/\.]+)\.aspx$/gi)>-1)
			pCatId = pPageId = unescape(RegExp.$1).replace("+"," ");
		else if(G_PS_PATHNAME.search(/\/home\.aspx/gi)>-1)
			pCatId = pPageId = psGetValueFromBreadcrumb("2");
		else
		{
			pCatId = psGetValueFromBreadcrumb("2");
			pPageId = psGetValueFromBreadcrumb("last");
		}	
		psCreatePageviewTag(pPageId,pCatId,null,null);
	}	
	else if(psGetDomain(G_PS_URL_PATH).search(/ideas/gi)>-1)
	{	
		pCatId = psGetCatIdForIdeas();
		if(G_PS_PATHNAME.search(/\/(tos|privacy)\/?/gi)>-1)
			pPageId = psIsEqual(RegExp.$1,"tos") ? "Terms of Service" : "Privacy Policy";
		else
		{
			if(psCheckElementExist(document.getElementById("views")))
			{			
				if(G_PS_PATHNAME.search(/^\/forums\/(17494\-version\-9|23324\-windward\-arrow\-3)(.+)?/gi)>-1)
				{	
					if(psCheckArrayExist(RegExp.$2))
					{
						pPageId = (RegExp.$2).split("/");
						pPageId = psCheckArrayExist(pPageId[pPageId.length-1]) ? pPageId[pPageId.length-1] : pPageId[pPageId.length-2] ;	
						pPageId = pCatId+" - "+pPageId+" ideas";
					}
					else
						pPageId = pCatId+" - top ideas";
				}	
			}
			else
				pPageId = psGetValueFromTitle();
		}
		if(G_PS_PATHNAME.search(/\/users\//gi)>-1)
			pCatId = "Ideas User"
		psCreatePageviewTag(pPageId,pCatId,null,null);
		window.setInterval("psDoSearch();","1000");
	}
	else if(G_PS_PATHNAME.search(/^\/faq\.htm/gi)>-1)
		psCreatePageviewTag("Frequently Asked Questions","Support",null,null);
	else if(psIsEqual(pPageId,"Windward Reports"))	
	{
		var pH1 = document.getElementsByTagName("h1");
		if(psCheckArrayExist(pH1))
		{	
			if(G_PS_PATHNAME.search(/\/(samples|mailmerge|tconsultation|java_lp|demo_complete|package_comp)(\.htm)?/gi)>-1)
			switch(RegExp.$1)
			{
				case "mailmerge" 		: pCatId = "Solutions";break;
				case "tconsultation" 	: pCatId = "Consult";break;
				case "java_lp" 			: pCatId = "Products";break;
				case "demo_complete" 	: pCatId = "Applications";break;
				case "package_comp" 	: pCatId = "Products";break;
				default 				: pCatId = RegExp.$1;break;
			}
			psCreatePageviewTag(pH1[0].innerHTML,pCatId,null,null);
		}	
	}
	else if(G_PS_PATHNAME.search(/^\/(javahelp)\/samples/gi)>-1)	
		psCreatePageviewTag(G_PS_PATHNAME,RegExp.$1,null,null);	
	else if(G_PS_PATHNAME.search(/\/(autotag\_|Videos\/|apps\/|dotnethelp\/|javahelp\/|autotaghelp\/)/gi)>-1)
	{	
		var pCatId = psIsEqual(RegExp.$1,"apps/") ? "Applications" : RegExp.$1;
		psCreatePageviewTag(pPageId,(pCatId).replace(/[\_\/]/gi,""),null,null);
	}
	else if(psCheckElementExist(pUL = document.getElementById("nav3")))
	{		
		var pCatId = pPageId;
		var pLi = pUL.getElementsByTagName("Li");
		if(psCheckArrayExist(pLi))
		{
			for(i=0;i<pLi.length;i++)
			{
				var pDivParent = document.getElementById("nav3_"+i+"b");
				var pDiv = document.getElementById("sc_tab"+i);
				var pPattern = new RegExp(G_PS_PATHNAME,"gi");
				if(psCheckElementExist(pDiv) && psCheckElementExist(pDivParent) && (pDivParent.innerHTML.search(pPattern)>-1 || pDiv.innerHTML.search(pPattern)>-1))
				{				
					pCatId = psCleanValue(pDivParent.innerHTML);	
					break;
				}	
			}
		}		
		psCreatePageviewTag(pPageId,pCatId,null,null);
	}
	else if(psCheckArrayExist(pPageId))	
		psCreatePageviewTag(pPageId,G_PS_PREFIX,null,null);
	else 
		psCreatePageviewTag(G_PS_PATHNAME,"ADD URL",null,null);
	psSetValueToCookie(G_PS_COOKIE_FLAG,"$Log","");	
	psSetValueToCookie(G_PS_COOKIE_FLAG,"$Reg","");
}

function psIsSearchView()
{
	if(G_PS_PATHNAME.search(/\/(search\.htm|OSSSearchResults\.aspx|search\/?$)/gi)>-1)
		return true;
	if(psCheckArrayExist(psCleanValue(psGetElementValueById("search_results"))))
		return true;
	if(psCheckArrayExist(psGetElementsByClassName(document,"div","gsc-resultsbox-visible")))
		return true;
	if(psCheckElementExist(pSearchTopic=document.getElementById("topic_search")) && !psIsEqual(pSearchTopic.style.display,"none"))	
		return true;
	return false;
}
/*============================ END NAVIGATION ================================*/

/*===================== BEGIN TAGGING BUSSINESS LOGIC ========================*/
function psPostProductView()
{
	var pCatId = RegExp.$1;
	var prd = new psProduct();
	if(prd.getProduct())
	{
		psCreatePageviewTag("PRODUCT:"+prd.name,pCatId,null,null);
		psCreateProductviewTag(prd.id,prd.name,pCatId);
	}
}

function psPostAccountView()
{
	var pPage = RegExp.$1;
	pCatId = "Store";
	if(psCheckArrayExist(pHeader = psGetElementsByClassName(document,"td","header")))
		pPageId = psCleanValue(pHeader[0].innerHTML);	
	if(psIsEqual(pPage,"login"))
		psHijackLogin();
	if(psIsEqual(pPage,"register"))
	{
		if(psIsEqual(pPageId,"Register"))
		{
			psHijackRegister();
			window.setInterval(function(){
				if(psCheckElementExist(pError = document.getElementById("ctl00_ContentPlaceHolder1_formRegister_vldRegister")) && !psIsEqual(pError.style.visibility,"hidden"))
					psSetValueToCookie(G_PS_COOKIE_FLAG,"$RegTemp","");
			},"1000");
		}
		else
		{
			psHijackUpdate();
		}	
	}
	if(psIsEqual(pPage,"user") && psCheckArrayExist(pProf = psGetValueFromCookie(G_PS_COOKIE_FLAG,"$RegTemp")))
	{
		pProf = pProf.split("-_-");
		psCreateRegistrationTag(pProf[0],pProf[1],pProf[2],pProf[3],pProf[4]);
		psSetValueToCookie(G_PS_COOKIE_FLAG,"$RegTemp","");
	}
	psCreatePageviewTag(pPageId,pCatId,null,null);
}

function psPostSearchView()
{	
	var pResult = 0;
	var pSearchTerm = psCleanValue(decodeHtml(unescape(psGetSearchTerm().replace(/\+/g," "))));
	if(psIsSearchSuccessful())
	{
		pPageId = "SEARCH SUCCESSFUL";	
		pResult = 1;
		var pPageNum = 1;
		var pContainer = psGetElementsByClassName(document,"table","ms-propertysheet");
		pContainer = psCheckArrayExist(pContainer) ? pContainer[0] : document.getElementById("search_results");
		pContainer = psCheckElementExist(pContainer) ? pContainer : document.getElementById("topic_search");
		pContainer = psCheckElementExist(pContainer) ? pContainer : document.getElementById("contentcolumn");
		pContainer = psCheckElementExist(pContainer) ? pContainer : document.getElementById("cse");
		if(psCheckElementExist(pContainer))
		{
			if(psGetDomain(G_PS_URL_PATH).search(/helpdesk/gi)>-1)
			{
				if(psCheckArrayExist(pRes = psGetElementsByClassName(document,"div","content content_grey")) && pRes.length>1 && psCheckArrayExist(pH2 = pRes[1].getElementsByTagName("h2")))
					pResult = psCleanPrice(pH2[0].innerHTML);					
				if(psCheckArrayExist(pPageNo = psGetElementsByClassName(document,"div","pagination")) && psCheckArrayExist(pPageNo = psGetElementsByClassName(pPageNo[0],"span","current")))
					pPageNum = psCleanPrice(pPageNo[0].innerHTML);				
			}
			else if(psGetDomain(G_PS_URL_PATH).search(/wiki/gi)>-1)
			{
				if(psCheckArrayExist(pRes = psGetElementsByClassName(pContainer,"div","srch-stats")))
				{			
					if(pRes[0].innerHTML.search(/\s*of\s*(about\s*)?(\d+)\.?/gi)>-1)
						pResult = RegExp.$2;						
				}
				if(psCheckArrayExist(pPageNo = psGetElementsByClassName(pContainer,"div","srch-Page srch-Page-bg")))
				{
					if(psHtmlDecode(pPageNo[0].innerHTML).search(/\<\/a\>\s*(\d+)\s*\<a([^\>]+)\>/gi)>-1)
						pPageNum = RegExp.$1;	
				}					
			}
			else if(psGetDomain(G_PS_URL_PATH).search(/ideas/gi)>-1)
			{
				if(psCheckArrayExist(pRes = pContainer.getElementsByTagName("small")))	
					pResult = psCleanPrice(pRes[0].innerHTML);
			}
			else if(psGetDomain(G_PS_URL_PATH).search(/blogs/gi)>-1)
			{
				if(psCheckArrayExist(pPaging = psGetElementsByClassName(document,"div","gsc-cursor-page gsc-cursor-current-page")))
					pPageNum = psCleanPrice(pPaging[0].innerHTML);	
			}
			if(pResult==0)
				pPageId = "SEARCH UNSUCCESSFUL";
			if(pPageNum>1)
				pPageId+=" Page "+pPageNum;
			if(psCheckArrayExist(pWikiRefine = psGetElementValueById("ctl00_PlaceHolderTitleBreadcrumb_SearchBox_SBScopesDDL")))
				pPageId+="-_-"+psTrim(pWikiRefine.substring(pWikiRefine.indexOf(":")+1,pWikiRefine.length));
		}
		else
		{
			pPageId = "SEARCH";
			pResult = null;
			pSearchTerm = null;
		}
	}
	else
		pPageId = "SEARCH UNSUCCESSFUL";
	psCreatePageviewTag(pPageId,"SEARCH",pSearchTerm,pResult);
	//Hijack paging for Blogs
	if(psCheckArrayExist(pPaging = psGetElementsByClassName(document,"div","gsc-cursor-page")))
	{
		for(i=0;i<pPaging.length;i++)
		{
			pPaging[i].oldFunc = pPaging[i].onclick;
			pPaging[i].onclick = function()
			{
				setTimeout("psPostSearchView();","1300");
				if(this.oldFunc!=null)
					return this.oldFunc();
			}
		}
	}
}

function psDoSearch()
{	
	if(psGetDomain(G_PS_URL_PATH).search(/helpdesk/gi)>-1)
	{
		var pLoading  = document.getElementById("topic_search_loading");
		if(psCheckElementExist(pLoading) && psIsEqual(pLoading.className,"loading") && G_PS_SEARCH_STATUS==0)
			G_PS_SEARCH_STATUS = 1;
		if(psCheckElementExist(pLoading) && !psIsEqual(pLoading.className,"loading") && G_PS_SEARCH_STATUS==1)
			G_PS_SEARCH_STATUS = 2;	
	}
	if(psGetDomain(G_PS_URL_PATH).search(/ideas/gi)>-1)
	{
		var pSearchBtn  = psGetElementsByClassName(document,"input","submit submit");
		if(psCheckArrayExist(pSearchBtn) && psIsEqual(pSearchBtn[0].style.display,"none") && G_PS_SEARCH_STATUS==0)
			G_PS_SEARCH_STATUS = 1;
		if(psCheckArrayExist(pSearchBtn) && !psIsEqual(pSearchBtn[0].style.display,"none") && G_PS_SEARCH_STATUS==1)
			G_PS_SEARCH_STATUS = 2;	
	}
	if(G_PS_SEARCH_STATUS==2)
	{
		G_PS_SEARCH_STATUS = 0;
		if(psIsSearchView())
			psPostSearchView();
	}
}

function psGetSearchTerm()
{
	if(psGetDomain(G_PS_URL_PATH).search(/helpdesk/gi)>-1)
	{
		var pSearch = psGetElementValueById("suggestions_query") ;
		return psCheckArrayExist(pSearch) ? pSearch : psGetElementValueById("query");
	}
	else if(psGetDomain(G_PS_URL_PATH).search(/blogs/gi)>-1)
	{
		if(psCheckArrayExist(pInput = psGetElementsByClassName(document,"input"," gsc-input")))
			return pInput[0].value;
	}
	else if(psGetDomain(G_PS_URL_PATH).search(/wiki/gi)>-1)
	{
		return psGetValueFromUrl(G_PS_URL_PATH,"k");
	}
	else if(psGetDomain(G_PS_URL_PATH).search(/ideas/gi)>-1)
	{
		return psGetElementValueById("query");
	}
	else
	{
		return psGetValueFromUrl(G_PS_URL_PATH,"q");
	}
	return null;
}

function psIsSearchSuccessful()
{
	var pBody = document.getElementsByTagName("body");
	if(psCheckArrayExist(pBody))
	{
		if(pBody[0].innerHTML.search(/(because\s*of\s*a\s*service\s*error|The\s*search\s*request\s*was\s*unable\s*to\s*connect\s*to\s*the\s*Search\s*Service\.|did\s*not\s*match\s*any\s*documents|No\s*Results|Try\s*to\s*add\s*query\s*terms\.|No\s*results\s*matching\s*your\s*search\s*were\s*found\.|No\s*ideas\s*found)/gi)>-1)
			return false;	
		if(psCheckArrayExist(pH2 = psGetElementsByClassName(document,"h2","empty_suggestion_set")) && pH2[0].innerHTML.search(/No\s*topics\s*found/gi)>-1)	
			return false;
	}
	return true;
}

function psHijackSearchBlogs()
{
	if(psCheckArrayExist(pTable = psGetElementsByClassName(document,"form","gsc-search-box")))
	{
		pTable[0].oldFunc = pTable[0].onsubmit;
		pTable[0].onsubmit = function(e)
		{		
			if(psCheckArrayExist(pTextbox = psGetElementsByClassName(document,"input"," gsc-input")) && pTextbox[0].value!="")			
				setTimeout("psPostSearchView();","1300");
			if(this.oldFunc!=null)
				return this.oldFunc();
		}
	}
	if(psCheckArrayExist(pSearchBtn = psGetElementsByClassName(document,"input","gsc-search-button")))
	{
		pSearchBtn[0].oldFunc = pSearchBtn[0].onclick;
		pSearchBtn[0].onclick = function()
		{	
			if(psCheckArrayExist(pTextbox = psGetElementsByClassName(document,"input"," gsc-input")) && pTextbox[0].value!="")			
				setTimeout("psPostSearchView();","1300");
			if(this.oldFunc!=null)
				return this.oldFunc();
		}
	}
}

function psPostCartView()
{
	if(psCheckArrayExist(pHeader = psGetElementsByClassName(document,"td","header")))
		pPageId= psCleanValue(pHeader[0].innerHTML);
	if(psIsEqual(pPageId,"Place Order"))	
	{
		pCatId = "CART";
		psSendShopAction("shop5");
		if(psCheckElementExist(pNext = document.getElementById("ctl00_ContentPlaceHolder1_wizCart_StartNavigationTemplateContainerID_StartNextButton")))
		{
			pNext.oldFunc = pNext.onclick;
			pNext.onclick = function()
			{
				psSendShopAction("shop5");
				if(this.oldFunc!=null)
					return this.oldFunc();
			}
		}
	}
	else
	{
		pCatId = "CHECKOUT";		
		if(psIsEqual(pPageId,"Register"))
		{
			if(!psCheckArrayExist(pWelcome = psGetElementValueById("ctl00_lblUser")) || pWelcome.search(/Welcome\s*:\s*\w+/gi)<0)
				psHijackRegisterCart();
		}	
		else if(psIsEqual(pPageId,"Login"))
		{
			if(psCheckElementExist(pNext = document.getElementById("ctl00_ContentPlaceHolder1_wizCart_StepNavigationTemplateContainerID_StepNextButton")))
			{
				psDoHijackLogin(pNext,"onclick");
			}
		}
		else if(pPageId.search(/Windward\s*Online\s*Store\s*Maintenance/gi)>-1)
		{
			if(psCheckElementExist(pNext = document.getElementById("ctl00_ContentPlaceHolder1_wizCart_StepNavigationTemplateContainerID_StepNextButton")))
			{
				pNext.oldFunc = pNext.onclick;
				pNext.onclick = function()
				{
					if(psCheckElementExist(pCheck = document.getElementById("ctl00_ContentPlaceHolder1_wizCart_cbUpsellSupport")) && pCheck.checked)
					{
						var pPrice = psGetElementValueById("ctl00_ContentPlaceHolder1_wizCart_lblUpsellSupport");
						pPrice = psCleanPrice(psCleanValue(pPrice.substring(pPrice.indexOf("$")+1,pPrice.length)));
						psCreateShopAction5Tag("1 year maintenance (20%/year of software licenses)","1 year maintenance (20%/year of software licenses)",1,pPrice,"Maintenance");
						psDisplayShop5s();
					}
					if(this.oldFunc!=null)
						return this.oldFunc();
				}
			}
		}
		else if(pPageId.search(/Invoice\s*\:\s*(\d+)\s*Date/gi)>-1)
		{
			pPageId = "Order Completed - Invoice " + RegExp.$1;			
			psPostOrderView();
		}
	}
	psCreatePageviewTag(pPageId,pCatId,null,null);
}

function psPostOrderView()
{
	psSendShopAction("shop9");
	var uP = new psProfile();
	uP.readProfile();
	var pOrderId = psGetElementValueById("ctl00_ContentPlaceHolder1_wizCart_lblInvNo");
	var pTotal = psGetElementValueById("ctl00_ContentPlaceHolder1_wizCart_lblInvTotal");
	var pCity = psGetElementValueById("ctl00_ContentPlaceHolder1_wizCart_lblInvCustCity");
	var pState = psGetElementValueById("ctl00_ContentPlaceHolder1_wizCart_lblInvCustState");
	var pZip = psGetElementValueById("ctl00_ContentPlaceHolder1_wizCart_lblInvCustZip");
	uP.city = psCheckArrayExist(pCity) ? psCleanValue(pCity) : null;
	uP.zipcode = psCheckArrayExist(pZip) ? psCleanValue(pZip) : null;
	uP.state = psCheckArrayExist(pState) ? psCleanValue(pState) : null;
	uP.writeProfile();
	psPostRegistration();
	psCreateOrderTag(pOrderId, pTotal, 0, uP.cusId, uP.city, uP.state, uP.zipcode);
}

/* PURPOSE: send shop5, shop9
 * Parameter pShop : "shop5" or "shop9"
*/
function psSendShopAction(pShop)
{
	var pTotal = 0;
	if(psCheckArrayExist(pTableCart = psGetElementsByClassName(document,"table","orderTable")))
	{
		if(psCheckArrayExist(pTBody = pTableCart[0].getElementsByTagName("tbody")))
		{
			if(psCheckArrayExist(pRows = pTBody[0].rows))
			{
				var prd = new psProduct();
				var pSendShop5 = false;
				var pSendShop9 = false;
				var pRowBack = psIsEqual(pShop,"shop9") ? 1 : 3;
				for(i=0;i<pRows.length-pRowBack;i++)
				{	
					if(psIsEqual(pShop,"shop9"))
					{
						if(prd.getItem9(pRows[i]))
						{
							var pOrderId = psGetElementValueById("ctl00_ContentPlaceHolder1_wizCart_lblInvNo");
							var pTotal = psGetElementValueById("ctl00_ContentPlaceHolder1_wizCart_lblInvTotal");
							var uP = new psProfile();
							uP.readProfile();
							psCreateShopAction9Tag(prd.id,prd.name,prd.quantity,prd.price,uP.cusId,pOrderId,pTotal,prd.catId);
							pSendShop9 = true;
						}		
					}
					else
					{
						if(prd.getItem5(pRows[i],i,pTotal))
						{
							pTotal = prd.total;
							psCreateShopAction5Tag(prd.id,prd.name,prd.quantity,prd.price,prd.catId);
							pSendShop5 = true;
						}
					}		
				}
				if(pSendShop5)
					psDisplayShop5s();
				if(pSendShop9)
					psDisplayShop9s();
			}
		}	
	}
}

function psHijackLogin()
{
	if(psCheckElementExist(pLogin = document.getElementById("ctl00_ContentPlaceHolder1_btnLogin")))
	{
		psDoHijackLogin(pLogin,"onclick");
	}
	if(psCheckArrayExist(pTable = document.getElementsByTagName("table")))
	{
		for(i=0;i<pTable.length;i++)
		{
			if(pTable[i].innerHTML.search(/Username\s*\:/gi)>-1 && pTable[i].innerHTML.search(/User\s*Login/gi)<0)
			{
				psDoHijackLogin(pTable[i],"onkeypress");
			}
		}
	}	
}

function psDoHijackLogin(pElement,pEvent)
{
	pElement.oldFunc = pElement[pEvent];
	pElement[pEvent] = function(e)
	{
		var pDoHijack = true;
		if(psIsEqual(pEvent,"onkeypress"))
		{
			var pKey = window.event ? event.keyCode : e.keyCode ;
			pDoHijack = (pKey == 13);
		}
		if(pDoHijack)
		{		
			var pUser = psGetElementValueById("ctl00_ContentPlaceHolder1_wizCart_txtUsername2");
			var pUser = psCheckArrayExist(pUser) ? pUser : psGetElementValueById("ctl00_ContentPlaceHolder1_txtUsername");
			if(psCheckArrayExist(pUser))
			{
				var uP = new psProfile();
				uP.cusId = pUser;
				uP.writeProfile();
				psSetValueToCookie(G_PS_COOKIE_FLAG,"$Log","yes");
			}
		}
		if(this.oldFunc!=null)
			return this.oldFunc();
	}
}

function psHijackUpdate()
{
	if(psCheckElementExist(pForm = document.getElementById("aspnetForm")))
	{
		pForm.oldFunc = pForm.onsubmit;
		pForm.onsubmit = function()
		{
			var pEmail = psGetElementValueById("ctl00_ContentPlaceHolder1_formRegister_txtEmail");
			if(psCheckEmail(pEmail))
			{
				var uP = new psProfile();
				uP.cusId = psGetElementValueById("ctl00_ContentPlaceHolder1_formRegister_lblUsername");
				uP.email = pEmail;
				var pCity = psGetElementValueById("ctl00_ContentPlaceHolder1_formRegister_txtCity");
				uP.city = psCheckArrayExist(pCity) ? pCity : null;				
				var pZip = psGetElementValueById("ctl00_ContentPlaceHolder1_formRegister_txtZip");
				uP.zipcode = psCheckArrayExist(pZip) ? pZip : null;
				var pState = psGetElementValueById("ctl00_ContentPlaceHolder1_formRegister_txtState");
				uP.state = psCheckArrayExist(pState) ? pState : null;
				uP.writeProfile();
				psSetValueToCookie(G_PS_COOKIE_FLAG,"$Reg","yes");
			}
			if(this.oldFunc!=null)
				return this.oldFunc();
		}
	}
}

function psHijackRegister()
{
	if(psCheckElementExist(pRegister = document.getElementById("ctl00_ContentPlaceHolder1_formRegister_btnRegister")))
	{
		pRegister.oldFunc = pRegister.onclick;
		pRegister.onclick = function()
		{
			setTimeout(function(){
				var pEmail = psGetElementValueById("ctl00_ContentPlaceHolder1_formRegister_txtEmail");
				if(psCheckEmail(pEmail))
				{				
					var pCusId = psGetElementValueById("ctl00_ContentPlaceHolder1_formRegister_txtUsername");
					var pCity = psGetElementValueById("ctl00_ContentPlaceHolder1_formRegister_txtCity");
					pCity = psCheckArrayExist(pCity) ? pCity : null;				
					var pZip = psGetElementValueById("ctl00_ContentPlaceHolder1_formRegister_txtZip");
					pZip = psCheckArrayExist(pZip) ? pZip : null;
					var pState = psGetElementValueById("ctl00_ContentPlaceHolder1_formRegister_txtState");
					pState = psCheckArrayExist(pState) ? pState : null;
					psSetValueToCookie(G_PS_COOKIE_FLAG,"$RegTemp",pCusId+"-_-"+pEmail+"-_-"+pCity+"-_-"+pState+"-_-"+pZip);
				}
				
			},"2000");
			if(this.oldFunc!=null)
				return this.oldFunc();		
		}
	}
}

function psHijackRegisterCart()
{
	if(psCheckElementExist(pNext = document.getElementById("ctl00_ContentPlaceHolder1_wizCart_StepNavigationTemplateContainerID_StepNextButton")))
	{
		pNext.oldFunc = pNext.onclick;
		pNext.onclick = function()
		{
			var pEmail = psGetElementValueById("ctl00_ContentPlaceHolder1_wizCart_txtEmail");
			if(psCheckEmail(pEmail))
			{
				var uP = new psProfile();
				uP.cusId = psGetElementValueById("ctl00_ContentPlaceHolder1_wizCart_txtUsername");
				uP.email = pEmail;
				var pCity = psGetElementValueById("ctl00_ContentPlaceHolder1_wizCart_txtCity");
				uP.city = psCheckArrayExist(pCity) ? pCity : null;				
				var pZip = psGetElementValueById("ctl00_ContentPlaceHolder1_wizCart_txtZip");
				uP.zipcode = psCheckArrayExist(pZip) ? pZip : null;
				var pState = psGetElementValueById("ctl00_ContentPlaceHolder1_wizCart_txtState");
				uP.state = psCheckArrayExist(pState) ? pState : null;
				uP.writeProfile();
				psSetValueToCookie(G_PS_COOKIE_FLAG,"$Reg","yes");
			}
			if(this.oldFunc!=null)
				return this.oldFunc();
		}
	}
}

function psIsLoginSuccessful()
{
	if(psCheckArrayExist(pBody = document.getElementsByTagName("body")))
	{
		if(psCheckArrayExist(pWelcome = psGetElementValueById("ctl00_lblUser")) && pWelcome.search(/Welcome\s*:\s*\w+/gi)>-1)
			return true;
		if(G_PS_PATHNAME.search(/^\/apps\/cart\.aspx/gi)>-1 && pBody[0].innerHTML.search(/Select\s*Payment\s*Method/gi)>-1)
			return true;
	}
	if(!psCheckArrayExist(G_PS_URL_REFERRER))
		return false;
	return false;
}

function psIsRegisterSuccessful()
{
	if(psCheckArrayExist(pBody = document.getElementsByTagName("body")))
	{		
		if(G_PS_PATHNAME.search(/^\/apps\/cart\.aspx/gi)>-1 && pBody[0].innerHTML.search(/Select\s*Payment\s*Method/gi)>-1)
			return true;
		if(G_PS_PATHNAME.search(/^\/apps\/user\.aspx/gi)>-1)
			return true;
		if(G_PS_PATHNAME.search(/^\/apps\/register\.aspx/gi)>-1 && !psCheckArrayExist(psGetElementValueById("ctl00_ContentPlaceHolder1_formRegister_vldRegister")))
			return true;
	}
	if(!psCheckArrayExist(G_PS_URL_REFERRER))
		return false;
	return false;
}

function psHijackToSetCookie(pElement,pCookieKey,pCatId)
{
	pElement.oldFunc = pElement.onclick;
	pElement.onclick = function HijackToSetCookie()
	{
		psSetValueToCookie(G_PS_COOKIE_FLAG,pCookieKey,pCatId);
		if(this.oldFunc!=null)
			return this.oldFunc();
	}
}

function psPostRegistration()
{
	var uP = new psProfile();	
	if(uP.readProfile())
		psCreateRegistrationTag(uP.cusId, uP.email, uP.city, uP.state, uP.zipcode, uP.newsletter, uP.subscribe);
}

function psGetValueFromTitle()
{
	var pTitle = document.getElementsByTagName("title");
	if(psCheckArrayExist(pTitle))
	{
		var pValue = pTitle[0].innerHTML;
		pValue = pValue.indexOf("-")>-1 ? pValue.substring(0,pValue.indexOf("-")) : pValue;
		pValue = escape(pValue);
		pValue = pValue.toLowerCase().indexOf("%u2013")>-1 ? pValue.substring(0,pValue.toLowerCase().indexOf("%u2013")) : pValue;
		pValue = pValue.toLowerCase().indexOf("%3a")>-1 ? pValue.substring(pValue.toLowerCase().indexOf("%3a")+3,pValue.length) : pValue;
		return psCleanValue(unescape(pValue));
	}
	return null;
}

/* PURPOSE: Get value from an item of Breadcrumb
 * pPosition : position of item that you will get. It can be a Integer number (go from 1 to number of last item) or last (for the last item)
 * Default value : the last item 
*/
function psGetValueFromBreadcrumb(pPosition)
{
	var pBreadcrumb = document.getElementById("ctl00_PlaceHolderTitleBreadcrumb_ContentMap");
	if(psCheckElementExist(pBreadcrumb))
	{
		pBreadcrumb = psHtmlDecode(psCleanHtmlTag(pBreadcrumb.innerHTML)).split(">");
		if(!psCheckArrayExist(pPosition) || psIsEqual(pPosition,"last") || pPosition > pBreadcrumb.length || pPosition <= 0)
			return pBreadcrumb[pBreadcrumb.length-1];
		else
			return pBreadcrumb[pPosition-1];
	}
	return null;
}
function psGetCatIdForIdeas()
{
	var pRibbon = document.getElementById("ribbon");
	if(psCheckElementExist(pRibbon))
	{
		var pH2 = pRibbon.getElementsByTagName("h2");
		if(psCheckArrayExist(pH2))
			return psCleanValue(pH2[0].innerHTML);
	}
	return null;
}
/*====================== END TAGGING BUSSINESS LOGIC =========================*/

/*======================= GENERAL UTILITY FUNCTION ===========================*/
/* PURPOSE: constructor for product
 * Note: you can add more methods to psProduct in its prototype
 * RETURN: none
 */
function psProduct()
{
    this.id = null;
    this.name = null;
    this.catId = null;
    this.price = null;
    this.quantity = null;
	this.total = null;

	this.reset = function()
	{
		this.id = null;
		this.name = null;
		this.catId = null;
		this.price = null;
		this.quantity = null;
		this.total = null;
	}
	/*
	 * Extracting product info from source code for posting productview tag
	 */
	this.getProduct = function()
	{
		try
		{
			this.reset(); // DO NOT REMOVE THIS IMPORTANT STATEMENT!
			var pContent = document.getElementById("form_content_left");
			pContent = psCheckElementExist(pContent) ? pContent : document.getElementById("products_content_left");			
			if(psCheckElementExist(pContent))
				if(psCheckArrayExist(pH1 = pContent.getElementsByTagName("h1")))
					this.id = this.name = psCleanValue(pH1[0].innerHTML);
			return true;
		}
		catch (ex) { return false; }
	}
	/*
	 * Extracting product info from source code specified by the "current" row
	 * of items table in the shopping cart
	 * pIndex : index of current row, use to get element id
	 * pTotal : total of current cart, use to calculate the 20% maintenance
	 */
	this.getItem5 = function(itemRow,pIndex,pTotal)
	{
		try
		{
			this.reset(); // DO NOT REMOVE THIS IMPORTANT STATEMENT!
			pIndex = pIndex<10 ? "0"+pIndex : pIndex;
			if(psCheckElementExist(pQuant = document.getElementById("ctl00_ContentPlaceHolder1_wizCart_rptrProducts_ctl"+pIndex+"_txtQuantity")) && (pQuant.value<=0 || !psCheckArrayExist(pQuant.value) || !psIsInteger(pQuant.value)))
				return false;
			if(psCheckArrayExist(pCells = itemRow.cells) && pCells.length>=5)
			{	
				this.id = this.name = psCleanValue(pCells[1].innerHTML);				
				if(itemRow.innerHTML.search(/(\d+)\%\/year\s*of\s*software\s*licenses/gi)>-1)
				{
					var rate = RegExp.$1;
					if(psCheckElementExist(pChkMaintenance = document.getElementById("ctl00_ContentPlaceHolder1_wizCart_cbSupport")) && pChkMaintenance.checked)
					{						
						this.price = pTotal*(rate)/100;
						if(this.price>0)
						{
							this.quantity = 1;
							this.catId = "maintenance";
						}
						else
							return false;
					}
					else
						return false;
				}
				else
				{					
					this.quantity = psCleanPrice(pQuant.value);
					this.price = psCleanPrice(psCleanValue(pCells[3].innerHTML));
					if(pCells[1].innerHTML.search(/href\=\"?.+\/([^\/\.]+)\.htm\"?/gi)>-1)
						this.catId = RegExp.$1;
					this.total = pTotal + this.quantity * this.price;
				}
			}
			return true;
		}
		catch (ex) { return false; }
	}
	/*
	 * Extracting product info from source code specified by the "current" row
	 * of items table in the receipt page
	 */
	this.getItem9 = function(itemRow)
	{
		try
		{
			this.reset(); // DO NOT REMOVE THIS IMPORTANT STATEMENT!
			if(psCheckArrayExist(pCells = itemRow.cells) && pCells.length>=5)
			{	
				this.id = this.name = psCleanValue(pCells[1].innerHTML);				
				if(itemRow.innerHTML.search(/Maintenance\s*declined/gi)>-1)
					return false;
				if(itemRow.innerHTML.search(/(\d+)\%\/year\s*of\s*software\s*licenses/gi)>-1)
				{											
					this.price = psCleanPrice(psCleanValue(pCells[4].innerHTML));
					if(psCheckArrayExist(this.price) && this.price>0)
					{
						this.quantity = 1;
						this.catId = "maintenance";
					}
					else
						return false;
				}
				else
				{					
					this.quantity = psCleanPrice(psCleanValue(pCells[2].innerHTML));
					this.price = psCleanPrice(psCleanValue(pCells[3].innerHTML));
					if(pCells[1].innerHTML.search(/href\=\"?.+\/([^\/\.]+)\.htm\"?/gi)>-1)
						this.catId = RegExp.$1;
				}
			}
			return true;
		}
		catch (ex) { return false; }
	}
}

/* PURPOSE: constructor for profile
 * Note: you can add more methods to psProfile in its prototype
 * RETURN: none
 */
function psProfile()
{
	this.cusId = null;
    this.email = null;
    this.city = null;
    this.state = null;
    this.zipcode = null;
	this.newsletter = null;
	this.subscribe = null;
	/*
	 * Get user profile from cookie
	 */
	this.readProfile = function()
	{
		try
		{
			this.cusId = psGetCookie(G_PS_COOKIE_PROFILE);
			if (this.cusId != null)
			{
				var buf = this.cusId.split('|');
				for (var i=0; i<buf.length; i++)
				{
					var tempVal = buf[i];
					// when NULL is written to cookie, it becomes string, not literal constant
					buf[i] = (tempVal=="null" ? null : tempVal); 
				}
				this.cusId = buf[0];
				this.email = buf[1];
				this.city = buf[2];
				this.state = buf[3];
				this.zipcode = buf[4];
				this.newsletter = buf[5];
				this.subscribe = buf[6];
			}
			if (!this.cusId)
					this.cusId = psGenerateRandomValue();
			return true;
		}
		catch (ex) { return false; }
	}
	/*
	 * Set user profile to cookie
	 */
	this.writeProfile = function()
	{
		try
		{
			if (this.cusId == null)
				return;
			// make sure that the data contains 4 parts separated by 3 '|'
			var data = this.cusId + "|" + this.email + '|' + this.city + '|' + this.state + '|' 
				+ this.zipcode + "|" + this.newsletter + '|' + this.subscribe;
			// store on cookie
			psSetCookie(G_PS_COOKIE_PROFILE, data);
			//
			// NOTE: To persist profile as persistent cookie, pass G_PS_COOKIE_LIFETIME as the third param instead of null
			// psSetCookie(G_PS_COOKIE_PROFILE, data, G_PS_COOKIE_LIFETIME);
			//
			return true;
		}
		catch (ex) { return false; }
	}
}

function psIsEqual()
{
	for (var i=0; i<arguments.length; i++)
	{
		if(arguments[0] == null || arguments[i] == null)
		{
			return false;
		}
		else if(arguments[0].toUpperCase() != arguments[i].toUpperCase())
		{
			return false;
		}
	}
	return true;
}

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, ">");
    }

    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 decodeHtml(strValue)
{
	if (strValue!=null)
	{
		strValue = strValue.replace(/\%2F/gi,"/");
		strValue = strValue.replace(/\%3F/gi,"?");
		strValue = strValue.replace(/\%3D/gi,"=");
		strValue = strValue.replace(/\%26/gi,"&");
		strValue = strValue.replace(/\%40/gi,"@");		
		strValue = strValue.replace(/\%20/gi," ");
	}
	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;   
	var count = 0; 
	while (totalsize > 3500 && count <1000)
	{
		var l1 = encodeHtml(catCookie).length;
		cookieArray = catCookie.split("#");
		cookieArray.pop();
		catCookie = cookieArray.join("#");
		var l2 = encodeHtml(catCookie).length;                                                    
		totalsize -=  (l1-l2);
		count ++;
	}   
	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;
}

function psCheckArrayExist(pArrElement)
{
    return (typeof(pArrElement) == "undefined" || pArrElement == null || pArrElement.length <= 0) ? false : true;
}

function psCheckElementExist(pElement)
{
    return (typeof(pElement) == "undefined" || pElement == null) ? false : true;
}

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 psCleanHtmlTag(pValue)
{
    return (typeof(pValue)=="string") ? pValue.replace(/\<+.+?\>+/g, "") : null;
}

function psCleanValue(pValue)
{
	return psCheckArrayExist(pValue) ? psTrim(psHtmlDecode(psCleanPageId(psCleanHtmlTag(pValue)))) : pValue;
}
/* PURPOSE : To make sure the input value is an Integer
 * 		@pNumber : any value that you want to check
 * RETURN : true | false	
 */
function psIsInteger(pNumber)
{
	try
	{		
		return pNumber == parseInt(pNumber) ? true : false ;
	}
	catch(err) {return false;}
	return false;
}
/********************************************************/
/* WRAPPER FOR COREMETRICS' TAG FUNCTIONS               */
/********************************************************/
function psCreatePageviewTag(pId, pCatId, pSrchTerm, pSrchResult) 
{
	pId = psCleanPageId(pId);
	pId = psShorttenPageID(pId);
	//pCatId = psCleanCatId(pCatId);
    if (pSrchResult != null)
        pSrchResult += "";
    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);
    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);
    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);
    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 psCreateConversionEventTag(pId, pActionType, pCatID, pPoints) 
{
	pCatID = psCleanCatId(pCatID);
    if (G_PS_DEBUG_MODE == 1 || G_PS_DEBUG_MODE == 3)
        alert("cmCreateConversionEventTag(" + pId + ", " + pActionType + ", " + pCatID + ", " + pPoints + ")");
    if (G_PS_DEBUG_MODE == 2 || G_PS_DEBUG_MODE == 3)
        cmCreateConversionEventTag(pId, pActionType, pCatID, pPoints);
}

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 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);
}

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();
}
/*===========================END GENERAL UTILITY FUNCTION ==================*/
