var flashDiv = null;
var pdfDiv = null;
var pdfIFrame = null;
var htmlDiv = null;

// Show a flash movie with arguments at a specific location:
////////////////////////////////////////////////////////////
function showFlash(movie, vars, x, y, width, height) 
{
    if (flashDiv != null)
        removeFlash();

    flashDiv = document.createElement('div');

    flashDiv.style.position = 'absolute';
    flashDiv.style.left = x;
    flashDiv.style.top = y;
    flashDiv.style.width = width;
    flashDiv.style.height = height;

    flashDiv.innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' +
                                 'id="wizard" width="100%" height="100%"' + 
                                 'codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">' + 
                             '<param name="movie" value="' + movie + '" />' + 
                             '<param name="quality" value="high" />' + 
                             '<param name="bgcolor" value="#869ca7" />' + 
                             '<param name="allowScriptAccess" value="sameDomain" />' + 
                             '<param name="allowFullScreen" value="false" />' + 
                             '<param name="flashVars" value="' + vars + '"/>' + 
                             '<embed src="' + movie + '" pluginspage="http://www.adobe.com/go/getflashplayer"' + 
                                    'quality="high" bgcolor="#869ca7" width="100%" height="100%" name="flashContent"' + 
                                    'align="middle" play="true" loop="false" quality="high" allowScriptAccess="sameDomain"' + 
                                    'allowFullScreen="false" type="application/x-shockwave-flash"' + 
                                    'wmode="opaque" flashVars="' + vars + '">' + 
                             '</embed>' + 
                         '</object>';

    var bodyTag = document.getElementsByTagName("body")[0];
    bodyTag.appendChild(flashDiv);
}

// Remove the flash player:
///////////////////////////
function removeFlash() 
{
    if (flashDiv != null) 
    {
        var bodyTag = document.getElementsByTagName("body")[0];
        bodyTag.removeChild(flashDiv);
        flashDiv = null;
    }
}

// Show a PDF file at a specific location:
//////////////////////////////////////////
function showPdf(file, x, y, width, height) 
{
    if (pdfDiv != null)
        removePdf();

    pdfDiv = document.createElement('div');

    pdfDiv.style.position = 'absolute';
    pdfDiv.style.left = x;
    pdfDiv.style.top = y;
    pdfDiv.style.width = width;
    pdfDiv.style.height = height;

    var bodyTag = document.getElementsByTagName("body")[0];
    bodyTag.appendChild(pdfDiv);

    pdfIFrame = document.createElement('iframe');

    pdfIFrame.id = new Date().getTime();
    pdfIFrame.src = file;
    pdfIFrame.style.border = 'none';
    pdfIFrame.style.width = width;
    pdfIFrame.style.height = height;

    pdfDiv.appendChild(pdfIFrame);
}

// Remove the PDF file:
///////////////////////
function removePdf() 
{
    if ((pdfDiv != null) && (pdfIFrame != null)) 
    {
        pdfDiv.removeChild(pdfIFrame);
        pdfIFrame = null;

        var bodyTag = document.getElementsByTagName("body")[0];
        bodyTag.removeChild(pdfDiv);
        pdfDiv = null;
    }
}

function hidePdf() 
{
    if (pdfDiv != null)
        pdfDiv.style.visibility = 'hidden';
}

function revealPdf() 
{
    if (pdfDiv != null)
        pdfDiv.style.visibility = 'visible';
}

// Update the PDF file being shown:
///////////////////////////////////
function updatePdf(file) 
{
    if (pdfIFrame != null)
        pdfIFrame.src = file;

    // Dynamic update doesn't work on Safari on Windows!
}

// Move the PDF to the specified location:
//////////////////////////////////////////
function positionPdf(x, y, width, height) 
{
    if ((pdfDiv != null) && (pdfIFrame != null)) 
    {
        pdfDiv.style.left = x;
        pdfDiv.style.top = y;
        pdfDiv.style.width = width;
        pdfDiv.style.height = height;

        pdfIFrame.style.width = width;
        pdfIFrame.style.height = height;
    }
}

// Show the HTML file:
//////////////////////
function showHtml(url, x, y, width, height) 
{
    if (htmlDiv != null)
        removeHtml();

    htmlDiv = document.createElement('div');

    htmlDiv.style.position = 'absolute';
    htmlDiv.style.left = x;
    htmlDiv.style.top = y;
    htmlDiv.style.width = width;
    htmlDiv.style.height = height;

    var bodyTag = document.getElementsByTagName("body")[0];
    bodyTag.appendChild(htmlDiv);

    htmlDiv.innerHTML = "<iframe id='htmlFrameId' src='" + url + "' frameborder='0' allowTransparency='true'" + 
                                "style='width:" + width + "; height: " + height + "'></iframe>";
}

