var ICON_WIDTH=13;
var ICON_HEIGHT=16;

var autoLogin=false;
var ll_is=new browserInfo();
var ll_IDs=new Object();
var ll_linksCount=0;
var ll_linkTxt=new Object();
var ll_tip=new Object();
var ll_status=new Object();
var ll_desc=new Object();
var ll_options=new Object();
var ll_namesRes=";";
var ll_namesNoRes=";";
var llApplet,urlMain,urlPages,ns_appPath;
var STHost="";
var llAppletFound=false;
var sticn=new Object();
sticn[0]="offline";
sticn[0x20]="active";
sticn[0x40]="away";
sticn[0x60]="away";
sticn[0x80]="dnd";
sticn[0x220]="activeMobile";
sticn[0x260]="awayMobile";
var ll_statusTip;
var countPlaces=new Array();
var STLANGS="en,zh,th,sv,pt,no,nl,ko,ja,it,fr,fi,es,de,da,zh_tw,pl,ru";
var STDEF_LANG="en";
var ll_loggedIn=false;
//flag that indicates if the web page need to pass the reverse proxy for using the sametime 
//server - do not change this variable.
var isRProxy=false; 

//The following parameters need to be changed by the administrator
// in case of reverse proxy environment
//var ll_RProxyName="https://proxy.ibm.com";
//var ll_AffinityId="st1";

///////////   Automatic Away Code /////////////////////////////////////

var g_nInterval = 600;
var g_nIntervalCounter = 0;
var g_fIsFirstTime = false;
var g_strLoginName = "";
var g_strLastToolTip = null;
var g_nLastMouseX = -1;
var g_nLastMouseY = -1;
var g_fIsAAActive = true;


function
SetAutoawyInterval (nInterval) {
	g_nInterval = nInterval;
}


function ActiveAutoawy () {
	try {
		document.onmousemove = OnMouseMove;
		document.onkeypress = OnMouseMove;

		if (parent.frames.length != 0) {
			for (var i = 0; i < parent.frames.length; i++) {
				var nodeFrame = parent.frames[i];
				
				if (typeof nodeFrame.document != "unknown") {
					nodeFrame.document.onmousemove = OnMouseMove;
					nodeFrame.document.onkeypress = OnMouseMove;
				}

				for (var j = 0; j < nodeFrame.frames.length; j++) {
					var nodeChildFrame = nodeFrame.frames[j];
					
					if (typeof nodeChildFrame.document != "unknown") {
						nodeChildFrame.document.onmousemove = OnMouseMove;
						nodeChildFrame.document.onkeypress = OnMouseMove;
					}
				}
			}

			window.setTimeout (strFunction, 1);
		}
		else {
			window.setTimeout (strFunction, 1);
		}
	}
	catch (e) {
		//alert (e.message);
	}
}

function LoginNameFixUp (strName) {
	var arName = strName.split ("%s");
	return arName[1];
}

function GetIntervalCounter () {

	var nIntervalCounter = 0;
	try {
		nIntervalCounter = window.parent.opener.g_nIntervalCounter;
		
		if (typeof(nIntervalCounter) != "undefined") {
			g_nIntervalCounter = nIntervalCounter;
		}
		else {
			nIntervalCounter = g_nIntervalCounter;
		}
	}
	catch (e) {
		nIntervalCounter = g_nIntervalCounter;
	}

	return nIntervalCounter;
}

var strFunction = function() {
	try {
		var nStatus = STLinksMyStatus();

		if (GetIntervalCounter() > g_nInterval) {
			if (!g_fIsFirstTime && (nStatus != 128 && nStatus != 96)) {
				g_strLastToolTip = STLinksMyStatusMessage();
				STLinksSetMyStatus (64, LoginNameFixUp(TIP_AUTOAWAY));

				g_fIsFirstTime = true;
			}
		}
		else if (GetIntervalCounter() <= g_nInterval){			
			if (g_fIsFirstTime) {

				STLinksSetMyStatus(32, g_strLastToolTip);
				g_fIsFirstTime = false;
			}
		}
		g_nIntervalCounter++;
		window.setTimeout (strFunction, 1000);
	} 
	catch (e){}
};

