/************************************************************************************/
/* $Revision: $
 * $Id: $
 *
 * Author: Coremetrics/PSD 
 * Coremetrics  v1.1, 12/05/2008
 * COPYRIGHT 1999-2008 COREMETRICS, INC. 
 * ALL RIGHTS RESERVED. U.S.PATENT PENDING
 * Disclaimer: Coremetrics is not responsible for hosting or maintenance or this file
 *
 */
/************************************************************************************/
//Production data warehouse flag
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;

// current page url
var G_PS_URL_PATH = "" + document.location;
var G_PS_PATHNAME = document.location.pathname.toLowerCase();
var G_PS_QUERYSTRING = document.location.search.toLowerCase();
var G_PS_URL_REFERRER = document.referrer.toLowerCase();
var G_PS_COOKIE_LIFETIME = 432000; // 5*24*60*60 = 5 days
// cookie name
var G_PS_COOKIE_CATID = "PS_CATID";
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_FLAG = "PS_FLAG";		// used as a "session" variable to handle events between pages
// current category ID while browsing/searching/refining, etc
var G_PS_CUR_CATID = null;
var G_PS_CK_ALL="PS_ALL";
var G_PS_CK_SIGNIN="PS_CK_SIGNIN";
var G_PS_SEP="-_-";
/*========================= END GLOBAL VARIABLES =============================*/

/*=========================== BEGIN NAVIGATION ===============================*/
// Navigation logic should go here!

if(psGetUrlPath().indexOf("solarcity.com")>=0)
{
	if (psIsSearchView())
	{
		psPostSearchView();
	}
	else if (psIsProductView())
	{
		psPostProductView();
	}
	else if (psIsCartView())
	{
		psPostCartView();
	}
	else if (psIsOrderView())
	{
		psPostOrderView();
	}
	else 
	{	
		psSendPageViewTag();
		//psCreatePageviewTag(G_PS_PATHNAME, "ADD URL"); // Other pages go to "ADD URL" category
	}
	
}
/*
 * Determine if the page is the search result page
 */
function psIsSearchView()
{
	//
	// TO-Do: Your logic to determine the search page goes here
	//
	return false;
}

/*
 * Determine if the page is the product detail page
 */
function psIsProductView()
{
	//
	// TO-Do: Your logic to determine the product detail page goes here
	//
	return false;
}

/*
 * Determine if the page is the shopping cart page
 */
function psIsCartView()
{
	//
	// TO-Do: Your logic to determine the shopping cart page goes here
	//
	return false;
}

/*
 * Determine if the page is the thank you page
 */
function psIsOrderView()
{
	//
	// TO-Do: Your logic to determine the receipt page goes here
	//
	return false;
}
/*============================ END NAVIGATION ================================*/


/*===================== BEGIN TAGGING BUSSINESS LOGIC ========================*/

/* PURPOSE: Compare case-insensitive strings
 * RETURN: true: strings are not null and the same
 *         false: any of the string is null or not the same
 */

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;
}

/* PURPOSE: Get inner text of an object or clean remove html tags of a particular string
 * RETURN: resultant string or null object
 */
function psGetInnerText(pTagOjb)
{
	if (pTagOjb != null)
	{
		if (typeof(pTagOjb) == "object")
			return pTagOjb.innerHTML.replace(/\<+.+?\>+/g, "");
		else
			return pTagOjb.replace(/\<+.+?\>+/g, "");
	}
	return null;
}

/* PURPOSE: Remove all unaccepted characters in categoryid, including
 * [, ', ", :, comma,]
 * RETURN: string
 */
