/* TLE Active Site Client Javascript
   Copyright 2008 TLE - AMM

   Version:
   1.0 - 2 October 2008
   
   Dependencies: 
   none   
*/

// Global objects
clientBrowser = '';
clientMozBrowser = '';
clientVersion = 0;
clientPlatform = '';
clientScreenX = 0;
clientScreenY = 0;

// Get client details
function getClient() {
    
    var type = '';
    var moztype = '';
    var ver = 0;
    var plat = '';
    
    // Init browser objects
    var d = document;
    var n = navigator;
    var nav = n.appVersion;
    var nan = n.appName;
    var nua = n.userAgent;
    
    // Init objects
    var old, mac, win, lin;
    var op, saf, konq, moz, ie;
    var moz_types = new Array( 'Firebird', 'Phoenix', 'Firefox', 'Iceweasel', 'Galeon', 'K-Meleon', 'Camino', 'Epiphany', 'Netscape6', 'Netscape', 'MultiZilla', 'Gecko Debian', 'rv' );
    
    // Test document object
    if (!document.layers) {
        dom = (d.getElementById) ? d.getElementById : false;
    }
    else {
        type = 'Netscape';
        ver = 4;
    }
    
    // Get platform objects
    old = (nav.substring(0,1) < 4);
    mac = (nav.indexOf('Mac') != -1);
    win = (((nav.indexOf('Win') != -1) || (nav.indexOf('NT') != -1)) && !mac) ? true : false;
    lin = (nua.indexOf('Linux') != -1);

    // Get browser type objects
    op = (nua.indexOf('Opera') != -1);
    saf = (nua.indexOf('Safari') != -1);
    konq =(!saf && (nua.indexOf('Konqueror') != -1)) ? true : false;
    moz = ((!saf && !konq) && (nua.indexOf('Gecko') != -1)) ? true : false;
    ie = ((nua.indexOf('MSIE') != -1) && !op);
    
    // Process browser type
    if (op) {                   // Opera
        str_pos = nua.indexOf('Opera');
        ver = nua.substr((str_pos + 6),4);
        type = 'Opera';
    }
    else if (saf) {             // Safari
        str_pos = nua.indexOf('Safari');
        ver= nua.substr((str_pos + 7),5);
        type = 'Safari';
    }
    else if (konq) {            // Konqueror
        str_pos = nua.indexOf('Konqueror');
        ver = nua.substr((str_pos + 10),3);
        type = 'Konqueror';
    }
    else if (ie) {              // Internet Explorer
        str_pos = nua.indexOf('MSIE');
        ver = nua.substr((str_pos + 5),3);
        type = 'Internet Explorer';
    }
    else if (moz)               // Mozilla variant
    {
        // Find mozilla type in the known array
        pattern = /[(); \n]/;        
        rv_pos = nua.indexOf( 'rv' );
        rv_full = nua.substr( rv_pos + 3, 6 );
        rv_slice = ( rv_full.search( pattern ) != -1 ) ? rv_full.search( pattern ) : '';
        (rv_slice) ? rv_full = rv_full.substr( 0, rv_slice ) : '';
        nu = rv_full.substr( 0, 3 );
        for (i = 0; i < moz_types.length; i++) {
            if (nua.indexOf( moz_types[i]) != -1) {
                moz_brow = moz_types[i];
                break;
            }
        }
        
        // Get mozilla type if not found in the array 
        if (moz_brow) {
            str_pos=nua.indexOf(moz_brow);
            moz_brow_nu = nua.substr( (str_pos + moz_brow.length + 1 ) ,3);
            moz_brow_nu = ( isNaN( moz_brow_nu ) ) ? moz_brow_nu = nu: moz_brow_nu;
            moz_brow_nu_sub = nua.substr( (str_pos + moz_brow.length + 1 ), 8);
            sub_nu_slice = ( moz_brow_nu_sub.search( pattern ) != -1 ) ? moz_brow_nu_sub.search( pattern ) : '';
            (sub_nu_slice) ? moz_brow_nu_sub = moz_brow_nu_sub.substr( 0, sub_nu_slice ) : '';
        }
        
        // Process mozilla type
        if (moz_brow == 'Netscape6') {                          // Handle Netscape6
            moz_brow = 'Netscape';
        }
        else if (moz_brow == 'rv' || moz_brow == '') {          // Default value if no other gecko name fit 
            moz_brow = 'Mozilla';
        } 
        if (!moz_brow_nu) {                                     // Use rv number if nothing else is available
            moz_brow_nu = nu;
            moz_brow_nu_sub = nu;
        }
        
        moztype = moz_brow; 
        type = 'Mozilla';
        ver = moz_brow_nu; 
    }
    else {                      // default to navigator app name
        type = 'Unknown';
    }
 
/*
op5=(op&&(nu.substring(0,1)==5));
op6=(op&&(nu.substring(0,1)==6));
op7=(op&&(nu.substring(0,1)==7));
op8=(op&&(nu.substring(0,1)==8));
op9=(op&&(nu.substring(0,1)==9));
ie4=(ie&&!dom);
ie5=(ie&&(nu.substring(0,1)==5));
ie6=(ie&&(nu.substring(0,1)==6));
ie7=(ie&&(nu.substring(0,1)==7));
*/
    
    // Process platform
    if (lin)
        plat = 'Linux';
    else if (mac)
        plat = 'Macintosh';
    else if (win)
        plat = 'Windows';
    else
        plat = 'Unknown';
        
    // Screen size
    clientScreenX = screen.width;
    clientScreenY = screen.height;
    
    // Write globals
    clientBrowser = type;
    clientMozBrowser = moztype;
    clientVersion = parseFloat(ver);
    clientPlatform = plat;
    
    // Debug
    //alert('browser='+clientBrowser + ' version=' + clientVersion + ' on ' + clientPlatform + ' : ' + clientScreenX + 'x' + clientScreenY);
}

// Check client
function checkClient() {

    // Init
    bClient = false;
    
    // Allowed clients
    if ((clientBrowser.indexOf('Internet Explorer') == 0) && (clientVersion >= 7.0)) bClient = true;
    if ((clientBrowser.indexOf('Opera') == 0) && (clientVersion >= 8.0)) bClient = true;
    if ((clientBrowser.indexOf('Safari') == 0) && (clientVersion >= 4.0)) bClient = true;    
    if (clientBrowser.indexOf('Mozilla') == 0) {
       if ((clientMozBrowser.indexOf('Firefox') == 0) && (clientVersion >= 3.0)) bClient = true;    
    }
    
    return bClient; 
}

