/**
 * Copyright 2002 Earth Resource Mapping Pty Ltd. This document contains unpublished source code of
 * Earth Resource Mapping Pty Ltd. This notice does not indicate any intention to publish the source
 * code contained herein.
 *
 * ecwplugins/lib/Scripts/NCSCheck.js
 *
 */


// Test for ECW installation.  This code will test to see if the ECW plugin has
// already been installed, and if it is compatible with the browser.
// The page will be redirected to /ecwplugins/download.htm or /ecwplugins/sorry.htm
// if the plugin is not installed or not supported.
// The full check function

function getPluginInstallMethod(name) {
	  var name = "NCSPluginInstallMethod";
      var prefix = name + "="
      var cookieStartIndex = document.cookie.indexOf(prefix)
      if (cookieStartIndex == -1)
         return null
      var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex +
         prefix.length)
      if (cookieEndIndex == -1)
         cookieEndIndex = document.cookie.length
      return unescape(document.cookie.substring(cookieStartIndex +
         prefix.length,
		 cookieEndIndex))
}

//
// ECWCheck()
//
// Returns: 0 Unsupported
//         -1 Supported, but need to install plugin
//          1 Win/IE and have native plugin
//			2 Win/Netscape4 and have native plugin
//			3 Win/Netscape6 and have native plugin
//			4 Mac/Netscape6 and have native plugin
//			5 Java Applet
//			6 JavaScript DHTML NCSJSView
//
function ECWCheck() {
	var sBrowser = navigator.appName;
	var sAgent   = navigator.userAgent.toLowerCase();	
	var nVersion = parseFloat(navigator.appVersion);
	var nResult = 0;
	
	// must be >= IE 4.0 or >= Netscape 4.5
	if(sAgent.indexOf("opera") != -1) {
		nResult = 0; // Opera masquerading as IE or Netscape
	} else {
		if( sBrowser == "Netscape" && nVersion >= 4.5 && nVersion < 5.0) {
			nResult = 2; // Netscape 4.5 or higher
		} else if (sBrowser == "Netscape" && nVersion >= 5.0) {
			nResult = 3; // Netscape 6 or Mozilla 5
		} else if( sBrowser == "Microsoft Internet Explorer" && nVersion >= 4.0 ) {
			nResult = 1; // Internet Explorer 4 or above
		}
	}
	
	if(sAgent.indexOf("win") != -1) {
		sPlatform = "Windows";
	} else if(sAgent.indexOf("mac") != -1) {
		sPlatform = "Macintosh";
	} else if(sAgent.indexOf("sunos") != -1) {
		sPlatform = "Solaris";
	} else if(sAgent.indexOf("linux") != -1) {
		sPlatform = "Linux";
	} else {
		sPlatform = "Unknown";
		nResult = 0;
	}
	
	if(nResult > 0) {
		if(sPlatform == "Windows") {
			//if(NCSPluginInstallMethod == 0) {
			if (getPluginInstallMethod("NCSPluginInstallMethod") == "JAVA") {
				// Always Java
				nResult = 5;
			} else {
				// Running Windows, have to check native IE/Netscape plugin
				if(nResult == 2) {
					// Win/Netscape
					if(ECWCheckNetscape4(ECW_NETSCAPE_MIME_TYPE, IMAGE_WEB_SERVER_VERSION_DOT) == 0) {
						// Don't have plugin yet
						nResult = -1;
					}
				}
				else if (nResult == 3) {
					// Win/Netscape
					if(ECWCheckNetscape6(ECW_NETSCAPE_MIME_TYPE, IMAGE_WEB_SERVER_VERSION_DOT) == 0) {
						// Don't have plugin yet
						nResult = -1;
					}
				} else if(nResult == 1) {
					// Win/IE
					if(ECWCheckActiveX(ECW_ACTIVEX_NAME, IMAGE_WEB_SERVER_VERSION) == 0) {
						// Don't have plugin yet
						nResult = -1;
					}
				}
			}
		} else if(sPlatform == "Macintosh") {
		
			// Use Java only on Safari 1.2.4 (Mac OS 10.3) or above. 
			// May need to add Firefox-Mozilla support here for Java also.
			if (sAgent.indexOf("applewebkit") != -1)
			{
				var str = navigator.appVersion.split("Safari/");
				if (parseFloat(str[1]) >= 125.11) {
					nResult = 5;
				} else {
					nResult = 0;
				}
			}
			// Running on Firefird
			else if (sAgent.indexOf("gecko") != -1 && sAgent.indexOf("firefox"))
			{
				alert("Your browser is unsuported on the Macintosh platform. To use Java on the Macintosh, please try Safari 1.2.4 or later (OS X 10.3)");
				nResult = 0;
			}
			else
			{
				alert("Your browser is unsuported on the Macintosh platform. To use Java on the Macintosh, please try Safari 1.2.4 or later (OS X 10.3)");
				nResult = 0;
			}

		} else if(((sPlatform == "Solaris") || (sPlatform == "Linux")) && ((nResult == 2) || (nResult == 3))) {
			// Solaris|Linux on any version of Netscape
			nResult = 5;			
		} else {
			// Currently Unsupported
			nResult = 0;
		}
	}
	if(nResult == 0 || (getPluginInstallMethod("NCSPluginInstallMethod") == "HTML")) {
		// Either unsupported by native/java, or want NCSJSView
		nResult = 6;
	}
	return(nResult);
}

