/****************************************************************
 * Copyright © 2006-2007 Bell Helicopter Textron Canada Limited *
 *                     All Rights Reserved.                     *
 * Use of this code is subject to a License Agreement with Bell *
 * Helicopter Textron. Any use, modification, reproduction,     *
 * copying or redistribution that is not allowed by the License *
 * Agreement is prohibited.                                     *
 ****************************************************************/

function check_browser()
{
// check for MSIE
var errorLog = '';
var isMSIE = false;
try {
    var err = new Error(123, 456);
    if (err.message == '456') isMSIE = true;
} catch (e) {
    errorLog += e.message + "\n";
}

// check for KHTML (Safari/Apple WebKit/Konqueror etc).
var isKHTML = false;
try {
    // catch window property that seems to be specific to KHTML library
    if ((document != null) // document must be referenced at least once
        && (typeof window['[[DOMDocument.prototype]]'] == "object"))
        isKHTML = true;
} catch (e) {
    errorLog += e.message + "\n";
}

// check for Gecko (Firefox)
var isGecko = false;
try {
    var o = { };
    if (window === o.__parent__)
        isGecko = true;
} catch(e) {
    errorLog += e.message + "\n";
}

// check for Opera
var isOpera = false;
try {
    if ( (window.opera!=null) && (typeof window.opera.version == 'function')
        && (window.opera.version()!='') )
        isOpera = true;
} catch(e) {
    errorLog += e.message + "\n";
}

// check for Chrome
var isChrome = false;
try {
    if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1)        
        isChrome = true;
} catch(e) {
    errorLog += e.message + "\n";
}

// check that we have an acceptable browser
if (errorLog!='') {
    alert("An error occured while checking for your browser type.\n"
        + "The application may not work with this browser.\n"
        + "\n"
        + "Details: " + errorLog);
}
if (isOpera) {
    alert("It appears that you are using Opera. This browser (at least up to"
        + " version 9.02) will not display correctly the application."
        + "\n\n"
        + "Please check the help page for a list of recommanded browsers.\n");
} else if ((isMSIE?1:0) + (isKHTML?1:0) + (isGecko?1:0) + (isChrome?1:0) != 1) {
    alert("The compatibility of the application has not been tested with your browser.\n"
        + "\n"
        + "Please check the help page for a list of recommanded browsers. You"
        + " are likely to experience problems with other browsers."
        + "\n"
        + "(test result: " + (isMSIE?1:0) + (isKHTML?1:0) + (isGecko?1:0) + (isChrome?1:0) + ")");
}

// check for Adobe Reader
var hasPDF = false;
var hasAdobeReader = false;
var readerVersion = null;
try {
    if (window.navigator.mimeTypes.length) {
        var mt = window.navigator.mimeTypes['application/pdf'];
        if (mt) {
            hasPDF = true;
            var p = mt.enabledPlugin;
            if (p) {
                if (/^Adobe Acrobat/.test(p.name)) {
                    hasAdobeReader = true;
                    readerVersion = '0.00';
                    if (/^Adobe Acrobat/.test(p.description)) {
                        var tmp = /Version ([^ ]+)/.exec(p.description);
                        if ((tmp!=null) && (tmp[1]!='')) {
                            readerVersion = tmp[1];
                        }
                    } else if (/^Adobe PDF/.test(p.description)) {
                        hasAdobeReader = true;
                        // Version info was removed from Adobe 8 plug-in
                        // description, so there is no way to know.
                        readerVersion = '8.00';
                    }
                } else if (/^Adobe Reader/.test(p.name)) {
                    hasAdobeReader = true;
                    var tmp = /Reader ([0-9]+\.[0-9]+)/.exec(p.name);
                    if ((tmp!=null) && (tmp[1]!='')) {
                        readerVersion = tmp[1];
                    } else {
                        readerVersion = '7.0';
                    }
                }
            }
        }
    }   
    if (isMSIE && (typeof ActiveXObject == 'function') && !readerVersion) {
        try {
            var o = new ActiveXObject('AcroPDF.PDF');
            hasPDF = true;
            var s = o.GetVersions();
            if (s!='') {
                hasAdobeReader = true;
                var max = '';
                var tmp = s.split(',');
                for (var k in tmp) {
                    var v = new String(tmp[k]).replace(/.*=/, '');
                    if (v > max) max = v;
                }
                readerVersion = max;
            }
        } catch(e) { }
    }
} catch(e) {
    alert("An error occured while checking for PDF capabilities."
        + "\n\nDetails: " + e.message);
}
if (!hasPDF) {
    alert("No PDF support was detected. Please close all browser windows"
        + " and install Adobe Reader before using this application in order"
        + " to be able to read the manuals.");
} else if (!hasAdobeReader) {
    alert("PDF support was detected, but the check for Adobe Reader failed."
        + " Please make sure that Adobe Reader is properly installed and"
        + " configured.");
} else if (parseInt(readerVersion.replace(/\..*/, '') < 7)) {
    alert("An old version of Adobe Reader was found (version " + readerVersion
        + '). Versions older than 7.0.5 will not work correctly. Please'
        + ' upgrade your version of Adobe Reader.');
}

// get the redirect location
var tab1 = (new String(window.location.href)).split('?', 2);
var newURI = '';
if (tab1.length>1) {
    var tab2 = tab1[1].split('&');
    for (var k2 in tab2) {
        var tab3 = tab2[k2].split('=', 2);
        if ( (tab3.length>1) && (tab3[0]=='query') ) {
            newURI = decodeURIComponent(tab3[1]);
            break;
        }
    }
}
if (newURI=='') {
    newURI = 'action=login';
}
newURI = tab1[0] + '?' + newURI;

// redirect to requested page or login
window.location.replace(newURI);
}
