//CONSTANTS
var InsideOutChat_Server = "http://213.84.211.148/io/";
var InsideOutChat_TypeMessage = "Typ hier uw bericht.";
var InsideOutChat_PowerByLink = "http://www.inside-out.nl";
var InsideOutChat_OpacityCaption = "Opacity:";
var InsideOutChat_NameTabHeader = "Type your chat nick name below and click `Change`.";
var InsideOutChat_EMailTabHeader = "Type your e-mail below to receive the chat transcript.";
var InsideOutChat_String_Idle = "Idle";
var InsideOutChat_String_Sending = "Sending...";
var InsideOutChat_String_Operator = "Operator";
var InsideOutChat_CookieBase = "1_3_1_30";

var InsideOutChat_Alignment = {horizontal:0,vertical:0};
var InsideOutChat_Padding = {left:40,right:50,top:40,bottom:50};
var InsideOutChat_NormalSize = {width:353,height:250};
var InsideOutChat_MinimizedSize = {width:353,height:22};
var InsideOutChat_UserDeviation = null;
var InsideOutChat_UserOpacity = null;
var InsideOutChat_BlinkCounter = 0;
var InsideOutChat_Opacity = 1;
var InsideOutChat_InputClicked = 0;
var InsideOutChat_AlreadyOpened = 0;
var InsideOutChat_AutoOpened = 0;


var InsideOutChat_State = 0;
var InsideOutChat_ChatDiv = null;
var InsideOutChat_ServerBaseURL = "";
var InsideOutChat_DisplayInfoReceived = 0;
var InsideOutChat_StyleName = "default";
var InsideOutChat_WelcomeText = "";
var InsideOutChat_ImageURL = "";
var InsideOutChat_LanguageList = "";
var InsideOutChat_SendMessages = new Array();
var InsideOutChat_CommunicationState = 0;
var InsideOutChat_CommunicateASAP = 0;
var InsideOutChat_Request = null;
var InsideOutChat_SendRequestTimeout;
var InsideOutChat_ShiftDown = 0;
var InsideOutChat_ReceviedMessages = new Array();
var InsideOutChat_LastSentId = 0;
var InsideOutChat_LastCommand = "";
var InsideOutChat_UnreadMessageAvailable = 0;
var InsideOutChat_BlinkCounter = 0;
var InsideOutChat_Enabled = "no";
var InsideOutChat_InputClicked = 0;

//HELPERS
function InsideOutChat_SafeParseInt(value)
{
	var result = parseInt(value);
	if(result.toString() == "NaN")return 0;
	return result;
}

function GetElementPosition(obj) {
	var position = {x:0, y:0, width:0, height:0};
	var curleft = curtop = 0;
	var currentObject = obj;
	if (currentObject.offsetParent) {
		curleft = currentObject.offsetLeft;
		curtop = currentObject.offsetTop;
		while (currentObject = currentObject.offsetParent) {
			curleft += currentObject.offsetLeft;
			curtop += currentObject.offsetTop;
		}
	}
	position.x = curleft;
	position.y = curtop;
	position.width = obj.offsetWidth;
	position.height = obj.offsetHeight;
	return position;
}

function DisableSelection(target){
	if (typeof target.onselectstart != "undefined")
	{
		target.onselectstart = function(){return false;}
	}
	else if (typeof target.style.MozUserSelect != "undefined")
	{
		target.style.MozUserSelect="none";
	}
	else
	{
		target.onmousedown=function(){return false;}
	}
	target.style.cursor = "default";
}

function InsideOutChat_GetWindowSize()
{
	var result = {width:0,height:0}
	if( typeof( window.innerWidth ) == 'number' ) { 
		result.width = window.innerWidth;
		result.height = window.innerHeight;
	} 
	else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		result.width = document.documentElement.clientWidth;
		result.height = document.documentElement.clientHeight;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		result.width = document.body.clientWidth;
		result.height = document.body.clientHeight;
	}
	return result;
}

function InsideOutChat_GetScroll()
{
	var result = {x:0,y:0}
	if (self.pageYOffset)
	{
		result.x = self.pageXOffset;
		result.y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		result.x = document.documentElement.scrollLeft;
		result.y = document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		result.x = document.body.scrollLeft;
    	result.y = document.body.scrollTop;
	}
	return result;
}

//POPUP POSITION
function InsideOutChat_GetFinalAlignment()
{
	var result = InsideOutChat_Alignment;
	if(window.Promptchat_ForceHorizontalAlignment)
	{
		if(Promptchat_ForceHorizontalAlignment == "left")result.horizontal = 0;
		if(Promptchat_ForceHorizontalAlignment == "center")result.horizontal = 1;
		if(Promptchat_ForceHorizontalAlignment == "right")result.horizontal = 2;
	}
	if(window.Promptchat_ForceVerticalAlignment)
	{
		if(Promptchat_ForceVerticalAlignment == "top")result.vertical = 0;
		if(Promptchat_ForceVerticalAlignment == "center")result.vertical = 1;
		if(Promptchat_ForceVerticalAlignment == "bottom")result.vertical = 2;
	}
	return result;
}

function InsideOutChat_GetFinalPadding()
{
	var result = InsideOutChat_Padding;
	if(window.Promptchat_ForceHorizontalOffset)
	{
		var forcedPadding = parseInt(Promptchat_ForceHorizontalOffset);
		if((forcedPadding > 0) && (forcedPadding < 1000))
		{
			result.left = forcedPadding;
			result.right = forcedPadding;
		}
	}
	if(window.Promptchat_ForceVerticalOffset)
	{
		var forcedPadding = parseInt(Promptchat_ForceVerticalOffset);
		if((forcedPadding > 0) && (forcedPadding < 1000))
		{
			result.top = forcedPadding;
			result.bottom = forcedPadding;
		}
	}
	return result;
}

function InsideOutChat_CalculatePosition(size, fix)
{
	var result = {x:0,y:0};
	var alignment = InsideOutChat_GetFinalAlignment();
	var padding = InsideOutChat_GetFinalPadding();
	var scroll = InsideOutChat_GetScroll();
	var winSize = InsideOutChat_GetWindowSize();
	
	if(alignment.horizontal == 0)result.x = scroll.x + padding.left;
	else if(alignment.horizontal == 1)result.x = scroll.x + InsideOutChat_SafeParseInt((winSize.width - size.width) / 2);
	else result.x = scroll.x + winSize.width - size.width - padding.right;
	
	if(alignment.vertical == 0)result.y = scroll.y + padding.top;
	else if(alignment.vertical == 1)result.y = scroll.y + InsideOutChat_SafeParseInt((winSize.height - size.height) / 2);
	else result.y = scroll.y + winSize.height - size.height - padding.bottom;
	
	if(InsideOutChat_UserDeviation != null)
	{
		result.x += InsideOutChat_UserDeviation.x;
		result.y += InsideOutChat_UserDeviation.y;
	}
	
	if(fix)
	{
		if(result.x - scroll.x < 0)result.x = scroll.x;
		if(result.y - scroll.y < 0)result.y = scroll.y;
		if(result.x - scroll.x + size.width > winSize.width - 20)result.x = winSize.width - 20 - size.width + scroll.x;
		if(result.y - scroll.y + size.height > winSize.height - 20)result.y = winSize.height - 20- size.height + scroll.y;
	}
	
	return result;
}

