// ==============================================================================
// Client~ConneXion LiveStats & Chat Gateway script http://www.clientconnexion.com
// ==============================================================================
// This script should be placed in the root directory of your web site. It should
// be included in each page you want to track.
// Place the Include on your page at the place where you want the Click-To-Chat
// graphic to appear.
// ===============================================================================

var sWOGateway       = "204.232.154.240:8080";    // specify the external address:port of the Client~ConneXion Gateway server
var sWOGatewaySSL    = "204.232.154.240:8443"; // specify the external address:port of the Client~ConneXion Gateway server SSL interface
var sWODomain        = "www.sonju.com";  // the domain you are monitoring
var sWODepartment    = ""; // optional department
var sWOChatstart     = "http://204.232.154.240/chat/chatstart.htm"; // url to the chatstart.htm page
var sWOLanguage      = ""; // language for the chat window
var sWOBackgroundURL = ""; // optional custom background url
var sWOResponse      = "Y"; // set to a blank string if you do NOT want a auto greet chat
var sWOInvite        = "Y";  // set to a blank string if you do NOT want invite requests to be polled
var sWOPreselect     = "D"; // set to D to pre-select Department before starting chat, O to pre-select Operator

var sWOSession;
var sWOUrl;
var sWOUser="";
var sWOPage="";
var sWOProtocol=window.location.protocol;

// specify a user name here if you want a specific user name instead of a session cookie
// sWOUser="my user";

// specify a page name here if you want a specific page name shown in Client~ConneXion instead of the window.location
// sWOPage="my page";

var sWOCost=0;    // specify a visit cost
var sWORevenue=0; // specify the revenue generated by the visitor

// optional contact information. Specify values for these variables if you want contact info for the visitor passed to Client~ConneXion
var sWOName="";
var sWOCompany="";
var sWOEmail="";
var sWOTelephone="";

/*
	Begin ClientConneXion Custom Variables all are required to perform properly
 */
	var ClientConneXionColor = "Black";  // Blue or Black
	var ClientConneXionCloseImageBlue = "http://www.clientconnexion.com/images/ClientConnexionCloseImageBlue.gif"; //close button
	var ClientConneXionCloseImageBlack = "http://www.clientconnexion.com/images/ClientConnexionCloseImageBlack.gif"; //close button
	var ClientConneXionSendImageBlue = "http://www.clientconnexion.com/images/ClientConnexionSendImageBlue.gif"; //close button
	var ClientConneXionSendImageBlack = "http://www.clientconnexion.com/images/ClientConnexionSendImageBlack.gif"; //close button
 	var ClientConneXionPhrase = "Please wait. A representative will be with you shortly";
 	var ClientConneXionMessage = "Hello, I saw you on our website. May I assist you with something?"
 	var ClientConneXionBlinks = 3; // how many times operator shows before a message
 	var ClientConneXionShowLink = ""; // set to blank if you do not want a chat link
	var ClientConneXionDelay = 4; // (seconds) set this for a delayed display of the chat window

	var ClientConneXionDivWidth = 200; // what is the width of this object
	var ClientConneXionDivHeight = 300; // what is the height of this object **this includes ClientConneXionActionDivHeight
	var ClientConneXionActionDivHeight = 25; // height of the div for move and close
	//var ClientConneXionActionDivBgImage = "/images/ClientConneXionActionDivBg.jpg"; // background image a move & close
	var ClientConneXionDivTopOffset = 0; // top padding
	var ClientConneXionDivBottomOffset = 5; // botttom padding
	var ClientConneXionDivLeftOffset = 0; // left padding
	var ClientConneXionDivRightOffset = 0; // right padding
	var ClientConneXionDivPlacement = "bottom_right"; // bottom_right (default), bottom_left, top_right, top_left
/*
	End ClientConneXion Custom Variables
 */
 /*
 	browser detection & position issue fix
 */

	var browser=navigator.appName;
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
	 var version=new Number(RegExp.$1) // capture x.x portion and store as a number
	}
	var tmphtmlheight = document.getElementsByTagName("html")[0].style.height;
	var tmphtmlwidth = document.getElementsByTagName("html")[0].style.width;
	var tmpbodyheight = document.getElementsByTagName("body")[0].style.height;
	var tmpbodywidth = document.getElementsByTagName("body")[0].style.width;
	document.getElementsByTagName("html")[0].style.height = "100%";
	document.getElementsByTagName("html")[0].style.width = "100%";
	document.getElementsByTagName("body")[0].style.height = "100%";
	document.getElementsByTagName("body")[0].style.width = "100%";

	var uscr = getUserScreen();

	var windowheight = uscr.height;
	var windowwidth = uscr.width;
	document.getElementsByTagName("html")[0].style.height = tmphtmlheight;
	document.getElementsByTagName("html")[0].style.width = tmphtmlwidth;
	document.getElementsByTagName("body")[0].style.height = tmpbodyheight;
	document.getElementsByTagName("body")[0].style.width = tmpbodywidth;
 /*
 	end browser detection & position issue fix
 */

