//
// clientdetect.js
// Copyright (c) 2003 Citrix Systems, Inc. All Rights Reserved.
//

function detectClient() {
    // Pull userAgent from browser.
    var usrAgt = navigator.userAgent.toLowerCase();
    // isApplet is automatically set by the NFuse Web Site Wizard.
    var isApplet = false;

    // #########################
    // ### BROWSER DETECTION ###
    // #########################
    // booleans:
    // ie  : Internet Explorer
    // nav : Netscape Navigator

    // A small percentage of IE browsers return 'Internet Explorer' instead
    // of 'MSIE' in usrAgt.
    var ie = ((usrAgt.indexOf("msie") != -1)
              || (usrAgt.indexOf("internet explorer") != -1));

    // A small percentage of IE browsers do not include 'compatible' in
    // usrAgt; also check for 'MSIE'.
    var nav = ((usrAgt.indexOf('mozilla') != -1)
               && (usrAgt.indexOf('msie') == -1)
               && (usrAgt.indexOf('spoofer') == -1)
               && (usrAgt.indexOf('compatible') == -1)
               && (usrAgt.indexOf('opera') == -1)
               && (usrAgt.indexOf('webtv') == -1));

    // ##########################
    // ### PLATFORM DETECTION ###
    // ##########################
    // booleans:
    // osWin32      osSun      osLinux
    // osWin16      osSgi      osSco
    // osOs2        osHpux     osTru64
    // osMac        osAix      osUnix

    //  32-BIT WINDOWS
    //  Native: 32-bit ICA Client || Web: ActiveX ICA Client if (ie=true),
    //  32-bit Plug-in ICA Client if (nav=true)
    //  http://download.citrix.com/cgi-bin/license.cgi?client=w32
    osWin32 = (((usrAgt.indexOf("win95") != -1)
                || (usrAgt.indexOf("windows 95") != -1)
                || (usrAgt.indexOf("win98") != -1)
                || (usrAgt.indexOf("windows 98") != -1))
               || ((usrAgt.indexOf("winnt") != -1)
                       || (usrAgt.indexOf("windows nt") != -1))
               || ((this.major >= 4)
                       && (navigator.platform == "Win32"))
               || (usrAgt.indexOf("win32") != -1)
               || (usrAgt.indexOf("32bit") != -1));

    // Windows CE
    osWinCE = (usrAgt.indexOf("windows ce") != -1);

    //  16-BIT WINDOWS
    //  Native: Win16 ICA Client || Web: 16-bit Plug-in ICA Client if (ie
    //  || nav)
    //  http://download.citrix.com/cgi-bin/license.cgi?client=w16
    osWin16 = ((usrAgt.indexOf("win") != -1)
               || (usrAgt.indexOf("16bit") != -1)
               || (usrAgt.indexOf("windows 3.1") != -1)
               || (usrAgt.indexOf("windows 16-bit") != -1));

    // Netscape 4.08 browsers have problems running our Javascript to
    // detect the client. We don't do any client detection for this
    // browser.
    noDetectAgent = nav && (usrAgt.indexOf("4.08") !=  -1);

    //  OS/2
    //  Native: Win16 ICA Client || Web: Java Applet ICA Client
    //  http://download.citrix.com/cgi-bin/license.cgi?client=w16
    osOs2   = ((usrAgt.indexOf("os/2") != -1)
               || (navigator.appVersion.indexOf("OS/2") != -1)
               || (usrAgt.indexOf("ibm-webexplorer") != -1));

    //  MACINTOSH
    //  Native: Macintosh ICA Client || Web: Java Applet ICA Client
    //  http://download.citrix.com/cgi-bin/license.cgi?client=mac
    osMac    = (usrAgt.indexOf("mac") != -1);

    //  SUN SUNOS, SOLARIS/SPARC, SOLARIS/X86

    //  Native: SunOS or Solaris/Sparc or Solaris/x86 ICA Client || Web:
    //  Java Applet ICA Client
    //  Having trouble differentiating these; all show as 'sunos'.
    //  The version numbers might be a good tip-off if we want to refine
    //  this further.  In the meantime, having all 3 clients offered might
    //  be a good idea.
    //  http://download.citrix.com/cgi-bin/license.cgi?client=unix_sunos
    //  http://download.citrix.com/cgi-bin/license.cgi?client=unix_sparc
    //  http://download.citrix.com/cgi-bin/license.cgi?client=unix_x86
    osSun   = (usrAgt.indexOf("sunos") != -1);

    //  SGI UNIX ICA Client
    //  Native: SGI ICA Client || Web: Java Applet ICA Client
    //  http://download.citrix.com/cgi-bin/license.cgi?client=unix_sgi
    osSgi  = (usrAgt.indexOf("irix") != -1);

    //  HP/UX UNIX ICA Client
    //  Native: HP/UX ICA Client || Web: Java Applet ICA Client
    //  http://download.citrix.com/cgi-bin/license.cgi?client=unix_hpux
    osHpux  = (usrAgt.indexOf("hp-ux") != -1);

    //  IBM AIX UNIX
    //  Native: IBM AIX ICA Client || Web: Java Applet ICA Client
    //  http://download.citrix.com/cgi-bin/license.cgi?client=unix_ibm
    osAix   = (usrAgt.indexOf("aix") != -1);

    //  Linux
    //  Native: Linux ICA Client || Web: Java Applet ICA Client
    //  http://download.citrix.com/cgi-bin/license.cgi?client=linux
    osLinux = (usrAgt.indexOf("linux") != -1);

    //  SCO UNIX
    //  Native: SCO ICA Client || Web: Java Applet ICA Client
    //  http://download.citrix.com/cgi-bin/license.cgi?client=unix_sco
    osSco   = (usrAgt.indexOf("sco") != -1) || (usrAgt.indexOf("unix_sv") != -1);

    //  Compaq Tru64 UNIX ICA Client (DEC)
    //  Native: Compaq Tru64 ICA Client || Web: Java Applet ICA Client
    //  http://download.citrix.com/cgi-bin/license.cgi?client=unix_tru64
    osTru64   = ((usrAgt.indexOf("dec") != -1)
                 || (usrAgt.indexOf("osf1") != -1)
                 || (usrAgt.indexOf("dec_alpha") != -1)
                 || (usrAgt.indexOf("alphaserver") != -1)
                 ||  (usrAgt.indexOf("ultrix") != -1)
                 || (usrAgt.indexOf("alphastation") != -1)
                 || (usrAgt.indexOf("digitalunix") != -1)
                 || (usrAgt.indexOf("tru64") != -1));

    //  Non-matched variety of UNIX... should probably point to a "let the
    //  user decide" sort of page.  Also marks any UNIX-variety OSes caught
    //  above.  Most matches will likely come from Sinix, BSD, FreeBSD,
    //  Reliant, etc...
    osUnix  = ((usrAgt.indexOf("x11") != -1)
               || osSun
               || osSgi
               || osHpux
               || osAix
               || osSco
               || osTru64
               || osLinux);

    // ### Setting of icaClientCode, used with icaClientInfo.js to retrieve
    // data on appropriate ICA client.
    // ICACLIENT CODES:
    //      -1: UnSupported client
    //   1: Win32
    //   2: Win16
    //   3: Win16_Plugin
    //   4: Mac
    //   5: Solaris/Sparc
    //   6: Solaris/x86
    //   7: SunOS
    //   8: SGI
    //   9: HP/UX
    //  10: IBM/AIX
    //  11: SCO
    //  12: DEC/Tru64
    //  13: Linux
    //  14: JAVA Application
    //  15: Java Applet
    //  16: Windows CE

    icaClientCode = -1;

    // Some WBTs report themselves as "Windows NT Windows CE" so the order we check the
    // OS type is crucial.
    if (osWinCE) {
        icaClientCode = 16;
    } else if (osWin32) {
        icaClientCode = 1;
    } else if (osWin16) {
        icaClientCode = 2;
    } else if (isApplet) {
        icaClientCode = 15;
    } else if (osOs2) {
        icaClientCode = 2;
    } else if (osMac) {
        icaClientCode = 4;
    } else if (osSun) {
        icaClientCode = 0;
    } else if (osSgi) {
        icaClientCode = 8;
    } else if (osHpux) {
        icaClientCode = 9;
    } else if (osAix) {
        icaClientCode = 10;
    } else if (osSco) {
        icaClientCode = 11;
    } else if (osTru64) {
        icaClientCode = 12;
    } else if (osLinux) {
        icaClientCode = 13;
    } else {
        icaClientCode = 14;
    }

    // ### Setting of icaObjectCode.
    // ICAOBJECT CODES:
    //   0: Native
    //   1: ActiveX
    //   2: Plugin32
    //   3: Plugin16

    // Some WBTs report themselves as "Windows NT Windows CE" so the order we check the
    // OS type is crucial.
    if (ie && osWinCE) {
        icaObjectCode = 1;
        icaClientAvailable = true;
    } else if (ie && osWin32) {
        icaObjectCode = 1;
        icaClientAvailable = hasIcaObjVal();
    } else if (nav && osWin32) {
        icaObjectCode = 2;
        icaClientAvailable = isPlugIn();
    } else if (osWin16 && (ie || nav)) {
        icaObjectCode = 3;
        icaClientAvailable = isPlugIn();
    } else {
        icaObjectCode = 0;
        icaClientAvailable = false;
    }

    // Add 100 to indicate that we don't want any client detection.
    if (noDetectAgent) {
        icaObjectCode += 100;
    }

    // ### Setting of icaBrowserCode
    // ICABROWSER CODES:
    //       0: Unknown
    //       1: IE
    //       2: Netscape

    if (ie) {
        icaBrowserCode = 1;
    } else if (nav) {
        icaBrowserCode = 2;
    } else {
        icaBrowserCode = 0;
    }

    // Get screen resolution for embedded apps as a percentage of screen size
    var ScreenWidth, ScreenHeight, strResolution;
    ScreenWidth = window.screen.width;
    ScreenHeight = window.screen.height;
    strResolution = ScreenWidth + 'x' + ScreenHeight;

    // ### Store cookies on user browser for NFuse use.

    document.cookie = "icaClientCode=" + icaClientCode + ";";
    document.cookie = "icaObjectCode=" + icaObjectCode + ";";
    document.cookie = "icaClientAvailable=" + icaClientAvailable + ";";
    document.cookie = "icaBrowserCode=" + icaBrowserCode + ";";
    document.cookie = "icaScreenResolution=" + strResolution + ";";
}

function isPlugIn() {
    var idx;
    var plugIn;
    var mimeIdx;
    var mimeCount;
    var found = false;
    var count = navigator.plugins.length;

    for (idx = 0; (!found) && (idx < count); idx++) {
        plugIn = navigator.plugins[idx];
        mimeCount = plugIn.length;
        for (mimeIdx = 0; (!found) && (mimeIdx < mimeCount); mimeIdx++) {
            if (plugIn[mimeIdx].type == "application/x-ica") {
                found = true;
            }
        }
    }
    return found;
}

/*
 * Perform client detection
 */
function clientdetection(url) {
    detectClient();
    var newloc = new String(((url == null) || (url == "")) ? location : url);
    var separator = (newloc.indexOf("?") < 0) ? "?" : "&";
    newloc = newloc + separator + "ClientDetection=On";
    location.replace(newloc);
}
