// Date last modified =  20091015
// Modified by =  DC

 var lpMTagConfig = {
        "lpServer" : "chat.ally.ca",
        "lpNumber" : "LPallycachat",
        "lpProtocol" : (document.location.toString().indexOf("https:")==0) ? "https" : "http"
} 
	
/*
pageStartTime is set with a timestamp as soon as the page starts loading
*/
lpMTagConfig.pageStartTime = (new Date()).getTime();

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);

/*
The code below send a PAGEVAR to LP with the time [iin seconds] it took the page to load. Code is executed in the onload event
*/
lpMTagConfig.calculateSentPageTime = function () {
	var t = (new Date()).getTime() - lpMTagConfig.pageStartTime;
	lpAddVars('page','pageLoadTime', Math.round(t/1000)+" sec");
};

if (window.attachEvent)window.attachEvent("onload",lpMTagConfig.calculateSentPageTime);
else window.addEventListener("load",lpMTagConfig.calculateSentPageTime,false);

//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();
//Extra actions to be taken once the code executes
if (typeof(lpMTagConfig.onLoadCode)=="undefined") lpMTagConfig.onLoadCode = new Array();
//Dynamic Buttons Array
if(typeof(lpMTagConfig.dynButton)=="undefined") lpMTagConfig.dynButton=new Array();

// Function that sends variables to LP - By Scope
function lpAddVars(scope,name,value) {
	if (name.indexOf('OrderTotal')!=-1 && (value=='' || value==0)) {   // pass 0 value to all but OrderTotal
		return;
	}
	
	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;
	}	
}
// Preventing long cookie transfer for IE based browsers.
function onloadEMT() { 
	var LPcookieLengthTest=document.cookie;
	if (lpMTag.lpBrowser == "IE" && LPcookieLengthTest.length>1900){
		lpMTagConfig.sendCookies=false;
	}
}