function InsideOutChat_GetPopupSize()
{
	if(InsideOutChat_State == 2)
		return InsideOutChat_NormalSize;
	else if(InsideOutChat_State == 4)
		return InsideOutChat_MinimizedSize;
	else return {width:0,height:0}
}

function InsideOutChat_GetPopupPosition()
{
	var result = {x:0,y:0}
	if(InsideOutChat_ChatDiv != null)
	{
		result.x = InsideOutChat_SafeParseInt(InsideOutChat_ChatDiv.style.left);
		result.y = InsideOutChat_SafeParseInt(InsideOutChat_ChatDiv.style.top);
	}
	return result
}

function InsideOutChat_UpdatePosition()
{
	var communicationDelay = (new Date()).valueOf() - InsideOutChat_LastCommunicationTime;
	if(communicationDelay < 5000)InsideOutChat_SetNetworkStatus(2);
	else if(communicationDelay < 8000)InsideOutChat_SetNetworkStatus(1);
	else InsideOutChat_SetNetworkStatus(0);
	InsideOutChat_BlinkCounter = (InsideOutChat_BlinkCounter + 1) % 10000;
	var move = 0;
	var target = null;
	if((!InsideOutChat_Drag_Active) && (InsideOutChat_ChatDiv != null))
	{
		target = InsideOutChat_CalculatePosition(InsideOutChat_GetPopupSize(), true);
		if(target != null)
		{
			var current = InsideOutChat_GetPopupPosition();
			var xd = current.x - target.x;
			var yd = current.y - target.y;
			if(xd != 0)
			{
				var xs = InsideOutChat_SafeParseInt(xd / 2);
				if(xs == 0)xs = xd;
				InsideOutChat_ChatDiv.style.left = (current.x - xs) + "px";
			}
			if(yd != 0)
			{
				var ys = InsideOutChat_SafeParseInt(yd / 2);
				if(ys == 0)ys = yd;
				InsideOutChat_ChatDiv.style.top = (current.y - ys) + "px";
			}
		}
	}
	if((InsideOutChat_UnreadMessageAvailable) && (InsideOutChat_ChatDiv != null))
	{
		if(InsideOutChat_State == 2)
		{
			InsideOutChat_UnreadMessageAvailable = 0;
		}
		else
		{
			if(InsideOutChat_State == 4)
			{
				if((InsideOutChat_BlinkCounter % 25) < 12)
				{					
					document.getElementById("InsideOutChat_PopupHeader").style.background= "url(" + InsideOutChat_ServerBaseURL + "LS/Images/" + InsideOutChat_StyleName + "/PopupTopBack.gif)";
				}
				else
				{
					document.getElementById("InsideOutChat_PopupHeader").style.background= "url(" + InsideOutChat_ServerBaseURL + "LS/Images/" + InsideOutChat_StyleName + "/PopupTopBackBlink.gif)";
				}
			}
		}
	}
	setTimeout("InsideOutChat_UpdatePosition();", 50);
}

//POPUP DRAG AND DROP
var InsideOutChat_Drag_Active = 0;
var InsideOutChat_Drag_StartMouse = null;
var InsideOutChat_Drag_StartDiv = null;
var InsideOutChat_OldMouseMoveHandler = 0;
var InsideOutChat_OldMouseUpHandler = 0;

function InsideOutChat_Drag_Start(event)
{
	if(!InsideOutChat_Drag_Active)
	{
		InsideOutChat_Drag_Active = 1;
		InsideOutChat_Drag_StartMouse = {x:0,y:0}
		InsideOutChat_Drag_StartMouse.x = event.clientX;
		InsideOutChat_Drag_StartMouse.y = event.clientY;
		InsideOutChat_Drag_StartDiv = InsideOutChat_GetPopupPosition();
		InsideOutChat_OldMouseMoveHandler = document['onmousemove'];	
		document['onmousemove'] = InsideOutChat_Drag_MouseMoveHandler;
		InsideOutChat_OldMouseUpHandler = document['onmouseup'];	
		document['onmouseup'] = InsideOutChat_Drag_MouseUpHandler;				
	}
	return false;
}

function InsideOutChat_Drag_Move(event)
{
	var event = event || window.event;
	if(InsideOutChat_Drag_Active)
	{
		var currentDivX = InsideOutChat_Drag_StartDiv.x + (event.clientX - InsideOutChat_Drag_StartMouse.x);
		var currentDivY = InsideOutChat_Drag_StartDiv.y + (event.clientY - InsideOutChat_Drag_StartMouse.y);
		InsideOutChat_ChatDiv.style.left = currentDivX + "px";
		InsideOutChat_ChatDiv.style.top = currentDivY + "px";
	}
}

function InsideOutChat_Drag_End(event)
{
	if(InsideOutChat_Drag_Active)
	{				
		InsideOutChat_Drag_Active = 0;
		document['onmousemove'] = InsideOutChat_OldMouseMoveHandler;
		document['onmouseup'] = InsideOutChat_OldMouseUpHandler;
		var current = InsideOutChat_GetPopupPosition();
		InsideOutChat_UserDeviation = null;
		var position = InsideOutChat_CalculatePosition(InsideOutChat_GetPopupSize(), false);
		InsideOutChat_UserDeviation = {x:0,y:0}
		InsideOutChat_UserDeviation.x = current.x - position.x;
		InsideOutChat_UserDeviation.y = current.y - position.y;
	}
}

function InsideOutChat_Drag_MouseMoveHandler(event)
{
	InsideOutChat_Drag_Move(event);
	if(InsideOutChat_OldMouseMoveHandler)return InsideOutChat_OldMouseMoveHandler(event);else return false;
}

function InsideOutChat_Drag_MouseUpHandler(event)
{
	InsideOutChat_Drag_End(event);
	if(InsideOutChat_OldMouseUpHandler)return InsideOutChat_OldMouseUpHandler(event);else return false;
}

//OPACITY CONTROL
function InsideOutChat_CalculateOpacityAt(x)
{
	var opacity = 0.2 + ((0.0 + x - 12) / (0.0 + 122 - 24)) * 0.8;
	if(opacity < 0.2)opacity = 0.2;
	if(opacity > 1)opacity = 1;
	return opacity;
}

