var g_js_startTagFix = '</';
var g_msie_windows = 0;
var g_vbs_detectPossible = 0;

if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1))
{
	g_msie_windows = 1;
	document.writeln('<script language="VBscript">');
	document.writeln('Dim vbs_detectPossible');
	document.writeln('vbs_detectPossible = 0');
	document.writeln('If ScriptEngineMajorVersion >= 2 then');
	document.writeln('  vbs_detectPossible = 1');
	document.writeln('End If');

	// Inline the vbs_getADMVersion function
	document.writeln('Function vbs_getADMVersion(activeXname)');
	document.writeln('on error resume next');
	document.writeln('Dim oObj');
	document.writeln('Set oObj = CreateObject(activeXname)');
	document.writeln('If (IsObject(oObj) = True) Then');
	document.writeln('vbs_getADMVersion = oObj.GetVersion()');
	document.writeln('Exit Function');
	document.writeln('Else');
	document.writeln('vbs_getADMVersion = 0');
	document.writeln('End If');
	document.writeln('vbs_getADMVersion = 0');
	document.writeln('End Function');
	document.writeln('</' +'script>');
}

// optional function for MSIE browsers only
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1))
{
  g_msie_windows = 1;
  document.writeln('<script language="VBscript">');
  document.writeln('If ScriptEngineMajorVersion >= 2 then');
  document.writeln('  g_vbs_detectPossible = 1');
  document.writeln('End If');
  document.writeln(g_js_startTagFix+'script>');
}

// Javascript function to return the current version
// of the download manager
function js_getClientESDVersion()
{
	// Params needed for detection
	var plugindescription, pluginxtension, pluginmime, activeXname, bOpera;
	plugindescription = 'ESD Manager';
	pluginxtension = 'aomver';
	pluginmime = 'application/aom-getversion';
	activeXname = 'ADM.Document';
	bOpera = navigator.appName.toLowerCase().indexOf("opera")!=-1;
    
  // local state variables
  var i,pluginUndetectable=0, oPlugin=new Object();

  // the current version
  var sESDVer = 0;

  // Then we check to see if it's an MSIE browser that you can actually
  // check for the plugin in question.
  if (!bOpera && g_msie_windows && g_vbs_detectPossible)
  {
      pluginUndetectable = 0;
  }
  else
  {
      pluginUndetectable = 1;
  }

  // If it has a real plugins or mimetypes array, we look there for the plugin first.  Used for Netscape.

  if(navigator.plugins)
  {
  	  // First refresh the plugins so that we catch us if we were just loaded
      navigator.plugins.refresh();

      numPlugins = navigator.plugins.length;
      if (numPlugins > 1)
      {
	  	if (navigator.mimeTypes && navigator.mimeTypes[pluginmime] && navigator.mimeTypes[pluginmime].enabledPlugin && (navigator.mimeTypes[pluginmime].suffixes.indexOf(pluginxtension) != -1))
	  	{
	  		// seems like we have it, let's just make sure and check the version (if specified)
	      		if ((navigator.appName == 'Netscape') && (navigator.appVersion.indexOf('4.0') != -1))
	     	 	{
	     	 		// Netscape can't handle the references to navigator.plugins by number, sooo...
		  			for(i in navigator.plugins)
		  			{
		      			if ((navigator.plugins[i].description.indexOf(plugindescription) != -1) || (i.indexOf(plugindescription) != -1))
		      			{
		      				sESDVer = js_getVerFromDescription(navigator.plugins[i].description);
				  			break;
		      			}
		  			}
	      		}
	      		else
	      		{
		  			for (i = 0; i < numPlugins; i++)
		  			{
		      			oPlugin = navigator.plugins[i];

				      	if ((oPlugin.description.indexOf(plugindescription) != -1) || (oPlugin.name.indexOf(plugindescription) != -1))
				      	{
			      			sESDVer = js_getVerFromDescription(oPlugin.description);
					  		break;
				      	}
		  			}
	      		}
	  	}
	  	return sESDVer;
    }
    // Try the MSIE Automation route...
    else if((g_msie_windows == 1) && !pluginUndetectable)
    {
	 	 sESDVer = vbs_getADMVersion(activeXname);
	 	 return sESDVer;
    }
  }
  return sESDVer;
}

// Parse the MIME type description in the plugin to get the version of the
// ESD Manager
function js_getVerFromDescription(plugindescription)
{
	var ss, i, val, sVer;
	ss = plugindescription.split(" ");
	for (i=0; i < ss.length; i++)
	{
		val = parseFloat(ss[i]);
		if (val)
		{
			sVer = val.toString(10);
			return sVer;
		}
	}
	return 0;
}