function psCleanCatId(pCatId)
{
    return (pCatId != null) ? pCatId.replace(/[\'\":,]/g, "") : null;
}

function psCleanPageId(pPageId)
{	
	pPageID=pPageId.replace("%e2%84%a2","");
	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; 
}

/* PURPOSE: Remove all leading & trailing spaces of a string
 * Note: [&nbsp;] is also considered as a space
 * RETURN: string
 */
function psTrim(pStr)
{
	if (pStr == null || typeof(pStr) != "string")
		return pStr;
	return (pStr) ? pStr.replace(/&nbsp;|\u00A0/gi, ' ').replace(/^\s+|\s+$/g, '') : null;
}
/* PURPOSE: extract value from the URL
 * in format of http://xxx.com/page.ext?key1=value1&key2=value2
 * RETURN: string value of the parameter
 */
function psGetValueFromUrl(pUrl, pKey)
{
    var re = new RegExp("[?&]" + pKey + "=([^&$]*)", "i");
    if (pUrl.search(re) == -1)
		return null;
    return unescape(RegExp.$1);
}

/* PURPOSE: returns the value of an element based on element_id
 * @pValueFlag: TRUE means VALUE  attribute of SELECT object returned, not innerHTML
 * RETURN: 
 *  Normal tag: decoded innerHTML
 *  INPUT tag: value attribute
 *  SELECT tag: decoded label of the selected option
 */
function psGetElementValueById(pTagId, pValueFlag)
{
    var tag = document.getElementById(pTagId);
    return psGetElementValue(tag, pValueFlag);
}

/* PURPOSE: returns the value of an element based on element object
 * Note: this function returns decoded text
 * to avoid "double" decode, don't invoke psHtmlDecode on returned value again
 * @pValueFlag: TRUE means VALUE  attribute of SELECT object returned, not innerHTML
 * RETURN: 
 *  Normal tag: decoded innerHTML
 *  INPUT tag: value attribute
 *  SELECT tag: decoded label of the selected option
 *  NULL: if element not exist
 */
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;
}

/* PURPOSE: validate email format
 * RETURN: boolean
 */
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;
}

/* PURPOSE: convert special HTML characters to normal character
 * Note: for each project, this function needs to be updated
 * RETURN: decoded string
 */
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;
}

/* PURPOSE: extract domain part in the URL
 * RETURN: domain
 */
function psGetDomain(pUrl){
    var se = /^https*\:\/\/([^\/]+)/gi;
    return (pUrl.search(se) > -1) ? RegExp.$1 : null;
}

/* PURPOSE: remove unnecessary characters (dollar sign, comma, quote, minus, etc) 
 * from price to make it work properly with parseFloat/parseInt
 * RETURN: well-formed price
 */
function psCleanPrice(pPrice)
{
	var pattern = /[^0-9\.]/gi;
    return (pPrice != null ? pPrice.toString().replace(pattern, "") : null);
}

/* PURPOSE: retrieve cookie value
 * RETURN: string
 */
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;
}

/* PURPOSE: set cookie value
 * Note: if the designated cookie is too big, the old items will be removed
 * because cookie size is limited to 4K
 * @pLifeTime in seconds
 * pDomain: don't specify if using current domain
 * RETURN: boolean
 */
function psSetCookie(pCookieName, pCookieValue, pLifeTime, pDomain)
{
    if (!pCookieName)
		return false;

	if(pLifeTime == "delete") 
    {         
        CC(pCookieName, pDomain);//delete cookie by calling coremetrics's cookie function
        return true;
    }
    // set cookie by calling coremetrics's cookie function
    var expire = (pLifeTime) ? (new Date((new Date()).getTime() + (1000 * pLifeTime))).toGMTString() : null;
    
    return CB(pCookieName, escape(pCookieValue), expire, pDomain);
}

/* PURPOSE: set value in cookie in format of:
 * #key1~value1#key2~value2
 * RETURN: string
 * NOTE: Use null or '' for pValue to remove the pair specified by pKey
 */
function psSetValueToCookie(pCookieName, pKey, pValue)
{
	// "normalize" input parameters
	pCookieName = psTrim(pCookieName);
	pKey = (pKey != null ? psTrim(pKey).toLowerCase() : pKey);
	// 
	var catCookie = psGetCookie(pCookieName);
	if (catCookie == null)
		catCookie = '';

	if (catCookie.indexOf(pKey) >=0) // Store before -> remove the old value
	{
        var reg = new RegExp("#" + pKey + "~([^#]*)", "gi");
        catCookie = catCookie.replace(reg, "");
	}
	// remove the last items (eldest items) until cookie size < 3500	
	if (pValue != null && pValue != '')
	{
		catCookie = "#" + pKey + "~" + pValue + catCookie;
		var cookieArray = null;
		while (catCookie.length > 3500)
		{
			cookieArray = catCookie.split("#");
			cookieArray.pop();
			catCookie = cookieArray.join("#");
		}
	}
	// Save to cookie
	psSetCookie(pCookieName, catCookie, G_PS_COOKIE_LIFETIME);
}