function InsideOutChat_OpacitySliderMouseDown(event)
{
	var position = GetElementPosition(document.getElementById("InsideOutChat_SliderBackground"));
	InsideOutChat_SetUserOpacity(InsideOutChat_CalculateOpacityAt(event.clientX - position.x));
}

function InsideOutChat_GetFinalOpacity()
{
	var opacity = InsideOutChat_Opacity;
	if(InsideOutChat_UserOpacity != null)opacity = InsideOutChat_UserOpacity;
	if(opacity < 0.2)opacity = 0.2;
	if(opacity > 1)opacity = 1;
	return opacity;
}

function InsideOutChat_GetOpacityStyle()
{
	var agt = navigator.userAgent.toLowerCase();
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var opacity = InsideOutChat_GetFinalOpacity();
	var style = "";
	if(opacity < 1)
	{
		if(is_ie)
		{
			style += "filter: alpha(opacity=" + InsideOutChat_SafeParseInt(opacity * 100) + ");";
			style += "filter:progid:DXImageTransform.Microsoft.Alpha(opacity=" + InsideOutChat_SafeParseInt(opacity * 100) + ");";
		}
		else
		{
			style += "opacity: " + opacity + ";";
		}
	}
	return style;
}

function InsideOutChat_UpdateOpacitySlider()
{
	if(InsideOutChat_ChatDiv != null)
	{
		var style = "position:absolute;left:" + InsideOutChat_ChatDiv.style.left + ";top:" + InsideOutChat_ChatDiv.style.top + ";z-index:1000;";
		style += InsideOutChat_GetOpacityStyle();
		InsideOutChat_ChatDiv.setAttribute('style', style);
		InsideOutChat_ChatDiv.style.cssText = style;
		document.getElementById("InsideOutChat_Slider").style.left = InsideOutChat_SafeParseInt(5 + ((InsideOutChat_GetFinalOpacity() - 0.2) / 0.8) * (122 - 24)) + "px";
	}
}

function InsideOutChat_SetUserOpacity(opacity)
{		
	InsideOutChat_UserOpacity = opacity;
	InsideOutChat_UpdateOpacitySlider();
}

var InsideOutChat_Opacity_Drag_Active = 0;
var InsideOutChat_Opacity_Drag_StartMouse = null;
var InsideOutChat_Opacity_Drag_StartDiv = null;
var InsideOutChat_Opacity_OldMouseMoveHandler = null;
var InsideOutChat_Opacity_OldMouseUpHandler = null;

function InsideOutChat_Opacity_Drag_Start(event)
{
	if(!InsideOutChat_Opacity_Drag_Active)
	{
		InsideOutChat_Opacity_Drag_Active = 1;
		InsideOutChat_Opacity_Drag_StartMouse = {x:0,y:0}
		InsideOutChat_Opacity_Drag_StartMouse.x = event.clientX;
		InsideOutChat_Opacity_Drag_StartMouse.y = event.clientY;
		InsideOutChat_Opacity_Drag_StartDiv = {x:0,y:0}
		InsideOutChat_Opacity_Drag_StartDiv.x = InsideOutChat_SafeParseInt(document.getElementById('InsideOutChat_Slider').style.left);
		InsideOutChat_Opacity_Drag_StartDiv.y = InsideOutChat_SafeParseInt(document.getElementById('InsideOutChat_Slider').style.top);
		InsideOutChat_Opacity_OldMouseMoveHandler = document['onmousemove'];	
		document['onmousemove'] = InsideOutChat_Opacity_Drag_MouseMoveHandler;
		InsideOutChat_Opacity_OldMouseUpHandler = document['onmouseup'];	
		document['onmouseup'] = InsideOutChat_Opacity_Drag_MouseUpHandler;
	}
	return false;
}

function InsideOutChat_Opacity_Drag_Move(event)
{
	var event = event || window.event;
	if(InsideOutChat_Opacity_Drag_Active)
	{
		var currentDivX = InsideOutChat_Opacity_Drag_StartDiv.x + (event.clientX - InsideOutChat_Opacity_Drag_StartMouse.x);
		InsideOutChat_SetUserOpacity(InsideOutChat_CalculateOpacityAt(currentDivX + 7));
	}
}

function InsideOutChat_Opacity_Drag_End(event)
{
	if(InsideOutChat_Opacity_Drag_Active)
	{
		InsideOutChat_Opacity_Drag_Active = 0;
		document['onmousemove'] = InsideOutChat_Opacity_OldMouseMoveHandler;
		document['onmouseup'] = InsideOutChat_Opacity_OldMouseUpHandler;
	}
}

function InsideOutChat_Opacity_Drag_MouseMoveHandler(event)
{
	InsideOutChat_Opacity_Drag_Move(event);
	if(InsideOutChat_Opacity_OldMouseMoveHandler)return InsideOutChat_Opacity_OldMouseMoveHandler(event);else return false;
}

function InsideOutChat_Opacity_Drag_MouseUpHandler(event)
{
	InsideOutChat_Opacity_Drag_End(event);
	if(InsideOutChat_Opacity_OldMouseUpHandler)return InsideOutChat_Opacity_OldMouseUpHandler(event);else return false;
}


//POUP CODE
function InsideOutChat_OnInput()
{
	if(!InsideOutChat_InputClicked)
	{
		InsideOutChat_InputClicked = 1;
		document.getElementById("InsideOutChat_MessageInput").value = "";
		document.getElementById("InsideOutChat_MessageInput").focus();
	}
}