if(sWOUser==""){
	// create a unique session cookie for the visitor
	var dt=new Date();
	var sWOCookie=document.cookie.toString();
	if(sWOCookie.indexOf("Client~ConneXion")==-1){
		sWOSession=parseInt(Math.random()*1000)+"-"+dt.getTime();
		document.cookie="Client~ConneXion="+sWOSession+";expires=Thu, 31-Dec-2020 00:00:00 GMT;";
	}
	sWOCookie=document.cookie.toString();
	if(sWOCookie.indexOf('Client~ConneXion')==-1){
		sWOSession="";
	} else {
		var s=sWOCookie.indexOf("Client~ConneXion=")+7;
		var e=sWOCookie.indexOf(";",s);
		if(e==-1)e=sWOCookie.length;
		sWOSession=sWOCookie.substring(s,e);
	}
}

if(sWOProtocol=="https:")sWOGateway=sWOGatewaySSL;
if(sWOUser!="")sWOSession=sWOUser;
if(sWOPage=="")sWOPage=escape(window.location);
if(sWOProtocol=="file:")sWOProtocol="http:";

sWOUrl=sWOProtocol+"//"+sWOGateway+"/stat.gif?u="+sWOSession+"&d="+sWODomain;
if(sWODepartment.length>0)sWOUrl+="&t="+escape(sWODepartment);
sWOUrl+="&p='"+sWOPage+"'&r='"+escape(document.referrer)+"'";
if(sWOCost!=0)sWOUrl+="&c="+sWOCost;
if(sWORevenue!=0)sWOUrl+="&v="+sWORevenue;
if(sWOName!="" || sWOCompany!="" || sWOEmail!="" || sWOTelephone!="")sWOUrl+="&n="+sWOName+"|"+sWOCompany+"|"+sWOEmail+"|"+sWOTelephone;
if(sWOResponse==""){
	sWOUrl+="&response=g";sWOChatstart+="?domain="+sWODomain+"&lang="+sWOLanguage;
	if(sWOBackgroundURL!="")sWOChatstart+="&bg="+sWOBackgroundURL;
	if(sWODepartment.length>0)sWOChatstart+="&dept="+escape(sWODepartment);
	if(sWOPreselect.length>0)sWOChatstart+="&select="+sWOPreselect;
	if(ClientConneXionShowLink) {
		document.write("<a style=\"cursor: pointer;\" onclick=\"openClientConneXion();return false;\"><img border=\"0\" src=\""+sWOUrl+"\"><\/a>");
	} else {
		if(document.layers)document.write("<layer name=\"Client~ConneXion\" visibility=hide><img src=\""+sWOUrl+"\" height=1 width=1><\/layer>");
		else document.write("<div id=\"Client~ConneXion\" STYLE=\"position:absolute;visibility:hidden;\"><img src=\""+sWOUrl+"\" height=1 width=1><\/div>");
	}
} else {
	sWOUrl+="&response=g";sWOChatstart+="?domain="+sWODomain+"&lang="+sWOLanguage;
	if(sWOBackgroundURL!="")sWOChatstart+="&bg="+sWOBackgroundURL;
	if(sWODepartment.length>0)sWOChatstart+="&dept="+escape(sWODepartment);
	if(sWOPreselect.length>0)sWOChatstart+="&select="+sWOPreselect;
	if(ClientConneXionShowLink) {
		document.write("<a style=\"cursor: pointer;\" onclick=\"openClientConneXion();return false;\"><img border=\"0\" src=\""+sWOUrl+"\"><\/a>");
	} else {
		if(document.layers)document.write("<layer name=\"Client~ConneXion\" visibility=hide><img src=\""+sWOUrl+"\" height=1 width=1><\/layer>");
		else document.write("<div id=\"Client~ConneXion\" STYLE=\"position:absolute;visibility:hidden;\"><img src=\""+sWOUrl+"\" height=1 width=1><\/div>");
	}
	/* create chat box */
	createClientConneXion();
}
sWOUrl=sWOProtocol+"//"+sWOGateway+"/invite.js?domain="+sWODomain;
if(sWOInvite=="Y")document.write("<sc"+"ript language='Javascript' src='"+sWOUrl+"'><\/scr"+"ipt>");