/* PURPOSE: get value stored in cookie in format of:
 * #key1~value1#key2~value2
 * RETURN: string
 */
function psGetValueFromCookie(pCookieName, pKey)
{
	// "normalize" input parameters
	pCookieName = psTrim(pCookieName);
	pKey = psTrim(pKey);
	// extract catId associated with the specified key (pKey)
    var catCookie = psGetCookie(pCookieName);
    if (catCookie != null)
    {
        var re = new RegExp("#" + pKey + "~([^#$]+)", "i");
		if (catCookie.search(re) == -1)
			return null;

        return RegExp.$1;
    }
    return null;
}

/********************************************************/
/* WRAPPER FOR COREMETRICS' TAG FUNCTIONS               */
/********************************************************/
function psCreatePageviewTag(pId, pCatId, pSrchTerm, pSrchResult) 
{
	pId = psCleanPageId(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);
    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);
    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) 
{
    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 ==================*/

/*===========================FUNCTIONS BY DUC NGUYEN==================*/
/* 	#SECTION : 	Methods that can be reused in other project		 */

// Find element by name
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;
}

// Find element by name
function psGetElementByName(psDocument, psElementTagName, psElementName, psElementType)
{
    // Find all elements that tag name is specified
    var arrInputs = psDocument.getElementsByTagName(psElementTagName);
    if(arrInputs == null)
    {
        return null;
    }
    for(var i = 0; i < arrInputs.length; i ++ )
    {
        // Find one that has specified name
        if(arrInputs[i].name.toLowerCase() == psElementName.toLowerCase())
        {
            // If element type is provided
            if(typeof(psElementType) != "undefined" && psElementType != "")
            {
                // Find it
                if(arrInputs[i].type.toLowerCase() == psElementType)
                {
                    return arrInputs[i];
                }
            }
            else
            {
                return arrInputs[i];
            }
        }
    }
    return null;
}

// Get elements which name is known
function psGetElementsByName(psDocument, psElementTagName, psElementName, psElementType)
{
    var arrResult = new Array();
    var index = 0;
    // Find all elements that tag name is specified
    var arrInputs = psDocument.getElementsByTagName(psElementTagName);
    if(arrInputs == null)
    {
        return null;
    }
    for(var i = 0; i < arrInputs.length; i ++ )
    {
        // Find one that has specified name
        if(arrInputs[i].name.toLowerCase() == psElementName.toLowerCase())
        {
            // If element type is provided
            if(typeof(psElementType) != "undefined" && psElementType != "")
            {
                // Find it
                if(arrInputs[i].type.toLowerCase() == psElementType)
                {
                    arrResult[index] = arrInputs[i];
                    index ++ ;
                }
            }
            else
            {
                arrResult[index] = arrInputs[i];
                index ++ ;
            }
        }
    }

    return arrResult;
}

/* 	#SECTION : Methods to check element exist or not */
// Check if array is exist or not
function psCheckArrayExist(pArrElement)
{
    if(typeof(pArrElement) == "undefined" || pArrElement == null || pArrElement.length <= 0)
    {
        return false;
    }

    return true;
}

// Check an element exist or not
function psCheckElementExist(pElement)
{
    if(typeof(pElement) == "undefined" || pElement == null)
    {
        return false;
    }

    return true;
}

function psCleanText(pText)
{
	var temp=pText;
	temp=temp.replace(/[\n]/g, "");	
	temp=psTrim(temp);
	return psTrim(temp);
}

function psGetUrlPath()
{
	return G_PS_URL_PATH.toLowerCase();
}

function psGetParentNode(pElement)
{	
	var temp=pElement.parentNode;	
	if(psCheckElementExist(temp)==false)
	{
		temp=pElement.parentElement;
		if(psCheckElementExist(temp)==false)
		{
			return null;
		}
	}
	
	return temp;
}

function psSaveCurCatID2Ck(pCurCatID)
{
	pCurCatID=pCurCatID.replace(/[$]/g, "@");
	psSetValueToCookie(G_PS_CK_ALL, G_PS_CUR_CATID, pCurCatID);
}

function psGetCurCatID()
{
	var temp=psGetValueFromCookie(G_PS_CK_ALL, G_PS_CUR_CATID);
	if(psCheckElementExist(temp)==false)
	{
		temp="Bookmark";
	}
	temp=temp.replace(/[@]/g, "$");
	return temp;
}

