var hcUpdateState = 0;
var hcControlImage;
var hcIsImage = false;
var hcCounter = 0;
var hcCmd = "knockPage";
var hcTimeout = 5;
var hcSendCounter = 0;

var hcPageID = null;

var hcLeft = -1;
var hcTop = -1;

var hcNS = (document.layers) ? true : false;
var hcIE = (document.all) ? true : false;
var hcDOM = (document.getElementById) ? true : false;
if (hcIE)
	hcDOM = false;
var hcMAC = (navigator.platform) && (navigator.platform.toUpperCase().indexOf("MAC") >= 0);
if (hcNS)
	hcMAC = false;
var hcShowImage = false;

var hcPos = - 30;
var HumanStep = 3;
var hcDir = HumanStep;
var hcBorder = 100;
var hcAnimate = false;

var hcNeedImage;
var hcCloseImage;
var hcImageFetched = false;

var hcimage = hcGetImage("hcIcon");
if (hcimage)
	hcicon = hcimage.src;

var hcParam = null;
var hcOpenVars = null;

var loadingImage = false;

var layerWidth = 400;
var layerHeight = 300;

var clickurl;

var HCinit = true;

var lpForcePopup = ("{forcePopup}" == "true");

if ("{newChannel}" == "true") {
	var lpPosY = 100;
	var lpPosX = 100;
	var lpOperatorViewable= 'true';
	var lpOperatorPageType= 'CoBrowse';
}

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='blank' id='controlImage' name='controlImage'></div>");
	hcControlImage = document.controlImage;
}


function getE(id)
{
	if (document.getElementById)
		return document.getElementById(id);
}

function objshow(obj)
{
	if (hcNS)
		obj.visibility="show";
	else
		obj.style.visibility="visible";
}

function hcDate()
{
	var d = new Date();

	return d.getTime();
}

function hcSendRequest()
{
	hcSendCounter++;
	if (hcSendCounter == 5)
		hcIsImage = false;

	if (! hcIsImage) {
		if ((! hcDOM) && (! hcMAC))
			hcControlImage= new Image;
		if (hcPageID==null) hcPageID=Math.round(Math.random()*9999999999);
		var u = 'http://server.iad.liveperson.net/hc/5296924/?site=5296924' +
					'&cmd=' + hcCmd +
					'&page=' + escape(document.location) +
					'&title=' + escape(document.title) +
					'&referrer=' + escape(document.referrer) +
					'&id=' + hcPageID +
					'&d=' + hcDate();
		if ((typeof(tagVars) != "undefined") && (tagVars != "") && (hcCmd!="knockPage")) {
			u = u + "&" + tagVars;
			tagVars = "";
		}
		if ( (typeof(lpOperatorViewable) != "undefined") && (lpOperatorViewable == "true") ) {
			u = u + "&cobrowse=true&cookie=" + escape(document.cookie);
            if (typeof(lpOperatorPageType) != "undefined") {
                u = u  + "&cobrowsetitle=" + escape(lpOperatorPageType);
            }
            if (typeof(lpOperatorPageUrl) != "undefined") {
                u = u  + "&cobrowseurl=" + escape(lpOperatorPageUrl);
            }
		}
                if (hcCmd == "knockPage")
                	hcCmd = "startPage";
                else
			hcCmd = "inPage";
		hcControlImage.src = u;
		hcIsImage = true;
		hcSendCounter = 0;
	}
}

function putIE()
{
}
function putNS()
{
}
function handlePopup(w, h)
{
}


//   START RULES CODE

function placeLayers(content, closeimg)
{
	if (hcIE) {
		xP = (document.body.clientWidth - layerWidth) / 2;
		tP = (document.body.clientHeight - layerHeight) / 2;
	} else if ((hcNS) || (hcDOM)) {
		xP = (window.innerWidth - layerWidth) / 2;
		tP = (window.innerHeight - layerHeight) / 2;
	}
	if ((hcIE) || (hcDOM)) {
		ctobj = content.style;
		clobj = closeimg.style;
	} else {
		ctobj = content;
		clobj = closeimg;
	}
	ctobj.left = xP;
	ctobj.top = tP;
	clobj.left = xP;
	clobj.top = parseInt(ctobj.top) + layerHeight;
}


