var SCRIPT_VERSION = "1.1";

if (typeof(lpNumber) == "undefined")
    lpNumber = '65645376';

var LPLOCALVIDCOOKIE = lpNumber + '-VID';
var LPLOCALSKEYCOOKIE = lpNumber + '-SKEY';

var hcUpdateState = 0;

var pageLocation = document.location.toString();

var lpfcLocation = document.location.toString();
var lpfcProtocol = "http";
if ((lpfcLocation != null) && (lpfcLocation.indexOf("https:") == 0))
	lpfcProtocol = "https";

var hcBase = lpfcProtocol + '://server.iad.liveperson.net/hc/'+lpNumber+'/';
var hcImageURL = lpFixProtocol("http://server.iad.liveperson.net/hcp/Gallery/ChatButton-Gallery/English/General/1a/");
if (typeof(lpCustomImageURL) != "undefined")
    hcImageURL = lpFixProtocol(lpCustomImageURL);
var hcStaticImageURL = hcImageURL;

var hcControlImage;
var hcIsImage = false;
var hcCounter = 0;
var hcCmd = "knockPage";
var scriptType = "SERVERBASED";
var hcTimeout = 5;
var hcSendCounter = 0;
var	visitorStatus = "INSITE_STATUS";

if (typeof(hcPageID) == "undefined")
    hcPageID = null;

var hcLeft = -1;
var hcTop = -1;

var lpfcProtocol = "http";
if ((lpfcLocation != null) && (lpfcLocation.indexOf("https:") == 0))
	lpfcProtocol = "https";

var hcNS = (document.layers) ? true : false;
var hcIE = (document.all) ? true : false;
var hcDOM = (document.getElementById) ? true : false;
var hcSafari = (navigator.userAgent) && (navigator.userAgent.toUpperCase().indexOf("SAFARI") >= 0);
if (hcIE)
	hcDOM = false;
var hcMAC = (navigator.platform) && (navigator.platform.toUpperCase().indexOf("MAC") >= 0);
if (hcNS)
	hcMAC = false;

var hcLoadingImage = false;
var lpJavaSupport = isJavaEnabled();

if (hcDOM)
{
	hcControlImage = document.createElement('IMG');
	hcControlImage.style.visibility = "hidden";
	document.body.appendChild(hcControlImage);
} else
if (hcMAC)
{
	document.writeln("<div style='visibility:hidden'><img src='" + lpfcProtocol + "://server.iad.liveperson.net/hcp/window/common/spacer.gif' id='hcControlImage' name='hcControlImage'></div>");
	hcControlImage = document.hcControlImage;
}

function isJavaEnabled()
{
	var rc = false;
	var agent = navigator.appName;
	ver= parseInt(navigator.appVersion);
	if (agent == "Microsoft Internet Explorer") {
		if ((ver >= 4) && navigator.javaEnabled())
			rc = true;
	} else {
        for (i=0; i < navigator.plugins.length; i++)
            rc = rc || (navigator.plugins[i].name.toUpperCase().indexOf("JAVA")	!= -1);
	}

	return rc;
}

function hcGetObj(id)
{
	if (document.getElementById)
		return document.getElementById(id);
	else
	if (document.all)
		return document.all(id);
}

function hcDate()
{
	var d = new Date();
	return d.getTime();
}


function lpFixProtocol(str) {
    if ((str != null) && (str.indexOf("http:") == 0) && (lpfcProtocol == "https"))
        return lpfcProtocol + str.substring(4);
	return str;
}

function hcCheckImages()
{
	if (hcIsImage) {
		if (((hcDOM) || (hcMAC)) && (! hcControlImage.complete) && (! hcSafari))
		{
			hcLoadingImage = true;
			return;
		}
		var w = hcControlImage.width;
		var h = hcControlImage.height;
		hcLoadingImage = false;

		return;
	}
}

function lpAppendVisitorCookies(url) {
    var index = url.indexOf("?");
    if (index == -1) {
				var needsAmp = url.indexOf("&") != 0;
        url = '&visitor=' + lpParseLocalVisitorID() + '&msessionkey=' + lpParseLocalSessionKey() + ( needsAmp ? "&" : "") + url;
    } else {
        var firstChunk = url.substring(0, index + 1);
        var secondChunk = url.substring(index + 1);
				var needsAmp = secondChunk.indexOf("&") != 0;
        url = firstChunk + 'visitor=' + lpParseLocalVisitorID() + '&msessionkey=' + lpParseLocalSessionKey() + ( needsAmp ? "&" : "") + secondChunk;
    }
    return url;
}

