/*
 * webchat.js
 * ------------
 * A simple script to plug the userplane chat room into a zoomshare site.
 */

var use_chat = true;
var zs_chat_user = "";
var zs_chat_name = "";
var zs_chat_public = false;
var zs_chat_needs_remote_load = false;

function setup_zs_chat_vars ()
{
	var elements = location.host.split(".");
	var prefix = "";

	// Now that we know we're not in tools, we sort out the zsname stuff
	if (location.host.match (/zoomshare.com$/i))
	{
		prefix = "zoom_";
	}
	else
	{
		prefix = "dwqa_";
	}
	if (elements[0] == "www")
	{
		zs_chat_name = elements[1];
	}
	else
	{
		zs_chat_name = elements[0];
	}
	zs_chat_user = prefix + zs_chat_name;

	return;
}
setup_zs_chat_vars();


var cookieExpires = "Fri, 1 Jan 2100 00:00:01 UTC";
var HTML = '\
<div id="webchatControl" class="webchatControl">\
	<form method="get" name="UserplaneLogin" id="UserplaneLogin">\
		<table cellpadding="0" cellspacing="0">\
			<tbody>\
				<tr>\
					<td class="wcToggleControl" id="wcToggleControl" align="right">\
						<a href="javascript://" onClick="toggle ();" class="wcToggleChat"><div id="togglechat">Click to Chat [+]</div></a>\
					</td>\
				</tr>\
				<tr id="wc_chatlogin_row" style="display:none;">\
					<td class="wcLoginControl" id="wcLoginControl">\
						<span class="wcChatname">Live Chat</span><br>\
						<input name="companyID" value="'+zs_chat_user+'" type="hidden">\
						<input value="0" name="silent" type="hidden">\
						<input id="username" maxlength="50" name="username" value="Enter Name to Chat" onFocus="eraseValue (this);" class="wcLoginInput" type="text"><br>\
						<input id="launchPrivate" name="openPrivate" value="Join this Site&#39;s Live Chat" onClick="return openBuddyList ();" class="wcLoginButton" type="button"><br>\
					</td>\
				</tr>\
			</tbody>\
		</table>\
	</form>\
</div>\
';
//						<input id="launchPublic" name="openPublic" value="Join Community Live Chat" onClick="return openGlobalBuddyList ();" class="wcLoginButton" type="button"><br>\

function loadChatControl ()
{
	var cssTag = document.createElement ("link");
	var chatContainer = document.createElement ("div");
	var ctl = null;

	if (location.pathname.match (/:tools/i))
	{
		// Do nothing here.
		return;
	}

	// Add the CSS include
	cssTag.setAttribute ("rel", "stylesheet");
	cssTag.setAttribute ("href", "/lib/suffixapp/webchat/webchat.css");
	document.getElementsByTagName("head")[0].appendChild (cssTag);
	
	// Generate chat control
	document.write (HTML);
	
	if (toolbar_height && toolbar_height > 0)
	{
		ctl = document.getElementById("webchatControl");
		
		ctl.style.top = toolbar_height + "px";
	}
	
	if (getCookie (document, "zs_webchat_hide_login") == "no")
	{
		toggle ();
	}
	
	return;
}

function toggle ()
{
	var target = null;
	var texttarget = "";
	
	if (document.getElementById)
	{
		target = document.getElementById ("wc_chatlogin_row");
		texttarget = document.getElementById ("togglechat");

		if (target.style.display == "none")
		{
			target.style.display = "";
			texttarget.innerHTML = "Hide Chat [-]";
			setCookie (document, "zs_webchat_hide_login", "no", cookieExpires);
		}
		else
		{
			target.style.display = "none";
			texttarget.innerHTML = "Click to Chat [+]";
			setCookie (document, "zs_webchat_hide_login", "yes", cookieExpires);
		}
	}
	
	return;
}

function eraseValue (field)
{
	field.value = '';
	
	return;
}

function openGlobalBuddyList ()
{
	var f = document.getElementById ("UserplaneLogin");
	var holder = f.companyID.value;
	var toReturn = false;

	f.companyID.value = "zoom_chat";
	zs_chat_public = true;
	toReturn = openBuddyList();
	f.companyID.value = holder;
	zs_chat_public = false;

	return toReturn;
}

