// support.js
// Tries to extract as much useful information from a browser as possible.
// http://ctyjhu.org/online/support
// last updated 2009/03/25
// by Francis Uy

// showMessage() is the exact popup function used by WebCT 4.0
function showMessage(message_url)	{
  m_win = window.open(message_url,"comp","menubar=1,scrollbars,resizable=1,width=640,height=454");
  m_win.focus();
} // end showMessage()

// change the recipient based on the subject
// simplistic, but may become more interesting if needed
function sendHelp(myselect, sendto)	{
var i = myselect.selectedIndex;
var jhu = '@jhu.edu';
var ctyo = '@ctyonline.net';
if (i==0)
	{ alert('Please select your Software and Course'); }
else if ( (myselect.options[i].text.indexOf('Writing') > -1) || (myselect.options[i].value.indexOf('Writing') > -1) )
	{ sendto.value = 'ben.reynolds'+jhu; }
else if ( (myselect.options[i].text.indexOf('Password') > -1) || (myselect.options[i].value.indexOf('Password') > -1) )
	{ sendto.value = 'ctypassword'+ctyo; }
else
	{ sendto.value = 'ctyonlinetech'+jhu+',ctydistance'+jhu; }
// sendto.value = ( (myselect.options[i].text.indexOf('Writing') > -1)||(myselect.options[i].value.indexOf('Writing') > -1) ) ? ('ben.reynolds'+jhu) : ('ctyonlinetech'+jhu+',ctydistance'+jhu);
return (i);
} // end sendHelp()

// simple client side input checking
function validate(myform)	{
return (sendHelp(myform.subject, myform.recipient) && checkEadd(myform.email));
} // end validate()


// if you need to repeat the same code several times, pull it out as a subroutine.

// if the new version number is higher than the old, use it
function verMax(myfield, newval)	{
	var foo = parseFloat(myfield.value);
	if (isNaN(foo) || (foo < parseFloat(newval)))
		myfield.value = newval;
} // end varMax()

// if the string doesn't have a period, return the whole thing.
// now returns two characters before the decimal
function dotIndex(plugString)	{
	return (plugString.indexOf('.')<2) ? (0) : (plugString.indexOf('.')-2);
} // end dotIndex()

// return the version number from the desired subfield.
function mimeTest(mimeo, myfield, myswitch)	{
	var plugin = mimeo.description;
	var enabled = ((mimeo.enabledPlugin) && (mimeo.enabledPlugin.description));
	switch(myswitch)	{
	case 'n':
	case 'name':
		if (enabled)	plugin = mimeo.enabledPlugin.name;
		break;
	case 't':
	case 'type':
		plugin = mimeo.type;
		break;
	case 'd':
	case 'description':
	default:
		if (enabled)	plugin = mimeo.enabledPlugin.description;
	} // end switch
	verMax(myfield, plugin.substring(dotIndex(plugin)));
} // end mimeTest()

// obnoxious ActiveX because IE/Win doesn't support .plugins or .mimeTypes
function msieTest(mString, x)	{
	try	{
		var msieObj = new ActiveXObject(mString + x);
		return true;	// if ActiveX doesn't error out
	}
	catch(e)	{	return false;	}
} // end msieTest()

// ***** FOLLOWING SECTION OF THE SCRIPT RUNS AS-IS; NOT A FUNCTION *****

var myform = document.forms[0];

// output javascript values to the form table.
if (myform.userAgent)	myform.userAgent.value = navigator.userAgent;
if (myform.version)	myform.version.value = (msie) ? navigator.appVersion.substr(msieVer+5) : navigator.appVersion;
if (myform.platform)	myform.platform.value = navigator.platform;
if (myform.lang)	myform.lang.value = (navigator.language) ? navigator.language : navigator.systemLanguage;
if (myform.java)	myform.java.value = navigator.javaEnabled();
if (myform.refer)	myform.refer.value = document.referrer;
if (myform.screensize) myform.screensize.value = screen.width +' x '+ screen.height + ' /' + screen.colorDepth;
if (!document.cookie)	document.cookie = 'test=yes';
if (myform.cookie)	myform.cookie.value = (document.cookie) ? document.cookie : 'no';

// plugin detection for all browsers capable of telling me
// start by assuming the plugins are not installed
myform.flash.value = 'no';
myform.quicktime.value = 'no';
myform.acrobat.value = 'no';