function openClientConneXion() {
	window.open(sWOChatstart,'new_win','width=484,height=361');
	hideClientConneXionDiv();
	return false;
}

/*
	ClientConneXion HTML
 */
function createClientConneXion() {
	var str = "";

	if(ClientConneXionColor == "Black")
		str += "<div id=\"ClientConneXionDiv\" style=\"display: none; border: solid 1px #000;\">\n";
	else
		str += "<div id=\"ClientConneXionDiv\" style=\"display: none; border: solid 1px #9EB6CE;\">\n";
	str += "<div id=\"ClientConneXionActionDiv\">\n";
	str += "<a id=\"ClientConneXionClose\" href=\"javascript:void(0)\" onclick=\"hideClientConneXionDiv()\">\n";
	if(ClientConneXionColor == "Black")
		str += "<img border=\"0\" src=\"" + ClientConneXionCloseImageBlack + "\" alt=\"close\" />\n";
	else
		str += "<img border=\"0\" src=\"" + ClientConneXionCloseImageBlue + "\" alt=\"close\" />\n";
	str += "</a>\n";
	str += "</div>\n";
	if(ClientConneXionColor == "Black") {
		str += "<div style=\" border: solid 1px #000; width: "+(ClientConneXionDivWidth-20)+"px; height: 170px; margin: 0 4px; padding: 5px; background-color: #FFF; text-align: left;\"><strong>"+ClientConneXionPhrase+"</strong><br /><span id=\"ClientConneXionMessage\" style=\"display: none; font-weight: normal !important;\">Operator...</span></div>\n";
		str += "<div style=\" border: solid 1px #000; width: "+(ClientConneXionDivWidth-10)+"px; height: 50px; margin: 5px 4px; background-color: #FFF;\">\n";
		//str += "<form id=\"ClientConneXionForm\" onsubmit=\"openClientConneXion();return false;\">\n";
		str += "<input id=\"ClientConneXionGreet\" type=\"text\" style=\"border: none; width: 130px; float: left; margin-top: 15px;\" />\n";
		str += "<input onclick=\"openClientConneXion();return false;\" type=\"image\" src=\""+ClientConneXionSendImageBlack+"\" style=\"border: none; float: left; margin-top: 8px;\" />\n";
		//str += "</form>\n";
		str += "</div>\n";
		str += "<span style=\"font-size:.5em; margin-right: 5px; color:#FFF;\" id=\"ClientConneXionPower\">Powered By&nbsp;&nbsp;&nbsp;<br style=\"margin-right: 5px;\" /><a style=\"text-decoration: none; font-size: 1.3em;color:#FFF;\" target=\"_blank\" href=\"http://www.clientconnexion.com\">Client~ConneXion</a></span><br />\n"
	} else {
		str += "<div style=\" border: solid 1px #9EB6CE; width: "+(ClientConneXionDivWidth-10)+"px; height: 190px; margin: 0 4px; padding: 5px; background-color: #FFF; text-align: left;\"><strong>"+ClientConneXionPhrase+"</strong><br /><span id=\"ClientConneXionMessage\" style=\"display: none; font-weight: normal !important;\"\">Operator...</span></div>\n";
		str += "<div style=\" border: solid 1px #9EB6CE; width: "+(ClientConneXionDivWidth-10)+"px; height: 50px; margin: 5px 4px; background-color: #FFF;\">\n";
		//str += "<form id=\"ClientConneXionForm\" onsubmit=\"openClientConneXion();return false;\">\n";
		str += "<input id=\"ClientConneXionGreet\" type=\"text\" style=\"border: none; width: 130px; float: left; margin-top: 15px;\" />\n";
		str += "<input onclick=\"openClientConneXion();return false;\" type=\"image\" src=\""+ClientConneXionSendImageBlue+"\" style=\"border: none; float: left; margin-top: 8px;\" />\n";
		//str += "</form></div>\n";
		str += "</div>\n";
		str += "<span style=\"font-size:.5em; text-align:right; margin-right: 5px;color:#FFFFFF;\" id=\"ClientConneXionPower\">Powered By&nbsp;&nbsp;&nbsp;<br /><a style=\"text-decoration: none; font-size: 1.3em;color:#FFFFFF;\" target=\"_blank\" href=\"http://www.clientconnexion.com\">Client~ConneXion</a></span><br />\n"
	}
	str += "</div>\n";
	document.write(str);

	/* style chat box */
	styleClientConneXion();
}
/*
	ClientConneXion Styles
 */