function OnMouseMove (e) {	
	try {
		if (typeof (window.parent.opener.g_fIsAAActive) != "undefined") {
			if (window.parent.opener.g_fIsAAActive) {
				window.parent.opener.g_nIntervalCounter = 0;
			}
			else {
				window.parent.opener.g_fIsAAActive = true;
			}
		}
		else {
			if (g_fIsAAActive) {
				g_nIntervalCounter = 0;
			}
			else {
				g_fIsAAActive = true;
			}
		}
	} 
	catch (err) {
		try {
			var nLastMouseX = -1;
			var nLastMouseY = -1;
			var chLastChar = 0;
			
			if (ll_is.ie) {
				nLastMouseY = window.event.clientY;
				nLastMouseX = window.event.clientX;
				chLastChar = window.event.keyCode;
			}
			else {
				nLastMouseY = e.pageX;
				nLastMouseX = e.pageY;
				chLastChar = e.which;
			}

			var fIsMouse = false;

			if (
				nLastMouseX != g_nLastMouseX &&
				nLastMouseY != g_nLastMouseY &&
				g_nLastMouseX != -1 &&
				g_nLastMouseY != -1
				
			){
				g_nIntervalCounter = 0;
				fIsMouse = true;
			}
			
			if (
				chLastChar != 0 &&
				!fIsMouse
			){
				g_nIntervalCounter = 0;
			}

			g_nLastMouseX = nLastMouseX;
			g_nLastMouseY = nLastMouseY;
		}
		catch (e) {
			//alert ("error :: "+e.message);
		}
	}
}


function
SetPauseActivity (
){
	try {
		window.parent.opener.g_fIsAAActive = false;
	}
	catch (e) {
		g_fIsAAActive = false;
	}
}

///////////   Automatic Away Code /////////////////////////////////////

function browserInfo()
{
	var agent = navigator.userAgent.toLowerCase();
	this.major = parseInt(navigator.appVersion);
	this.ns = ((agent.indexOf('mozilla')!=-1) && ((agent.indexOf('spoofer')==-1) && (agent.indexOf('compatible') == -1)));
	this.ns6 = (agent.indexOf('netscape6')!=-1);
	this.ie = (agent.indexOf("msie") != -1);
	this.dom = ((agent.indexOf("msie") != -1) || (agent.indexOf("gecko")!=-1));
	this.win = (agent.indexOf("win")!=-1);
	this.nowin = (agent.indexOf("linux")!=-1 || agent.indexOf("sun")!=-1 || agent.indexOf("aix")!=-1);
}

var g_vecHEX = new Array();
g_vecHEX[1]="0"; g_vecHEX[2]="1"; g_vecHEX[3]="2"; g_vecHEX[4]="3"; 
g_vecHEX[5]="4"; g_vecHEX[6]="5"; g_vecHEX[7]="6"; g_vecHEX[8]="7"; 
g_vecHEX[9]="8"; g_vecHEX[10]="9"; g_vecHEX[11]="A"; g_vecHEX[12]="B"; 
g_vecHEX[13]="C"; g_vecHEX[14]="D"; g_vecHEX[15]="E"; g_vecHEX[16]="F";