function psSaveCurPageID2Ck(pCurPageID)
{
	if(psCheckElementExist(pCurPageID)==false)
	{
		pCurPageID=psGetPageIDfrUrl_1();
	}
	pCurPageID=pCurPageID.replace(/[$]/g, "@");
	psSetValueToCookie(G_PS_CK_ALL, G_PS_CUR_PAGEID, pCurPageID);
}

function psGetCurPageID()
{
	var temp=psGetValueFromCookie(G_PS_CK_ALL, G_PS_CUR_PAGEID);
	if(psCheckElementExist(temp)==false)
	{
		temp=psGetCurCatID();
		if(psCheckElementExist(temp)==false)
		{
			temp=psGetPageIDfrUrl_1();
		}
	}
	temp=temp.replace(/[@]/g, "$");
	return temp;
}

function psIsBookmark()
{
	if (G_PS_URL_REFERRER!=""){
		if (psGetDomain(G_PS_URL_REFERRER).indexOf("allejewelry.com")>=0)
		{
			return false;
		}
	}
	
	return true;
}

function psGetH1()
{
	var arrTemp=document.getElementsByTagName("h1");
	if(psCheckArrayExist(arrTemp)==false)
	{
		return null;
	}
	temp=arrTemp[0];
	
	temp=psGetInnerText(temp);
	temp=temp.replace(/[\n]/gi, "");
	temp=psTrim(temp);	
	return temp;
}

function psIsTopMenuPage()
{
	var temp="/";
	if(G_PS_PATHNAME.indexOf(temp)>=0)
	{
		var arrTemp=G_PS_PATHNAME.split(temp);		
		if(arrTemp.length==3)
		{
			if(arrTemp[0]=="" && arrTemp[arrTemp.length-1]=="default.aspx")
			{
				return true;
			}
		}
	}
	return false;
}

function psGetFirstFilePath()
{
	var temp="/";
	if(G_PS_PATHNAME.indexOf(temp)>=0)
	{
		var arrTemp=G_PS_PATHNAME.split(temp);
		if(arrTemp.length>1)
		{
			return arrTemp[1];
		}
	}
	return null;

}

function psGetFirstFilePath_1()
{
	var temp="/";	
	
	if(G_PS_URL_REFERRER.indexOf("solarcity.com")>=0)
	{
		var temp1=G_PS_URL_REFERRER.indexOf("solarcity.com");
		temp1=G_PS_URL_REFERRER.substring(temp1+13);
		if(temp1.indexOf(temp)>=0)
		{
			var arrTemp=temp1.split(temp);
			if(arrTemp.length>1)
			{
				return arrTemp[1];
			}
		}
	}
	
	return psGetFirstFilePath();

}


function psGetSecondFilePath()
{
	var temp="/";
	if(G_PS_PATHNAME.indexOf(temp)>=0)
	{
		var arrTemp=G_PS_PATHNAME.split(temp);
		if(arrTemp.length>2)
		{
			return arrTemp[2];
		}
	}
	return null;
}

function psGetThirdFilePath()
{
	var temp="/";
	if(G_PS_PATHNAME.indexOf(temp)>=0)
	{
		var arrTemp=G_PS_PATHNAME.split(temp);
		if(arrTemp.length>3)
		{
			return arrTemp[3];
		}
	}
	return null;
}

function psGetTitle()
{
	return document.title;
}

function psGetContentTitle()
{
	var arrTemp=psGetElementsByClassName(document, "h1", "contenttitle");
	if(psCheckArrayExist(arrTemp)==false)
	{
		arrTemp=document.getElementsByTagName("h1");
		if(psCheckArrayExist(arrTemp)==false)
		{
			return null;
		}
	}
	return psCleanText(psGetInnerText(arrTemp[0]));
}

function psGetJobTitle()
{
	var temp=document.getElementById("ctl00_contentPlaceholderMain_frmJob_title");
	if(psCheckElementExist(temp)==false)
	{
		temp=psGetElementsByClassName(document, "div", "jobtitle");
		if(psCheckArrayExist(temp)==false)
		{
			return null;
		}
		temp=temp[0];
		temp=psCleanText(psGetInnerText(temp));
		return temp;
	}
	
	temp=psCleanText(psGetInnerText(temp));
	return temp;
}