function styleClientConneXion() {
	var ClientConneXionDiv = idCCX('ClientConneXionDiv');
      ClientConneXionDiv.style.color = "#000";


    var setPos = function()
    {

    	var uscr = getUserScreen();

	    var windowheight = uscr.height;
	    var windowwidth = uscr.width;

    	switch(ClientConneXionDivPlacement) {
    		case "bottom_left":
    			ClientConneXionDiv.style.top = (windowheight - ClientConneXionDivHeight - ClientConneXionDivBottomOffset) + uscr.top + "px";
    			ClientConneXionDiv.style.left = ClientConneXionDivLeftOffset + "px";
    			break;
    		case "top_right":
    			ClientConneXionDiv.style.top = ClientConneXionDivTopOffset + uscr.top + "px";
    			ClientConneXionDiv.style.left = (windowwidth - ClientConneXionDivWidth - 18 - ClientConneXionDivRightOffset) + "px";
    			break;
    		case "top_left":
    			ClientConneXionDiv.style.top = ClientConneXionDivTopOffset + uscr.top + "px";
    			ClientConneXionDiv.style.left = ClientConneXionDivLeftOffset + "px";
    			break;
    		default:
    			ClientConneXionDiv.style.top = (windowheight - ClientConneXionDivHeight - ClientConneXionDivBottomOffset) + uscr.top + "px";
    			ClientConneXionDiv.style.left = (windowwidth - ClientConneXionDivWidth - 18 - ClientConneXionDivRightOffset) + "px";
    			break;
    	}
    };

    setPos();

	if((browser == "Microsoft Internet Explorer" && version < 7)
	    || (browser == "Microsoft Internet Explorer" && document.compatMode == 'BackCompat')) {

		ClientConneXionDiv.style.position = "absolute";

        window.attachEvent('onscroll', setPos);
        window.attachEvent('onresize', setPos);

	} else {
		ClientConneXionDiv.style.position = "fixed";
	}
	ClientConneXionDiv.style.zIndex = "50000";
	if(ClientConneXionColor == "Black")
		ClientConneXionDiv.style.backgroundColor = "#545454";
	else
		ClientConneXionDiv.style.backgroundColor = "#D0DEF0";
	ClientConneXionDiv.style.width = ClientConneXionDivWidth + "px";
	ClientConneXionDiv.style.height = ClientConneXionDivHeight + "px";
	//ClientConneXionDiv.style.display = "block";

	var ClientConneXionActionDiv = idCCX('ClientConneXionActionDiv');
	ClientConneXionActionDiv.style.width= ClientConneXionDivWidth + "px";
	ClientConneXionActionDiv.style.height= ClientConneXionActionDivHeight + "px";
	//ClientConneXionActionDiv.style.background = "#FFF url(" + ClientConneXionActionDivBgImage + ") repeat-x";
	if(ClientConneXionColor == "Black")
		ClientConneXionActionDiv.style.background = "#545454";
	else
		ClientConneXionActionDiv.style.background = "#D0DEF0";
	ClientConneXionActionDiv.style.cursor = "move";

	var ClientConneXionClose = idCCX('ClientConneXionClose');
	if(browser == "Microsoft Internet Explorer") {
		ClientConneXionClose.style.styleFloat = "right";
	} else {
		ClientConneXionClose.style.cssFloat = "right";
	}
	ClientConneXionClose.style.margin = "3px 5px";
	ClientConneXionClose.style.border = "0";
	ClientConneXionClose.style.cursor = "pointer";


	//var ClientConneXionIframe = idCCX('ClientConneXionIframe');
	//ClientConneXionIframe.style.width = ClientConneXionDivWidth + "px";
	//ClientConneXionIframe.style.height = (ClientConneXionDivHeight - ClientConneXionActionDivHeight) + "px";
	//ClientConneXionIframe.style.visibility = "visible";
}
/*
	Sets embed tags for flash so html can be seen over flash
*/
function fixEmbeddedObjects() {
	var allembed = document.getElementsByTagName("embed");
	for(var i = 0; i < allembed.length; i++) {
		if(allembed[i].wmode == undefined) {
			allembed[i].setAttribute("wmode", "transparent");
		}
	}
}
/*
	hides ClientConneXionDiv
*/
function hideClientConneXionDiv() {
	if(idCCX('ClientConneXionDiv')) {
		idCCX('ClientConneXionDiv').style.display = 'none';
		var ClientConneXionGreet = idCCX('ClientConneXionGreet');
		ClientConneXionGreet.value = "";
		//idCCX('ClientConneXionIframe').src = "";
	}
}
/*
	shows ClientConneXionDiv
*/
function showClientConneXionDiv() {
	idCCX('ClientConneXionDiv').style.display = 'block';
	var ClientConneXionGreet = idCCX('ClientConneXionGreet');
	ClientConneXionGreet.focus();
	setTimeout('ClientConneXionOperatorShow(0)',1000);
}
function ClientConneXionOperatorShow(cnt) {
	if(cnt < ClientConneXionBlinks) {
		idCCX('ClientConneXionMessage').style.display = 'block';
		setTimeout('ClientConneXionOperatorHide('+cnt+')',750);
	} else {
		idCCX('ClientConneXionMessage').innerHTML = ClientConneXionMessage;
		idCCX('ClientConneXionMessage').style.display = 'block';
	}

}
function ClientConneXionOperatorHide(cnt) {
	idCCX('ClientConneXionMessage').style.display = 'none';
	setTimeout('ClientConneXionOperatorShow('+(cnt+1)+')',750);
}
/*
	drag and drop ClientConneXionDiv
*/
function startDragClientConneXionDiv(ClientConneXionEvent) {
	if(!ClientConneXionEvent) {
		var ClientConneXionEvent = window.event;
	}
	var ClientConneXionTarget = ClientConneXionEvent.target ? ClientConneXionEvent.target : ClientConneXionEvent.srcElement;
	if(ClientConneXionTarget.id != 'ClientConneXionActionDiv'){
		return;
	}
	ClientConneXionTarget = idCCX("ClientConneXionDiv");
	ClientConneXionTarget.style.left = !ClientConneXionTarget.style.left ? "0px" : ClientConneXionTarget.style.left;
	ClientConneXionTarget.style.top = !ClientConneXionTarget.style.top ? "0px" : ClientConneXionTarget.style.top;

	offsetX = ClientConneXionEvent.clientX;
	offsetY = ClientConneXionEvent.clientY;
	coordX = parseInt(ClientConneXionTarget.style.left);
	coordY = parseInt(ClientConneXionTarget.style.top);
	draging = true;

	document.onmousemove = dragClientConneXionDiv;
}
function dragClientConneXionDiv(ClientConneXionEvent) {
	if(!ClientConneXionEvent) {
		var ClientConneXionEvent = window.event;
	}
	if(!draging) {
		return;
	}
	var ClientConneXionTarget = idCCX("ClientConneXionDiv");
	ClientConneXionTarget.style.left = (coordX + ClientConneXionEvent.clientX - offsetX) + 'px';
	ClientConneXionTarget.style.top = (coordY + ClientConneXionEvent.clientY - offsetY) + 'px';
	return false;
}
function stopDragClientConneXionDiv() {
	draging = false;
}
/*
	generic getElementById function
*/
function idCCX(idname) {
	return document.getElementById(idname);
}
/*
	set drag & drop; fix flash
*/
window.onload = function() {
	if(sWOResponse != "") {
		document.onmousedown = startDragClientConneXionDiv;
		document.onmouseup = stopDragClientConneXionDiv;
		setTimeout("fixEmbeddedObjects()", 2000);
		setTimeout("showClientConneXionDiv()", (parseInt(ClientConneXionDelay)*1000));
	}
}