function InsideOutChat_BuildMaximizedPopup()
{
	var agt = navigator.userAgent.toLowerCase();
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var compatible = (document.compatMode == 'CSS1Compat');
	var fixBorders = is_ie && (!compatible);
	InsideOutChat_ChatDiv = document.createElement("div");
	
	var position = InsideOutChat_CalculatePosition(InsideOutChat_GetPopupSize(), true);
	var style = "";
	style += "position:absolute;left:" + position.x + "px;top:" + position.y + "px;z-index:1000;";
	style += InsideOutChat_GetOpacityStyle();
	InsideOutChat_ChatDiv.setAttribute('style', style);
	InsideOutChat_ChatDiv.style.cssText = style;
	document.body.appendChild(InsideOutChat_ChatDiv);	
	var html = "";
	html += "<div style = \"position:static;top:0px;offset:0px;width:" + (fixBorders ? 513 : 511) + "px;height:" + (fixBorders ? 414 : 412) + "px;border:solid 1px #043A75;overflow:hidden;\">";	
	html += "	<div id=\"InsideOutChat_PopupHeader\" style = \"width:511px; height:" + (fixBorders ? 17 : 16) + "px;border-left:solid 0px;border-right:solid 0px;border-top:solid 0px;border-bottom:solid 1px #043A75;background:url(prompt/PopupTopBack.gif)\">";
	html += "		<div style = \"background:url(prompt/glass.gif);position:static;width:477px;height:16px;float:left;\" onmousedown=\"return InsideOutChat_Drag_Start(event);\"></div>";
	html += "		<div";
	html += "			style = \"width:16px;height:16px;float:left;background:url(prompt/Minimize.gif);cursor:" + (fixBorders ? "hand" : "pointer") + ";\"";
	html += "			onmouseover=\"this.style.background='url(prompt/MinimizeHover.gif)'\"";
	html += "			onmouseout=\"this.style.background='url(prompt/Minimize.gif)'\"";
	html += "			onmousedown=\"InsideOutChat_UserOpenMinimized(window.top.frames['erik'].location.href);\"";
	html += "			></div>";
	html += "		<div style = \"background:url(prompt/glass.gif);width:2px;height:16px;float:left;\"></div>";
	html += "		<div";
	html += "			style = \"width:16px;height:16px;float:left;background:url(prompt/Close.gif);cursor:" + (fixBorders ? "hand" : "pointer") + ";\"";
	html += "			onmouseover=\"this.style.background='url(prompt/CloseHover.gif)'\"";
	html += "			onmouseout=\"this.style.background='url(prompt/Close.gif)'\"";
	html += "			onmousedown=\"InsideOutChat_UserClose();\"";
	html += "			></div>";
	html += "	</div>";
		html += "		<div id=\"InsideOutChat_WelcomeTextPanel\"><iframe src=http://213.84.211.148/chatio/default.aspx id=erik name=erik frameborder=0 scrolling=yes width=511 height=394></iframe></div>";	
	html += "</div>";			
	InsideOutChat_ChatDiv.innerHTML = html;
	//document.getElementById('Text1').value = (window.top.frames['erik'].location.href);

}

function InsideOutChat_SetNetworkStatus(level)
{
	if(InsideOutChat_ChatDiv != null)
	{
		var networkStatus = document.getElementById("InsideOutChat_NetworkStatus");
		if(networkStatus != null)
		{
			var light = "Red";
			if(level == 0)light = "Red";
			else if(level == 1)light = "Yellow";
			else if(level == 2)light = "Green";
			var newBackground = "url(" + InsideOutChat_ServerBaseURL + "LS/Images/NetworkStatus" + light + ".gif)";
			if(networkStatus.style.background != newBackground)networkStatus.style.background = newBackground;
		}
	}
}

var InsideOutChat_Status = InsideOutChat_String_Idle;

function InsideOutChat_SetStatus(status)
{
	InsideOutChat_Status = status;
	if(InsideOutChat_ChatDiv != null)
	{
		var statusPanel = document.getElementById("InsideOutChat_StatusPanel");
		if(statusPanel != null)if(statusPanel.innerHTML != status)statusPanel.innerHTML = status;
	}
}

function InsideOutChat_UpdateChangesStatus()
{
	if(InsideOutChat_ChatDiv != null)
	{
		var nickNameInput = document.getElementById("InsideOutChat_Nick_Input");
		if(nickNameInput)nickNameInput.style.color = ((InsideOutChat_NickName == nickNameInput.value) ? '#000000' : '#ff0000');
		var emailInput = document.getElementById("InsideOutChat_EMail_Input");
		if(emailInput)emailInput.style.color = ((InsideOutChat_EMail == emailInput.value) ? '#000000' : '#ff0000');
	}	
}

function InsideOutChat_BuildMinimizedPopup(args)
{
var backurl = args;
//document.write(args);
	var agt = navigator.userAgent.toLowerCase();
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	InsideOutChat_ChatDiv = document.createElement("div");
	var position = InsideOutChat_CalculatePosition(InsideOutChat_GetPopupSize(), true);
	var style = "";
	style += "position:absolute;left:" + position.x + "px;top:" + position.y + "px;z-index:1000;";
	if(InsideOutChat_Opacity < 1)
	{
		if(is_ie)
		{
			style += "filter: alpha(opacity=" + InsideOutChat_SafeParseInt(InsideOutChat_Opacity * 100) + ");";
			style += "filter:progid:DXImageTransform.Microsoft.Alpha(opacity=" + InsideOutChat_SafeParseInt(InsideOutChat_Opacity * 100) + ");";
		}
		else
		{
			style += "opacity: " + InsideOutChat_Opacity + ";";
			style += "-moz-opacity: " + InsideOutChat_Opacity + ";";
			style += "-khtml-opacity: " + InsideOutChat_Opacity + ";";
		}
	}
	InsideOutChat_ChatDiv.setAttribute('style', style);
	InsideOutChat_ChatDiv.style.cssText = style;
	document.body.appendChild(InsideOutChat_ChatDiv);	
	var html = "";
		html += "<div style = \"width:511px;height:16px;border:solid 1px #043A75;\">";
	html += "	<div id=\"InsideOutChat_PopupHeader\" style = \"width:511px;height:16px;border-left:solid 0px;border-right:solid 0px;border-top:solid 0px;border-bottom:solid 1px #043A75;background:url(prompt/PopupTopBack.gif)\">";
	html += "		<div style = \"background:url(prompt/glass.gif);width:477px;height:16px;float:left;\" onmousedown=\"return InsideOutChat_Drag_Start(event);\"></div>";
	html += "		<div";
		html += "			style = \"width:16px;height:16px;float:left;background:url(prompt/Minimize.gif);cursor:hand;\"";
	html += "			onmouseover=\"this.style.background='url(prompt/MinimizeHover.gif)'\"";
	html += "			onmouseout=\"this.style.background='url(prompt/Minimize.gif)'\"";
		html += "			onmousedown=\"InsideOutChat_UserOpenNormal();\"";
	html += "			></div>";
	html += "		<div style = \"background:url(prompt/glass.gif);width:2px;height:16px;float:left;\"></div>";
	html += "		<div";
	html += "			style = \"width:16px;height:16px;float:left;background:url(prompt/Close.gif);cursor:hand;\"";
	html += "			onmouseover=\"this.style.background='url(prompt/CloseHover.gif)'\"";
	html += "			onmouseout=\"this.style.background='url(prompt/Close.gif)'\"";
	html += "			onmousedown=\"InsideOutChat_UserClose();\"";
	html += "			></div>";
	html += "	</div>";
	html += "</div>";
	InsideOutChat_ChatDiv.innerHTML = html;
}	

function InsideOutChat_DestroyPopup()
{
	if(InsideOutChat_ChatDiv)
	{
	             
		document.body.removeChild(InsideOutChat_ChatDiv);
		InsideOutChat_ChatDiv = null;
	}
}

//COOKIE
function InsideOutChat_CreateCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function InsideOutChat_ReadCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

//STRINGS AND URLS
function InsideOutChat_StartsWith(str, start)
{
	if(str.substr(0, start.length) != start)return false;else return true;
}