function psGetCurrentDropMenuDiv()
{
	var temp=document.getElementById("dropmenu1");
	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)
				{
					var temp1=arrTemp[i].href;
					temp1=temp1.toLowerCase();					
					if(psGetUrlPath().indexOf(temp1)>=0)
					{
						temp=psGetParentNode(arrTemp[i]); 
						return temp;
					}
				}
			}
		}
	}
	
	var temp=document.getElementById("dropmenu2");
	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)
				{
					var temp1=arrTemp[i].href;
					temp1=temp1.toLowerCase();
					if(psGetUrlPath().indexOf(temp1)>=0)
					{
						temp=psGetParentNode(arrTemp[i]); 
						return temp;
					}
				}
			}
		}
	}
	
	return null;
}

function psGetResidentialCatID()
{	
	var temp=psGetCurrentDropMenuDiv();
	if(psCheckElementExist(temp)==false)
	{
		return "residential";
	}
	
	temp=temp.id;
	temp=temp.toLowerCase();
	if(temp!="chromemenu")
	{
		var temp1=document.getElementById("chromemenu");
		if(psCheckElementExist(temp1)==true)
		{
			var arrTemp=temp1.getElementsByTagName("a");
			if(psCheckArrayExist(arrTemp)==true)
			{
				for(var i=0;i<arrTemp.length;i++)
				{
					if(psCheckElementExist(arrTemp[i].rel)==true)
					{
						if(arrTemp[i].rel.toLowerCase()==temp)
						{
							if(psCheckElementExist(arrTemp[i].title)==true)
							{
								return arrTemp[i].title;
							}
							else
							{
								return "residential";
							}
						}
					}
				}
			}
		}
	}
	else
	{		
		var temp=document.getElementById("chromemenu");
		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)
					{
						var temp1=arrTemp[i].href;
						temp1=temp1.toLowerCase();
						if(psGetUrlPath().indexOf(temp1)>=0)
						{
							if(arrTemp[i].rel.toLowerCase==temp)
							{
								if(psCheckElementExist(arrTemp[i].title)==true)
								{
									return arrTemp[i].title;
								}
								else
								{
									return "residential";
								}
							}
						}
					}
				}
			}
		}
	}
	return "residential";
}

function psGetThirdTitleBar()
{
	var temp=document.getElementById("third-title-bar");
	if(psCheckElementExist(temp)==false)
	{
		temp=document.title;
	}
	
	temp=psCleanText(psGetInnerText(temp));
	return temp;
}

