// This is a port of the VBS version.
//     --ad Fri Jul 18 11:12:03 2008

var ecwpActiveXAvailable;

/*
 * Copyright 2011 ERDAS. This document contains
 * unpublished source code of ERDAS.
 * This notice does not indicate any intention to publish
 * the source code contained herein.
 *
 * This will scan for plugins for all versions
 * of Internet Explorer.
 * The script will only be called if this is known to be IE, Win32
 */
function ECWCheckActiveX (activeXname, sECWVersion) {

        if (typeof ecwpActiveXAvailable !== "boolean") {

                ecwpActiveXAvailable = (
                        function (activeXname, sECWVersion) {

                                var MyVersionObject, sVersion;

	                        // Create an NCSVersion Object, new for Image Web Server v1.5+
                                try {

                                        MyVersionObject = new ActiveXObject("NCSVersion.NCSVersion.1");

                                } catch (e) {

                                        return false;
                                }

                                if (typeof MyVersionObject !== "object") {

                                        return false;
                                }

                                try {

                                        sVersion = MyVersionObject.GetVersionString (activeXname);

                                } catch (e) {

                                        return false;
                                }

		                // Got the version, check it's good enough.
                                //
		                // ..split up into major, minor, sub and build.
		                var VerArray = sVersion.split(",");
                                var ECWVerArray = sECWVersion.split(",");

                                var nV1, nV2;
		                nV1 = + (VerArray.shift ());
		                nV2 = + (ECWVerArray.shift ());

                                if (nV1 < nV2) {

                                        // Major version too old.
                                        return false;
                                }

		                if (nV1 === nV2) {

                                        // then same Major version, check minor
			                nV1 = + (VerArray.shift ());
			                nV2 = + (ECWVerArray.shift ());

			                if (nV1 < nV2) {

                                                // then Minor too old
                                                return false;
                                        }

			                if (nV1 === nV2) {

                                                // then same minor version, check sub
				                nV1 = + (VerArray.shift());
				                nV2 = + (ECWVerArray.shift());

				                if (nV1 < nV2) {

                                                        // then Sub too old
                                                        return false;
                                                }

				                if (nV1 === nV2) {

                                                        // then Same sub, check build number
					                nV1 = + (VerArray.shift ());
					                nV2 = + (ECWVerArray.shift ());

					                if (nV1 < nV2) {

                                                                // then Build number too old
						                return false;
                                                        }
                                                }
                                        }
                                }

                                MyVersionObject = null;
                                return true;
                        }) (activeXname, sECWVersion);

        }

        return ecwpActiveXAvailable;
}