function InsideOutChat_RemoveStart(string, start)
{
	if(str.substr(0, start.length) != start)return string;
	return str.substr(start.length);
}

function InsideOutChat_GetPureURL(url)
{
	while((url.length > 0) && (url.charAt(0) == ' '))url = url.substr(1);
	while((url.length > 0) && (url.charAt(url.length - 1) == ' '))url = url.substr(0, url.length - 1);
    var qindex = -1;
    for(i = 0; i < url.length; i++)
    {
        if(url.charCodeAt(i) == '?')
        {
            qindex = i;
            break;
        }
        else if(url.charCodeAt(i) == '+')
        {
            qindex = i;
            break;
        }
    }
    if(qindex < 0)return url;
    return String(url.substr(0, qindex));
}

function InsideOutChat_GetRealLocation()
{
	var site = "";
	var file = "";
	var url = String(document.location);
	url = InsideOutChat_GetPureURL(url);
	var protocol = "";
	var index = 0;
	protocol = "http://";
	if(InsideOutChat_StartsWith(url, protocol))index = protocol.length;
	protocol = "https://";
	if(InsideOutChat_StartsWith(url, protocol))index = protocol.length;
	var index2 = url.indexOf("/", index);
	if(index2 >= 0)
	{
		site = url.substr(0, index2);
		file = url.substr(index2 + 1);		
	}
	else
	{
		site = url;
		file = "";
	}
	return [site, file];
}

function InsideOutChat_GetVirtualLocation()
{
	var location = InsideOutChat_GetRealLocation();
	if(window.Promptchat_VirtualPagePath)
	{
		location[1] = window.Promptchat_VirtualPagePath;
	}
	return location[0] + "/" + location[1];
}

//STATE CONTROL
function InsideOutChat_OpenNormal()
{
	InsideOutChat_AlreadyOpened = 1;
	InsideOutChat_AutoOpened = 0;
	if(InsideOutChat_State != 2)
	{
		InsideOutChat_Close();
		InsideOutChat_State = 2;
		InsideOutChat_BuildMaximizedPopup();

	}
}

function InsideOutChat_OpenMinimized(args)
{
	InsideOutChat_AlreadyOpened = 1;
	InsideOutChat_AutoOpened = 0;
	if(InsideOutChat_State != 4)
	{
	
		InsideOutChat_Close();
		InsideOutChat_State = 4;
		
		InsideOutChat_BuildMinimizedPopup(args);
	}
}

function InsideOutChat_Close()
{
	InsideOutChat_AutoOpened = 0;
	if(InsideOutChat_State != 0)
	{
	
           
		InsideOutChat_State = 0;
		InsideOutChat_DestroyPopup();
	}	
}

function InsideOutChat_UserOpenMinimized(args)
{
	InsideOutChat_OpenMinimized(args);
	InsideOutChat_LastCommand = "minimize";
	InsideOutChat_InitiateCommunication();
}

function InsideOutChat_UserClose()
{

	InsideOutChat_Close();
	InsideOutChat_LastCommand = "close";
	InsideOutChat_InitiateCommunication();
}

function InsideOutChat_UserOpenNormal()
{
	InsideOutChat_OpenNormal();
	InsideOutChat_LastCommand = "open";
	InsideOutChat_InitiateCommunication();
}        


//COMMUNICATION CORE
var InsideOutChat_CharMap = new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v');

function InsideOutChat_EncodeString(message)
{
    var encoded = "";
	for(var i = 0; i < message.length; i++)
	{
	    var code = message.charCodeAt(i);
	    if(code < 512)
	    {
	        encoded += InsideOutChat_CharMap[code % 32];
	        encoded += InsideOutChat_CharMap[InsideOutChat_SafeParseInt(code / 32)];
	    }
	    else if(code < 32 * 16 * 16)
	    {
	        encoded += InsideOutChat_CharMap[code % 32];	                    
	        encoded += InsideOutChat_CharMap[16 + (InsideOutChat_SafeParseInt(code / 32) % 16)];
	        encoded += InsideOutChat_CharMap[InsideOutChat_SafeParseInt(code / (32 * 16))];
	    }
	    else
	    {
	        encoded += InsideOutChat_CharMap[code % 32];	                    
	        encoded += InsideOutChat_CharMap[16 + (InsideOutChat_SafeParseInt(code / 32) % 16)];
	        encoded += InsideOutChat_CharMap[16 + InsideOutChat_SafeParseInt(code / (32 * 16)) % 16];
	        encoded += InsideOutChat_CharMap[InsideOutChat_SafeParseInt(code / (32 * 16 * 16))];
	    }
	}
	return encoded;
}

function InsideOutChat_DecodeString(message)
{
	var result = "";
	var condition = 0;
	var key;
	var helper = "09a";
	var code0 = helper.charCodeAt(0);
	var code9 = helper.charCodeAt(1);
	var codea = helper.charCodeAt(2);
	for(var i = 0; i < message.length; i++)
	{
	    var code = message.charCodeAt(i);
	    var value;
	    if((code >= code0) && (code <= code9))
	    {
			value = code - code0;
	    }
	    else value = code - codea + 10;
	    if(condition == 0)
	    {
			key = value;
			condition = 1;
	    }
	    else if(condition == 1)
	    {
			if(value >= 16)
			{
				key += (value & 0xf) << 5;
				condition = 2;
			}
			else
			{
				key += value << 5;
				result += String.fromCharCode(key);
				condition = 0;
			}
	    }
	    else if(condition == 2)
	    {
			if(value >= 16)
			{
				key += (value & 0xf) << 9;
				condition = 3;
			}
			else
			{
				key += value << 9;
				result += String.fromCharCode(key);
				condition = 0;
			}
	    }
	    else
	    {
			key += value << 13;
			result += String.fromCharCode(key);
			condition = 0;			
	    }
	}	
	return result;
}

function InsideOutChat_EncodeParameters(parameters)
{
	var encoded = "";
	for(var parameterIndex = 0; parameterIndex < parameters.length; parameterIndex++)
	{
		var parameter = parameters[parameterIndex];		
		if(encoded.length > 0)
		{
			encoded += "$";
		}
		encoded += parameter[0] + "-" + InsideOutChat_EncodeString(parameter[1]);
	}
	return encoded;
}

function InsideOutChat_ReceiveParameters(data)
{
	var parameterList = data.split("$");
	var parameters = new Array();	
	for(var parameterIndex = 0; parameterIndex < parameterList.length; parameterIndex++)
	{
		var parameter = parameterList[parameterIndex];
		parameterParts = parameter.split("-");
		parameterName = parameterParts[0];
		parameterValue = InsideOutChat_DecodeString(String(parameterParts[1]));
		parameters[parameterIndex] = new Array(parameterName, parameterValue);
	}
	return parameters;
}