function waitforload()
{
	if (HCinit)
		return;
	if (hcNS)
	{
		layerWidth = document.hcpopupgif.document.images.hcpopupimage.width;
		layerHeight = document.hcpopupgif.document.images.hcpopupimage.height;
		placeLayers(document.hcpopupgif, document.hcpopupclose)
		objshow(document.hcpopupgif);
		objshow(document.hcpopupclose);
	} else if ((hcIE) || (hcDOM))
	{
		layerWidth = getE("hcpopupimage").width;
		layerHeight = getE("hcpopupimage").height;
		placeLayers(getE("hcpopupgif"), getE("hcpopupclose"))
		objshow(getE("hcpopupgif"));
		objshow(getE("hcpopupclose"));
	}
}

function waitforhtmlload()
{
	if (hcNS)
	{
		placeLayers(document.hcpopuplayer, document.hcpopupclose)
		objshow(document.hcpopuplayer);
		objshow(document.hcpopupclose);
	} else if ((hcIE) || (hcDOM))
	{
		placeLayers(getE("hcpopuplayer"), getE("hcpopupclose"))
		objshow(getE("hcpopuplayer"));
		objshow(getE("hcpopupclose"));
	}
}

function checkwidth()
{

	if (getE("hcpopupimage").width == 1)
		setTimeout('checkwidth()', 1000);
	else
		waitforload();
}

function closePopup()
{
	if (hcNS)
	{
		document.hcpopuplayer.visibility="hide";
		document.hcpopupgif.visibility="hide";
		document.hcpopupclose.visibility="hide";
	} else if (hcIE || hcDOM)
	{
		getE("hcpopuplayer").style.visibility ="hidden";
		getE("hcpopupgif").style.visibility ="hidden";
		getE("hcpopupclose").style.visibility ="hidden";
	}
}

function putIE()
{
	put('<iframe name="hcpopuplayer" id="hcpopuplayer"  width="300" height="100" src="http://server.iad.liveperson.net/hcp/html/blankhtml.html" scrolling="no" frameborder="0" style="z-index:88;position:absolute;visibility:hidden;left:10;top:10"></iframe>');
	put('<div name="hcpopupgif" id="hcpopupgif" style="z-index:89;position:absolute;cursor=hand; visibility:hidden;left:0;top:10"><a href="#" onClick="goto_redirect(); return false;"><img name="hcpopupimage" id="hcpopupimage" onload="waitforload()" src="http://server.iad.liveperson.net/hcp/width/img1.gif" border="0"></a></div>');
	put('<div name="hcpopupclose" id="hcpopupclose"  onClick="closePopup();" style="z-index:89;position:absolute;cursor=hand; visibility:hidden;left:0;top:10"><a href="#" onClick="closePopup(); return false;"><img name="hcpopupcloseimage" id="hcpopupcloseimage" src="http://server.iad.liveperson.net/hcp/width/img1.gif" border="0"></a></div>');
}

function putNS()
{
	put('<layer name="hcpopuplayer" pagex="10" pagey="10" z-index="88" width="300" height="100" clip="0,0,1,1" visibility="hide" src="http://server.iad.liveperson.net/hcp/html/blankhtml.html"></layer>');
	put('<layer name="hcpopupgif" pagex="0" pagey="10" z-index="89" visibility="hide"><a href="#" onClick="goto_redirect(); return false;"><img name="hcpopupimage" id="hcpopupimage" src="http://server.iad.liveperson.net/hcp/width/img1.gif"  border="0"></a></layer>');
	put('<layer name="hcpopupclose" pagex="0" pagey="10" z-index="89" visibility="hide"><a href="#" onClick="closePopup(); return false;"><img name="hcpopupcloseimage" id="hcpopupcloseimage" src="http://server.iad.liveperson.net/hcp/width/img1.gif" border="0"></a></layer>');
}
function goto_redirect()
{
	window.location = clickurl;
}

