var lhAskedForCall = new Image();
var lhCookieAvailImg = new Image();
var lhOperAvailImg = new Image();
var lhWindowSizeAvailImg = new Image();
var lhChkMsg = true;
var lhSendFlag = 1;
var lhRnd;
var lhAcceptCookie;
var lhC, lhG, lhP, lhA, lhV, lhPl, lhDm, lhRf, lhTl;
var lhAvailWindowSizeWidth;
var lhHaschatted = false;
var lhTimeid;
var lhA=navigator.appName;
var lhV=navigator.appVersion;
var lhPl=navigator.platform;
var lhDm=document.domain;
var lhRf=document.referrer;
var lhTl=document.title;
var lhPg = location.href;
var lhcount = 0;
var N6 = (document.getElementById && !document.all) ? true : false; //To determine netscape 6

if (!lhPg)
{
	lhPg = "local url";	
}

function lhLoadChat(companyId, groupName, pageId)
{
	lhHaschatted = true;
	var name = "chat";
//window.open(url,winName,"width="+winWidth+",height="+winHeight+","+status)
//status='toolbar=no,menubar=no,directories=no,resizable=yes'
if(document.layers)
winStatus='toolbar=no,menubar=no,directories=no,resizable=no';
else
winStatus='toolbar=no,menubar=no,directories=no,resizable=yes';

//alert(lhWindowSizeAvailImg.width);

	if(parseInt(lhWindowSizeAvailImg.width) == 8)
	{
		lhG1 = unescape(groupName);

		var url = "http://browse.livehelper.com/servlet/lhChatRedirect?ACTION=SENDNAMEENTRYSCREEN&WINDOWSIZE=1&COMPANYID=" + companyId + "&GROUPNAME=" + escape(lhG1) + "&RND=" + lhRnd;
		//window.open(url, name, 'width=450,height=350,toolbar=no,menubar=no,directories=no,resizable=yes');
		window.open(url, name, "width=450,height=350"+winStatus);
	}
	else
	{
		lhG1 = unescape(groupName);

		var url = "http://browse.livehelper.com/servlet/lhChatRedirect?ACTION=SENDNAMEENTRYSCREEN&WINDOWSIZE=0&COMPANYID=" + companyId + "&GROUPNAME=" + escape(lhG1) + "&RND=" + lhRnd;
		//window.open(url, name, 'width=295,height=300,toolbar=no,menubar=no,directories=no,resizable=yes');
		window.open(url, name, "width=295,height=300"+winStatus);
	}
}


function lhGetWindowSizeAvailability(lhC,lhG)
{
	lhG1 = unescape(lhG);

	var lhR = Math.random()+1;
	windowSizeSrc = "http://browse.livehelper.com/servlet/hpLiveHelperBrowse?ACTION=CHATWINDOWSIZE&COMPANYID=" + escape(lhC) + "&GROUPNAME=" + escape (lhG1) + "&r=" + escape(lhR);
	lhWindowSizeAvailImg.src = windowSizeSrc;
	lhAvailWindowSizeWidth=lhWindowSizeAvailImg.width;
	lhSendFlag = 4;
}

function lhHereIE() 
{
	//alert("inside lhHereIE")
	sc = "http://browse.livehelper.com/servlet/hpLiveHelperBrowse?ACTION=STILLHERE&RND=" + lhRnd + "&c=" + lhC + "&nocache=" + Math.random();
	var test = new Image();
	test.src = sc;
	lhAskedForCall = test;
	if(lhAskedForCall.width!=0 && lhAskedForCall.width!=28 && lhAskedForCall.width!=8)
	{
		lhInterval = 30000;
	}
	else
		lhInterval = 20000;

	setTimeout('lhHereIE()',lhInterval);
}




function lhHere() 
{	
	//alert("Before lhAskedForCall.width "+lhAskedForCall.width);
	sc = "http://browse.livehelper.com/servlet/hpLiveHelperBrowse?ACTION=STILLHERE&RND=" + lhRnd + "&c=" + lhC + "&nocache=" + Math.random();
	if(N6)
	{
		//alert("inside ns 6");
		var test = new Image();
		test.src = sc;
		lhAskedForCall = test;
	}
	else
		lhAskedForCall.src = sc;
	//alert("After lhAskedForCall.width "+lhAskedForCall.width);
	if(lhAskedForCall.width!=0 && lhAskedForCall.width!=28 && lhAskedForCall.width!=8)
	{
		lhInterval = 30000;
	}
	else
		lhInterval = 20000;
	setTimeout('lhHere()',lhInterval);
}