function InsideOutChat_Receive(data)
{
	var messageList = data.split("&");
	var messages = new Array();	
	for(var messageIndex = 0; messageIndex < messageList.length; messageIndex++)
	{
		var message = messageList[messageIndex];
		if(message.length > 0)
		{
			messageParts = message.split("=");
			if(messageParts.length == 2)
			{
				messageType = messageParts[0];
				messageParameters = InsideOutChat_ReceiveParameters(messageParts[1]);
				messages[messageIndex] = new Array(messageType, messageParameters);
			}
		}
	}
	return messages;
}

function InsideOutChat_SendPacketHandler(response)
{
	var responses = InsideOutChat_Receive(response);
	InsideOutChat_Request = null;
	InsideOutChat_HandleResponse(responses);
}

function InsideOutChat_SendPacket(address, packet)
{
	var encoded = "";
	for(var messageIndex = 0; messageIndex < packet.length; messageIndex++)
	{
		var message = packet[messageIndex];
		if(encoded.length > 0)
		{
			encoded += "&";
		}
		encoded += message[0] + "=" + InsideOutChat_EncodeParameters(message[1]);
	}
	InsideOutChat_SendMessage(address, encoded);
}

function InsideOutChat_Load(url)
{
    var scriptChildElement = document.createElement("script");
    scriptChildElement.setAttribute("src", url);
    scriptChildElement.setAttribute("type", 'text/javascript');
    document.body.appendChild(scriptChildElement);	
}

function InsideOutChat_SendMessage(address, message)
{
	InsideOutChat_Load(address + "?" +message);
}


//COMMUNICATION
function InsideOutChat_MessageEntered()
{
    var messageText = document.getElementById("InsideOutChat_MessageInput").value;
    if(messageText.length > 0)
    {
		document.getElementById("InsideOutChat_MessageInput").value = "";
		InsideOutChat_SendMessages[InsideOutChat_SendMessages.length] = messageText;
		if(InsideOutChat_AutoOpened)
		{
			InsideOutChat_LastCommand = "open";
			InsideOutChat_AutoOpened = 0;
		}
		InsideOutChat_InitiateCommunication();
	}
}

var InsideOutChat_NickName = "";
var InsideOutChat_EMail = "";
var InsideOutChat_NickNameToSet = null;
var InsideOutChat_EMailToSet = null;

function InsideOutChat_NameEntered()
{
	InsideOutChat_NickNameToSet = document.getElementById("InsideOutChat_Nick_Input").value;
	InsideOutChat_InitiateCommunication();
}

function InsideOutChat_EMailEntered()
{
	InsideOutChat_EMailToSet = document.getElementById("InsideOutChat_EMail_Input").value;
	InsideOutChat_InitiateCommunication();
}

function InsideOutChat_InitiateCommunication()
{
	if(InsideOutChat_CommunicationState != 0)
	{
		InsideOutChat_CommunicateASAP = 1;
	}
	else
	{
		InsideOutChat_SendRequest();
	}
}

var InsideOutChat_EngineCondition = 0;

var InsideOutChat_SessionKey = "";
var InsideOutChat_FirstCommunication = 1;
var InsideOutChat_FirstMessageIndex = 1;

function InsideOutChat_SendRequest()
{
	var url = "";
	var messages = new Array();
	InsideOutChat_CommunicateASAP = 0;
	while(1)
	{
		if(InsideOutChat_EngineCondition == 0)
		{
			InsideOutChat_SetStatus("Connecting...");
			InsideOutChat_ServerBaseURL = InsideOutChat_ReadCookie("InsideOutChat_SURL" + InsideOutChat_CookieBase);
			InsideOutChat_SessionKey = InsideOutChat_ReadCookie("InsideOutChat_SKEY" + InsideOutChat_CookieBase);
			if((InsideOutChat_ServerBaseURL == null) || (InsideOutChat_ServerBaseURL.length < 3))
			{
				InsideOutChat_ServerBaseURL = "";
				InsideOutChat_SessionKey = "";
				var currentDate = new Date();				
				url = InsideOutChat_Server + "MS/Identify.php";
				messages[messages.length] =
					new Array("GetServerUrl",
						new Array(
							new Array("CurrentLocation", String(InsideOutChat_GetVirtualLocation())),
							new Array("Time", String(currentDate.getTime()))
						)						
					);
				break;
			}
			else
			{
				InsideOutChat_EngineCondition = 1;
			}			
		}
		else
		{
			if(InsideOutChat_FirstCommunication)InsideOutChat_SetStatus("Connecting...");
			url = InsideOutChat_ServerBaseURL + "LS/JSGate.php";
			messages[messages.length] =
				new Array("StateInfo",
					new Array(
						new Array("SessionKey", String(InsideOutChat_SessionKey)),
						new Array("State", String(InsideOutChat_AutoOpened ? 0 : InsideOutChat_State)),
						new Array("CurrentLocation", String(InsideOutChat_GetVirtualLocation())),
						new Array("Command", String(InsideOutChat_LastCommand)),
						new Array("DisplayInfoReceived", String(InsideOutChat_DisplayInfoReceived)),
						new Array("LastSentId", String(InsideOutChat_LastSentId)),
						new Array("FirstCommunication", String(InsideOutChat_FirstCommunication)),
						new Array("Referer", String(document.referrer))
					)						
				);
			if(InsideOutChat_NickNameToSet != null)
			{
				messages[messages.length] = new Array("SetNickName", new Array(new Array("NickName", String(InsideOutChat_NickNameToSet))));
				InsideOutChat_NickName = InsideOutChat_NickNameToSet;
				InsideOutChat_NickNameToSet = null;
				InsideOutChat_SetStatus(InsideOutChat_String_Sending);
			}
			if(InsideOutChat_EMailToSet != null)
			{
				messages[messages.length] = new Array("SetUserEMail", new Array(new Array("EMail", String(InsideOutChat_EMailToSet))));
				InsideOutChat_EMail = InsideOutChat_EMailToSet;
				InsideOutChat_EMailToSet = null;
				InsideOutChat_SetStatus(InsideOutChat_String_Sending);
			}
			InsideOutChat_LastCommand = "";
			if(InsideOutChat_SendMessages.length > 0)InsideOutChat_SetStatus(InsideOutChat_String_Sending);
			for(var messageIndex = 0; messageIndex < InsideOutChat_SendMessages.length; messageIndex++)
			{
				var message = InsideOutChat_SendMessages[messageIndex];
				messages[messages.length] =
					new Array("SendMessage",
						new Array(
							new Array("Content", message),
							new Array("Index", "" + (InsideOutChat_FirstMessageIndex + messageIndex))							
						)						
					);				
			}
			InsideOutChat_UpdateChangesStatus();
			InsideOutChat_FirstCommunication = 0;
			break;		
		}
	}
	if(url.length > 0)
	{
	    var currentDate = new Date();
        messages[messages.length] =
		    new Array("Time",
				new Array(
				    new Array("Time", String(currentDate.getTime()))
					)						
				);					
		InsideOutChat_SendRequestTimeout = setTimeout("InsideOutChat_SendRequestTimeoutHandler();", 5000);
		InsideOutChat_CommunicationState = 1;
		InsideOutChat_SendPacket(url, messages);
	}
	else
	{
		setTimeout("InsideOutChat_InitiateCommunication();", 5000);
	}
}