function handlePopup(w, h)
{
	{
		HCinit = false;
        var modLength=1000;
        var wLength=w%modLength;
        var hLength=h%modLength;
        var wCode=(w-wLength)/modLength;
        var hCode=(h-hLength)/modLength;
        if (wCode == 1) {
            var gifurl='http://server.iad.liveperson.net/hc/5296924/?cmd=ruleRedirect&site=5296924&page=' + escape(document.location) +'&id=' + hcPageID +'&rulecmd=ShowGif&d=' + hcDate();
            var closeurl='http://server.iad.liveperson.net/hc/5296924/?cmd=ruleRedirect&site=5296924&page=' + escape(document.location) +'&id=' + hcPageID +'&rulecmd=ShowGifClose&d=' + hcDate();
            clickurl='http://server.iad.liveperson.net/hc/5296924/?cmd=ruleRedirect&site=5296924&page=' + escape(document.location) +'&id=' + hcPageID +'&rulecmd=ClickThrough&d=' + hcDate();
            if (hcNS)
            {
				document.hcpopupclose.document.open();
				document.hcpopupclose.document.writeln('<a href="" onClick="closePopup(); return false;"><img name="hcpopupcloseimage" id="hcpopupcloseimage" src="' + closeurl + '" border="0"></a>');
				document.hcpopupclose.document.close();
				document.hcpopupgif.document.open();
				document.hcpopupgif.document.writeln('<a href="" onClick="goto_redirect(); return false;"><img name="hcpopupimage" id="hcpopupimage" src="' + gifurl + '"  border="0" onload="waitforload()"></a>');
				document.hcpopupgif.document.close();
            } else if ((hcIE) || (hcDOM))
            {
				if (hcDOM)
					checkwidth();
				getE("hcpopupimage").src = gifurl;
				getE("hcpopupcloseimage").src = closeurl;
			}

        } else if (wCode == 2) {
	    layerWidth = wLength;
	    layerHeight = hLength;
		var xP = 0;
		var tP = 0;

		if (hcIE) {
			xP = (document.body.clientWidth - layerWidth) / 2;
			tP = (document.body.clientHeight - layerHeight) / 2;
		} else if (hcNS) {
			xP = (window.innerWidth - layerWidth) / 2;
			tP = (window.innerHeight - layerHeight) / 2;
		} else if (hcDOM) {
			xP = (window.innerWidth - layerWidth) / 2;
			tP = (window.innerHeight - layerHeight) / 2;
		}

            var winurl='http://server.iad.liveperson.net/hc/5296924/?cmd=ruleRedirect&site=5296924&page=' + escape(document.location) +'&id=' + hcPageID +'&rulecmd=ShowWindow&d=' + hcDate();
            var popupWin=window.open(winurl, 'ruleWindow_5296924', 'width='+wLength+',height='+hLength+',top='+tP+',left='+xP+',menubar=no,scrollbars=2');
            popupWin.focus();
        } else if (wCode==3) {
	    layerWidth = wLength;
	    layerHeight = hLength;
            var winurl='http://server.iad.liveperson.net/hc/5296924/?cmd=ruleRedirect&site=5296924&page=' + escape(document.location) +'&id=' + hcPageID +'&rulecmd=ShowLayeredHtml&d=' + hcDate();
            if (hcNS)
            {
	            var closeurl='http://server.iad.liveperson.net/hc/5296924/?cmd=ruleRedirect&site=5296924&page=' + escape(document.location) +'&id=' + hcPageID +'&rulecmd=ShowHtmlClose&d=' + hcDate();
                document.hcpopuplayer.width = layerWidth;
                document.hcpopuplayer.height = layerHeight;
				document.hcpopuplayer.clip.right = layerWidth;
				document.hcpopuplayer.clip.bottom = layerHeight;
				document.hcpopupclose.document.open();
				document.hcpopupclose.document.writeln('<a href="" onClick="closePopup(); return false;"><img name="hcpopupcloseimage" id="hcpopupcloseimage" src="' + closeurl + '"  border="0"></a>');
				document.hcpopupclose.document.close();
				document.hcpopuplayer.onload = waitforhtmlload;
                document.hcpopuplayer.src = winurl;
            } else if (hcIE)
            {
                document.hcpopuplayer.location = winurl;
	            var closeurl='http://server.iad.liveperson.net/hc/5296924/?cmd=ruleRedirect&site=5296924&page=' + escape(document.location) +'&id=' + hcPageID +'&rulecmd=ShowHtmlClose&d=' + hcDate();
                document.hcpopupcloseimage.src = closeurl;
                getE("hcpopuplayer").style.width = layerWidth;
                getE("hcpopuplayer").style.height = layerHeight;
				placeLayers(getE("hcpopuplayer") ,getE("hcpopupclose"));
                objshow(getE("hcpopuplayer"));
                objshow(getE("hcpopupclose"));
            } else if (hcDOM)
            {
                getE("hcpopuplayer").src = winurl;
	            var closeurl='http://server.iad.liveperson.net/hc/5296924/?cmd=ruleRedirect&site=5296924&page=' + escape(document.location) +'&id=' + hcPageID +'&rulecmd=ShowHtmlClose&d=' + hcDate();
                getE("hcpopupcloseimage").src = closeurl;
				placeLayers(getE("hcpopuplayer") ,getE("hcpopupclose"));
                getE("hcpopuplayer").style.width = layerWidth;
                getE("hcpopuplayer").style.height = layerHeight;
                objshow(getE("hcpopuplayer"));
                objshow(getE("hcpopupclose"));
            }
        }
        if (hCode==1) hcTimeOut=1;
    }
}