function lhGetImgIE()
{
	var r = Math.random()+1;
	var sc = "a";
	lhChkMsg = false;
	
	if(lhSendFlag == 6)
	{
		clearTimeout(lhTimeid);
		return;
	}
	
	if(lhSendFlag == 1)
	{
		lhSendFlag=4;
		sc = "http://browse.livehelper.com/servlet/hpLiveHelperBrowse?ACTION=NEW&RND=" + r;
		lhCookieAvailImg.src = sc;
	}

 	if (lhSendFlag==4) { 
		if (lhWindowSizeAvailImg.width == 8 || lhWindowSizeAvailImg.width == 28 || lhWindowSizeAvailImg.width == 10)
		{
			lhSendFlag = 2;
		}		
		else if (lhWindowSizeAvailImg.width == 5){
			lhSendFlag = 6;
		}
	}
	if (lhSendFlag==2) 
	{
	//alert("inside lhSendFlag 2");
	//alert("lhWindowSizeAvailImg.width==="+lhWindowSizeAvailImg.width)
		//if(document.layers)
		//setTimeout('lhHere()',8000);//added by rags on 06 oct 2001
		lhG1 = unescape(lhG);
		lhA1 = unescape(lhA);
		lhV1 = unescape(lhV);
		lhP1 = unescape(lhP);
		lhDm1 = unescape(lhDm);
		lhRf1 = unescape(lhRf);
		lhT1 = unescape(lhTl);


		lhSendFlag = 5;
	sc = "http://browse.livehelper.com/servlet/hpLiveHelperBrowse?ACTION=ISTHERENEWMSG&RND=" + lhRnd + "&p=" + lhP + "&c=" + lhC + "&g=" + escape (lhG1) + "&r=" + r + "&a=" + escape(lhA1) + "&v=" + escape(lhV) + "&pl=" + escape(lhPl) + "&dm=" + escape(lhDm) + "&rf=" + escape(lhRf) + "&tl=" + escape(lhTl)+ "&cs=true" + "&pg=" + lhPg;
		lhAskedForCall.src = sc;
	}	
	if (lhAskedForCall.width!=0 && lhSendFlag==5)  // or if the 2nd cookie has been set
		lhSendFlag = 3;
	
	if(lhSendFlag == 3)
	{
		//if((document.all)||(N6))
		lhHereIE();
		clearTimeout(lhTimeid);
		return;
	}
	lhTimeid = setTimeout('lhGetImgIE()', 3000);
}




// added by nitish to put opera in browse list starts
function lhOperaNewmsg()
{
	var r = Math.random()+1;
	var sc = "a";
		//alert("lok");

	sc = "http://browse.livehelper.com/servlet/hpLiveHelperBrowse?ACTION=ISTHERENEWMSG&RND=" + lhRnd + "&p=" + lhP + "&c=" + lhC + "&g=" + escape (lhG) + "&r=" + r + "&a=" + escape(navigator.userAgent) + "&v=" + escape(lhV) + "&pl=" + escape(lhPl) + "&dm=" + escape(lhDm) + "&rf=" + escape (lhRf) + "&tl=" + escape (lhTl)+ "&cs=true" + "&pg=" + lhPg;

	lhAskedForCall.src = sc;	

	setTimeout('lhOperaHere()',20000);
}

function lhOperaHere() 
{
var r = Math.random()+1;
	var sc = "a";
	sc = "http://browse.livehelper.com/servlet/hpLiveHelperBrowse?ACTION=STILLHERE&RND=" + lhRnd + "&c=" + lhC + "&nocache=" + Math.random();
	lhAskedForCall.src = sc;
	setTimeout('lhOperaHere()',20000);
}
// added by nitish to put opera in browse list ends