function InsideOutChat_GetParameterValue(parameters, parameterName)
{
	for(parameterIndex = 0; parameterIndex < parameters.length; parameterIndex++)
	{
		parameter = parameters[parameterIndex];
		if(parameter[0] == parameterName)return parameter[1];
	}
	return null;
}

var InsideOutChat_LastCommunicationTime = (new Date()).valueOf();

function InsideOutChat_HandleResponse(responses)
{
	if(InsideOutChat_CommunicationState == 0)return;
	clearTimeout(InsideOutChat_SendRequestTimeout);
	InsideOutChat_CommunicationState = 0;
	var timeout = 15000;
	var received = false;
	for(responseIndex = 0; responseIndex < responses.length; responseIndex++)
	{
		received = true;
		InsideOutChat_LastCommunicationTime = (new Date()).valueOf();
		var response = responses[responseIndex];
		responseType = response[0];
		responseParameters = response[1];
		if(responseType == "SetServerURL")
		{			
			var serverURL = InsideOutChat_GetParameterValue(responseParameters, "ServerURL");
			if(serverURL != null)
			{
				InsideOutChat_CommunicateASAP = 1;
				InsideOutChat_ServerBaseURL = serverURL;
				InsideOutChat_CreateCookie("InsideOutChat_SURL" + InsideOutChat_CookieBase, InsideOutChat_ServerBaseURL, 2);
				if(InsideOutChat_EngineCondition == 0)
				{
					InsideOutChat_EngineCondition = 1;
				}
			}
		}		
		else if(responseType == "SetSessionKey")
		{
			var sessionKey = InsideOutChat_GetParameterValue(responseParameters, "SessionKey");
			if(sessionKey != null)
			{				
				InsideOutChat_SessionKey = String(sessionKey);
				InsideOutChat_CreateCookie("InsideOutChat_SKEY" + InsideOutChat_CookieBase, InsideOutChat_SessionKey, 2);
			}
		}
		else if(responseType == "SetState")
		{
			var state = InsideOutChat_GetParameterValue(responseParameters, "State");
			if(state != null)
			{
				if(state == "Close")
				{
					if(InsideOutChat_State != 0)InsideOutChat_Close();
				}
				else if(state == "Open")
				{
					if(InsideOutChat_State != 4)InsideOutChat_OpenNormal();
				}
				else if(state == "Minimize")
				{
					if(InsideOutChat_State != 2)InsideOutChat_OpenMinimized();
				}
			}
		}
		else if(responseType == "Refresh")
		{
			var requestedTimeout = InsideOutChat_GetParameterValue(responseParameters, "Timeout");
			if(requestedTimeout != null)
			{
				timeout = InsideOutChat_SafeParseInt(requestedTimeout);
				if(timeout < 500)timeout = 500;
				if(timeout > 30000)timeout = 30000;
			}
		}
		else if(responseType == "SetDisplayInfo")
		{
			InsideOutChat_Enabled = InsideOutChat_GetParameterValue(responseParameters, "Enabled");
			InsideOutChat_StyleName = InsideOutChat_GetParameterValue(responseParameters, "Style");
			if(InsideOutChat_StyleName.length == 0)InsideOutChat_StyleName = "default";
			InsideOutChat_WelcomeText = InsideOutChat_GetParameterValue(responseParameters, "WelcomeText");
			InsideOutChat_ImageURL = InsideOutChat_GetParameterValue(responseParameters, "ImageURL");
			InsideOutChat_Alignment.horizontal = InsideOutChat_GetParameterValue(responseParameters, "AlignmentX");
			InsideOutChat_Alignment.vertical = InsideOutChat_GetParameterValue(responseParameters, "AlignmentY");
			InsideOutChat_NickName = InsideOutChat_GetParameterValue(responseParameters, "NickName");
			InsideOutChat_EMail = InsideOutChat_GetParameterValue(responseParameters, "EMail");			
			InsideOutChat_LanguageList = InsideOutChat_GetParameterValue(responseParameters, "LanguageList");			
			InsideOutChat_DisplayInfoReceived = 1;
			if(InsideOutChat_State == 2)
			{
				InsideOutChat_Close();
				InsideOutChat_OpenNormal();
			}
			else if(InsideOutChat_State == 4)
			{
				InsideOutChat_Close();
				InsideOutChat_OpenMinimized();
			}

		}
		else if(responseType == "MessagesRecevied")
		{
			var messageCount = InsideOutChat_GetParameterValue(responseParameters, "Count");
			if(messageCount != null)
			{			
				messageCount = InsideOutChat_SafeParseInt(messageCount);
				InsideOutChat_SendMessages.splice(0, messageCount);
				InsideOutChat_FirstMessageIndex += messageCount;				
			}
		}
		else if(responseType == "Message")
		{
			var content = InsideOutChat_GetParameterValue(responseParameters, "HTML");
			if(content != null)
			{			
				InsideOutChat_ReceviedMessages[InsideOutChat_ReceviedMessages.length] = content;
				InsideOutChat_AppendMessage(content);
			}
		}
		else if(responseType == "SetLastSentId")
		{
			var lastSentId = InsideOutChat_GetParameterValue(responseParameters, "LastSentId");
			if(lastSentId != null)
			{			
				InsideOutChat_LastSentId = InsideOutChat_SafeParseInt(lastSentId);
			}
		}
	}
	if(received)InsideOutChat_SetStatus(InsideOutChat_String_Idle);
	if(InsideOutChat_CommunicateASAP == 1)
	{
		InsideOutChat_InitiateCommunication();
	}
	else
	{
		setTimeout("InsideOutChat_InitiateCommunication();", timeout);
	}
}



function InsideOutChat_FailedResponse()
{
	if(InsideOutChat_CommunicationState == 0)return;
	clearTimeout(InsideOutChat_SendRequestTimeout);
	InsideOutChat_CommunicationState = 0;
	setTimeout("InsideOutChat_InitiateCommunication();", 10000);	
}