function psSendPageViewTag()
{
	var pageId="Home";
	var catId="Home";
	if(psGetUrlPath().indexOf("solarguard.solarcity.com/")>=0)
	{
		catId="customer care";
		if(G_PS_PATHNAME.indexOf("kiosk/login/default.aspx")>=0)
		{
			pageId="customer care login";			
			psRegisterListener_Login();
		}
		else
		if(G_PS_PATHNAME.indexOf("kiosk/solarguard.aspx")>=0)
		{
			pageId="solarguard monitor";
		}
		else
		if(G_PS_PATHNAME.indexOf("kiosk/welcome.aspx")>=0)
		{
			pageId="customer care home";
			psSendRegistrationTag();
		}
		else
		{
			pageId=psGetH1();
			if(psCheckElementExist(pageId)==false)
			{
				return;
			}
		}		
	}
	else
	if(psGetUrlPath().indexOf("solarlease.solarcity.com/solarbidlite/")>=0)
	{
		catId="Calculator";		
		if(G_PS_PATHNAME.indexOf("mappage.aspx")>=0)
		{
			pageId="Calculator Step 2: Map Page";
			//psCreateConversionEventTag("estimator", 1, "residential");
		}
		else
		if(G_PS_PATHNAME.indexOf("/thanksreferral.aspx")>=0)
		{
			pageId="Chart savings emailed";
		}
		else
		if(G_PS_PATHNAME.indexOf("thanks.aspx")>=0)
		{
			var temp=psGetValueFromUrl(psGetUrlPath(), "id");
			if(psCheckElementExist(temp)==false ||temp=="")
			{
				pageId="Calculator thank you";
			}
			else
			{
				pageId="residential lead form thank you";
			}
		}
		else
		if(G_PS_PATHNAME.indexOf("consultation.aspx")>=0)
		{
			var temp=psGetValueFromUrl(psGetUrlPath(), "id");
			if(psCheckElementExist(temp)==false)
			{
				pageId="residential lead form";
				psCreateConversionEventTag("consultation", 1, "residential");
			}
			else
			{
				pageId="request consultation";
			}
		}
		else
		if(G_PS_PATHNAME.indexOf("estimator.aspx")>=0)
		{
			if(psGetUrlPath()=="http://solarlease.solarcity.com/solarbidlite/estimator.aspx?zip=&bill=")
			{
				psCreateConversionEventTag("estimator", 1, "residential");
			}
			var temp=psGetValueFromUrl(psGetUrlPath(), "id");
			if(psCheckElementExist(temp)==false)
			{
				temp=psGetValueFromUrl(psGetUrlPath(), "zip");
				if(psCheckElementExist(temp)==false)
				{
					temp=catId;
				}
				else
				{
					pageId="Calculator start";					
				}
			}
			else
			{
				pageId="savings chart";
			}
		}
		if(G_PS_PATHNAME.indexOf("solarbidlite/thanks.aspx")>=0)
		{			
			if(G_PS_URL_REFERRER=="http://solarlease.solarcity.com/solarbidlite/consultation.aspx")
			{
				psCreateConversionEventTag("consultation", 2, "residential");
			}
			else
			{
				psCreateConversionEventTag("estimator", 2, "residential");
			}			
		}
	}
	else
	if(G_PS_PATHNAME=="/" || G_PS_PATHNAME=="/default.aspx" )
	{
		pageId="SolarCity Home";
		catId="Home";
	}
	else
	if(psGetFirstFilePath()=="solarlease")
	{
		catId="residential";
		if(G_PS_PATHNAME.indexOf("solarlease/solarcitysolar101.aspx")>=0 || G_PS_PATHNAME.indexOf("solarlease/default.aspx")>=0)
		{
			pageId=catId+" Home";
		}
		else
		{
			pageId=psGetThirdTitleBar();
			catId=psGetResidentialCatID();			
			
			if(G_PS_PATHNAME.indexOf("canopy-mounting-system.aspx")>=0)
			{
				pageId="canopy mounting system";
			}
			else
			if(G_PS_PATHNAME.indexOf("solarlease/solarcityadvantage.aspx")>=0)
			{
				pageId="SolarCity Advantage";
			}
		}
	}
	else
	if(psGetFirstFilePath()=="referral" || psGetFirstFilePath()=="salestools" || psGetFirstFilePath()=="aboutus" || psGetFirstFilePath()=="media-center" || psGetFirstFilePath()=="contact" || psGetFirstFilePath()=="community-program" || psGetFirstFilePath()=="video" || psGetFirstFilePath()=="news" || psGetFirstFilePath()=="events" || psGetFirstFilePath()=="downloads" || psGetFirstFilePath()=="presentation" || psGetFirstFilePath()=="states")	
	{
		pageId=psGetFirstFilePath()+": "+psGetTitle();
		catId=psGetFirstFilePath();
	}
	else
	if(psGetFirstFilePath()=="campaigns")
	{
		pageId=psGetTitle();
		catId=psGetFirstFilePath();
	}
	else
	if(psGetFirstFilePath()=="customercare" || psGetFirstFilePath()=="client-portfolio")
	{
		pageId=psGetTitle();
		catId=psGetFirstFilePath_1();
		
		if(G_PS_PATHNAME.indexOf("/client-portfolio/commercial/roof-mount.aspx")>=0 || G_PS_PATHNAME.indexOf("/client-portfolio/commercial/non-penetrating-roof-mount.aspx")>=0 || G_PS_PATHNAME.indexOf("/client-portfolio/commercial/ground-mount.aspx")>=0 || G_PS_PATHNAME.indexOf("/client-portfolio/residential/ground-mount.aspx")>=0 || G_PS_PATHNAME.indexOf("/client-portfolio/residential/cement-tile-roof-mount.aspx")>=0 || G_PS_PATHNAME.indexOf("/client-portfolio/residential/wood-shake-roof-mount.aspx")>=0 || G_PS_PATHNAME.indexOf("/client-portfolio/residential/flat-roof.aspx")>=0 || G_PS_PATHNAME.indexOf("/client-portfolio/residential/comp-shingle.aspx")>=0 || G_PS_PATHNAME.indexOf("/client-portfolio/residential/curved-tile.aspx")>=0)
		{
			catId="portfolio";
		}		
	}
	else
	if(psGetFirstFilePath()=="hr")
	{		
		catId=psGetFirstFilePath();
		if(G_PS_PATHNAME.indexOf("hr/careers.aspx")>=0)
		{
			pageId="careers";
		}
		else
		{
			pageId=psGetJobTitle();
		}
	}
	else
	if(psGetFirstFilePath()=="pressreleases")
	{
		catId="pressreleases";
		var temp=psGetThirdFilePath();
		if(psCheckElementExist(temp)==false)
		{
			temp=psGetSecondFilePath();
		}
		temp=temp.replace("%e2%84%a2","");
		pageId=temp;
	}
	else	
	if(psIsTopMenuPage()==true)
	{
		catId=psGetFirstFilePath();
		pageId=psGetTitle()+" Home";		
	}
	else
	if(psGetSecondFilePath()=="learn-about-solar" || psGetSecondFilePath()=="products-and-services")
	{
		catId=psGetFirstFilePath()+"-"+psGetSecondFilePath();
		pageId=psGetFirstFilePath()+": "+psGetContentTitle();
		
		if(G_PS_PATHNAME.indexOf("/learn-about-solar/how-solar-systems-work.aspx")>=0)
		{
			catId=psGetFirstFilePath();
		}
	}
	else
	if(psGetFirstFilePath()=="commercial" || psGetFirstFilePath()=="government")
	{
		catId=psGetFirstFilePath();
		pageId=catId+": "+psGetContentTitle();
	}
	else
	if(G_PS_PATHNAME.indexOf("/privacy-policy.aspx")>=0)
	{
		catId="privacy";
		pageId=psGetContentTitle();
	}
	else
	if(G_PS_PATHNAME.indexOf("/about-us.aspx")>=0)
	{
		catId="aboutus";
		pageId=psGetContentTitle();
	}
	else
	if(G_PS_PATHNAME.indexOf("/contact-us.aspx")>=0)
	{
		catId="contact";
		pageId=psGetContentTitle();
	}
	else
	if(G_PS_PATHNAME.indexOf("/terms-of-use.aspx")>=0)
	{
		catId="terms";
		pageId=psGetContentTitle();
	}
	else
	if(G_PS_PATHNAME.indexOf("errorpage.aspx")>=0 || G_PS_PATHNAME.indexOf("404.aspx")>=0)
	{
		pageId="Error";
		catId=pageId;
	}
	else
	{
		pageId=G_PS_PATHNAME;
		catId="Add url";
	}
	if(psCheckElementExist(pageId)==false)
	{
		return;
	}
	psCreatePageviewTag(pageId, catId);
}

