// $Id: systemcheck.js,v 1.1 2003/10/31 09:27:06 hilleb Exp $
/*******************************************************************************
 * SystemCheck() - checks for browser type, version and plugins                *
 ******************************************************************************/
function SystemCheck()
{
}
// clients
SystemCheck.CLIENT_UNKNOWN      = 0;
SystemCheck.CLIENT_IE           = 1;
SystemCheck.CLIENT_OPERA        = 2;
SystemCheck.CLIENT_NS_OLD       = 4;
SystemCheck.CLIENT_NS_GECKO     = 8;
SystemCheck.CLIENT_MOZILLA      = 16;
SystemCheck.CLIENT_FIREBIRD     = 32;
SystemCheck.CLIENT_GECKO        = SystemCheck.CLIENT_NS_GECKO | SystemCheck.CLIENT_MOZILLA | SystemCheck.CLIENT_FIREBIRD;
SystemCheck.CLIENT_NSPLUGINS    = SystemCheck.CLIENT_GECKO | SystemCheck.CLIENT_NS_OLD;

// operation system
SystemCheck.OS_UNKNOWN      = 0;
SystemCheck.OS_WIN          = 1;
SystemCheck.OS_MAC          = 2;

// list of known plugins
SystemCheck.PLUGINS = new Array();
    SystemCheck.PLUGINS['svg viewer'] = new Array();
        SystemCheck.PLUGINS['svg viewer'][SystemCheck.CLIENT_IE] = 'Adobe.SVGCtl';
        SystemCheck.PLUGINS['svg viewer'][SystemCheck.CLIENT_NSPLUGINS] = 'image/svg-xml';
    SystemCheck.PLUGINS['shockwave director'] = new Array();
        SystemCheck.PLUGINS['shockwave director'][SystemCheck.CLIENT_IE] = 'SWCtl.SWCtl.1';
        SystemCheck.PLUGINS['shockwave director'][SystemCheck.CLIENT_NSPLUGINS] = 'application/x-director';
    SystemCheck.PLUGINS['shockwave flash'] = new Array(); 
        SystemCheck.PLUGINS['shockwave flash'][SystemCheck.CLIENT_IE] = 'ShockwaveFlash.ShockwaveFlash.1';
        SystemCheck.PLUGINS['shockwave flash'][SystemCheck.CLIENT_NSPLUGINS] = 'application/x-shockwave-flash';
    SystemCheck.PLUGINS['realplayer'] = new Array();
        SystemCheck.PLUGINS['realplayer'][SystemCheck.CLIENT_IE] = 'rmocx.RealPlayer G2 Control.1';
        SystemCheck.PLUGINS['realplayer'][SystemCheck.CLIENT_NSPLUGINS] = 'audio/x-pn-realaudio-plugin';
    SystemCheck.PLUGINS['quicktime'] = new Array();
        SystemCheck.PLUGINS['quicktime'][SystemCheck.CLIENT_IE] = 'QuickTimeCheckObject.QuickTimeCheck.1';
        SystemCheck.PLUGINS['quicktime'][SystemCheck.CLIENT_NSPLUGINS] = 'video/quicktime';
    SystemCheck.PLUGINS['windows media'] = new Array();
        SystemCheck.PLUGINS['windows media'][SystemCheck.CLIENT_IE] = 'MediaPlayer.MediaPlayer.1';
        SystemCheck.PLUGINS['windows media'][SystemCheck.CLIENT_NSPLUGINS] = 'application/x-mplayer2';
    SystemCheck.PLUGINS['acrobat reader'] = new Array();
        SystemCheck.PLUGINS['acrobat reader'][SystemCheck.CLIENT_IE] = 'PDF.PdfCtrl.5';
        SystemCheck.PLUGINS['acrobat reader'][SystemCheck.CLIENT_NSPLUGINS] = 'application/pdf';
// ... to be continued ;-)        

// dynamically write VBScript Function for plugin-detection
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
    document.writeln('<script language="VBscript">');
    document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
    document.writeln('\'this next function will detect most plugins');
    document.writeln('Function detectActiveXControl(activeXControlName)');
    document.writeln('  on error resume next');
    document.writeln('  detectActiveXControl = False');
    document.writeln('  If ScriptEngineMajorVersion >= 2 then');
    document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
    document.writeln('  End If');
    document.writeln('End Function');
    document.writeln('</scr' + 'ipt>');
}

SystemCheck.prototype.getClient = function()
{
    var appName   = navigator.appName.toLowerCase();
    var userAgent = navigator.userAgent.toLowerCase();
    // opera & ie
    if (appName.indexOf('microsoft internet explorer') >= 0) {
        if (userAgent.indexOf('opera') >= 0) {
            return SystemCheck.CLIENT_OPERA;
        } else if (userAgent.indexOf('msie') >= 0) {
            return SystemCheck.CLIENT_IE;
        }
    // netscape & co.
    } else if (appName.indexOf('netscape') >= 0) {
        if (navigator.product && navigator.product.toLowerCase().indexOf('gecko') >= 0) {
            if (navigator.vendor != undefined) {
                var vendor = navigator.vendor.toLowerCase();
                if (vendor.indexOf('netscape') >= 0) {
                    return SystemCheck.CLIENT_NS_GECKO;
                } else if (vendor.indexOf('firebird') >= 0) {
                    return SystemCheck.CLIENT_FIREBIRD;
                } else if (vendor.length == 0 && userAgent.indexOf('gecko') >= 0) {
                    return SystemCheck.CLIENT_MOZILLA;
                }
            } else {
                return SystemCheck.CLIENT_GECKO;
            }
        }
        return SystemCheck.CLIENT_NS_OLD;
    }
    return SystemCheck.CLIENT_UNKNOWN;
}

