   var i;
   i = 0
   var LS_WebSite = "http://www.beansoftware.com/Live-Support/";
   var serverData;
   var xmlRequest;
   var chatDeclined = 0;
      
   function LiveSupportLoop()
   {
      i++;
      var LS_Referrer = document.referrer;
      var LS_Image = document.getElementById("LiveSupport_Image");
	  var LS_ImageSrc = LS_WebSite + "ShowLSButtonImage.aspx?id=" + i;
      
	  // ??? Try to optimize this, information about referrer is practically needed only first time
	  //if(i < 10)
	  //{
	     LS_ImageSrc += "&ref=" + escape(LS_Referrer);
	  //}
	  LS_Image.src = LS_ImageSrc;
	  
	  // ??? Add support for chat initializaton
	  CheckOperatorInvitation(i);
      setTimeout("LiveSupportLoop()", 10000); // ??? timeout should be changable by administrator
   }   
   
   function CheckOperatorInvitation(i)
   {
		var e;
		try {
        xmlRequest = new XMLHttpRequest();
		}
		catch(e) {
			try {
            xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e) {
				xmlRequest = null;
				return false;
			}
		}
		var LS_CheckParamURL = LS_WebSite + "getLSParameters.aspx?id=" + i;  // ???
		xmlRequest.onreadystatechange = xmlStateCheck;
		xmlRequest.open('GET', LS_CheckParamURL, true);
		xmlRequest.send(null);
   }
   
   function xmlStateCheck()
   {
		// if request is completed
		if(xmlRequest.readyState==4){
			// if status == 200 display text file
			if(xmlRequest.status==200){
				// gets data from server
				serverData=xmlRequest.responseText;
				if(serverData == "1")
				{
					ShowInvitationDialog();
				}
			}
			else{
				// alert('Failed to get response :'+ xmlRequest.statusText);
			}
		}
   }
   
   function Hide_Live_Chat_Invitation()
   {
   // ??? Check is it working and check browser compatibility
		var LS_div = document.getElementById("LS_Invitation");
		LS_div.style.visibility = "hidden";
   }
   
   function DeclineChat()
   {
	chatDeclined=1;
   }
   
   function ShowInvitationDialog()
   {
	var LS_div = document.getElementById("LS_Invitation");
	if(chatDeclined == 0)
	{
		if(LS_div == null)
		{
			LS_div = document.createElement('div');
			LS_div.id = "LS_Invitation"; // ??? try to avoid duplicate names
			LS_div.style.position = "absolute";
			LS_div.style.top = "200px";
			LS_div.style.left = "200px";
			LS_div.innerHTML = "<MAP NAME=myimagemap><AREA HREF='javascript:Open_Live_Chat_Window();Hide_Live_Chat_Invitation();' SHAPE=RECT COORDS=0,0,419,216><AREA HREF='javascript:Open_Live_Chat_Window();Hide_Live_Chat_Invitation();' SHAPE=RECT COORDS=0,216,319,279><AREA HREF='javascript:Hide_Live_Chat_Invitation();DeclineChat();' SHAPE=RECT COORDS=326,218,429,280></MAP><img border='0' src='http://www.beansoftware.com/Live-Support/Invitation-Dialogs/layer-Subsilver.gif' usemap='#myimagemap' />"; // ???
			document.body.appendChild(LS_div);
		}
		else
		{
			LS_div.style.visibility = "visible";
		}
	}
   }
   
   function Open_Live_Chat_Window() 
   {
        open("http://www.beansoftware.com/Live-Support/AjaxChat.aspx", "", 'width=500,height=500');
   }
   
   LiveSupportLoop();