function lhGetImg()
{
var lhBrowser = navigator.userAgent;    // special
if (lhBrowser.search(/Opera/) != -1)    // for 
	lhOperaNewmsg();                    // Opera  


	if(!document.layers && !N6)
	{
		//alert("ie");
		lhGetImgIE();
		return;
	}

testImgWidth=parseInt(lhWindowSizeAvailImg.width);
	var r = Math.random()+1;
	var sc = "a";
	lhChkMsg = false;
	
	if(lhSendFlag == 6)
	{
		clearTimeout(lhTimeid);
		return;
	}
	
	if(lhSendFlag == 1)
	{
		lhSendFlag=4;
		sc = "http://browse.livehelper.com/servlet/hpLiveHelperBrowse?ACTION=NEW&RND=" + r;
		lhCookieAvailImg.src = sc;
	}

 	if (lhSendFlag==4) { 
		if (lhWindowSizeAvailImg.width == 8 || lhWindowSizeAvailImg.width == 28 || lhWindowSizeAvailImg.width == 10)
		{

			lhSendFlag = 2;
		}		
		else if (lhWindowSizeAvailImg.width == 5){
			lhSendFlag = 6;
		}
	}
	if (lhSendFlag==2) 
	{
		if(document.layers)
			setTimeout('lhHere()',8000);//added by rags on 06 oct 2001
		lhG1 = unescape(lhG);
		lhA1 = unescape(lhA);
		lhV1 = unescape(lhV);
		lhP1 = unescape(lhP);
		lhDm1 = unescape(lhDm);
		lhRf1 = unescape(lhRf);
		lhT1 = unescape(lhTl);


		lhSendFlag = 5;
		sc = "http://browse.livehelper.com/servlet/hpLiveHelperBrowse?ACTION=ISTHERENEWMSG&RND=" + lhRnd + "&p=" + lhP + "&c=" + lhC + "&g=" + escape (lhG1) + "&r=" + r + "&a=" + escape(lhA1) + "&v=" + escape(lhV) + "&pl=" + escape(lhPl) + "&dm=" + escape(lhDm) + "&rf=" + escape(lhRf) + "&tl=" + escape(lhTl)+ "&cs=true" + "&pg=" + lhPg;
		lhAskedForCall.src = sc;
	}	
	//if (lhAskedForCall.width!=0 && lhSendFlag==5)  // or if the 2nd cookie has been set
	if ( (testImgWidth != 0) && (lhSendFlag == 5))
		lhSendFlag = 3;
	
	if(lhSendFlag == 3)
	{
		if(N6)
			lhHere();
		clearTimeout(lhTimeid);
		return;
	}
	lhTimeid = setTimeout('lhGetImg()', 3000);
}

function lhGetOperatorAvailability(lhC,lhG)
{
	lhG1 = unescape(lhG);

	var lhR = Math.random()+1;
	sc = "http://216.198.223.167/servlet/hpLiveHelperChat?ACTION=ISOPERATORONLINE&COMPANYID=" + escape(lhC) + "&GROUPNAME=" + escape (lhG1);
	lhOperAvailImg.src = sc;
	lhOperAvailImgWidth=lhOperAvailImg.width;
}


function setIconBsyImg(companyId, groupName, pageId, availImg, notAvailImg, lhType)
{

	lhGetWindowSizeAvailability(companyId,groupName);

			if (lhType == "image") 
			{
				lhSetIcone(companyId, groupName, pageId, availImg, notAvailImg, lhType)
				return;
			}
	lhGetOperatorAvailability(companyId,groupName)
	document.write('<br>');
	document.write('<table border="0" cellspacing="0" cellpadding="0">');
	document.write('<tr><td align="middle" valign="top">');
	document.write("<p>");
	var lnk = "<a href='javascript:lhLoadChat(\"" + companyId + "\", \"" + groupName + "\", \"" + pageId + "\")'>";
	document.write (lnk);
	if(parseInt(lhOperAvailImg.width) == 8)
	{
			var chk = availImg;
	} 
	else 
	{
			var chk = notAvailImg;
	}
    document.write(chk);
	document.write("</a>");
	document.write('</td></tr>');
	document.write('</table>');
	var ch = "lhCheck4messages('" + companyId + "', '" + groupName + "', '" + pageId + "')";
	setTimeout(ch, 0);
}


function setIconBsyImg_NoTracker(companyId, groupName, pageId, availImg, notAvailImg, lhType)
{

	lhGetWindowSizeAvailability(companyId,groupName);

	if (lhType == "image") {
		lhSetIcone_noTracker(companyId, groupName, pageId, availImg, notAvailImg, lhType)
		return;
	}

	lhGetOperatorAvailability(companyId,groupName)

	document.write('<table border="0" cellspacing="0" cellpadding="0">');
	document.write('<tr><td align="middle" valign="top">');
	document.write("<p>");
	var lnk = "<a href='javascript:lhLoadChat(\""+companyId+"\",\""+groupName+"\",\""+pageId+"\")'>";
	document.write (lnk);
		
	if(parseInt(lhOperAvailImg.width) == 8)
	{
			if (lhType == "text")  
			var chk = availImg;
			else					
			var chk="<img src='"+availImg+"' alt='Click here for live customer assistance' border=0>";

	} 
	else 
	{
			if (lhType == "text")
			var chk = notAvailImg;
			else 
			var chk="<img src='"+notAvailImg+"' alt='Click here to email us' border=0>";
	}
	document.write(chk);
	document.write("</a>");
	document.write('</td></tr>');
	document.write('</table>');
}


