//***********************************************************************************DISCLAIMER************************************************************************************
// NOTE: By downloading and using this software you expressly agree to the following: This software is for the limited use only in connection with the LivePerson services purchased by you. 
// Once the LivePerson services are no longer in use, you must cease all use of the software and destroy all copies. 
// The software is provided to you “as is” and without any warranty of any kind, express, implied or otherwise, including without limitation, any warranty of merchantability or fitness for a particular purpose.  
// In no event shall LivePerson or any of its affiliates be liable for any direct, indirect, special, incidental, or consequential damages or any other damages of any kind, whether LivePerson
// or any of its affiliates have been advised of the possibility of such loss, however caused, and on any theory of liability, arising out of or related to the possession, use or performance of this software.  
// IF YOU DO NOT AGREE TO THE FOREGOING TERMS, DO NOT DOWNLOAD THE SOFTWARE OR USE THE SOFTWARE.- 
//***********************************************************************************DISCLAIMER************************************************************************************

var isProduction = new RegExp(/http.*\/\/(?!stage).*\.(com|net)/).test(location.href);
var accountNum;
if (isProduction) accountNum = "44153975"; 
else accountNum = "62267457"; // TESTING

var lpMTagConfig = {
        'lpServer' : "sales.liveperson.net",
        'lpNumber' : accountNum,  //Production account is 44153975 and clone/testing is 62267457
        'lpProtocol' : (document.location.toString().indexOf("https:")==0) ? "https" : "http",
		'sendCookies' : false
	}
	
if (typeof(lpMTagConfig.onLoadCode)=='undefined') {
    lpMTagConfig.onLoadCode = new Array();
}
lpMTagConfig.onLoadCode[lpMTagConfig.onLoadCode.length] = postTest;

function lpAddMonitorTag(src){if(typeof(src)=='undefined'||typeof(src)=='object'){src=lpMTagConfig.lpMTagSrc?lpMTagConfig.lpMTagSrc:'/hcp/html/mTag.js';}if(src.indexOf('http')!=0){src=lpMTagConfig.lpProtocol+"://"+lpMTagConfig.lpServer+src+'?site='+lpMTagConfig.lpNumber;}else{if(src.indexOf('site=')<0){if(src.indexOf('?')<0)src=src+'?';else src=src+'&';src=src+'site='+lpMTagConfig.lpNumber;}};var s=document.createElement('script');s.setAttribute('type','text/javascript');s.setAttribute('charset','iso-8859-1');s.setAttribute('src',src);document.getElementsByTagName('head').item(0).appendChild(s);}

if (window.attachEvent) window.attachEvent('onload',lpAddMonitorTag);
else window.addEventListener("load",lpAddMonitorTag,false);

//Dynamic Buttons Array
if(typeof(lpMTagConfig.dynButton)=="undefined") lpMTagConfig.dynButton=new Array();

//Variables Arrays - By Scope
if (typeof(lpMTagConfig.pageVar)=='undefined') lpMTagConfig.pageVar = new Array();
if (typeof(lpMTagConfig.sessionVar)=='undefined') lpMTagConfig.sessionVar = new Array();
if (typeof(lpMTagConfig.visitorVar)=='undefined') lpMTagConfig.visitorVar = new Array();

// Function that sends variables to LP - By Scope
function lpAddVars(scope,name,value) 	{
	if (value != 0 && value != "")  //This is optional, depends if client wants to pass 0 or blank values
	{
	value=lpTrimSpaces(value.toString());
	switch (scope){
		case "page": 
			lpMTagConfig.pageVar[lpMTagConfig.pageVar.length] = escape(name)+"="+escape(value);
			break;
		case "session": 
			lpMTagConfig.sessionVar[lpMTagConfig.sessionVar.length] = escape(name)+"="+escape(value);
			break;
		case "visitor": 
			lpMTagConfig.visitorVar[lpMTagConfig.visitorVar.length] = escape(name)+"="+escape(value);
			break;
		}
	}
}
//Variables Arrays - By Scope using POST
if (typeof(lpMTagConfig.pageVarPost)=='undefined') lpMTagConfig.pageVarPost = new Array();
if (typeof(lpMTagConfig.sessionVarPost)=='undefined') lpMTagConfig.sessionVarPost = new Array();
if (typeof(lpMTagConfig.visitorVarPost)=='undefined') lpMTagConfig.visitorVarPost = new Array();

// Function that sends variables to LP - By Scope using POST
function lpAddVarsPost(scope,name,value) 	{
	if (value != "")  //This is optional, depends if client wants to pass blank values
	{
		switch (scope){
		case "page": 
			lpMTagConfig.pageVarPost[lpMTagConfig.pageVarPost.length] = escape(name)+"="+escape(value);
			break;
		case "session": 
			lpMTagConfig.sessionVarPost[lpMTagConfig.sessionVarPost.length] = escape(name)+"="+escape(value);
			break;
		case "visitor": 
			lpMTagConfig.visitorVarPost[lpMTagConfig.visitorVarPost.length] = escape(name)+"="+escape(value);
			break;
		}
	}
}
//Visitor activity indicator
//lpMTagConfig.enableActivityMon =<true/false>; //By default true
//lpMTagConfig.inactivityPeriod=<Inactivity Period in Sec>; //By default 120
//lpMTagConfig.actPollingInterval =<value in sec>; // By default 3 sec


//Prevent Invitation shown off page
lpMTagConfig.lpInvitePreventOffpage = true; // <true/false>  Can be change to false.