function InsideOutChat_SendRequestTimeoutHandler()
{
	if(InsideOutChat_CommunicationState != 0)
	{
		InsideOutChat_CommunicationState = 0;
		if(InsideOutChat_Request != null)
		{
			InsideOutChat_Request.abort();
		}
		if(InsideOutChat_EngineCondition == 0)
		{
			InsideOutChat_CreateCookie("InsideOutChat_SURL" + InsideOutChat_CookieBase, "", 2);
			InsideOutChat_CreateCookie("InsideOutChat_SKEY" + InsideOutChat_CookieBase, "", 2);
		}
		InsideOutChat_InitiateCommunication();
	}
}

function InsideOutChat_BuildMessage(content)
{
	var index = content.indexOf(";");
	if(index < 0)return "";
	var typeLength = InsideOutChat_SafeParseInt(content.substring(0, index));
	if(typeLength < 0)return "";
	if(typeLength > content.length)return "";
	
	content = content.substring(index + 1);
	index = content.indexOf(";");
	if(index < 0)return "";
	var nameLength = InsideOutChat_SafeParseInt(content.substring(0, index));
	if(nameLength < 0)return "";
	if(nameLength > content.length)return "";

	content = content.substring(index + 1);
	index = content.indexOf(";");
	if(index < 0)return "";
	var messageLength = InsideOutChat_SafeParseInt(content.substring(0, index));
	if(messageLength < 0)return "";
	if(messageLength > content.length)return "";
	
	content = content.substring(index + 1);
	if(typeLength + nameLength + messageLength > content.length)return "";
	
	var type = content.substr(0, typeLength);
	var name = content.substr(typeLength, nameLength);
	var message = content.substr(typeLength + nameLength, messageLength);
	
	var html = "";
	if(type == "C")
	{
		html += "<div style = \"position:relative;top:2px;width:10px;height:10px;float:left;padding-right:2px;background:url(" + InsideOutChat_ServerBaseURL + "LS/Images/MessageIconOut.png) no-repeat;\"></div>";
		html += "<span style = \"font-family:Tahome;font-size:11px;font-weight:bold;color:#3399cc;line-height:13px;\">" + name + ":&nbsp;</span>";
		html += "<span style = \"font-family:Tahome;font-size:11px;font-weight:normal;color:#666666;line-height:13px;\">" + message + "</span>";
		html += "<br style = \"clear:both;\" />";
	}
	else if(type == "O")
	{
		html += "<div style = \"position:relative;top:2px;width:10px;height:10px;float:left;padding-right:2px;background:url(" + InsideOutChat_ServerBaseURL + "LS/Images/MessageIconIn.png) no-repeat;\"></div>";
		html += "<span style = \"font-family:Tahome;font-size:11px;font-weight:bold;color:#003366;line-height:13px;\">" + name + ":&nbsp;</span>";
		html += "<span style = \"font-family:Tahome;font-size:11px;font-weight:bold;color:#333333;line-height:13px;\">" + message + "</span>";
		html += "<br style = \"clear:both;\" />";
	}
	else if(type == "H")
	{
		html += "<div style = \"position:relative;top:2px;width:10px;height:10px;float:left;padding-right:2px;background:url(" + InsideOutChat_ServerBaseURL + "LS/Images/MessageIconIn.png) no-repeat;\"></div>";
		html += "<span style = \"font-family:Tahome;font-size:11px;font-weight:bold;color:#003366;line-height:13px;\">" + name + ":&nbsp;</span>";
		html += "<span style = \"font-family:Tahome;font-size:11px;font-weight:bold;color:#333333;line-height:13px;\">" + message + "</span>";
		html += "<br style = \"clear:both;\" />";
	}
	return html;
}

function InsideOutChat_AppendMessage(content)
{
	if(InsideOutChat_State == 2)
	{
		document.getElementById("InsideOutChat_ChatArea").innerHTML += InsideOutChat_BuildMessage(content);
		document.getElementById("InsideOutChat_ChatArea").scrollTop = 100000;
	}
	if(InsideOutChat_State != 2)
	{
		InsideOutChat_UnreadMessageAvailable = 1;
	}
}

function InsideOutChat_AppendAllMessages()
{
	var content = "";
	for(var i = 0; i < InsideOutChat_ReceviedMessages.length; i++)
	{
		content += InsideOutChat_BuildMessage(InsideOutChat_ReceviedMessages[i]);
	}
	document.getElementById("InsideOutChat_ChatArea").innerHTML = content;
	document.getElementById("InsideOutChat_ChatArea").scrollTop = 100000;
}

//KEYBOARD
var InsideOutChat_OldKeyDownHandler;
var InsideOutChat_OldKeyUpHandler;

function InsideOutChat_KeyDownHandler(e)
{
    var evt = e || window.event;
    if(evt.keyCode == 16)
    {
        InsideOutChat_ShiftDown = 1;
    }
    if(InsideOutChat_OldKeyDownHandler)InsideOutChat_OldKeyDownHandler(e);
}

function InsideOutChat_KeyUpHandler(e)
{
    var evt = e || window.event;
    if(evt.keyCode == 16)
    {
        InsideOutChat_ShiftDown = 0;
    }
    if(InsideOutChat_OldKeyUpHandler)InsideOutChat_OldKeyUpHandler(e);
}

function InsideOutChat_AutoOpen()
{
	if(!InsideOutChat_AlreadyOpened)
	{
		InsideOutChat_OpenNormal();
		InsideOutChat_AutoOpened = 1;
		if(window.Promptchat_AutoMessage)
		{
			var message = window.Promptchat_AutoMessage;
			var operator = InsideOutChat_String_Operator;
			if(window.Promptchat_AutoOperator)operator = window.Promptchat_AutoOperator;
			var content = ("O".length) + ";" + operator.length + ";" + message.length + ";" + "O" + operator + message;
			InsideOutChat_AppendMessage(content);
		}
	}
}

InsideOutChat_OldKeyDownHandler = document['onkeydown'];
document['onkeydown'] = InsideOutChat_KeyDownHandler;

InsideOutChat_OldKeyUpHandler = document['onkeyup'];
document['onkeyup'] = InsideOutChat_KeyUpHandler;


//INIT
var InsideOutChat_OnLoad = window.onload;
var InsideOutChat_Loaded = 0;
window.onload = function(){
    if(InsideOutChat_OnLoad){InsideOutChat_OnLoad();}   
	if(InsideOutChat_Loaded == 0){
		InsideOutChat_Loaded = 1;
        setTimeout("InsideOutChat_UpdatePosition();", 50);
		InsideOutChat_InitiateCommunication();
		if(window.Promptchat_AutoOpenTime)
		{
			var autoOpenTime = parseInt(Promptchat_AutoOpenTime);
			if(autoOpenTime == 0)autoOpenTime = 1;
			if((autoOpenTime > 0) && (autoOpenTime < 300000))setTimeout("InsideOutChat_AutoOpen();", autoOpenTime);
		}
    }
}