SystemCheck.prototype.getClientVersion = function()
{
    var version = -1;
    if (this.isIE()) {
        version = navigator.appVersion.substr(navigator.appVersion.lastIndexOf('MSIE') + 5, 3);
    } else if (this.isOpera()) {
        version = navigator.userAgent.substr(navigator.userAgent.lastIndexOf('Opera') + 6, 3);
    } else if (this.isNS(true)) {
        version = navigator.appVersion.substr(0, 4);
    } else if (this.isNS()) {
        version = navigator.userAgent.substr(navigator.userAgent.lastIndexOf('Netscape/') + 9, 3);
    } else if (this.isMozilla()) {
        version = navigator.userAgent.substr(navigator.userAgent.lastIndexOf('rv:') + 3, 3);
    } else if (this.isFirebird()) {
        version = navigator.vendorSub;
    }
    return parseFloat(version);
}

SystemCheck.prototype.getOS = function()
{
    var os = navigator.platform.toLowerCase();
    if (os.indexOf('win') >= 0) {
        return SystemCheck.OS_WIN;
    } else if (os.indexOf('mac') >= 0) {
        return SystemCheck.OS_MAC;
    }
    return SystemCheck.OS_UNKNOWN;
}

SystemCheck.prototype.hasPlugin = function(identifier, searchMimeTypes)
{
    if (!identifier) return false;
    identifier = identifier.toLowerCase();
    if (navigator.plugins && navigator.plugins.length > 0) {
        for (var p = 0; p < navigator.plugins.length; ++p) {
           if (navigator.plugins[p].name.toLowerCase().indexOf(identifier) >= 0
               || navigator.plugins[p].description.toLowerCase().indexOf(identifier) >= 0) {
               // plugin found
               return true;
           }
        }
        // if no plugin was found, check for mime-types
        if (searchMimeTypes) {
            return this.knowsMimeType(identifier);
        }
    } else if (this.isIE() && this.isWin()) {
        if (SystemCheck.PLUGINS[identifier]) {
            identifier = SystemCheck.PLUGINS[identifier][SystemCheck.CLIENT_IE];
            return detectActiveXControl(identifier);
        }
    }
    return false;
}

SystemCheck.prototype.knowsMimeType = function(identifier)
{
    if (!identifier) return false;
    identifier = identifier.toLowerCase();
    if (navigator.mimeTypes && navigator.mimeTypes.length > 0) {
        if (SystemCheck.PLUGINS[identifier]) {
            identifier = SystemCheck.PLUGINS[identifier][SystemCheck.CLIENT_NSPLUGINS];
        }
        for (var m = 0; m < navigator.mimeTypes.length; ++m) {
            if (navigator.mimeTypes[m].type.toLowerCase().indexOf(identifier) >= 0) {
                if (navigator.mimeTypes[m].enabledPlugin != null) {
                    return true;
                }
            }
        }
    } else if (this.isIE() && this.isWin()) {
        if (SystemCheck.PLUGINS[identifier]) {
            identifier = SystemCheck.PLUGINS[identifier][SystemCheck.CLIENT_IE];
            alert(identifier);
            return detectActiveXControl(identifier);
        }
    }
    return false;
}

SystemCheck.prototype.isIE = function()
{
    return this.getClient() & SystemCheck.CLIENT_IE;
}

SystemCheck.prototype.isOpera = function()
{
    return this.getClient() & SystemCheck.CLIENT_OPERA
}

SystemCheck.prototype.isNS = function(old)
{
    return (old) ? this.getClient() & SystemCheck.CLIENT_NS_OLD
                 : this.getClient() & SystemCheck.CLIENT_NS_GECKO;
}

SystemCheck.prototype.isMozilla = function()
{
    return this.getClient() & SystemCheck.CLIENT_MOZILLA;
}

SystemCheck.prototype.isFirebird = function()
{
    return this.getClient() & SystemCheck.CLIENT_FIREBIRD;
}

SystemCheck.prototype.isGecko = function()
{
    return this.getClient() & SystemCheck.CLIENT_GECKO;
}

SystemCheck.prototype.isWin = function()
{
    return this.getOS() & SystemCheck.OS_WIN;
}

SystemCheck.prototype.isMac = function()
{
    return this.getOS() & SystemCheck.OS_MAC;
}

SystemCheck.prototype.hasRealPlayer = function()
{
    if (this.hasPlugin('realplayer') || this.knowsMimeType('realplayer')) {
        return true;
    }
    return false;
}

SystemCheck.prototype.hasWindowsMedia = function()
{
    if (this.hasPlugin('windows media') || this.knowsMimeType('windows media')) {
        return true;
    }
    return false;
}

SystemCheck.prototype.hasQuicktime = function()
{
    if (this.hasPlugin('quicktime') || this.knowsMimeType('quicktime')) {
        return true;
    }
    return false;
}