function escapeUnicodeMessage (strMessage) 
{
	var strMSG = "";

	while (strMessage.match(/&#(\d+);/)) {
		var nCode = RegExp.$1;
		eval ("nCode=\"\\x" + convertToHex(nCode) + "\"");
		strMessage = strMessage.replace(/&#(\d+);/i, nCode);
	}

	return strMessage;
}

function convertToHex (strCode)
{
	var nHigh = (strCode / 16);
	var strTmp = nHigh + "";
	strTmp = strTmp.substring(0, 2);
	nHigh = parseInt(strTmp, 10);
	var nLeft = g_vecHEX[nHigh+1];
	var nLow = (strCode - nHigh * 16);
	strTmp = nLow + "";
	strTmp = strTmp.substring(0, 2);
	nLow = parseInt(strTmp, 10);
	var nRight = g_vecHEX[nLow + 1];
	var strHex = nLeft + "" + nRight;
	return strHex;
}

function unicodeEscape(s)
{
	var ret = "";
	for(var i = 0 ; i < s.length ; i++)
		ret += s.charCodeAt(i).toString(16) + ",";
	return ret.substr(0, ret.length-1);	
}

function fromUnicode(s)
{
	var uniChars = s.match(/&#(\d{2,4});/g);
	if (uniChars == null)
		return s;
	for (var i = 0; i < uniChars.length; i++) {
		var c = uniChars[i];
		var code = c.substring(2,c.length-1);
		s = s.replace(c,String.fromCharCode(code))
	}
	return s;
}

function llOption(options,name,defVal)
{
	var a = (options.replace(/ /g,'')).split(";");
	for (var i=0; i<a.length; i++) {
		var pair = a[i].split('\:');
		if (pair[0].toLowerCase() == name.toLowerCase())
			return pair[1];
	}
	return defVal;	
}

function writeSametimeLink(id, txt, bResolve, options)
{
	document.write(prepareSametimeLink(id,txt,bResolve,options));
}

function prepareSametimeLink(linkId, linkText, bResolve, options)
{
	linkId = fromUnicode(linkId);
	var s = linkId.replace(/;/g,";;") + ";";
	if (bResolve == false) {
		if (ll_namesNoRes.indexOf(";"+s)==-1)
			ll_namesNoRes += s;
		if (ll_loggedIn)
			watchNames("",s);
	} 
	else if (ll_namesRes.indexOf(";"+s)==-1) {
		ll_namesRes += s;
		if (ll_loggedIn)
			watchNames(s,"");
	}			
	return prepareSametimeLinkNW(linkId, linkText, options);
}

function updateLinkStatus(linkId, stat, desc)
{
	ll_status[linkId] = stat;
	ll_desc[linkId] = desc;
	for(var tagId in ll_IDs[linkId]) {
		if (document.getElementById(tagId) != null) {
			var uname = ll_linkTxt[tagId];
			updateLinkHtml(tagId,linkId,stat, STLinksStatusTip(uname, stat, desc));
		}
	}
}

function offlineAllLinks()
{
	for(var id in ll_IDs)
		updateLinkStatus(id, 0); 
}

function onSTLinkClicked(linkId,tagId,event)
{
	if (typeof STLinkClicked != "undefined")
		STLinkClicked(linkId, ll_linkTxt[tagId], ll_status[linkId], event);
	else llApplet.createIm(linkId);
}

function LinkStatus(styleName, isLink)
{
	this.styleName = styleName;
	this.isLink = isLink;
}

function getHREF(linkId, tagId, bOnline, bOfflineLink, bTip_NS)
{
	if (!bOnline && !bOfflineLink)
		return ""
	else {
		var act= "onSTLinkClicked('" + linkId.replace(/'/g, "\\'").replace(/"/g, "&quot;") + "','" + tagId +"',event)";
		var s =" href=\"javascript:" + act + "\" onclick=\"" + act + ";return false;\" oncontextmenu=\"return false\"";
		return s;
	}
}

function getLinkTag(linkId, linkText, tagId, options)
{
	var tip = STLinksStatusTip(linkText, ll_status[linkId], ll_desc[linkId]);
	return '<SPAN ID="' + tagId + '">' + getInnerTag_IE(linkId, tagId, ll_status[linkId], tip, options) + '</SPAN>';
}

function getImgTag(statusType, tip, tagId)
{
	return "<img" + 
		((typeof tagId != "undefined") ? " name=" + tagId : "")  + 
		" border=0 align=absmiddle alt='" + tip.replace(/'/g, "&#039;") + 
		'\' src="'+ getImgUrl(statusType) +'" width=' + ICON_WIDTH + ' height=' + ICON_HEIGHT +">";
}

function getImgUrl(statusType)
{
	return urlPages + "/../img/" +
		((typeof sticn[statusType] == "undefined") ? sticn[0] : sticn[statusType]) + ".gif";
}

function updateLinkHtml(tagId,linkId,statusType,tip)
{
	var opt = ll_options[tagId];
	document.getElementById(tagId).innerHTML = getInnerTag_IE(linkId,tagId,statusType,tip,opt);
}

function getInnerTag_IE(linkId,tagId,statusType,tip,options)
{
	var styleName = statusType == 0 ? options.offlineStyle : options.onlineStyle;
	var s = '<nobr><A ' + getHREF(linkId, tagId, (statusType != 0), options.bOfflineLink) + 
		" title='"+tip.replace(/'/g, "&#039;")+"'" + ' CLASS="' + styleName + '">';
	if (!options.bText)
		s += getImgTag(statusType, tip);
	else if (!options.bIcon)
		s += ll_linkTxt[tagId];
	else s+= getImgTag(statusType, tip) + getIconSpace(options.iconSpace) + ll_linkTxt[tagId];
	return s + '</A></NOBR>';
}

function getIconSpace(n)
{
	var s = "<span class=iconSpace>";
	for (var i=0; i<n; i++)
		s += "&nbsp;";
	return s+"</span>";
}

function showSendDeniedMsg(err, userName)
{
	if (typeof ERR_MSG[err] == "undefined") {
		alert(escapeUnicodeMessage (ERR_MSG["SEND_DENIED"]));
	}
	else { 
		alert(escapeUnicodeMessage (ERR_MSG[err].replace(/%s/gi, userName)));
	}
}

function showLogoutMsg(reason)
{
	alert(escapeUnicodeMessage (ERR_MSG["LOGOUT"]));
}

function fireEvent (cmd)
{
	setTimeout(cmd,1);
}
	
function pollApplet()
{
	llApplet = document.applets["STLinksApp"];
	var cmd = "" + llApplet.getCommand();
	llAppletFound = true;
	if (cmd != "") 
		if (!ll_is.dom) {
			cmd = cmd.replace(/\\/g, "\\\\");
			cmd = cmd.replace(/"/g, "\\\"");
			eval(eval('"' + cmd + '"'));
		}
		else setTimeout(cmd,1);
}

function appletStarted()
{
	if ( autoLogin )
		llApplet.autoLogin();
	if (typeof STLinksAppletStarted=="function")
		STLinksAppletStarted();
}

function appletLoggedOut (reason)
{
	ll_loggedIn=false;
	for (var i=0; i<countPlaces.length; i++)
		st_placeCount(countPlaces[i].id,0);
	if (typeof STLinksLoggedOut=="function")
		STLinksLoggedOut(reason);
}

function appletLoggedIn(userId,displayName)
{
	ll_loggedIn=true;
	if (typeof top.stlinksAppletUp == "function")
		top.stlinksAppletUp();
	else {
		watchNames(ll_namesRes, ll_namesNoRes);
		for (var i=0; i<countPlaces.length; i++)
			llApplet.enterPlace(countPlaces[i].id,false,countPlaces[i].bDelay);
	}
	if (typeof STLinksLoggedIn == "function")
		STLinksLoggedIn(userId,displayName);
	if (!ll_is.dom) setTimeout("nsPlacesRefresh()",5000);	
	g_strLoginName = displayName;			
}

var c_nsRefresh=0;
function nsPlacesRefresh()
{
if (c_nsRefresh++<8) setTimeout("nsPlacesRefresh()",5000);
for (var i=0;i<countPlaces.length;i++)
	if (countPlaces[i].count!=0) st_placeCount(countPlaces[i].id,countPlaces[i].count);
}

function userStatusChanged(userId,displayName,statusType,statusDesc,groupName)
{
//	var w = (typeof top.updateLinkStatus == "function") ? top : this;
//	w.updateLinkStatus(userId,statusType,statusDesc);
	updateLinkStatus(userId,statusType,statusDesc);
	if (typeof STLinksUserStatusChanged == "function")
		STLinksUserStatusChanged(userId,displayName,statusType,statusDesc,groupName); 
}

function openImWindow (partnerId, partnerName, away, mobile, isSecured, winIndex, winName)
{
	partnerName=unicodeEscape(partnerName);
	partnerId=unicodeEscape(partnerId);

	open(urlPages+"/chatWindow.html?winType=IM&urlMain="+urlMain+"&PartnerName="+partnerName+
		"&partnerId="+partnerId+"&isSecured="+isSecured+(away ? "&away=1" : "")+(mobile ? "&mobile=1" : "")+
		"&meetings=" + (STLinksMeetingsEnabled() ? "1" : "0") +llApplet.anonymousAccessInfo(), "IM_" + winName, 
		"width="+WIN_IM_WIDTH+",height="+ WIN_IM_HEIGHT +
		",resizable=" + (!ll_is.dom? "no" : "yes") + 
		",status=no,menubar=no,scrollbars=no,location=no,directories=no,top="+winIndex*20+",left="+winIndex*20);
	SetPauseActivity();	
}

function openInviteWin(invitorName,chatId,isSecured,winIndex)
{
	open(urlPages + "/chatWindow.html?winType=INVITE&urlMain="+urlMain+"&chatId="+chatId+"&invitor="+unicodeEscape(invitorName)+"&isSecured="+isSecured,"_blank","width="+WIN_INVITE_WIDTH+",height="+WIN_INVITE_HEIGHT+",status=no,resizable=yes,menubar=no,location=no,scrollbars=no,directories=no,top="+winIndex*20+",left="+winIndex*20);		
	SetPauseActivity();
}

function openAnnounceWin(name,id,allowResponse,winIndex)
{
	open(urlPages + "/chatWindow.html?winType=ANNOUNCE&isSecured=0&urlMain="+urlMain+"&announcerId="+unicodeEscape(id)+"&announcerName="+unicodeEscape(name)+'&response='+allowResponse,"_blank","width="+WIN_INVITE_WIDTH+",height="+WIN_INVITE_HEIGHT+",status=no,resizable=yes,menubar=no,location=no,scrollbars=no,directories=no,top="+winIndex*20+",left="+winIndex*20);		
	SetPauseActivity();
}

function openStatusWindow()
{
	open(urlPages+"/changeStatus.html?urlMain="+urlMain,"_blank","width=330,height=270,status=no,menubar=no,scrollbars=no,resizable=yes,location=no,directories=no,top=100,left=100" );
}

function openNwayWindow(chatId,winIndex)
{
	open(urlPages + "/chatWindow.html?winType=NWAY&isSecured=1&urlMain="+urlMain+"&chatId="+chatId+
	"&meetings=" + (STLinksMeetingsEnabled() ? "1" : "0") +llApplet.anonymousAccessInfo(), "_blank",
		"width="+WIN_NWAY_WIDTH+",height="+WIN_NWAY_HEIGHT+",status=no,menubar=no,scrollbars=no,resizable=no,location=no,directories=no,top="+winIndex*20+",left="+winIndex*20); 
}

function openMeetingWin(url, winLeft, winTop, winW, winH, upgrade)
{
	var newURL=url;
	if (llApplet.isUsingRProxy())
	{
		newURL = llApplet.getRProxyNameAndPort();
		var temp = url;
		if (url.indexOf("http")!=-1)
		{
			temp = url.substring(url.indexOf('/')+2);
			temp = temp.substring(temp.indexOf('/'));
		}
		newURL+=temp;
	}
	open(newURL, "_blank", "resizable=" + (!ll_is.dom ? "no" : "yes")+ 
			",status=no,menubar=no,scrollbars=no,location=no,directories=no,left=" + winLeft + ",top=" + winTop + ",width=" + winW + ",height=" + winH );
}

function alphanum(s){return escape(s).replace(/%/g,"_").replace(/\./g,"_").replace(/\//g,"_")};

function openPlaceWin(id, title)
{
	if (!ll_loggedIn) {
		alert(escapeUnicodeMessage (ERR_MSG["LOGOUT"]));
	}
	else open(urlPages + "/chatWindow.html"+"?winType=PLACE&urlMain="+urlMain+"&placeId="+unicodeEscape(id)+"&title="+unicodeEscape(title)+"&isSecured=1",alphanum(id),"width="+WIN_PLACE_W+",height="+
		WIN_PLACE_H+",status=no,resizable=yes,menubar=no,location=no,scrollbars=no,directories=no,top=100,left=100");		
}

function openPlaceChat(id, title)
{
	if (!ll_loggedIn) {
		alert(escapeUnicodeMessage (ERR_MSG["LOGOUT"]));
	}
	else open(urlPages + "/chatWindow.html"+"?winType=PLACE_CHAT&urlMain="+urlMain+"&placeId="+unicodeEscape(id)+"&title="+unicodeEscape(title)+"&isSecured=1",alphanum(id)+"chat","width="+WIN_PCHAT_W+",height="+
		WIN_PCHAT_H+",status=no,resizable=" + (ll_is.dom ? "yes" : "no") + ",menubar=no,location=no,scrollbars=no,directories=no,top=100,left=100");		
}

function writeSTLinksApplet(loginName, key, byToken, organization)
{
	var port="";
	if (typeof ST25_TUNNELING_PORT != "undefined") {
		port=ST25_TUNNELING_PORT;
		if (port == "80") {
			if (urlMain.charAt(0)=='/')
				urlMain = "http://"+location.host + urlMain;
			urlMain = urlMain.replace(/ *(https?:\/\/)?(.*)/i, "$1community-$2"); 
		}
	}
	else {
		if (typeof HTTP_TUNNELING_PORT != "undefined")
			port=HTTP_TUNNELING_PORT;
		if ((typeof TUNNELING_ADDRESS != "undefined") && (TUNNELING_ADDRESS != "")) {
			if (urlMain.charAt(0) == '/')
				urlMain = "http://"+TUNNELING_ADDRESS+urlMain;		
			else urlMain = urlMain.replace(/ *(https?:\/\/)?[^\/]*\/(.*)/i, "$1" + TUNNELING_ADDRESS + "/$2");
		}
	}
	if (typeof byToken =="undefined")
		byToken=true;
	if (!ll_platform())
		return;
	var str = "<applet name='STLinksApp' code=com.lotus.sametime.stlinks.client.STLinksApplet.class codebase='" + urlMain + "' width=10 height=10"
	if (ll_is.dom)
		str += " MAYSCRIPT";
		
	if (STHost!="")
		str += "><param name='STHost' value='" + STHost + "'>";
		
	if (isRProxy)
	{
		var temp=window.location.toString();
		var start=temp.indexOf(window.location.hostname);
		start=temp.indexOf(':',start);
		var end=temp.indexOf('/',start);
		var port="";
		if ((start!=-1) && (end!=-1))
			port=temp.substring(start+1,end);
		var hostInfo=window.location.hostname;
		if (port!="")
			hostInfo=hostInfo+":"+port;
		if (temp.indexOf("https")!=-1)
			hostInfo="https://" + hostInfo;
		str += "><param name='RProxy' value='" + hostInfo;
		if (ll_AffinityId != "")
			str += "/" + ll_AffinityId;
		str += "'>"; 
	}
		
	str += "><param name='cabbase' value='stlinks.cab'>" +
			"<param name='archive' value='stlinks.jar'>";

	if ( loginName != null ) {
		str += "<param name='loginName' value=\"" + loginName.replace(/"/g, "&quot;") + "\">";
		if (key != "") {
			if ( byToken )
				str += "<param name='token' value='" + key + "'>";
			else
				str += "<param name='password' value=\"" + key + "\">";
		}
		autoLogin = true;
	}
	if (typeof organization != "undefined")
		str+= "<param name='organization' value=\"" + organization.replace(/"/g, "&quot;") + "\">";
	if (port != "")
		str += "<param name='port' value=\"" + port + "\">";
	
	str += "<param name='partnerName' value='main" + ((new Date).getTime() % 100000) + "'>" + "</applet>";
	
	document.writeln("<span style='position:absolute;left:0;top:0;visibility:hidden'>" + str+ "</span>");
	try {
		g_strLoginName = window.parent.opener.g_strLoginName;
	} 
	catch (err) {}

	if (g_nInterval != 0) {
		window.setTimeout ("ActiveAutoawy();", 4000);
	}

}

function writePlaceCounter(placeId, title, bDelay)
{
	var tag="ST_PLACE_"+countPlaces.length;
	document.write('<SPAN ID="' + tag + '">--</span>');
	p=new Object();
	p.id=placeId;
	p.title=title;
	p.bDelay=(typeof bDelay == "undefined")?false:bDelay;
	p.count=0;
	countPlaces[countPlaces.length++]=p;
	if (ll_loggedIn)
		llApplet.enterPlace(p.id,false,p.bDelay);
}

function st_placeCount(placeId,count)
{
	var ind=-1;
	for (var i=0;i<countPlaces.length;i++)
		if (countPlaces[i].id==placeId)
			ind=i;
	if (ind==-1) return;
	countPlaces[ind].count=count;	
	var s = (count==0) ? "--" : "<a href='javaScript:' onClick=\"openPlaceWin('" + placeId.replace(/(['"])/g,"\\$1") + "','" + countPlaces[ind].title.replace(/(['"])/g,"\\$1") + "');return false;\">" + count + "</a>";
	var tagId = "ST_PLACE_"+ind;
	document.getElementById(tagId).innerHTML = s;
}

function st_userJoined(id,name,placeId)
{
	if (typeof STLinksUserEnteredPlace != "undefined")
		STLinksUserEnteredPlace(id,name,placeId);
}

function st_userLeft(id,name,placeId)
{
	if (typeof STLinksUserLeftPlace != "undefined")
		STLinksUserLeftPlace(id,name,placeId);
}
function st_leftPlace(){};

function STLinksLogin(name, key, byToken, organization)
{
	if (typeof organization == "undefined")
		organization="";
	llApplet.login(name, key, byToken, organization);
}

function STLinksLogout()
{
	llApplet.logout();
}

function setSTLinksURL(main,lang,pages)
{
	main = main.replace(/ *(.*)/i, "$1");
	if (typeof ll_RProxyName != "undefined")
	{
		var host = ll_RProxyName.substring(ll_RProxyName.indexOf('/')+2);
		if (host==document.location.hostname)
			isRProxy = true;
	}
	if (!isRProxy)
	{
		if ((main.search(/^https?:\/\//i) == -1) && (main.charAt(0) != '/'))
			main = "http://" + main;
	}
	urlMain=main;
	if (!isRProxy && ll_is.ns && urlMain.indexOf("http")!=-1)
	{
		var host = urlMain.substring(urlMain.indexOf('/')+2);
		host = host.substring(0,host.indexOf('/'));
		if (host!=document.location.host)
		{
			STHost = host;
			if (ns_appPath == null)
				ns_appPath = "sametime/stlinks";
			urlMain=document.location.protocol + "//" + document.location.host + "/" + ns_appPath;
		}
	}
	if (isRProxy)
	{
		urlMain = urlMain.replace(/ *(.*)/i, "$1");
		var rel = urlMain;
		if (rel.indexOf("http")!=-1){
			rel = rel.substring(rel.indexOf('/')+2);
			rel = rel.substring(rel.indexOf('/')+1);
		}
		if (rel.substring(0,1) == "/")
		{
			rel = rel.substring(rel.indexOf('/')+1);
		}
		var affIdTest = rel.substring(0,rel.indexOf('/'));
		if (affIdTest == ll_AffinityId)
			rel = rel.substring(rel.indexOf('/')+1);
		urlMain = ll_RProxyName;
		if (ll_AffinityId != "") 
			urlMain += "/" + ll_AffinityId;
		urlMain += "/" + rel;
	}
	if ((typeof lang == "undefined") || (lang == ""))
		lang=STDEF_LANG;
	else {
		lang=lang.toLowerCase();
		if (STLANGS.indexOf(lang) == -1) {
			lang=lang.substring(0,2);
			if (STLANGS.indexOf(lang) == -1)
				lang=STDEF_LANG;
		}
	}
	if (typeof pages == "undefined")
		urlPages=urlMain;
	else urlPages=pages;
	urlPages += "/" + lang;
	if (location.hostname.indexOf('.') == -1)
		urlPages = urlPages.replace(/(https?:\/\/)?(\w+)\.([^\/:]*)([\/:].*)/i, "$1$2$4");
	document.write('<SCRIPT src="' + urlPages + '/res.js"><\/SCRIPT>');
	document.write('<SCRIPT src="' + urlMain + '/hostInfo.js"><\/SCRIPT>');
}

function StartPolling()
{
	setInterval( "pollApplet()", 200);
}

function ll_platform()
{
	if (ll_is.ns && (ll_is.major >= 5) && !ll_is.ns6)
		return (ll_is.win || ll_is.nowin);
	else if (ll_is.ie && (ll_is.major >= 4))
		return ll_is.win;
	else return false;
}

function watchNames(resNames, noResNames)
{
	llApplet.watchUsers(resNames, true);
	llApplet.watchUsers(noResNames, false);
}

function privateGroupsResult(success, result)
{
	if (success == 1) {
		if (typeof STLinksPrivateGroupsReceived != "undefined")
			STLinksPrivateGroupsReceived(result);
	}
	else if (typeof STLinksPrivateGroupsFailed != "undefined")
			STLinksPrivateGroupsFailed(result);
}

function STLinksStatusTip(uname, stat, desc)
{
	var strStatus = "";

	if (stat == 0) {
		strStatus = TIP_OFFLINE.replace(/%s/gi, uname);
	}
	else if (stat == 64) {
		strStatus = TIP_AUTOAWAY.replace(/%s/gi, uname);
	}
	else if ((stat == 32) && (desc == "")) {

		if (g_strLastToolTip != null) {
			if (g_strLastToolTip != "") {
				strStatus = g_strLastToolTip;
			}
			else {
				strStatus = TIP_ACTIVE.replace(/%s/gi, uname);
			}
		}
		else {
			strStatus = TIP_ACTIVE.replace(/%s/gi, uname);
		}
	}
	else if (desc.indexOf("undefined") >= 0) {
		if (g_strLastToolTip != null) {
			strStatus = uname+":"+g_strLastToolTip;
		}
		else {
			var str = TIP_ACTIVE.replace(/%s/gi, "");
			strStatus = desc.replace(/undefined/gi, str);
		}
	}
	else {
		strStatus = uname + ": " + desc;
	}
	return strStatus;
}

function STLinksPageLoaded()
{
	top.watchNames(ll_namesRes, ll_namesNoRes);
}

function STLinksMyStatus()
{
	return llApplet.getStatusType();
}

function STLinksMyStatusMessage()
{
	var statusDesc = "";
	statusDesc  = llApplet.getStatusDesc();
	var strStatus = new String(statusDesc).toString();
	if (strStatus.indexOf("undefined") >= 0)
	{
		statusDesc = LoginNameFixUp(TIP_ACTIVE);
	}

	return statusDesc;
}

function STLinksGetStatusType(){ return STLinksMyStatus(); }
function STLinksGetStatusDesc(){ return STLinksMyStatusMessage(); }

function STLinksSetMyStatus (stat, desc)
{
	llApplet.setStatus(stat, desc);
}

function STLinksWatchGroups(groups)
{
	llApplet.watchGroups(groups);
}

function STLinksWatchUsers(users, bResolve)
{
	llApplet.watchUsers(users, bResolve);
}

function STLinksResetList()
{
	llApplet.resetList();
	ll_status = new Object();
	ll_desc = new Object();
}

function STLinksGetPrivateGroups()
{
	llApplet.getPrivateGroups();
}

function STLinksAddToContactList(userName,group)
{
	llApplet.addToPrivateGroup(userName,group);
}

function STLinksCreateIM(partnerName)
{
	llApplet.createIm(partnerName);
}

function STLinksCreateMeeting(invitees,tools,topic,msg)
{
	llApplet.createMeeting(invitees,tools,topic,msg,false);
}

function STLinksEnterPlace(placeId, bDelay)
{
	llApplet.enterPlace(placeId, false, (typeof bDelay == "undefined")?false:bDelay);
}

function STLinksLeavePlace(placeId)
{
	llApplet.leavePlace(placeId, false);
}

function STLinksMeetingsEnabled()
{
	return llApplet.meetingsEnabled();
}

function STLinksAudioEnabled()
{
	return llApplet.audioEnabled();
}

function STLinksVideoEnabled()
{
	return llApplet.videoEnabled();
}

function STLinksStartCall(invitees, bResolve)
{
	llApplet.startCall(invitees, bResolve);
}

function setSTLinksNSCodeBase(codeBase)
{
	ns_appPath=codeBase;
}

function AddToContactListFailed(result)
{
	if (typeof STLinksAddToContactListFailed != "undefined")
		STLinksAddToContactListFailed(result);
}

function prepareSametimeLinkNW(linkId, linkText, options)
{
	if (typeof linkText =="undefined")
		linkText = linkId;
	if (typeof options == "undefined")
		options = "";
	var opt = new Object;
	opt.bText=(llOption(options,"text","yes").toLowerCase() == "yes");
	opt.bIcon=(llOption(options,"icon","yes").toLowerCase() == "yes");
	if (!opt.bText && !opt.bIcon)
		return;
	opt.bOfflineLink=(llOption(options,"offlineLink","no").toLowerCase() == "yes");
	opt.onlineStyle=llOption(options,"onlineStyle","online");
	opt.offlineStyle=llOption(options,"offlineStyle","offline");
	opt.iconSpace=llOption(options,"iconSpace",1);
	if (isNaN(parseInt(opt.iconSpace)))
		opt.iconSpace=1;
	if (!ll_platform()) {
		if (opt.bText)
			return(linkText); 
		else return "";
	}
	else {	
		if(!ll_IDs[linkId]){
			ll_IDs[linkId] = new Object();
			ll_status[linkId] = 0;
			ll_desc[linkId] = "";
		}
		var tagId = "";
		var currTime = (new Date()).getTime();
		for(var oldTagId in ll_IDs[linkId]) {
			if (document.getElementById(oldTagId) != null)
				ll_IDs[linkId][oldTagId] = 0;
			else {
				if ((ll_IDs[linkId][oldTagId] == 0) || (currTime - ll_IDs[linkId][oldTagId] > 20000)) {
					tagId = oldTagId;
					break;
				}
			}
		}
		if (tagId == "")
			tagId = "LLZ_" + ll_linksCount++;
		ll_IDs[linkId][tagId] = currTime;
		ll_options[tagId] = opt;
		ll_linkTxt[tagId] = linkText;
		return getLinkTag(linkId,linkText,tagId,opt);
	}
}