// Remove the HTML file:
////////////////////////
function removeHtml() 
{
    if (htmlDiv != null) 
    {
        var htmlIFrame = document.getElementById("htmlFrameId");

        if (htmlIFrame != null)
            htmlDiv.removeChild(htmlIFrame);

        var bodyTag = document.getElementsByTagName("body")[0];
        bodyTag.removeChild(htmlDiv);
        htmlDiv = null;
    }
}

function hideHtml() 
{
    if (htmlDiv != null)
        htmlDiv.style.visibility = 'hidden';
}

function revealHtml() 
{
    if (htmlDiv != null)
        htmlDiv.style.visibility = 'visible';
}

// Update the HTML file:
////////////////////////
function updateHtml(url) 
{
    if (htmlDiv != null) 
    {
        var htmlIFrame = document.getElementById("htmlFrameId");

        if (htmlIFrame != null)
            htmlIFrame.src = url;
    }
}

// Move the HTML to the specified location:
///////////////////////////////////////////
function positionHtml(x, y, width, height) 
{
    if (htmlDiv != null) 
    {
        htmlDiv.style.left = x;
        htmlDiv.style.top = y;
        htmlDiv.style.width = width;
        htmlDiv.style.height = height;

        var htmlIFrame = document.getElementById("htmlFrameId");

        if (htmlIFrame != null) 
        {
            htmlIFrame.style.width = width;
            htmlIFrame.style.height = height;
        }
    }
}

// Close the browser window/tab:
////////////////////////////////
function closeWindow() 
{
    window.close();
}

// Confirm Exit when document has changed:
//////////////////////////////////////////
window.onbeforeunload = confirmExit;

function confirmExit() 
{
    var flashPlayer = document.getElementById("flashPlayer");

    if (flashPlayer != null) 
    {
        if (flashPlayer.documentChanged()) 
        {
            flashPlayer.saveDocument();

            var exitMessage = flashPlayer.confirmExitMessage();
            
            if (exitMessage != "")
            {
                return flashPlayer.confirmExitMessage();

                // Returning a text from this event handler causes a popup to be displayed.
                // Note that this popup is needed to give the save request the chance to be send to the server.
            }
        }
    }
}

// Get an argument that was specified in the URL:
/////////////////////////////////////////////////
function getArgument(key, defaultValue) 
{
    var args = document.location.search;
    var n = args.indexOf(key + "=");

    if (n == -1)
        return defaultValue;

    var value = args.substr(n + key.length + 1);
    var m = value.indexOf("&");

    if (m != -1)
        value = value.substr(0, m);

    return unescape(value);
}

function getAdobeReaderVersion() 
{
    if (navigator.plugins && navigator.plugins.length) 
    {
        for (var x = 0; x < navigator.plugins.length; x++) 
        {
            if (navigator.plugins[x].description.indexOf('Adobe PDF Plug-In') != -1) 
            {
                return "8.0";
            } 
            else if (navigator.plugins[x].description.indexOf('Adobe Acrobat') != -1) 
            {
                var version = parseFloat(navigator.plugins[x].description.split('Version ')[1]);

                if (version.toString().length == 1)
                    version += ".0";

                return version;
            }
        }
    } 
    else if (window.ActiveXObject) 
    {
        try 
        {
            var oAcro7 = new ActiveXObject('AcroPDF.PDF.1');

            if (oAcro7 != null)
                return "7.0";
        } 
        catch(e) {}

        try 
        {
            var oAcro4 = new ActiveXObject('PDF.PdfCtrl.1');

            if (oAcro4 != null)
                return "4.0";
        } 
        catch(e) {}

        for (var x = 2; x < 10; x++) 
        {
            try 
            {
                var oAcro = eval("new ActiveXObject('PDF.PdfCtrl." + x + "');");

                if (oAcro != null)
                    return x + ".0";
            } 
            catch(e) {}
        }

    }

    return "Not installed";
}

function getBrowserName() 
{
    return BrowserDetect.browser;
}

function getBrowserVersion() 
{
    return BrowserDetect.version;
}

function getBrowserLanguage() 
{
    return navigator.appName;

    if (navigator.appName == 'Netscape')
        return navigator.language; 
    else
        return navigator.browserlanguage;
}

function setBrowserFocus() 
{
    document.getElementById('flashPlayer').focus(); // Doesn't work on Chrome, works on FF and IE.
}