function psSaveProfile2Ck_Login()
{
	var temp= document.getElementById("ctl00_cphMain_tbxEml");
	if(psCheckElementExist(temp)==false)
	{
		return;
	}
	temp=temp.value;
	psSetValueToCookie(G_PS_CK_ALL, G_PS_COOKIE_PROFILE, temp);
	psSetValueToCookie(G_PS_CK_ALL, G_PS_CK_SIGNIN, "false");
}

function psRegisterListener_Login()
{
	var temp=psGetElementByName(document, "form", "aspnetForm");
	if(psCheckElementExist(temp)==false)
	{
		return;
	}
	
	temp.oldFunc=temp.onsubmit;
	temp.onsubmit=function()
	{
		psSaveProfile2Ck_Login();
		if(psCheckElementExist(this.oldFunc)==true)
		{
			return this.oldFunc();
		}
	}
}

function psSendRegistrationTag()
{
	var temp=psGetValueFromCookie(G_PS_CK_ALL, G_PS_CK_SIGNIN);
	if(psCheckElementExist(temp)==false || temp=="false")
	{
		temp=psGetValueFromCookie(G_PS_CK_ALL, G_PS_COOKIE_PROFILE);
		if(psCheckElementExist(temp)==true)
		{
			psCreateRegistrationTag(temp, temp);
			psSetValueToCookie(G_PS_CK_ALL, G_PS_CK_SIGNIN,"true");
		}
	}
}