/**
 * 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/DownloadPlugin.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) || (nResult == 3)) {
					// Win/Netscape
					if(ECWCheckNetscape(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 = 6;
			}
			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) ||
		    (navigator.appName.indexOf("Microsoft") != -1)) {
			location.href = "/ecwplugins/DownloadPlugin.htm?url=" + encodeURIComponent(location.href);	
		} else {
			location.href = "/ecwplugins/Sorry.htm";
		}
		return false;
	}
	return true;
}

function ECWCheckNetscape(sECWMime, sECWVersion) {
	var nPlugins = navigator.plugins.length;
	
   	if (nPlugins > 0) {
		if (navigator.mimeTypes && navigator.mimeTypes[sECWMime] && navigator.mimeTypes[sECWMime].enabledPlugin )
		{
			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 {
			return (0);
		}
	}
	return(1);
}