function setIconImg(companyId, groupName, pageId,imgsrc)
{

	lhGetWindowSizeAvailability(companyId,groupName);

	document.write('<script language="javascript" src="http://www.livehelper.com/includes/livehelper.js"></script>');
	document.write('<table border="0" cellspacing="0" cellpadding="0">');
	document.write('<tr><td align="middle" valign="top">');
	document.write("<p>");
	var lnk = "<a href='javascript:lhLoadChat(\"" + companyId + "\", \"" + groupName + "\", \"" + pageId + "\")'>";
	document.write (lnk);
	var chk="<img src='"+imgsrc+"'>";
	var vin=document.write(chk);
	document.write("</a>");
	document.write('</td></tr>');
	document.write('</table>');
	lhCheck4messages(companyId, groupName, pageId);
}


function setIconText(companyId, groupName, pageId,text)
{

	lhGetWindowSizeAvailability(companyId,groupName);

	document.write('<table border="0" cellspacing="0" cellpadding="0">');
	document.write('<tr><td align="middle" valign="top">');
	document.write("<p>");
	var lnk = "<a href='javascript:lhLoadChat(\"" + companyId + "\", \"" + groupName + "\", \"" + pageId + "\")'>";
	document.write (lnk);
	var vin=document.write(text);
	document.write("</a>");
	document.write('</td></tr>');
	document.write('</table>');
	lhCheck4messages(companyId, groupName, pageId);
}


function setIcon_NoTracker(companyId, groupName, pageId)
{

	lhGetWindowSizeAvailability(companyId,groupName);

	document.write('<table border="0" cellspacing="0" cellpadding=0>');
	document.write('<tr><td align="middle" valign="top">');
	var lnk = "<a name='chatLink' href='javascript:lhLoadChat(\"" + companyId + "\", \"" + groupName + "\", \"" + pageId + "\");' target='_self'>";
	document.write (lnk);
	document.write('<img name="LivehelperIcon" src="http://www.livehelper.com/client_login/AdministrationScreens/image/logo9].gif" alt="Click here for live customer assistance" border="0"></a>');
	document.write('</td></tr>');
	document.write('</table>');
}


function setIcon(companyId, groupName, pageId)
{
	lhGetWindowSizeAvailability(companyId,groupName);

	document.write('<table border="0" cellspacing="0" cellpadding=0>');
	document.write('<tr><td align="middle" valign="top">');
	var lnk = "<a name='chatLink' href='javascript:lhLoadChat(\"" + companyId + "\", \"" + groupName + "\", \"" + pageId + "\");' target='_self'>";
	document.write (lnk);
	document.write('<img name="LivehelperIcon" src="http://www.livehelper.com/client_login/AdministrationScreens/image/logo9].gif" alt="Click here for live customer assistance" border="0"></a>');
	document.write('</td></tr>');
	document.write('</table>');

	//var ch = "lhCheck4messages('" + companyId + "', '" + groupName + "', '" + pageId + "');";
	//setTimeout(ch,0);
	lhCheck4messages(companyId, groupName, pageId);
}

function lhCheck4messages(companyId, groupName, pageId)
{
	 if (lhChkMsg)
	 {
		lhC = companyId;
		lhG = groupName;
		lhP = pageId;
		lhRnd = Math.random();
		lhGetImg();
	 }

	 if (lhAskedForCall.width == 8)
	 {
		  var name = "chat";
		  if(document.layers)
		  winStatus='toolbar=no,menubar=no,directories=no,resizable=no';
		  else
		  winStatus='toolbar=no,menubar=no,directories=no,resizable=yes';
		  if(parseInt(lhWindowSizeAvailImg.width) == 8)
			{
			lhG1 = unescape(groupName);

			var url = "http://browse.livehelper.com/servlet/lhChatRedirect?ACTION=SENDCHATSCREEN&WINDOWSIZE=1&COMPANYID=" + companyId + "&GROUPNAME=" + escape(lhG1) + "&CHATTYPE=Text&CLIENTNAME=Visitor&RND=" + lhRnd;
			//window.open(url, name, 'width=450,height=350,toolbar=no,menubar=no,directories=no,resizable=yes');
			window.open(url, name, "width=450,height=350"+winStatus);
			}
			else
			{
			lhG1 = unescape(groupName);

			var url = "http://browse.livehelper.com/servlet/lhChatRedirect?ACTION=SENDCHATSCREEN&WINDOWSIZE=0&COMPANYID=" + companyId + "&GROUPNAME=" + escape(lhG1) + "&CHATTYPE=Text&CLIENTNAME=Visitor&RND=" + lhRnd;
			//window.open(url, name, 'width=295,height=300,toolbar=no,menubar=no,directories=no,resizable=yes');
			window.open(url, name, "width=295,height=300"+winStatus);
			}
	    return;
	  }
	 var ch = "lhCheck4messages('" + companyId + "', '" + groupName + "', '" + pageId + "')";
	 setTimeout(ch, 3000);
}


