


var mServerName = 'http://webchat.a-cti.com:8082';
var mClientUrl  = escape(window.location.href );

var popupchat = { clientId: '1668601229' };

//alert( mClientUrl );

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() {
   // create a clientguid 
   var sessionId = generateClientGuid();
   mSessionId = sessionId;
//   var proChatIcon = document.getElementById("startWrapper");
   var chatIcon = document.getElementById("chatIcon");
//   var proChatNo = document.getElementById("closeButton");

// 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);
   }

//w390 h325      
   function startChat() {
//	alert(window.location.href);
//	alert(window.opener.location);

		var lPath = "http://webchat.a-cti.com:8082/chatAction.do?debug=false&clientId=1668601229&clientguid=null&ChatServerPath=http://webchat.a-cti.com:8082&clientUrl="+mClientUrl+"";
	   //var lPath = "http://webchat.a-cti.com:8082/chatAction.do?clientId=1668601229&clientguid=null&debug=false&ChatServerPath=http://webchat.a-cti.com:8082";
			
		/*	 if(popupchat.clientId == '2004618535') 
		      {
		        window.open(lPath,'WebChat','width=400,height=330');
		      } 
		      else if(popupchat.clientId == '1005956534') 
		      { 
		       window.open(lPath,'WebChat','width=390,height=330');
		      }
		      else
		      {
		       window.open(lPath,'WebChat','width=390,height=330');
		       
		      }*/
			
		/*	if(popupchat.clientId == '2004618535' || popupchat.clientId == '1005456964' || popupchat.clientId == '1005956534')		
		     {*/
				window.open(lPath,'WebChat','width=400,height=330');
			/* }
		      else
		     {
		       window.open(mServerName + '/pages/chatFrame.jsp?domainName='+window.location.host+'&clientUrl='+window.location.href+'&clientId='+popupchat.clientId+'&clientguid='+
		                                                generateClientGuid() +'&isPopout=new&debug=false','WebChat','width=390,height=325');
		       
		      }*/

        
  //      disableProactiveChat();
  
      // Google Analytics using clients existing on-page impl
      try {
    	  pageTracker._trackPageview('/chatInteraction');
    	  window.dump("Google Analytics detected. /chatInteraction");
      } catch(e) {
    	  window.dump("Error calling pageTracker or pageTracker does not exist.\n");
      }
      
   }

   // bind onclick event to mulitple icons
   function bindOnclickEvent(eventToBind) {
	var chatIcons = getElementsByClassName("chatIcon");
//	alert(chatIcons[0]);
	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;
  //proChatIcon.onclick = startChat;
  //proChatNo.onclick = disableProactiveChat;
 
  bindOnclickEvent(startChat); 

} //end onload

if(window.onload) {
	var load = window.onload;
	window.onload = function() {
		load();
		chatloader();
	}
} else {
	window.onload = function() {
		chatloader();
	}
}

//window.onunload = function() { 
/* this is for the client page, not the chat window! */
//	alert("Unloading...");
	
//}


// bind onclick event to multiple icons
/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
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);
};