//The Trim function returns a text value with the leading and trailing spaces removed
function lpTrimSpaces(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
// 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 unit variable purpose is to route the chat or call to the designated skill. <LOB> should be replaced with the skill name, i.e. : sales
try{
	if (typeof(lpUnit)=='undefined')	var lpUnit='deposits-sales';
	if(typeof(lpAddVars)!="undefined")	lpAddVars('page','unit',lpUnit);
	if (typeof(lpLanguage)=='undefined')	var lpLanguage='english';
	if(typeof(lpAddVars)!="undefined")	lpAddVars('session','language',lpLanguage);
	lpMTagConfig.defaultInvite = "chat-" + lpUnit+"-"+lpLanguage;
	}catch(e){}

lpMTagConfig.onLoadCode[lpMTagConfig.onLoadCode.length] = onloadEMT;

//############################
//# Omniture tracking calls  #
//############################
lpMTagConfig.inviteChatStart = function(objName) {
	var inviteObj = eval(objName);
	try{
		enableInlinePNG24InChat();
		if(lpMTagConfig.dynButton != null && lpMTagConfig.dynButton.length>0){
			lpMTagConfig['dynButton0'].MakeCall();
		}
	} catch(e){}
	return true;
};

lpMTagConfig.inviteChatShown = function(objName) {
	var inviteObj = eval(objName);
	try{
	//when the chatbox is offered up, fire off a link click
	s=s_gi(s_account);
	s.eVar45=s.pageName + ':Offered';
	s.events='event13'
	s.linkTrackVars='eVar45,events';
	s.linkTrackEvents='event13';
	s.tl(this,'o','Chat offer');


	} catch(e){}
	return true;
};
lpMTagConfig.inviteChatAccept = function(objName) {
	var inviteObj = eval(objName);
	try{
		//when the chatbox is accepted, fire off a link click
		s=s_gi(s_account);
		s.eVar45=s.pageName+':Accepted';
		s.events='event14';
		s.linkTrackVars='eVar45,events';
		s.linkTrackEvents='event14';
		s.tl(this,'o','Chat accepted');
	} catch(e){}
	return true;
};
lpMTagConfig.inviteChatDeclined = function(objName) {
	var inviteObj = eval(objName);
	try{
		//when the chatbox is accepted, fire off a link click
		s=s_gi(s_account);
		s.eVar45=s.pageName+':Declined';
		s.events='event22';
		s.linkTrackVars='eVar45,events';
		s.linkTrackEvents='event22';
		s.tl(this,'o','Chat declined');
	} catch(e){}
	
	//run the animation
	try{        
		$("#chatinvite").css({visibility: 'hidden'});
        $("#chatinvite").effect("transfer",
					{ to: "#chatlinktop" },
					500,
					function(){
						$("#chatlinktop").clone().attr("id", "chatclone").addClass("chatpulse").appendTo($('#chatlinktop').parent()).fadeOut(4000, 
						function() {$(this).remove();}).dequeue();
					});    
		}catch(e){} 

	return true;
};
lpMTagConfig.inviteChatTimeout = function(objName) {
	var inviteObj = eval(objName);
	try{
		//when the chatbox is accepted, fire off a link click
		s=s_gi(s_account);
		s.eVar45=s.pageName+':Timed Out';
		s.events='event23';
		s.linkTrackVars='eVar45,events';
		s.linkTrackEvents='event23';
		s.tl(this,'o','Chat time out');
	} catch(e){}
	return true;
};

if (typeof(lpMTagConfig.db1)=='undefined') {
	lpMTagConfig.db1 = new Object();
}
if (typeof(lpMTagConfig.db2)=='undefined') {
	lpMTagConfig.db2 = new Object();
}

lpMTagConfig.db1.dbClicked = function (objName, status) {
    objRef = eval(objName);	
    if (status == 'online') {
	try{
		//when the chatbox is requested by user
		s=s_gi(s_account);
		s.eVar45=s.pageName+':Requested';
		s.events='event13,event14';
		s.linkTrackVars='eVar45,events';
		s.linkTrackEvents='event13, event14';
		s.tl(this,'o','Chat requested');


	} catch(e){}
    }
	return true;
};
//######################################################

//overriding busy action
lpMTagConfig.db1.busyAction = function (objName) {
      objRef = eval(objName);
      var chatWinURL = objRef.getActionURL("Busy");
      chatWinURL = chatWinURL.replace(/forceOffline/,"SESSIONVAR%21BusyClickOverride");
      window.open(chatWinURL,'chat'+lpMTagConfig.lpNumber,'width=472,height=320,status=0,resizable=0,menubar=no,scrollbars=no,location=no');
};
//overriding offline action
lpMTagConfig.db1.offlineAction = function (objName) {
      objRef = eval(objName);
      var chatWinURL = objRef.getActionURL("Offline");
      window.open(chatWinURL,'chat'+lpMTagConfig.lpNumber,'width=472,height=320,status=0,resizable=0,menubar=no,scrollbars=no,location=no');
};
lpMTagConfig.db2.busyAction = function (objName) {
      objRef = eval(objName);
      var chatWinURL = objRef.getActionURL("Busy");
      chatWinURL = chatWinURL.replace(/forceOffline/,"SESSIONVAR%21BusyClickOverride");
      window.open(chatWinURL,'chat'+lpMTagConfig.lpNumber,'width=472,height=320,status=0,resizable=0,menubar=no,scrollbars=no,location=no');
};
//overriding offline action
lpMTagConfig.db2.offlineAction = function (objName) {
      objRef = eval(objName);
      var chatWinURL = objRef.getActionURL("Offline");
      window.open(chatWinURL,'chat'+lpMTagConfig.lpNumber,'width=472,height=320,status=0,resizable=0,menubar=no,scrollbars=no,location=no');
};

//SSO - URL of SSO Key generation page
//lpMTagConfig.inviteChatSSOurl = lpServerChatGenToken;
// This needs to be added for afterStartPage to work
lpMTagConfig.ifVisitorCode = [];
// LP Button Code - added SSO URL
//if(typeof(lpMTagConfig.dynButton)!="undefined") {
	//lpMTagConfig.dynButton[lpMTagConfig.dynButton.length] = {"name":"chat-"+lpUnit+"-"+lpLanguage,"pid":"lp-button-DIV","ovr":"lpMTagConfig.db1","SSOURL":lpServerChatGenToken,"afterStartPage": true};
//}

if(typeof(lpMTagConfig.dynButton)!="undefined") {
	lpMTagConfig.dynButton[lpMTagConfig.dynButton.length] = {"name":"chat-"+lpUnit+"-"+lpLanguage,"pid":"chatlinktop","ovr":"lpMTagConfig.db1","afterStartPage": true};
}

	//resolve IE 
function enableInlinePNG24InChat() {
		var arVersion = navigator.appVersion.split("MSIE"), version = parseFloat(arVersion[1]);
		if(((version >= 5.5) && (version < 7)) && (document.body.filters)) {
			var chatImage = document.getElementById('chatBackdrop');
			if (chatImage) {
				var img = chatImage, imgName = img.src.toUpperCase();
				if(imgName.substring(imgName.length-3, imgName.length) == "PNG") {
					var imgID = (img.id) ? "id='" + img.id + "' " : "";
					var imgClass = (img.className) ? "class='" + img.className + "' " : "";
					var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
					var imgStyle = "display:inline-block;" + img.style.cssText;
					if(img.align == "left") { imgStyle = "float:left;" + imgStyle; }
					if(img.align == "right") { imgStyle = "float:right;" + imgStyle; }
					if(img.parentElement.href) { imgStyle = "cursor:hand;" + imgStyle; }
					var strNewHTML = "<span " + imgID + imgClass + imgTitle + 
					" style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" +
					"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" +
					"(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
					img.outerHTML = strNewHTML;
				}
		   }
		}
}

