/*      browser-detect.js

        Just include this file in your html like this

        <script type="text/JavaScript" src="path/browser-detect.js">

        OS test  if (os.isWindows) your code (see which_os for list of os flags)
        Also innerHTML = "Your OS is " +os.platform

        Browser test if(browser.isFirefox) your code (see which_browser for list of browser flags)
        and (browser.version >= 6) your code. (versions are numeric)
*/


/* Determine which OS */
function which_os(){
    /* Set default values */
    this.isWindows = false;
    this.isMac = false;
    this.isLinux = false;
    this.isUnix = false;
    this.isSun = false;
    this.isWinCE = false;
    this.isOS_Unk = false;
    this.platform = "Unknown";       //name of platform/os

    if (typeof(window.navigator.platform) != 'undefined') {
        this.platform = window.navigator.platform;
        if (this.platform.toLowerCase().indexOf('windows ce') != -1)
            this.isWinCE = true;
        else if (this.platform.toLowerCase().indexOf('win') != -1)
            this.isWindows = true;
        else if (this.platform.toLowerCase().indexOf('mac') != -1)
            this.isMac = true;
        else if (this.platform.toLowerCase().indexOf('linux') != -1)
            this.isLinux = true;
        else if (this.platform.toLowerCase().indexOf('unix') != -1)
            this.isUnix = true;
        else if (this.platform.toLowerCase().indexOf('sun') != -1)
            this.isSun = true;
        else
            this.isOS_Unk = true;
    }
}

/* Now create an instance of which_os object an initial values*/
var os1 = new  which_os();

/* Deterimine which browser */
function which_browser(){

        /* Set default values */
        this.brand = "unk";     //e.g. IE, Mozilla
        this.version = 0;        //e.g. 7.2 numeric value
        this.cversion = "";      //e.g. 7.2.1 character value
        this.subversion = 0;
        this.isOpera = false;
        this.isMSIE = false;
        this.isMozilla = false;
        this.isFirefox = false;
        this.isNetscape = false;
        this.isSafari = false;
        this.isOmniWeb = false;
        this.isICab = false;
        this.isChimera = false;
        this.isCamino = false;
        this.isGaleon = false;
        this.isKonqueror = false;
        this.isKHTML  = false;
        this.isGecko = false;
        this.isUnk = false;
        var version = 0;
        var subversion = 0;

        var format_revision = function (str){

                var rvParts = str.split('.');
                var ver = rvParts[0] || "0";
                var dec = rvParts[1] || "0";
                dec = ("00" + dec).slice(-2);
                if (dec.length > 2)
                    dec = dec.slice(dec.length -2); //for IE5;
                var sub = rvParts[2] || "0";

                version = (ver + "." + dec) * 1;
                subversion = sub * 1;
            };


        var x, y, i, k;                 //working indices
        var agent = navigator.userAgent.toLowerCase();

        if (agent.indexOf("opera") != -1){

                //Even if spoofing identify as opera
                this.brand = "Opera";
                this.isOpera = true;

                //Opera/8.0 ( or Opera 7.60  [ or Opera 8.0

                x = agent.indexOf("opera") + "opera".length + 1;
                y = agent.indexOf(" (", x);
                if (y == -1)
                    y = agent.indexOf(" [", x);
        }
        else if (agent.indexOf("msie") != -1){
                this.brand = "MSIE";
                this.isMSIE = true;

                //MSIE 6.0;
                x = agent.indexOf("msie") + "msie".length + 1;
                y = agent.indexOf(";", x);
        }
        else if (agent.indexOf("firefox") != -1){
                this.brand = "Firefox";
                this.isFirefox = true;
                this.isGecko = true;
                x = agent.indexOf("firefox/") + "firefox/".length;
                y = agent.indexOf(" ", x);
                //this.cversion = navigator.vendorSub;
        }
        else if (navigator.vendor == "Netscape"){
                //vendor is new for ver 6 +
                this.brand = "Netscape";
                this.isNetscape = true;
                this.isGecko = true;
                this.cversion = navigator.vendorSub;
        }
        else if (agent.indexOf("netscape") != -1){
                //anything not above call 4
                this.brand = "Netscape";
                this.isNetscape = true;
                this.cversion = "4"
        }
        else if (agent.indexOf("camino") != -1){
            this.brand = "Camino";
            this.isCamino = true;

            //camino/0.7+
            x = agent("camino/");
            if (x != -1) {
                x += "camino/".length;
                y = agent.slice("+", x);
            }
        }
        else if (agent.indexOf("chimera") != -1){
            this.brand = "Chimera";
            this.isChimera = true;

            //Chimera/0.6
            x = agent.indexOf("chimera/");
            if (x != -1) {
                x += "chimera/".length;
                y = agent.indexOf(" ", x);
            }
        }
        else if (agent.indexOf("galeon") != -1){

            this.brand = "Galeon";
            this.isGaleon = true;

            //Galeon/1.3.18 or Galeon/1.2.0 (
            x = agent.indexOf("galeon/");
            if (x != -1) {
                x +=  "galeon/".length;
                y = agent.indexOf(" (", x);
            }
        }
        else if (navigator.vendor == 'KDE'){
                this.isKHTML  = true;

                //don't have a Mac so this is untested
                if (agent.indexOf("safari") != -1) {
                        this.brand = "Safari";
                        this.isSafari = true;

                        //Safari/100.1
                        x = agent.indexOf("safari/");
                        if (x != -1) {
                            x +=  "safari/".length;
                            y = agent.indexOf(" ", x);
                        }
                }
                else if (agent.indexOf("omniweb") != -1) {
                        this.brand = "OmniWeb";
                        this.isOmniWeb = true;

                        //OmniWeb/v563.34
                        x = agent.indexOf("omniweb/v");
                        if (x != -1) {
                            x +=  "omniweb/v".length;
                            y = agent.indexOf("-", x);
                        }
                }
                else if (navigator.appName == "Konqueror") {
                        this.brand = "Konqueror";
                        this.isKonqueror = true;

                        //KHTML/3.4.1 (  or Konqueror/3.4;
                        x = agent.indexOf("khtml/");
                        if (x != -1) {
                            x+= "khtml/".length;
                            y = agent.indexOf(" (", x);
                        }
                        else {
                            x = agent.indexOf("konqueror/") + "konqueror/".length;
                            y = agent.indexOf(";", x);
                        }
                }
                else {
                        this.brand = "unk-khtml";
                        x = -1;
                }
        }
        else if (navigator.product == "Gecko" ){

                //everyone claims mozilla in ua,
                //but a Gecko and not any above
                //is mozilla

                this.brand = "Mozilla";
                this.isMozilla = true;
                this.isGecko = true;

                //rv:1.7.7)
                x = agent.indexOf("rv:")
                if (x != -1) {
                    x += "rv:".length;
                    y = agent.indexOf(")", x);
                }
        }
        else {
                //There are too many others
                this.brand = "unknown";
                this.isUnk = true;
                x = -1
        }

        if (this.cversion == "") {
            if (x != -1) {
                if (y != -1)
                    this.cversion = agent.slice(x, y);
                else
                    this.cversion = agent.slice(x);
            }
        }
        if (this.cversion != "") {
            format_revision(this.cversion);
            this.version = version;
            this.subversion = subversion;
        }
}//eof which_browser

var browser1 = new which_browser();
