When Pearson first included i-Chat chat room software within its Companion Websites, they used a JavaScript provided by i-Chat. Unfortunately, this script had a number of shortcoming -- not the least of them being a couple of coding bugs. When Pearons was revamping its Companion Website architecture, I took on the task of rewriting this script to do a more accurate job of detecting the platform, browser, and presense of the i-Chat plugin.

SCRIPT LANGUAGE='JavaScript'>
<!--

// get_browser_version returns the numeric value of the person's 
// browser when passed navigator.appVersion
// grabs the first four characters of navigator.appVersion

function get_browser_version(app_version) {
	var browser_version = '';
	for (var i = 0; i < 3; i++) {
		browser_version = browser_version + app_version.substring(i, i + 1);
	}
	return browser_version;
}

// get_browser returns (basically) the brand of the person's browser

function get_browser() {
	return navigator.appName;
}

// get_platform returns what platform the person is using when
// passed navigator.appVersion

function get_platform(app_version) {
	var platform;
	if (app_version.indexOf('Win16') != -1) {
		platform = 'Win16';
        }
        else if (app_version.indexOf('Win32') != -1) 
		{
			platform = 'Win32';
        }
        else if (app_version.indexOf('WinNT') != -1) 
		{
			platform = 'WinNT';
        }
        else if (app_version.indexOf('Win') != -1) 
		{
			platform = 'Windows';
        }
		else if (app_version.indexOf('Mac') != -1) {
		if (app_version.indexOf('68K') != -1) {
			platform = 'Mac 68K';
		}
		else {
			platform = 'Mac PPC';
		}
	}
	else {
		platform = 'other';
	}
	return platform;
}

// get_plugin_status returns whether or not the plugin has been installed
// called by determine_client

function get_plugin_status(platform) {
	var status;
	if (platform == 'Mac 68K') {
		for (i = 0; i < navigator.plugins.length; i++) {
			if (navigator.plugins[i].name == 'ichat 68k') {
				status = 'on';
				return status;
			}
			else {
				status = 'off';
			}
		}
	}
	else if (platform == 'Mac PPC') {
		for (i = 0; i < navigator.plugins.length; i++) {
			if (navigator.plugins[i].name == 'ichat PPC') {
				status = 'on';
				return status;
			}
			else {
				status = 'off';
			}
		}
	}
	else if (platform == 'Windows' || platform == 'Win16' || platform == 'Win32' || platform == 'WinNT') {
		for (i = 0; i < navigator.plugins.length; i++) {
			if (navigator.plugins[i].name.indexOf('NPICHAT') != -1) {
				status = 'on';
				return status;
	        }
			else 
			{
				status = 'off';
			}
		}
	}
	else {
		status = 'unknown';
	}
	return status;
}

// called when the form is submitted by the user
// determines whether they have set their user name
// if not, sets it to guest then routes to check_client function

function bemyGuest() {
	var user_name = document.forms[0].username.value;
	guestuser = '';
	if (user_name == '') {
		user_name = 'guest';
	}
	else {
		guestuser = 'TRUE'
	}
	password = 'guest';
	document.forms[0].username.value = user_name;
	check_client();
}

// determine_client tries to determine if the plug-in should be used
// called by check_client and also by a script in the page

function determine_client() {
	var app_version = navigator.appVersion;
	version = get_browser_version(app_version);
	browser = get_browser();
	platform = get_platform(app_version);
	if (browser == 'Microsoft Internet Explorer') {
//		plugin_status = 'on';
		plugin_status = get_plugin_status(platform);
	}
	else if ((browser == 'Netscape') && (version >= 3)) {
		plugin_status = get_plugin_status(platform);
	}
	else {
		plugin_status = 'unknown';
	}

	if (plugin_status == 'on') {
		client = 'ichat Plugin';
	}
	else {
		client = 'Java Lite';
	}	
	return client;
}

// check_client figures makes a client selection based on info taken
// from the person's browser
// called by function bemyguest above
// this determines whether user has selected their chat client and, if not,
// chooses it for them.

function check_client() {
	selected_client = document.forms[0].client_type.options.selectedIndex;
		if (selected_client == 0) {
		client = determine_client();	
		document.forms[0].client_type.options[selected_client].value = client;
	}
}

//-->
</SCRIPT>
<BODY  MARGINWIDTH="0" MARGINHEIGHT="0" TOPMARGIN="0" LEFTMARGIN="0" BGCOLOR="#FFFFFF" LINK="#000066" VLINK="#666666" onLoad="document.loginform.username.focus();">
<FORM NAME="loginform" ACTION="http://chat.prenhall.com:4080/chat/+login_user" METHOD=POST onSubmit="bemyGuest();">
<!--input type=hidden name="onLoad" value="document.forms[0].username.focus();"-->
<input type=hidden name="start" value="/world/rooms/">

<BR>

<SCRIPT LANGUAGE=javascript>
<!--
determine_client();
if (platform == 'Mac 68K' || platform == 'Mac PPC')
	var doNothing = 1; 
else if (plugin_status != "on") {
        document.write ("<font face=arial size=-1>");
        document.write ("You do not have the iChat plugin installed.  It's not required, but if you plan to use chat often, you may wish to download the <a href='http://www.ichat.com/plugin/download/rooms.html'>free i-Chat plug-in</a>. If you prefer not to download the plug-in, you can use the Java chat client instead.");
        document.write ("</B></font>");
}
//-->
</SCRIPT>

      <font face="arial" size=-1>
To enter the chat room, type a nickname and click the button below. 
      </font>
 <BR>
<BR>

      <FONT FACE="arial" size=-1>Your first name:</FONT> <input type=text size="20" maxlength="20" name="username" valign=top>
<BR>
<BR>
      <FONT FACE="arial" size=-1>Your chat type:</FONT> <select name="client_type">
	<option value="ichat Plugin" select>Default (Recommended)</option>
	<option value="ichat Plugin">i-Chat Plugin (Netscape)</option>
	<option value="ichat Plugin">Active-X (MSIE)</option>
	<option value="Java Lite">Java (UNIX et al.)</option>
	<!--option value="HTML Client">HTML (last resort)</option-->
	</select>
<BR>
<BR>
<input type=hidden name="password" value="guest">
<input type=hidden name="guestuser" value="TRUE">
<input type=hidden name="guestlogin" value="TRUE">
<BR>
<BR>
      <input type=submit name="guestlogin" value="Enter Chat Area">
<BR>
<BR>
</form>