function openBuddyList ()
{
	var f = document.getElementById ("UserplaneLogin");
	var user =  f.username.value;
	var companyID = f.companyID.value;
	var validName = false;
	var xOffSet  = null;
	var yOffSet  = null;
	var features = null;
	var winName  = null;
	var theURL   = null;
	var myWin = null;

	user = user.replace (/^\s*(.*?)\s*$/, "$1");

	if (zs_chat_public == false && user.toLowerCase() == zs_chat_name.toLowerCase())
	{
		doAuth ();
		return false;
	}
	else if (user != '' && user != 'Enter Name to Chat') 
	{
		validName = true;
	}

	if (validName) 
	{
		var cleanedUpName = cleanupUsername (user)
		f.username.value = cleanedUpName;
		if (cleanedUpName == '')
		{
			alert ("You can only enter characters and numbers.");
		}
		else
		{
			xOffSet  = (screen.width - 225) / 2;
			yOffSet  = (screen.height - 500) / 2;
			features = 'width=738,height=550",toolbar=0,directories=0,menubar=0,status=0,location=0,scrollbars=0,resizable=1';
			winName  = "Userplane_Chatlite_" + replaceAlpha (zs_chat_user);
			theURL   = 'http://apps.userplane.com/chatlite/chat.cfm?name=' + cleanedUpName + '&domainID=' + companyID;
			myWin = window.open (theURL, winName, features);
		}
	}
	else 
	{
		alert ("You must enter a username first. Choose anything you like!");
	}
	return false;
}

function replaceAlpha (strIn)
{
	var strOut = "";
	var i = 0;
	var len = 0;
	var cChar = null;
	
	for (i = 0, len = strIn.length; i < len; i++)
	{
		cChar = strIn.charAt (i);
		if ((cChar >= 'A' && cChar <= 'Z')
			|| (cChar >= 'a' && cChar <= 'z')
			|| (cChar >= '0' && cChar <= '9'))
		{
			strOut += cChar;
		}
		else
		{
			strOut += "_";
		}
	}

	return strOut;
}

function cleanupUsername (strIn)
{
	var strOut = "";
	var i = 0;
	var len = 0;
	var cChar = 0;
	var old = null;

	for (i = 0, len = strIn.length; i < len; i++)
	{
		cChar = strIn.charAt (i);
		
		if ((cChar >= 'A' && cChar <= 'Z')
			|| (cChar >= 'a' && cChar <= 'z')
			|| (cChar >= '0' && cChar <= '9')
			|| cChar == ' ')
		{
			strOut += cChar;
		}
	}

	old = strOut;
	strOut = old.replace (/admin\s*/i, "");
	if (strOut != old)
	{
		
		alert ("You may not have the word 'admin' in your chat name anywhere.");
	}

	return strOut;
}

function getCookie (doc, name)
{
	var s = name + "=";
	var offset = 0;
	var end = null;

	if (doc.cookie.length > 0)
	{
		offset = doc.cookie.indexOf (s);
		if (offset != -1)
		{
			offset += s.length;
			end = doc.cookie.indexOf (";", offset);
			if (end == -1)
			{
				end = doc.cookie.length;
			}

			return unescape(doc.cookie.substring (offset, end));
		}
	}

	return null;
}

function setCookie (doc, name, value, expires)
{
	doc.cookie = name + "=" + escape(value) +
		((expires == null) ? "" : ("; expires=" + expires));
	
	return true;
}

function doAuth ()
{
	var useUrl = "/:tools:webchat:login";
	var win = window.open('', 'Userplane_Chatlite_AdminWindow', 'width=738,height=550",toolbar=0,directories=0,menubar=0,status=0,location=0,scrollbars=0,resizable=1');
	
	if (!win)
	{
		alert ("Sorry, but we have noticed that your popup-blocker has disabled a window that provides application functionality.  You will need to disable popup blocking on this site in order to fully utilize this tool.");
	}
	else
	{
		win.location.href = useUrl;
	}

	return;
}

try {
	loadChatControl();
}
catch (e) {
}
