﻿// Functions which can be used to detect the browser type and if it is supported.
// Modified from the original source found here: http://www.quirksmode.org/js/detect.html

// BrowserDetector Object
function BrowserDetector() {
    
    // Objects used to detect browser used.  The ordering here is important!!
    // The order of the fields must be kept the same as well.
    //--------------------------------------------------------------------------
    // 0-string         - The location of where to search for keywords for the browser type
    // 1-subString      - The keyword used to determin the browser type
    // 2-identity       - The browser name
    // 3-prop           - Alternative location to check for broser type
    // 4-versionSearch  - string used to search for the version of the browser
    // 5-supported      - if this browser type is supported
    // 6-minVersion     - the minimum version of this browser that is supported 
    //                    (Note: this must be set if supported is set)
    this.browserData = [
		[
		    // index 0
		    navigator.userAgent,
		    "Chrome",
		    "Chrome",
		    null,
		    null,
		    false,
		    0
		],
		[
		    // index 1
		    navigator.userAgent,
		    "OmniWeb",
		    "OmniWeb",
		    null,
		    "OmniWeb/",
		    false,
		    0
		],
		[
		    // index 2
		    navigator.vendor,
		    "Apple",
		    "Safari",
		    null,
		    "Version",
		    false,
		    0
		],
		[
		    // index 3	
		    null,
		    null,
		    "Opera",
		    window.opera,
		    null,
		    false,
		    0
		],
		[
		    // index 4
		    navigator.vendor,
		    "iCab",
		    "iCab",
		    null,
		    null,
		    false,
		    0
		],
		[
		    // index 5
		    navigator.vendor,
		    "KDE",
		    "Konqueror",
		    null,
		    null,
		    false,
		    0
		],
		[
		    // index 6
		    navigator.userAgent,
		    "Firefox",
		    "Firefox",
		    null,
		    null,
		    true,
		    0
		],
		[
		    // index 7
		    navigator.vendor,
		    "Camino",
		    "Camino",
		    null,
		    null,
		    false,
		    0
		],
		[
		    // index 8	
		    // for newer Netscapes (6+)
		    navigator.userAgent,
		    "Netscape",
		    "Netscape",
		    null,
		    null,
		    false,
		    0
		],
		[
		    // index 9
		    navigator.userAgent,
		    "MSIE",
		    "Explorer",
   		    null,
		    "MSIE",
		    true,
		    7
		],
		[
		    // index 10
		    navigator.userAgent,
		    "Gecko",
		    "Mozilla",
		    null,
		    "rv",
		    false,
		    0
		],
		[
		    // index 11	
		    // for older Netscapes (4-)
		    navigator.userAgent,
		    "Mozilla",
		    "Netscape",
		    null,
		    "Mozilla",
		    false,
		    0
		]
	]

    function searchVersion(dataString, versionSearchString) {
        var index = dataString.indexOf(versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + versionSearchString.length + 1));
    }

    function searchBrowserData(data) {
        var identity = null;
        var supported = null;
        var version = null;
        var found = false;
        var minVersion = 0;
        var index = 0;

        // loop through all the browserData
        while (!found && index < data.length) {
            var dataString = data[index][0];
            var dataProp = data[index][3];

            if (dataString) {
                if (dataString.indexOf(data[index][1]) != -1) {
                    found = true;
                }
            }
            else if (dataProp) {
                found = true;
            }

            if (!found) {
                index++;
            }
        }

        // Search for the version if we found a match to the type
        if (found) {
            var versionSearchString = data[index][4] || data[index][2];
            version = searchVersion(navigator.userAgent, versionSearchString)
		            || searchVersion(navigator.appVersion, versionSearchString)
		            || "an unknown version";
            identity = data[index][2];
            supported = data[index][5];
            minVersion = data[index][6];
        }
        else {
            identity = "an unknown browser";
        }

        // - version can only be used if the identity is a known browser
        // - minVersion can only be used if supported is true
        return [identity, version, supported, minVersion];
    }

    var returnArray = searchBrowserData(this.browserData);

    this.browser = returnArray[0];
    this.browserVersion = returnArray[1];
    this.browserSupported = returnArray[2];
    this.browserMinVersionSupported = returnArray[3];
}

function getBrowserIdentity(browserDetect) {
    return browserDetect.browser;
}

function getBrowserVersion(browserDetect) {
    return browserDetect.browserVersion;
}

function getBrowserMinVersion(browserDetect) {
    return browserDetect.browserMinVersionSupported;
}

function isBrowserVersionSupported(browserDetect) {
    return (browserDetect.browserVersion >= browserDetect.browserMinVersionSupported);
}

function isBrowserSupported(browserDetect) {
    return ((browserDetect.browserSupported == true)
                && isBrowserVersionSupported(browserDetect));
}

function isBrowserValid(browser) {
    return !(browser == "an unknown browser");
}

function isBrowserVersionValid(version) {
    return !(version == "an unknown version");
}