function lpFindCookie(key) {
    var s = document.cookie;
    var allCookies = s.split("; ");
    for (i = 0; i < allCookies.length; i++) {
        var keyval = allCookies[i].split("=");
        if (keyval != null && keyval.length == 2 && key == keyval[0])
            return keyval[1];
    }
    return "";
}

function lpParseLocalVisitorID() {
    return lpFindCookie(LPLOCALVIDCOOKIE);
}
function lpParseLocalSessionKey() {
    return lpFindCookie(LPLOCALSKEYCOOKIE);
}

function hcSendRequest()
{
	hcSendCounter++;
	if (hcSendCounter == 5)
		hcIsImage = false;

	if (! hcIsImage) {
		if (hcDOM) {
			document.body.removeChild(hcControlImage);
			hcControlImage = document.createElement('IMG');
			hcControlImage.style.visibility = "hidden";
			hcControlImage.style.position = "absolute";
			hcControlImage.style.top = "0px";
			hcControlImage.style.left = "-5000px";
			document.body.appendChild(hcControlImage);
		} else
		if (! hcMAC)
			hcControlImage= new Image;
		
		if (hcPageID==null) hcPageID=Math.round(Math.random()*9999999999);
		var windowname = window.name;
		if (windowname.length > 256)
			windowname = windowname.substring(0, 255);
		var u = hcBase + '?site=' + lpNumber +
					'&cmd=' + hcCmd +
					'&page=' + escape(pageLocation) +
					'&visitorStatus=' + escape(visitorStatus) +
					'&activePlugin=none' + 
					'&pageWindowName=' + escape(windowname) +
					'&javaSupport=' + (lpJavaSupport ? "true" : "false") +
					'&id=' + hcPageID +
					'&scriptVersion=' + SCRIPT_VERSION +
                    '&d=' + hcDate();
        u = lpAppendVisitorCookies(u);
        if (hcCmd == "knockPage")
			hcCmd = "startPage";
        else {
			if ((typeof(tagVars) != "undefined") && (tagVars != "")) {
				u = u + "&" + tagVars;
			}
			if ((typeof(customCommands) != "undefined") && (customCommands != "")) {
				u = u + "&" + customCommands;
				customCommands = "";
			}
			if ((typeof(customStaticCommands) != "undefined") && (customStaticCommands != "")) {
				u = u + "&" + customStaticCommands;
			}
			var activateCobrowse = false;
			if ( false ) {
				u = u + "&cobrowse=true";
				activateCobrowse = true;
			}

	        if ( (typeof(lpOperatorViewable) != "undefined") && (lpOperatorViewable == "true") ) {
				if (! activateCobrowse) {
					u = u + "&cobrowse=true";
					activateCobrowse = true;
				}
	            if (typeof(lpOperatorPageType) != "undefined") {
	                u = u  + "&cobrowsetitle=" + escape(lpOperatorPageType);
	            }
	            if (typeof(lpOperatorPageUrl) != "undefined") {
	                u = u  + "&cobrowseurl=" + escape(lpOperatorPageUrl);
	            }
	        }
			if (scriptType != null) {
				u = u + "&scriptType=" + scriptType;
				scriptType = null;
			}
			hcCmd = "inPage";
			if (activateCobrowse) {
				var cookies = document.cookie;
				if ((typeof(cookies) == "undefined") || (cookies == null))
					cookies = "";
				if (u.length <= 1990)
					while (escape(cookies).length + u.length > 1990) {
						var idx = cookies.lastIndexOf(";");
						if (idx >= 0)
							cookies = cookies.substring(0, idx);
						else
							cookies = "";
					}
				u = u + "&cookie=" + escape(cookies);
			}
		}
		if(typeof(document.title)!="undefined" && document.title.length>0){
    		u = u + '&title=' + escape(document.title);
	    	if(u.length>1990) u = u.substring(0,1990);
		}
		u = u + '&referrer=';
		var ref = escape(document.referrer);
		if (u.length + ref.length < 1990)
			u = u + ref;
		hcControlImage.src = u;
		hcIsImage = true;
		hcSendCounter = 0;
	}
}

function hcloop()
{
	if (hcTimeout < 0)
		return;

	if (hcCounter == 0)
	{
		if (! hcLoadingImage)
		{
			hcSendRequest();
			hcLoadTimer = 0;
		}
		else
		{
			hcLoadTimer++;
			if (hcLoadTimer == 5)
			{
				hcIsImage = false;
				hcSendRequest();
				hcLoadTimer = 0;
			}
		}
	}
	hcCounter = (hcCounter + 1) % hcTimeout;
	hcCheckImages();

	setTimeout('hcloop()', 1000);
}

function hcgo()
{
	setTimeout('hcloop()', 1);
}

hcgo();
