// variable where the refresh location is stored while called detect(url) function, check detect(url)
var usercapRefreshURL = "";

// function to refresh web content (or redirect to different url), check detect(url)
function usercapRefresh() {
    window.location.replace(usercapRefreshURL);
}

// main function where the browser capabilities are evaluated
function detect(url) {

    var resultString = "";

    var referrer='';
    var resolution='';
    var colorDepth='';
    var os='';
    var agt = navigator.userAgent.toLowerCase();
    var is_major = parseInt(navigator.appVersion);
    var is_minor = parseFloat(navigator.appVersion);
    var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1));
    var is_nav2 = (is_nav && (is_major == 2));
    var is_nav3 = (is_nav && (is_major == 3));
    var is_nav4 = (is_nav && (is_major == 4));
    var is_nav4up = (is_nav && (is_major >= 4));
    var is_navonly = (is_nav && ((agt.indexOf(';nav') != -1) || (agt.indexOf('; nav') != -1)) );
    var is_nav5 = (is_nav && (is_major == 5));
    var is_nav5up = (is_nav && (is_major >= 5));
    var is_ie   = (agt.indexOf('msie') != -1);
    var is_ie3  = (is_ie && (is_major < 4));
    var is_ie4  = (is_ie && (is_major == 4) && (agt.indexOf('msie 5.0')==-1) );
    var is_ie4up  = (is_ie  && (is_major >= 4));
    var is_ie5  = (is_ie && (is_major == 4) && (agt.indexOf('msie 5.0')!=-1) );
    var is_ie5up  = (is_ie  && !is_ie3 && !is_ie4);
    var is_opera = (agt.indexOf('opera') != -1);
    var is_webtv = (agt.indexOf('webtv') != -1);

    var resultResolution = "";
    var resultColorDepth = "";
    var resultOS = "";
    var resultCookies = "";
    var resultHasJava = "";
    var resultBrowser = "other";
    var resultBrowserVersion = "";
    var resultHasVB = "";
    var resultReferer = "";
    var resultFlashVersion = "";

    resultResolution = screen.width + ' x ' + screen.height;
    if ( navigator.appName.substring(0,9)=='Microsoft' ) {
        resultColorDepth = screen.colorDepth;
    } else {
        resultColorDepth = screen.pixelDepth;
    }

    idx = agt.indexOf('(');
    idxEnd = agt.indexOf(')', idx+1);
    agtParArr = agt.substring(idx+1, idxEnd).split(";");
    if (agtParArr.length == 2) {
        resultOS = agtParArr[0];
    } else if (agtParArr.length > 2) {
        resultOS = agtParArr[2];
    }

    document.cookie = "cookie_test=1";
    if ( ! document.cookie ) {
        resultCookies = "false";
    } else {
        resultCookies = "true";
    }

    if (navigator.javaEnabled()) {
        resultHasJava = "true";
    } else {
        resultHasJava = "false";
    }

    // firefox: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20050513 Fedora/1.0.4-1.3.1 Firefox/1.0.4:, extra:  Firefox/1.0.4
    //          mozilla/5.0 (windows; u; windows nt 5.1; cs-cz; rv:1.7.8) gecko/20050511 firefox/1.0.4
    // msie 5.0: mozilla/4.0 (compatible; msie 5.01; windows nt 5.0; .net clr 1.0.3705)
    // msie 5.5: mozilla/4.0 (compatible; msie 5.5; windows nt 5.1; sv1)
    // msie 6.0: mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1)
    // mozilla:  mozilla/5.0 (compatible; konqueror/3.1; linux)
    // netscape: mozilla/5.0 (windows; u; windows nt 5.1; en-us; rv:1.4) gecko/20030624 netscape/7.1 (ax)
    if (agt.match(/opera.8\.0/)) {
        resultBrowser = "opera 8.0";
    } else if (agt.indexOf('opera') != -1 ) {
        resultBrowser = "opera";
    } else if (agt.match(/safari.1\.2/)) {
        resultBrowser = "safari 1.2";
    } else if (agt.indexOf('safari') != -1 ) {
        resultBrowser = "safari";
    } else if (agt.indexOf('msie 5.0') != -1 ) {
        resultBrowser = "msie 5.0";
    } else if (agt.indexOf('msie 5.5') != -1 ) {
        resultBrowser = "msie 5.5";
    } else if (agt.indexOf('msie 6.0') != -1 ) {
        resultBrowser = "msie 6.0";
    } else if (agt.indexOf('msie 7.0') != -1 ) {
        resultBrowser = "msie 7.0";
    } else if (agt.indexOf('msie') != -1 ) {
        resultBrowser = "msie";
    } else if (agt.match(/firefox.1\.0/)) {
        resultBrowser = "firefox 1.0";
    } else if (agt.match(/firefox.1\.1/)) {
        resultBrowser = "firefox 1.1";
    } else if (agt.match(/netscape.8\.0/)) {
        resultBrowser = "netscape 8.0";
    } else if (agt.indexOf('netscape') != -1 ) {
        resultBrowser = "netscape";
    } else if ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1)) {
        resultBrowser = "mozilla";
    }

    //if (is_nav4up) {
    //	for (i=0; i<navigator.plugins.length; i++) {
    //		if ( navigator.plugins[i].description.indexOf('Shockwave Flash 4.0')!=-1 )
    //			flash4=1;
    //		if ( navigator.plugins[i].description.indexOf('Shockwave Flash 5.0')!=-1 )
    //			flash5=1;
    //	}
    //}

    //resultBrowserVersion = is_major + "." + is_minor
    resultReferer = document.referrer;

    resultHasJavaScript = "true";

    resultString = "?resultResolution=" + resultResolution;
    resultString = resultString + "&resultColorDepth=" + resultColorDepth;
    resultString = resultString + "&resultOS=" + resultOS;
    resultString = resultString + "&resultCookies=" + resultCookies;
    resultString = resultString + "&resultHasJava=" + resultHasJava;
    resultString = resultString + "&resultBrowser=" + resultBrowser;
    resultString = resultString + "&resultBrowserVersion=" + resultBrowserVersion;
    resultString = resultString + "&resultHasJavaScript=" + resultHasJavaScript;
    //resultString = resultString + "&agt=" + encodeURI(agt);
    //resultString = resultString + "&agt2=" + agt;
    if (url) {
        // after 5 seconds we redirect to different page
        usercapRefreshURL = unescape(url + resultString);
        setTimeout("usercapRefresh()", 2*1000);
    } else {
        resultString = "<img src='/ge/ge/bdm/ucap" + resultString + "' width='1px' height='1px' />";
        //resultString = "";
        document.write(resultString);
        document.close();
    }

} // detect()
