//----------------------------------------------------------------------------
// JavaScript routines for handling installation of ActiveX controls and 
// Netscape plug-ins.
//----------------------------------------------------------------------------

function PxControlInstall()
{
	this.strControlName  = "";
	this.strControlCLSID = "";
	this.strControlCAB   = "";
	this.strPluginMime   = "";
	this.strPluginJAR    = "";
	this.strCurrVersion  = "";
	this.strMinVersion   = "";
}

function PxControlInstall_Initialize(strControlName,
									 strControlCLSID, 
									 strControlCAB,
									 strPluginMime,
									 strPluginJAR,
									 strCurrVersion,
									 strMinVersion,
									 strName)
{
	// Save control information for later calls
	this.strControlName  = strControlName;
	this.strControlCLSID = strControlCLSID;
	this.strControlCAB   = strControlCAB;
	this.strPluginMime   = strPluginMime;
	this.strPluginJAR    = strPluginJAR;
	this.strCurrVersion  = strCurrVersion;
	this.strMinVersion   = strMinVersion;
	this.strName         = strName;

	// Initialize state
	this.bInstalled  = false;
	this.bOldVersion = false;

	// Check to see if it's installed
	this.CheckInstalled();
}
PxControlInstall.prototype.Initialize = PxControlInstall_Initialize;

function PxControlInstall_CheckInstalled()
{
	// Get control information based on browser
	if (!this.IsSupported())
		return;
	if (is_ie4up)
	{
		// Check to see if control is installed.  Check will return version number.
		this.installedVersion = GetActiveXVersion(this.strControlName);
		if (this.installedVersion == 0)
			return;
	}
	else
	{
		// Check to see if plug-in is installed.  Check will return version number.
		this.installedVersion = new String(this.GetPluginVersion());
		if (this.installedVersion == "0")
			return;

		// If not a full version, append build to desired version
		var aVer = this.installedVersion.split(",");
		if (aVer.length == 1)
		{
			var aDesiredVer = this.strCurrVersion.split(",");
			if (aDesiredVer.length == 4)
			{
				this.installedVersion = 
					aDesiredVer[0] + "," +
					aDesiredVer[1] + "," +
					aDesiredVer[2] + "," +
					this.installedVersion;
			}
		}
	}
	this.bInstalled = true;

	// Check to see whether version number matches
	if (this.CompareVersion(this.installedVersion, this.strMinVersion) < 0)
	{
		this.bOldVersion = true;
		return;
	}

	// Everything's good.
	return this.bInstalled;
}
PxControlInstall.prototype.CheckInstalled = PxControlInstall_CheckInstalled;

function PxControlInstall_IsSupported()
{
  if ( !is_win32 													  // only for Win32
	     || ( !is_nav && !is_ie )							// we only support plugins on these browsers
	     || ( is_nav && is_minor <= 4.08 )		// earlier than NS 4.08 
	     || ( is_ie4 && is_minor < 4.01 )			// earlier than IE 4.01 
			 || is_win95													// win95 has DLL hell problems
		 )
	{ 
		return false;
	}

	return true;
}
PxControlInstall.prototype.IsSupported = PxControlInstall_IsSupported;

function PxControlInstall_IsInstalled()
{
	if (!this.bInstalled)
		return false;
	if (this.bOldVersion)
		return false;
	return true;
}
PxControlInstall.prototype.IsInstalled = PxControlInstall_IsInstalled;

function PxControlInstall_CompareVersion(strControlVersion, strDesiredVersion)
{
	// Split up version string for control.  If not valid, then assume down-rev.
	var aControlVersion = strControlVersion.split(",");
	if (aControlVersion.length != 4)
		return -1;

	// Split up desired version string.
	var aDesiredVersion = strDesiredVersion.split(",");
	if (aDesiredVersion.length != 4)
		return -1;

	// Convert version components to numbers
	aControlVersion[0] = parseInt(aControlVersion[0]);
	aControlVersion[1] = parseInt(aControlVersion[1]);
	aControlVersion[2] = parseInt(aControlVersion[2]);
	aControlVersion[3] = parseInt(aControlVersion[3]);
	aDesiredVersion[0] = parseInt(aDesiredVersion[0]);
	aDesiredVersion[1] = parseInt(aDesiredVersion[1]);
	aDesiredVersion[2] = parseInt(aDesiredVersion[2]);
	aDesiredVersion[3] = parseInt(aDesiredVersion[3]);

	// Compare version components
	if (aControlVersion[0] < aDesiredVersion[0])
		return -1;
	else if (aControlVersion[0] > aDesiredVersion[0])
		return 1;
	else
	{
		if (aControlVersion[1] < aDesiredVersion[1])
			return -1;
		else if (aControlVersion[1] > aDesiredVersion[1])
			return 1;
		else
		{
			if (aControlVersion[2] < aDesiredVersion[2])
				return -1;
			else if (aControlVersion[2] > aDesiredVersion[2])
				return 1;
			else
			{
				if (aControlVersion[3] < aDesiredVersion[3])
					return -1;
				else if (aControlVersion[3] > aDesiredVersion[3])
					return 1;
				else
					return 0;
			}
		}
	}
}		
PxControlInstall.prototype.CompareVersion = PxControlInstall_CompareVersion;

function PxControlInstall_GetPluginVersion()
{
	// Check to see if we've got a version of the plug-in
	var mimeType = navigator.mimeTypes[this.strPluginMime];
	var plugin = null;
	if (mimeType)
		plugin = mimeType.enabledPlugin;
	if (plugin == null)
		return "0";

	// We've got the right plug-in.  Get the version number
	// Version is specified as (Vn) in the description.
	hasPlugIn = -1;
	var desc = plugin.description;
	var pos = desc.indexOf("(V");
	var plugInVersion = "1";
	if (pos > -1)
	{
		var posP = desc.indexOf(")", pos + 2);
		if (posP > -1)
			plugInVersion = desc.substr(pos + 2, posP - pos - 2);
	}
	return plugInVersion;
}
PxControlInstall.prototype.GetPluginVersion = PxControlInstall_GetPluginVersion;

function PxControlInstall_Install(serverPath)
{
	// Redirect to installation page
	var strBrws = "IE";
	if (isNav4)
		strBrws = "NS";
	window.location.href = serverPath + 
		"?Cmd=InstallControl" +
		"&CtlName=" + this.strControlName +
		"&CtlID=" + this.strControlCLSID +
		"&CtlCAB=" + this.strControlCAB +
		"&PlugMime=" + this.strPluginMime +
		"&PlugJar=" + this.strPluginJAR +
		"&Ver=" + this.strCurrVersion +
		"&Name=" + escape(this.strName) +
		"&Brws=" + strBrws;
}
PxControlInstall.prototype.Install = PxControlInstall_Install;