//   END RULES CODE

function hcHandleWidthHeight(w,h)
{
    if (h==1) {
        var wCode = w-w%2;
        var more = w%2;
        if (wCode == 30) {
		hcTimeout = -1;
        } else if (wCode == 40) {
		hcTimeout = 15;
        } else if (wCode == 50) {
		hcTimeout = 30;
        } else if (wCode == 60) {
            openChat(null, null);
        } else if (wCode == 70) {
            openEngageChat("engage", null);
        } else if (wCode == 80) {
			hcReloadIcon();
		} else if (wCode == 90) {
            		hcTimeout = 1;
		}
        if (more==1) hcTimeOut=1;
    } else handlePopup(w, h);
}

function hcCheckImages()
{
	if (hcIsImage) {
		if (((hcDOM) || (hcMAC)) && (! hcControlImage.complete))
		{
			loadingImage = true;
			return;
		}
		var w = hcControlImage.width;
		var h = hcControlImage.height;
		loadingImage = false;                                                                      

		if (w == 0)
			return;
		hcIsImage = false;
		hcHandleWidthHeight(w,h);
	}
}

var loadTimer = 0;

function hcloop()
{
	if (hcTimeout < 0)
		return;

	if (hcCounter == 0)
	{
		if (! loadingImage)
		{
			hcSendRequest();
			loadTimer = 0;
		}
		else
		{
			loadTimer++;
			if (loadTimer == 5)
			{
				hcIsImage = false;
		hcSendRequest();
				loadTimer = 0;
			}
		}
	}
	hcCounter = (hcCounter + 1) % hcTimeout;
	hcCheckImages();

	setTimeout('hcloop()', 1000);
}

function hcReloadIcon()
{
	if (hcimage)
		hcimage.src = hcicon + "&monitor=1&d=" + hcDate();
}

function openChat(param, openVars)
{
	var s = document.location;
	if (param != null)
		s = "(" + param + ") " + s;
	s = escape(s);

    var oparms = "";
    if (openVars != null)
        oparms = oparms + "&" + openVars;

    var url = 'http://server.iad.liveperson.net/hc/5296924/?cmd=file&file=visitorWantsToChat' + oparms + '&site=5296924&d=' + hcDate()+'&referrer='+s;
    var name = 'chat5296924';
    var params = 'width=472,height=320,menubar=no,scrollbars=0';

    if (typeof(lpOpenChat) == "function") {
        lpOpenChat(url, name, params);
    } else {
        window.open(url, name, params);
    }
}