function getUserScreen()
{
	var scrOfX = 0, scrOfY = 0;
	var w = window;

	if(!w.document || !w.document.documentElement)
	{
		return {left: 0, top: 0, width: 0, height: 0};
	}


	if( typeof( w.pageYOffset ) == 'number' )
	{
		//Netscape compliant
		scrOfY = w.pageYOffset;
		scrOfX = w.pageXOffset;
	} else if( w.document.body && ( w.document.body.scrollLeft || w.document.body.scrollTop ) )
	{
		//DOM compliant
		scrOfY = w.document.body.scrollTop;
		scrOfX = w.document.body.scrollLeft;
	} else if( w.document.documentElement && ( w.document.documentElement.scrollLeft || w.document.documentElement.scrollTop ) )
	{
		//IE6 standards compliant mode
		scrOfY = w.document.documentElement.scrollTop;
		scrOfX = w.document.documentElement.scrollLeft;
	}


	var width = (w.innerWidth)?w.innerWidth: w.document.documentElement.clientWidth;
	var height = (w.innerHeight)?w.innerHeight: w.document.documentElement.clientHeight;

	if (document.compatMode == 'BackCompat')
	{
	    width = w.document.body.clientWidth;
	    height = w.document.body.clientHeight;
	}

	return {left: scrOfX, top: scrOfY, 'width': width, 'height': height};

}
