



		

if (window.console == null || window.console == undefined || !window.console)
	console = {
		log : function() {
		},
		info : function() {
		},
		warn : function() {
		},
		error : function() {
		},
		trace : function() {
		},
		debug : function() {
		}
	};
else if (!console.debug)
	console.debug = function() {
	};


	

var	event		=	null;
var mClientUrl  = 	escape(window.location.href );
var serverPath 	= 	"http://webchat.a-cti.com:8082";
var clientId  	=	"1662980309";

try
{
if(typeof BrowserDetect == 'undefined')
	{
		var scriptTagBrowserDetect		=	document.createElement("script");
		scriptTagBrowserDetect.setAttribute("type","text/javascript"	);
		scriptTagBrowserDetect.setAttribute("src",serverPath+"/js/browserDetect.js");
		document.getElementsByTagName("head")[0].appendChild(scriptTagBrowserDetect);
	}
}
catch(e)
{
	console.info("Exception occured while trying to append the browserDetect.js file to the head :: "+e);
}

var popupchat = { clientId: '1662980309' };

var mSessionId = "";


/**
 * Generate a unique clientguid.
 * 
 */
function generateClientGuid() 
{
	var chars = "0123456789abcdefghiklmnopqrstuvwxyz";
	var string_length = 13;
	var lClientGuid = '';
	for (var i=0; i<string_length; i++) 
	{
		var rnum = Math.floor(Math.random() * chars.length);
		lClientGuid += chars.substring(rnum,rnum+1);
	}
	
	return lClientGuid;
}


var chatloader = function() 
{
	event		=	"load";
   var sessionId = generateClientGuid();
   mSessionId = sessionId;
   var chatIcon = document.getElementById("chatIcon");

// The client may decide to use all classes instead of using at least one id of chatIcon.  This eliminates that dependency.
   if(chatIcon == null || chatIcon == undefined) 
   {
		chatIcon = document.createElement("div");
		chatIcon.setAttribute("id","chatIcon");  
		chatIcon.setAttribute("style","display:none");
	    document.body.appendChild(chatIcon);
   }

     
   function startChat()
   {
	   
	   event		=	"chatInit";
	  // getBrowserNameAndVersion();
		getScriptTagInfo();
	   console.info("event:"+event);
	  var lPath = "http://webchat.a-cti.com:8082/chatAction.do?debug=false&clientId=1662980309&clientguid=null&ChatServerPath=http://webchat.a-cti.com:8082&clientUrl="+mClientUrl+"";
	  window.open(lPath,'WebChat','width=400,height=330');
       // Google Analytics using clients existing on-page impl
      try
      {
    	  pageTracker._trackPageview('/chatInteraction');
    	  console.log("Google Analytics detected. /chatInteraction");
      }
      catch(e)
      {
    	  console.log("Error calling pageTracker or pageTracker does not exist.\n");
      }
      
      try
      {
    	  console.log("Google Analytics detected. Pushed click to chat!");
    	  analytics.pushClickToChat();	//Calling the pushClickToChat function!
      }
      catch(e)
      {
    	  console.log("Error calling _gaq.push");
      }
      
   }

   // bind onclick event to mulitple icons
   function bindOnclickEvent(eventToBind)
   {
	   var chatIcons = getElementsByClassName("chatIcon");
	   for(var i=0; i<chatIcons.length; i++) 
	   {
		   chatIcons[i].onclick = eventToBind;
		   chatIcons[i].setAttribute("style","cursor:pointer"); 
	   }
   } 
 

  chatIcon.setAttribute("style","cursor:pointer;");
 
  chatIcon.onclick = startChat;
  
 
  bindOnclickEvent(startChat); 
  console.log("levent:"+event);
} 

	if(window.onload) 
	{
		var load = window.onload;
		window.onload = function() 
		{
			load();
			chatloader();
			getScriptTagInfo();
		}
	} 
	else 
	{
		window.onload = function() 
		{
			chatloader();
			getScriptTagInfo();
			
		}
	}

	var getElementsByClassName = function (className, tag, elm)
	{
		if (document.getElementsByClassName) 
		{
			getElementsByClassName = function (className, tag, elm) 
			{
				elm = elm || document;
				var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
				for(var i=0, il=elements.length; i<il; i+=1)
				{
					current = elements[i];
					if(!nodeName || nodeName.test(current.nodeName)) 
					{
						returnElements.push(current);
					}
			    }
			return returnElements;
		};
	}
	else if (document.evaluate)
	{
		getElementsByClassName = function (className, tag, elm) 
		{
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1)
			{
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try
			{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e)
			{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) 
			{
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else 
	{
		getElementsByClassName = function (className, tag, elm) 
		{
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1)
			{
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1)
			{
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) 
					{
						break;
					}
				}
				if (match) 
				{
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};

var script;
function getScriptTagInfo()
{
 try
 {
		var browserName	=	BrowserDetect.browser;
		console.info("browserName"+browserName);
	
		var browserVer	= BrowserDetect.version;
		console.info("browserVer"+browserVer);
	
		var os=BrowserDetect.OS;
		console.info("os"+os);
	
		console.info("inside the ready function ");
		console.info(serverPath);
		console.info("event:"+event);
		var reqString = serverPath + "/js/action/popupchat.do?method=getScriptTagInfo&clientId="+clientId+"&clientUrl="+mClientUrl+"&browserName="+browserName+"&browserVersion="+browserVer+"&os="+os+"&serverPath="+serverPath+"&event="+event+"&callback=processRequests";
		console.info("clientid "+clientId);
		script = document.createElement("script");
	   	script.setAttribute("type","text/javascript");
	   	script.setAttribute("src",reqString);
	   	document.body.appendChild(script);
		   
 }
 catch(e)
 {
	 console.log("Exception occurred while trying to get the browser details:: "+e);
 }
	
}

function processRequests(data)
{
  document.body.removeChild(script);
  console.log("success 		= " + data.success);
}


var analytics	=	
{
		accountNumber	:	'UA-28481882-1'	,
		checkTimer		:	null			,
		count			:	0				,
		check			:	function()
		{
			if( window._gaq  && _gaq.push)
			{
				console.info('Analytics is available!');
				//_gaq.push(['cs._setDomainName', 'webchat.conversionsupport.com']);
				_gaq.push('cs._setClientInfo', true);
				_gaq.push(['cs._setAccount', this.accountNumber]);		//Set the tracker to push events to our account
				this.pushScriptLoad();
			}
			else
			{
				if(this.count < 3)
				{
					console.info('Gonna check for analytics again!');
					this.checkTimer	=	setTimeout( 'analytics.check()' , 1500 );
					this.count++ ;
				}
				else
				{
					console.error('Analytics is not available!');
				}
			}
		},
		
		pushScriptLoad	:	function()
		{
			if( window._gaq  && _gaq.push)
			_gaq.push([ 'cs._trackEvent' , clientId , 'Script Load' , document.URL  ]);	//Pushing the event to our account
		},
		
		pushClickToChat	:	function()
		{
			if( window._gaq  && _gaq.push)
			_gaq.push([ 'cs._trackEvent' , clientId , 'Click to Chat' , document.URL  ]);	//Pushing the event to our account
		},
		
}

try
{
	analytics.check();
}
catch(e)
{
	console.error('Unable to set env for chat analytics! : '+e);
}