//
// ECWCheckPlugin()
// 
// Check version and redirect page as necessary.
//
function ECWCheckPlugin() {
	nResult = ECWCheck();
	
	// Redirect if not supported or not yet installed
	if( nResult == 0 ) {
		if( (typeof ECWNoPluginUrl != 'undefined') && (typeof ECWNoPluginLocation != 'undefined') ) {
			// redirect to noplugin specified page and clean up history if possible
			if( document.images )
				ECWNoPluginLocation = ECWNoPluginUrl;
			else
				ECWNoPluginLocation.href = ECWNoPluginUrl;
		}
		else {
			if( document.images ) {
				location = "/ecwplugins/Sorry.htm";
			} else {
				location.href = "/ecwplugins/Sorry.htm";
			}
		}
		return false;
	}
	else if( nResult == -1) {
	    if (navigator.appName.indexOf("Netscape") != -1 && parseFloat(navigator.appVersion) >= 5.0)
	    {
	    	location.href = "/ecwplugins/DownloadNetscape6Plugin.htm";
        }
        else if (navigator.appName.indexOf("Microsoft") != -1)
        {
        	location.href = "/ecwplugins/DownloadIEPlugin.htm";
        }
        else 
        {
            location.href = "/ecwplugins/Sorry.htm";
        }
		return false;
	}
	return true;
}

function ECWCheckNetscape4(sECWMime, sECWVersion) {
	var nPlugins = navigator.plugins.length;

	// Check to see if the Mime is installed - either in IE or Netscape
   	if (nPlugins > 0) {   	
		if(	navigator.mimeTypes && navigator.mimeTypes[sECWMime] && navigator.mimeTypes[sECWMime].enabledPlugin ) {

			if(netscape.softupdate.Trigger.UpdateEnabled()) {
       			var sVersion;
				sVersion = netscape.softupdate.Trigger.GetVersionInfo(ECW_NETSCAPE_REG_NAME);
				if(sVersion.compareTo(sECWVersion.replace(/,/g, ".")) < 0) {
					alert("This site requires a newer version of the ECW plug-in. You will be redirected to download the new plugin before continuing.");
					return(0);
				}
			} else {
				alert("You must enable 'SmartUpdate' before you can automatically download the Image Web Server ECW plugin.");
			}	
		}
		else {
			return (0);
		}
	}
	return(1);
}

function ECWCheckNetscape6(sECWMime, sECWVersion) {
	var nPlugins = navigator.plugins.length;
	
   	if (nPlugins > 0) {
		if(	navigator.mimeTypes && navigator.mimeTypes[sECWMime] && navigator.mimeTypes[sECWMime].enabledPlugin )
		{
			if ( InstallTrigger.enabled() ) {
				var result = InstallTrigger.compareVersion("plugins/IWS/ImageViewer", sECWVersion);
				if( result == -5 ) {
					// The user is running a buggy version of mozilla/firefox and InstallTrigger.compareVersion()
					// is broken.  Use a different method for checking version.
					var pluginName = navigator.mimeTypes[sECWMime].enabledPlugin.name;
					//extract the version from the name
					var pluginNameSplit = pluginName.split(" ");
					var pluginVersion = pluginNameSplit[pluginNameSplit.length-1];
	
					var requiredVersion = sECWVersion.split(".");
					var installedVersion = pluginVersion.split(",");
	
					//Check that installedVersion >= requiredVersion
					try {
						if( parseInt(installedVersion[0],10) == parseInt(requiredVersion[0],10) ) {
							if( parseInt(installedVersion[1],10) == parseInt(requiredVersion[1],10) ) {
								if( parseInt(installedVersion[2],10) == parseInt(requiredVersion[2],10) ) {
									if( parseInt(installedVersion[3],10) >= parseInt(requiredVersion[3],10) ) {
										return (1);
									}
								} else if( parseInt(installedVersion[2],10) > parseInt(requiredVersion[2],10) ) {
									return (1);
								}
							} else if( parseInt(installedVersion[1],10) > parseInt(requiredVersion[1],10) ) {
								return (1);
							}
						} else if( parseInt(installedVersion[0],10) > parseInt(requiredVersion[0],10) ) {
							return (1);
						}
					} catch( e ) {
					}
	
					return (0);
	
				} else if ( result < 0) {
					alert("This site requires a newer version of the ECW plug-in. You will be redirected to download the new plugin before continuing.");
					return (0);
				}
			} else {
				alert("You must enable 'Software Installation' before you can automatically download the Image Web Server ECW plugin.");
			}
		}
		else {
			return (0);
		}
	}
	return(1);
}

function xpinstallCallback(url, status)
{
	if (status != 0) {
		msg = "ECWPlugin.xpi FAILED to install correctly (status=" + status
			+ ").\n";
		alert(msg);
	} else {
		alert("Plugin installed successfully.");
	}
	navigator.plugins.refresh(false);
}

function xpinstall_file()
{
	bMac = (navigator.userAgent.indexOf('Mach-O') != -1 );
	xpiFile = 'ECWPlugin' + (bMac ? 'Mac' : '') + '.xpi';

	return xpiFile;	
}