function lhOpenchatlink(companyId,groupName,lhP) 
{
	var chlk = "lhOpenchat('"+companyId+"','"+groupName+"','"+lhP+"');";
	setTimeout (chlk,6000);		
}

function lhOpenchat(companyId,groupName,lhP) 
{
	lhGetOperatorAvailability(companyId,groupName)
	if ((!lhHaschatted) && (parseInt(lhOperAvailImg.width)== 8))
	{
		url = "http://www.livehelper.com/chat/want-to-chat.html?COMPANYID=" + companyId + "&GROUPNAME=" + groupName;
		name = "hello";
		var newWin=window.open(url, name, 'width=150,height=150,toolbar=no,menubar=no,directories=no,resizable=no');
	}
	else
	setTimeout("lhOpenchat('"+companyId+"','"+groupName+"','"+lhP+"')",3000)
}

function lhSetIcone(companyId, groupName, pageId,onlineurl,offlineurl,type)
{
	var lhRnd = Math.random();
	
	document.write('<table border="0" cellspacing="0" cellpadding=0>');
	document.write('<tr><td align="middle" valign="top">');
	var lnk = "<a name='chatLink' href='javascript:lhLoadChat(\"" + companyId + "\", \"" + groupName + "\", \"" + pageId + "\");' target='_self'>";
	document.write (lnk);
	var lnk2 = '<img name="LivehelperIcon" src="http://216.198.223.167/servlet/hpLiveHelperChat?ACTION=GIVEIMAGE&COMPANYID=' +companyId+ '&ONLINEURL=' + escape(onlineurl) + '&OFFLINEURL=' + escape(offlineurl)  + '&rnd=' + lhRnd + '" alt="Click here for live customer assistance" border="0"></a>';
	document.write(lnk2);
	document.write('</td></tr>');
	document.write('</table>');
	lhShowCorrectImage(companyId, groupName, pageId,onlineurl,offlineurl,type);

	var ch = "lhCheck4messages('" + companyId + "', '" + groupName + "', '" + pageId + "')";
	setTimeout(ch, 0);
}

function lhShowCorrectImage (companyId, groupName, pageId,onlineurl,offlineurl,type) {
	var lhRnd = Math.random();

	sc='http://216.198.223.167/servlet/hpLiveHelperChat?ACTION=GIVEIMAGE&COMPANYID='+ companyId + '&ONLINEURL=' + escape(onlineurl) + '&OFFLINEURL=' + escape(offlineurl)  + '&rnd=' + lhRnd ;
	document.images["LivehelperIcon"].src = sc;
	var link = 'lhShowCorrectImage("'+companyId+'","'+groupName+'","'+pageId+'","'+onlineurl+'","'+offlineurl+'","'+type+'");';

	setTimeout(link, 60000);
}


function lhSetIcone_noTracker(companyId, groupName, pageId,onlineurl,offlineurl,type)
{
	var lhRnd = Math.random();
	
	document.write('<table border="0" cellspacing="0" cellpadding=0>');
	document.write('<tr><td align="middle" valign="top">');
	var lnk = "<a name='chatLink' href='javascript:lhLoadChat(\"" + companyId + "\", \"" + groupName + "\", \"" + pageId + "\");' target='_self'>";
	document.write (lnk);
	var lnk2 = '<img name="LivehelperIcon" src="http://216.198.223.167/servlet/hpLiveHelperChat?ACTION=GIVEIMAGE&COMPANYID=' +companyId+ '&ONLINEURL=' + escape(onlineurl) + '&OFFLINEURL=' + escape(offlineurl)  + '&rnd=' + lhRnd + '" alt="Click here for live customer assistance" border="0"></a>';
	document.write(lnk2);
	document.write('</td></tr>');
	document.write('</table>');
	lhShowCorrectImage(companyId, groupName, pageId,onlineurl,offlineurl,type);

}