function openCredit()
{
	document.location = "http://www.liveperson.com/ref/lppb.asp";
}

function openEngageChat(param, openVars) {
	hcParam = param;
	hcOpenVars = openVars;
	if ((! lpForcePopup) && (hcIE || hcNS || hcDOM)) {
		hcShowTheImage();
	} else {
		window.open('http://server.iad.liveperson.net/hc/5296924/?cmd=file&file=wantsToChat&site=5296924&d=' + hcDate(), 'wanttochat5296924', 'width=273,height=138,menubar=no,scrollbars=no');
	}
}

function openWantsToChat()
{
	openEngageChat(null, null);
}

function hcPreload()
{
	hcNeedImage = new Image();
	hcNeedImage.src = "http://www.liveperson.com/images/icons/set7/need_help_on.gif";

	hcCloseImage = new Image();
	hcCloseImage.src = "http://www.liveperson.com/images/icons/set7/close_on.gif";
}

function hcSetImageGo(name, image, go)
{
	hcAnimate = go;
	hcSetImage(name, image);
}

function hcSetImage(name, image)
{
	hcGetImage(name).src = "http://www.liveperson.com/images/icons/set7/" + image;
}


function put(str)
{
	document.writeln(str);
}

if (hcIE || hcDOM) {
	put('<div id="mylayer" style="z-index:90;position:absolute;visibility:hidden;left:10;top:10">');
	put('<table border="0" cellspacing="0" cellpadding="0">');
	put('<tr><td><a name="needRef" href="#" onClick="return hcAcceptCall()" target="_self" onmouseover=hcSetImageGo("need_help","need_help_on.gif",false) onmouseout=hcSetImageGo("need_help","need_help_off.gif",true)><img name="need_help" onload="hcFloatIconLoaded()" border="0" width=250 height=100></a></td></tr>');
	put('<tr><td><a href="#" onClick="return hcRejectCall()" target="_self" onmouseover=hcSetImageGo("need_close","close_on.gif",false) onmouseout=hcSetImageGo("need_close","close_off.gif",true)><img name="need_close" border="0" width=250 height=18></a></td></tr>');
	put('</table></div>');
	putIE();
} else if (hcNS) {
	put('<layer name="mylayer" z-index="90" left="10" top="10" visibility="hidden">');
	put('<table border="0" cellspacing="0" cellpadding="0">');
	put('<tr><td><a href="#" onClick="return hcAcceptCall()" target="_self" onmouseover=hcSetImageGo("need_help","need_help_on.gif",false) onmouseout=hcSetImageGo("need_help","need_help_off.gif",true)><img name="need_help" src="http://www.liveperson.com/images/icons/set7/need_help_off.gif" onload="hcFloatIconLoaded()" border="0" width=250 height=100></a></td></tr>');
	put('<tr><td><a href="#" onClick="return hcRejectCall()" target="_self" onmouseover=hcSetImageGo("need_close","close_on.gif",false) onmouseout=hcSetImageGo("need_close","close_off.gif",true)><img name="need_close" src="http://www.liveperson.com/images/icons/set7/close_off.gif" border="0" width=250 height=18></a></td></tr>');
	put('</table></layer>');
	putNS();
}
function hcFloatIconLoaded()
{
	hcImageFetched = true;
}

