	var LiveHelpStatus = 0;

	function IDs() {
		this.SiteID = 0;
		this.HostID = 0;
		this.ClientID = 0;
		this.MonitorID = 0;
		this.CookieKey = '';
	}
	var oIDs = new IDs;
	var oChat = new Chat;

	oIDs.SiteID = mySiteID;
	oIDs.ClientID = getClientID(oIDs.SiteID);
	oIDs.MonitorID = LiveHelpMonitorID;
	oIDs.CookieKey = getCookieLiveHelp("WebStats");

	function getTemplate(TemplateName) {
		var Template = '';
		if (document.getElementById(TemplateName)) {
			var Template = document.getElementById(TemplateName).innerHTML.replace(/}/g, '>').replace(/{/g, '<');
		}
		return Template;
	}

	function parseXMLUpdate(xmlDoc) {
		var oList = new Array();
		var counter = 0;
		var oXML = xmlDoc.documentElement;

		if (oXML != null) {
			if (oXML.childNodes.length == 0) {
				//Timeout or some other error occured on page and Monitor Registration is lost. Create a new one as we are still browsing.
				POSTRegister(1, 1);	//Insert-Update
				return true;
			}
			for (var i = 0; i < oXML.childNodes.length; i++) {
				var Chatting = 0;
				var ChatDeny = 0;
				var ChatRequest = 0;
				var HostStatusID = 0;
				var OnlineImageExtension = '';
				var OfflineImageExtension = '';
				var OccupiedImageExtension = '';
				var OnlineImageDefault = '';
				var OfflineImageDefault = '';
				var OccupiedImageDefault = '';
				for (var j = 0; j < oXML.childNodes[i].childNodes.length; j++) {
					if (oXML.childNodes[i].childNodes[j].firstChild != null) {
						switch (oXML.childNodes[i].childNodes[j].nodeName) {
							case "Chatting":
								Chatting = oXML.childNodes[i].childNodes[j].firstChild.nodeValue;
								break;
							case "ChatDeny":
								ChatDeny = oXML.childNodes[i].childNodes[j].firstChild.nodeValue;
								break;
							case "ChatRequest":
								ChatRequest = oXML.childNodes[i].childNodes[j].firstChild.nodeValue;
								break;
							case "HostStatusID":
								HostStatusID = oXML.childNodes[i].childNodes[j].firstChild.nodeValue;
								break;
							case "OnlineImageExtension":
								OnlineImageExtension = oXML.childNodes[i].childNodes[j].firstChild.nodeValue;
								break;
							case "OfflineImageExtension":
								OfflineImageExtension = oXML.childNodes[i].childNodes[j].firstChild.nodeValue;
								break;
							case "OccupiedImageExtension":
								OccupiedImageExtension = oXML.childNodes[i].childNodes[j].firstChild.nodeValue;
								break;
							case "OnlineImageDefault":
								OnlineImageDefault = oXML.childNodes[i].childNodes[j].firstChild.nodeValue;
								break;
							case "OfflineImageDefault":
								OfflineImageDefault = oXML.childNodes[i].childNodes[j].firstChild.nodeValue;
								break;
							case "OccupiedImageDefault":
								OccupiedImageDefault = oXML.childNodes[i].childNodes[j].firstChild.nodeValue;
								break;
						}
					}
				}
				HostStatusID = parseInt(HostStatusID);
				if (ChatRequest != 0 && ChatDeny == 0 && Chatting == 0) {
					LoopStop();
					if (document.getElementById("LiveHelpIncoming")) {
						oLiveHelpIncoming = document.getElementById("LiveHelpIncoming");
						oLiveHelpIncoming.style.left = (screen.availWidth - 280) / 2;
						oLiveHelpIncoming.style.top = (screen.availHeight - 160) / 2;
				 		oLiveHelpIncoming.style.display = 'block';
				 	}
				}
				if (HostStatusID != LiveHelpStatus) {
					LiveHelpStatus = HostStatusID;
					var oLiveHelpStatus = document.getElementById("LiveHelpStatus");
					var sLiveHelpStatusImage = '';
					mReg = new RegExp('ImageType', 'g');
					mReg2 = new RegExp('ImageExtension', 'g');
					var sDefault = '';
					switch (LiveHelpStatus) {
						case 1:	//Available
							sDefault = (OnlineImageDefault != 0) ? "Default" : "";
							sLiveHelpStatusImage = getTemplate("LiveHelpStatusImage" + sDefault);
							sLiveHelpStatusImage = sLiveHelpStatusImage.replace(mReg, "Online");
							sLiveHelpStatusImage = sLiveHelpStatusImage.replace(mReg2, OnlineImageExtension);
							break;
						case 2:	//Busy
							sDefault = (OccupiedImageDefault != 0) ? "Default" : "";
							sLiveHelpStatusImage = getTemplate("LiveHelpStatusImage" + sDefault);
							sLiveHelpStatusImage = sLiveHelpStatusImage.replace(mReg, "Occupied");
							sLiveHelpStatusImage = sLiveHelpStatusImage.replace(mReg2, OccupiedImageExtension);
							break;
						case 3:	//Not Available
							sDefault = (OfflineImageDefault != 0) ? "Default" : "";
							sLiveHelpStatusImage = getTemplate("LiveHelpStatusImage" + sDefault);
							sLiveHelpStatusImage = sLiveHelpStatusImage.replace(mReg, "Offline");
							sLiveHelpStatusImage = sLiveHelpStatusImage.replace(mReg2, OfflineImageExtension);
							break;
					}
					if (oLiveHelpStatus) {
						oLiveHelpStatus.innerHTML = sLiveHelpStatusImage;
					}
				}
			}
		}
	}

	function parseXMLStatus(xmlDoc) {
		var oList = new Array();
		var counter = 0;
		var oXML = xmlDoc.documentElement;

		if (oXML != null) {
			var HostID = 0;
			for (var i = 0; i < oXML.childNodes.length; i++) {
				for (var j = 0; j < oXML.childNodes[i].childNodes.length; j++) {
					if (oXML.childNodes[i].childNodes[j].firstChild != null) {
						switch (oXML.childNodes[i].childNodes[j].nodeName) {
							case "HostID":
								HostID = oXML.childNodes[i].childNodes[j].firstChild.nodeValue;
								break;
						}
					}
				}
			}
			if (HostID != 0) {
				oIDs.HostID = HostID;
			}
		}
	}
	  
	function POSTRegister(Register, Browsing) {
		//var FirstTimeVisitorValue = FirstTimeVisitor ? 1 : 0;
		if (FirstTimeVisitor) {
			FirstTimeVisitorValue = 1;
		}
		else {
			FirstTimeVisitorValue = 0;
		}
		AjaxRequest.post(
			{
			  'url':LiveHelpFullHostName + '/livehelp_new/ajax_common_register.asp'
			  ,'parameters':{ 'Register':Register, 'SiteID':oIDs.SiteID, 'MonitorID':oIDs.MonitorID, 'ClientID':oIDs.ClientID, 'CurrentPage':Current_page_URL, 'CookieKey':oIDs.CookieKey, 'FirstTimeVisitor':FirstTimeVisitorValue, 'Browsing':Browsing }
			  ,'anotherParameter':'true'
			  ,'onSuccess':function(req) { }
		    ,'onError':function(req){ }
			}
		)
	}
	  
	function GETUpdate() {
		AjaxRequest.get(
			{
			  'url':LiveHelpFullHostName + '/livehelp_new/ajax_common_getchat.asp'
			  ,'parameters':{ 'MonitorID':oIDs.MonitorID }
			  ,'anotherParameter':'true'
				,'timeout':3000
				,'onTimeout':function(req){ }
			  ,'onSuccess':function(req) { parseXMLUpdate(req.responseXML); }
		    ,'onError':function(req){ }
			}
		)
	}

	function POSTChat() {
		AjaxRequest.post(
			{
			  'url':LiveHelpFullHostName + '/livehelp_new/ajax_common_postchat.asp'
			  ,'parameters':{ 'MonitorID':oIDs.MonitorID, 'SynchKey':'', 'Chatting':oChat.Chatting, 'ChatDeny':oChat.ChatDeny, 'ChatRequest':oChat.ChatRequest }
			  ,'anotherParameter':'true'
			  ,'onSuccess':function(req) { }
		    ,'onError':function(req){ }
			}
		)
	}

	function GETStatus() {
		AjaxRequest.get(
			{
			  'url':LiveHelpFullHostName + '/livehelp_new/ajax_frontend_getstatus.asp'
			  ,'parameters':{ 'SiteID':oIDs.SiteID }
			  ,'anotherParameter':'true'
				,'timeout':3000
				,'onTimeout':function(req){ }
			  ,'onSuccess':function(req) { parseXMLStatus(req.responseXML); }
		    ,'onError':function(req){ }
			}
		)
	}
	
	function Register() {
		POSTRegister(1, 1);	//Insert-Update
		GETStatus();
		GETUpdate();
		LoopStart("GETUpdate", 10000);
	}
	  
	function UnRegister() {
		if (document.getElementById("LiveHelpIncoming")) {
			oLiveHelpIncoming = document.getElementById("LiveHelpIncoming");
			oLiveHelpIncoming.style.display = "none";
		}
		oIDs.MonitorID = getMonitorID();
		if (oIDs.MonitorID != 0) {
			POSTRegister(3, 0);	//Update
		}
	}

	function DenyLiveHelp() {
		if (document.getElementById("LiveHelpIncoming")) {
			oLiveHelpIncoming = document.getElementById("LiveHelpIncoming");
			oLiveHelpIncoming.style.display = "none";
		}
		oChat.Chatting = 0;
		oChat.ChatDeny = 1;
		oChat.ChatRequest = 0;
		POSTChat();
		LoopStart("GETUpdate", 10000);
	}
	  
	function AcceptLiveHelp() {
		if (document.getElementById("LiveHelpIncoming")) {
			oLiveHelpIncoming = document.getElementById("LiveHelpIncoming");
			oLiveHelpIncoming.style.display = "none";
		}
		window.open(LiveHelpFullHostName + "/livehelp_new/login.asp?SiteID=" + oIDs.SiteID + "&MonitorID=" + oIDs.MonitorID, "LiveChat" + oIDs.MonitorID, "width=412, height=314, status=0, toolbar=0, resizable=0, menubar=0, scrollbars=0");
	}

	function ChatRequest() {
		if (LiveHelpStatus != 1) {
			window.open(LiveHelpFullHostName + "/livehelp_new/form.asp?SiteID=" + oIDs.SiteID, "LiveChat" + oIDs.MonitorID, "width=412, height=314, status=0, toolbar=0, resizable=0, menubar=0, scrollbars=0");
		}
		else {
			LoopStop();
			AcceptLiveHelp();
		}
	}
	MultipleLoadAdd('Register()');
	window.onbeforeunload = UnRegister;