// Variables submission using lpGetVariables
/*
function lpGetVariables() {
var udes = new Array();
udes['<Scope:page/session/visitor>'] = new Array();
udes['<Scope:page/session/visitor>'][<counter>] = '<Variable Name>=<Variable Value>'; //First counter =0 
return udes;
}
*/

// Immediate Data submission function
function lpSendData(varscope,varname,varvalue){
if(typeof(lpMTag)!='undefined' && typeof(lpMTag.lpSendData)!='undefined')
  lpMTag.lpSendData(varscope.toUpperCase() +'VAR!'+ varname + '=' + varvalue, true);
}

//The Trim function returns a text value with the leading and trailing spaces removed
function lpTrimSpaces(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

//Omit a specific cookie from the list of cookies that is being sent to LP with the monitor
lpMTagConfig.GetPageCookies = function () {
var cookies = document.cookie;
if ((typeof(cookies) == "undefined") || (cookies == null)) {
            cookies = "";
}
cookies = cookies.replace(/COOKIENAME=[a-zA-Z0-9\-!]*;?/,"");
return cookies;
};
function lpGetCookies() {
    var cookies = document.cookie;
    if ((typeof(cookies) == "undefined") || (cookies == null)) {
	cookies = "";
    }
    cookies = cookies.replace(/JSESSIONIDB2C=[a-zA-Z0-9\-!]*;?/,""); // Remove JSESSIONIDB2C from the cookie value                                              
    
    return cookies;
}
function postTest() {
    lpMonitorTag.prototype.lpMTagMain = function (forceGet) {   // The main LOOP of the Monitor tag
    
    // Redirects the user session to the clone account.
    var stgRedirEnabled = this.lpGetCookie('VZW-REDIRECT-TO-STAGING-ENABLED'); 
	   	if (typeof(stgRedirEnabled) != 'undefined' && stgRedirEnabled == 1) {
			lpMTagConfig.lpNumber = '62267457';
			this.lpURL = lpMTagConfig.lpProtocol + '://'+ lpMTagConfig.lpServer +'/hc/'+lpMTagConfig.lpNumber + '/' + lpMTagConfig.lpNumber;
	   }
       	   
	   var cParam = new hcArrayStorage();
           if (typeof(forceGet)=='undefined') {
               forceGet = false;
    	}
       
       if (this.lpCmd == 'mTagStartPage' || this.lpCmd == 'mTagInPage') {
            var doPost = false;
            var postData = new hcArrayStorage();

            if (typeof(lpMTagConfig.pageVarPost)!='undefined' && lpMTagConfig.pageVarPost.length>0) {
                for (var i=0; i<lpMTagConfig.pageVarPost.length; i++ ) {
                    lpMTagConfig.pageVar[lpMTagConfig.pageVar.length] = lpMTagConfig.pageVarPost[i];
                }
                lpMTagConfig.pageVarPost = new Array();
                doPost = true;
            }

            if (typeof(lpMTagConfig.sessionVarPost)!='undefined' && lpMTagConfig.sessionVarPost.length>0) {
                for (var i=0; i<lpMTagConfig.sessionVarPost.length; i++ ) {
                    lpMTagConfig.sessionVar[lpMTagConfig.sessionVar.length] = lpMTagConfig.sessionVarPost[i];
                }
                lpMTagConfig.sessionVarPost = new Array();
                doPost = true;
            }

            if (typeof(lpMTagConfig.visitorVarPost)!='undefined' && lpMTagConfig.visitorVarPost.length>0) {
                for (var i=0; i<lpMTagConfig.visitorVarPost.length; i++ ) {
                    lpMTagConfig.visitorVar[lpMTagConfig.visitorVar.length] = lpMTagConfig.visitorVarPost[i];
                }
                lpMTagConfig.visitorVarPost = new Array();
                doPost = true;
            }

            if (doPost) { // send only after the startPage was sent - i.e. we have a visitor    
			lpMTagConfig.sendCookies = false;
			lpMTagConfig.lpSendCookies = false;

    		cParam = this.lpSetCallParams(this.lpCmd); // sets the parameters for the call inside the global  lpCallParams array
	        cParam.add('cookie',lpGetCookies());
			var callType = lpConnLib.addToQueue(this.lpURL, cParam, this.MTagCallback, true, this.maxretries, false,true,1,null,true);
	        if (callType == 'POST') {
			if (this.lpCmd == 'mTagKnockPage') {
			            this.lpCmd = 'mTagStartPage';
			        }
			        else if (this.lpCmd == 'mTagStartPage'){
			            this.lpCmd = 'mTagInPage';
        		}
		        lpMTag.lpLoopTimer = setTimeout('lpMTag.lpMTagMain(true)', lpMTag.lpDelayAfterPost * 1000);
    		}
    		return;    		
            }
        }

		lpMTagConfig.sendCookies = false;
        lpMTagConfig.lpSendCookies = false;

        cParam = this.lpSetCallParams(this.lpCmd); // sets the parameters for the call inside the global  lpCallParams array
        cParam.add('cookie',lpGetCookies());  
		        
        var callType = lpConnLib.addToQueue(this.lpURL, cParam, this.MTagCallback, true, this.maxretries, forceGet);
        if (callType == 'POST') {
		if (this.lpCmd == 'mTagKnockPage') {
			    this.lpCmd = 'mTagStartPage';
			}
			else if (this.lpCmd == 'mTagStartPage'){
			    this.lpCmd = 'mTagInPage';
		}
		lpMTag.lpLoopTimer = setTimeout('lpMTag.lpMTagMain(true)', lpMTag.lpDelayAfterPost * 1000);
    	}
    };
}

