﻿function getSilverlightVersionOld() {
    var version = 'No Silverlight';
    var container = null;

    try {
        var control = null;
        var product = 'Silverlight';

        if (navigator.userAgent.indexOf("Linux") != -1) {
            product = "Silverlight (Moonlight)";
        }

        if (window.ActiveXObject) {
            control = new ActiveXObject('AgControl.AgControl');
        }
        else {
            if (navigator.plugins['Silverlight Plug-In']) {
                container = document.createElement('div');
                document.body.appendChild(container);
                container.innerHTML = '<embed type="application/x-silverlight" src="data:," />';
                control = container.childNodes[0];
            }
        }

        if (control) {
            if (control.isVersionSupported('4.0')) {
                version = product + '/4.0';
            }
            else
                if (control.isVersionSupported('3.0')) {
                    version = product + '/3.0';
                }
                else
                    if (control.isVersionSupported('2.0')) {
                        version = product + '/2.0';
                    }
                    else
                        if (control.isVersionSupported('1.0')) {
                            version = product + '/1.0';
                        }
        }
    }
    catch (e) { }

    if (container) {
        document.body.removeChild(container);
    }

    return version;
}

function getSilverlightVersionNew() {
    if (window.ActiveXObject) {
        var p = null;
        try { p = new ActiveXObject('AgControl.AgControl'); }
        catch (e) { return '-'; }
        if (!p) return '-';
        var v = 0;
        while (true) {
            if (p.IsVersionSupported((v + 1) + '.0')) { ++v; }
            else { break; }
        }
        var r = 0;
        while (true) {
            if (p.IsVersionSupported(v + '.' + (r + 1))) { ++r; }
            else { break; }
        }
        return v + '.' + r;
    } else if (navigator.plugins) {
        var p = navigator.plugins["Silverlight Plug-In"];
        if (!p) return '-';
        var d = p.description.match(/^\d+(\.\d+)?/);
        if (d) { return d[0]; } else { return '-'; }
    }
}

function getSilverlightVersion() {
    var ver = getSilverlightVersionNew();
    return ver;
}