function hcImageTimer()
{
	if (hcShowImage && hcImageFetched) {
		var top;
		var left;

		if (hcIE) {

			top = document.body.scrollTop;
			left = document.body.scrollLeft;

			if ((hcTop < 0) || ((hcTop == top) && (hcLeft == left))) {
				document.all.mylayer.style.visibility = "visible";
			} else {
				document.all.mylayer.style.visibility = "hidden";
			}

		} else if (hcNS) {

			top = pageYOffset;
			left = pageXOffset;

			if ((hcTop < 0) || ((hcTop == top) && (hcLeft == left))) {
				document.layers.mylayer.visibility = "visible";
			} else {
				document.layers.mylayer.visibility = "hidden";
			}
		} else if (hcDOM){
			top = pageYOffset;
			left = pageXOffset;
			if ((hcTop < 0) || ((hcTop == top) && (hcLeft == left))) {
				getE("mylayer").style.visibility = "visible";
			} else {
				getE("mylayer").style.visibility = "hidden";
			}
		}

		hcPlaceImage();

		hcTop = top;
		hcLeft = left;
	}

	setTimeout('hcImageTimer()', 250);
}

function hcAcceptCall()
{
	hcHideTheImage();

	openChat(hcParam, hcOpenVars);

	return false;
}

function hcRejectCall()
{
	hcHideTheImage();

	hcCmd = "rejectChat";
	hcCounter = 0;

	return false;
}

function hcHideTheImage()
{
	hcShowImage = false;

	if (hcIE) {
		document.all.mylayer.style.visibility = "hidden";
	} else if (hcNS) {
		document.layers.mylayer.visibility = "hidden";
	} else if (hcDOM) {
		getE("mylayer").style.visibility = "hidden";
	}
}

function hcShowTheImage()
{
	hcShowImage = true;

	hcSetImage("need_help","need_help_off.gif");
	hcSetImage("need_close","close_off.gif");

	hcAnimate = true;

	hcPreload();

	hcAnimateStart();
}

function hcAnimateStart()
{
	if (hcIE) {
		hcBorder = document.body.clientWidth;
	} else if (hcNS) {
		hcBorder = window.innerWidth;
	} else if (hcDOM) {
		hcBorder = window.innerWidth;
	}

	hcAnimateImage();
}

function getImageWidth(name)
{
	if (hcDOM)
		return (document.getElementsByTagName("IMG")[name]).width;
	else
	if (hcIE)
		return (document.all(name)).width;
	else
	if (hcNS)
		return (document[name]).width;
	else
		return null;
}

function hcAnimateImage()
{
	if (hcImageFetched && hcAnimate)
		hcPos = hcPos + hcDir;

	if (hcPos > hcBorder - 160)
		hcDir = - HumanStep;

	hcPlaceImage();

	if ((hcPos > 30) || (hcDir > 0))
		setTimeout("hcAnimateImage()", 20);
}

function hcPlaceImage()
{
	var y = 40;
	var x = hcPos;
	if (typeof(lpPosX) != "undefined")
		x = lpPosX;
	if (typeof(lpPosY) != "undefined")
		y = lpPosY;
	var obj = null;
	if (hcIE) {
		obj = document.all.mylayer.style;
	} else if (hcNS) {
		obj = document.layers.mylayer;
	} else if (hcDOM) {
		obj = getE("mylayer").style;
	}
    if (typeof(lpPlacementFunctionHook) == "function") {
		lpPlacementFunctionHook(obj);
 	} else {
		if (hcIE) {
			obj.left = document.body.scrollLeft + x;
			obj.top = document.body.scrollTop + y;
		} else if (hcNS) {
			obj.left = pageXOffset + x;
			obj.top = pageYOffset + y;
		} else if (hcDOM) {
			obj.left = pageXOffset + x;
			obj.top = pageYOffset + y;
		}
	}
}

function hcGetImage(name)
{
	return hcFindImage(document, name);
}

function hcFindImage(doc, name)
{
	if (hcDOM)
		return doc.getElementsByTagName("IMG")[name];
	var lays = doc.layers;

	if (! lays)
		return doc[name];

	for (var i = 0; i < doc.images.length; i++) {
		if (doc.images[i].name == name)
			return doc.images[i];
	}

	for (var l = 0; l < lays.length; l++) {
		img = hcFindImage(lays[l].document, name);
		if (img != null)
			return img;
	}

	return null;
}


function hcgo()
{
	hcloop();
	setTimeout('hcImageTimer()', 250);
}

hcgo();