if (msie)	{	// set by msie.js
	myform.flash.value = 'noX';
	for(i=9; i>1; i--)	{
		if (msieTest('ShockwaveFlash.ShockwaveFlash.', i))	{
			myform.flash.value = i;
			break;
		}
	}
	myform.quicktime.value = 'noX';
	try	{
		var msieObj = new ActiveXObject('QuickTimeCheckObject.QuickTimeCheck.1');
		myform.quicktime.value = (msieObj.QuickTimeVersion) ? parseFloat(msieObj.QuickTimeVersion.toString(16))/1000000 : '4 ?';
	}
	catch(e)	{	}
	myform.acrobat.value = 'noX';
	if (msieTest('AcroPDF.', 'PDF.1'))	{	myform.acrobat.value = 7;	}
	else	for(i=6; i>0; i--)	{
		if (msieTest('PDF.PDFCtrl.', i))	{
			myform.acrobat.value = i;
			break;
		}
	}
	if (msieTest('JavaWebStart.isInstalled', ''))	{	myform.java.value = '1.0';	}
	var jVers = new Array('1.3.0.0', '1.4.2.0', '1.5.0.0', '1.6.0.0');
	for(i=0; i<jVers.length; i++)	{
		if (msieTest('JavaWebStart.isInstalled.', jVers[i]))	{
			myform.java.value = jVers[i];
		}
	}
} // end if MSIE

// In theory I could get the same info from .plugins,
// but in practice .mimeTypes is often more accurate.
if ((navigator.mimeTypes) && (navigator.mimeTypes.length))	{
	var mimeo, plugin;
	for (i=0; i<navigator.mimeTypes.length; i++)	{
		mimeo = navigator.mimeTypes[i];
		if ((mimeo.description.indexOf('Flash')!=-1) || (mimeo.type.indexOf('flash')!=-1) || (mimeo.suffixes.indexOf('swf')!=-1))
		{	mimeTest(mimeo, myform.flash, 'd');	}
		if ((mimeo.description.indexOf('QuickTime')!=-1) || (mimeo.type.indexOf('quicktime')!=-1) || (mimeo.suffixes.indexOf('mov')!=-1))
		{	mimeTest(mimeo, myform.quicktime, 'n');	}
		if ((mimeo.description.indexOf('Acrobat')!=-1) || (mimeo.type.indexOf('pdf')!=-1) || (mimeo.suffixes.indexOf('pdf')!=-1))
		{	mimeTest(mimeo, myform.acrobat, 'd');	}
		if (mimeo.description.indexOf('Java ')!=-1)
		{	mimeTest(mimeo, myform.java, 't');	}
	} // end for mimeTypes
} // end if mimeTypes

// may as well check .plugins anyways; it's fairly short.
if ((navigator.plugins) && (navigator.plugins.length))	{
	var plugin, newval;
	for (i=0; i<navigator.plugins.length; i++ )	{
		plugin=navigator.plugins[i];
		if (plugin.name.indexOf('QuickTime') > -1)
			verMax(myform.quicktime, plugin.name.substring(dotIndex(plugin.name)));
		if (plugin.name.indexOf('Shockwave Flash') > -1)
			verMax(myform.flash, plugin.description.substring(plugin.description.indexOf('.')-1));
		if ((plugin.name.indexOf('Acrobat') > -1) || (plugin.name.indexOf('PDF') > -1))
			verMax(myform.acrobat, plugin.description.substring(dotIndex(plugin.description)));
		if (plugin.name.indexOf('Java ') > -1)
			verMax(myform.java, plugin.description.substring(dotIndex(plugin.description)));
	 } // end for plugins
} // end if plugins

// tell users to update if needed
var qtimeOK = (parseInt(myform.quicktime.value) >= 6);
var flashOK = (parseInt(myform.flash.value) >= 6);
var browsOK = (parseInt(myform.version.value) >= (5 + msie));
if (browsOK && qtimeOK && flashOK)	{
	document.writeln('<h2 class="heading2">Good news: your browser can probably run our courses.<\/h2>')   }
else	{
	document.writeln('<h3 class="heading3">You are using software that might not work with some of our courses.');    }
if (!browsOK)
	document.writeln('<h4> &bull; <a href="http://yourhtmlsource.com/browserupgrades.html" target="popwin">Please upgrade your web browser<\/a>.<\/h4>');
if (!qtimeOK)
	document.writeln('<h4> &bull; <a href="http://apple.com/quicktime/download/" target="popwin">Please upgrade your QuickTime version<\/a>.<\/h4>');
if (!flashOK)
	document.writeln('<h4> &bull; <a href="http://macromedia.com/go/getflashplayer" target="popwin">Please upgrade your Flash version<\/a>.<\/h4>');
if ((myform.acrobat) && (parseInt(myform.acrobat.value) < 6))
	document.writeln('<h4> &bull; <a href="http://adobe.com/products/acrobat/readstep2.html" target="popwin">Please upgrade your Adobe Reader version<\/a>.<\/h4>');
