// This is a little JavaScript piece to "sniff" the browser type, name, and version;
// it does not cover all cases, but most commonly used browsers - I think.
// browser_sniff() assumes that it is called within an <OL>/<UL> environment.
// 
// (c) December 2007 - Andreas Wahle

function version_check (idstr, uastr, isalias) {
    var idlow = idstr.toLowerCase() + "/";
    var idlen = idlow.indexOf("/");
    var iduas = uastr.indexOf(idlow);

    if (iduas != -1 ) {
        var idbeg = iduas + idlen + 1;
        var idptr = idbeg;
        var idval = 1;
        while (idval) {
            var current = uastr[idptr];
            if (current == '.' || 
                (current >= '0' && current <= '9') || 
                (current >= 'a' && current <= 'z')) idptr = idptr + 1;
            else idval = 0;
        }
        document.write ((isalias ? ", alias " : " (identified as " )
                        + idstr + " " + uastr.substring(idbeg,idptr));
        return true;
    }
    else return false;
}

function browser_sniff () {
    var uagent=navigator.userAgent.toLowerCase();
    var browser=navigator.appName;
    var gecko=uagent.indexOf("gecko/");
    var msie=uagent.indexOf("msie ");

    if (browser != "") { // the selection of browsers too sniff for is rather arbitrary ;-)
        var found = false; // many browsers have multiple aliases...
        document.write ("<LI><p>You are currently using a " + browser + " or equivalent browser");
        found |= version_check ("SeaMonkey", uagent, found);
        found |= version_check ("Camino", uagent, found);
        found |= version_check ("Epiphany", uagent, found);
        found |= version_check ("Netscape", uagent, found);
        found |= version_check ("Firefox", uagent, found);
        found |= version_check ("Chrome", uagent, found);
        found |= version_check ("Safari", uagent, found);
        found |= version_check ("Konqueror", uagent, found);
        found |= version_check ("Opera", uagent, found);
        found |= version_check ("version", uagent, found);
        if (found) document.write (")");
    }
    else document.write ("<LI><p>You are currently using a browser that cannot be identified");
    if (gecko != -1) {
        var rvStart=uagent.indexOf("rv:");
        var rvVersion=uagent.substring(rvStart,rvStart+20) + ")";
        var rvEnd=rvVersion.indexOf(")");
        var rvstring=rvVersion.substring(3,rvEnd);
        var gbstring=uagent.substring(gecko+6,gecko+10) + "-" + uagent.substring(gecko+10,gecko+12) + "-" + uagent.substring(gecko+12,gecko+14);
        document.write (", congratulations for a good choice. ");
        document.write ("This is <a href=http://developer.mozilla.org/en/Gecko>Gecko</a> revision " + rvstring + ", build date " + gbstring);
        document.write (". If you want to check for any newer version, please follow one of the links.</p>");
    }
    else if (browser == "Netscape" && parseInt(navigator.appVersion) < 5) {
        document.write (". This appears to be an older Netscape " + parseFloat(navigator.appVersion) + " version. ");
        document.write ("Minimum requirement for my web pages is Netscape 2.0, but you may want to upgrade anyway. ");
        document.write ("Please follow the links on the left for newer <a href=http://www.mozilla.org>Mozilla</a> applications.</p>");
    }
    else if (uagent.indexOf("compatible") != -1 && msie != -1) {
        document.write (" (identified as IE" + uagent.substring(msie+5,msie+6) + ")");
        document.write (". While Microsoft's default browser should be sufficient for my web pages, I'd recommend ");
        document.write ("using <a href=http://www.mozilla.org>Mozilla</a> cross-platform open-source applicatons instead. ");
        document.write ("Just follow the links to the left.</p>");
    }
    else {
        document.write (". While my web page should work well with <i>any</i> browser, ");
        document.write ("I'd recommend giving <a href=http://www.mozilla.org>Mozilla</a> applicatons a try. ");
        document.write ("Just follow the links to the left.</p>");
    }
}
