﻿function check_open_window(downloadUrl)
{
    // Only called by IE 7 to see if we launched the program ok.
    if (comradePopup.closed == false)
    {
        //The popup is still open.  In IE we launched ok, in other browsers, we don't know.
        comrade_popup_close();
    }
    else
    {
        //IE 7 will tell us we have a closed window when the protocol failed, but it is still open.  Close it.
        comrade_fail_ie(downloadUrl);
    }
}

function check_open_iframe(downloadUrl)
{
   try
   {
        if (window.frames["dlfrm"].location.href.length > 0)
        {
             comrade_popup_close();
        }
        else
        {
            comrade_fail_ie(downloadUrl);
        }
   }
   catch (err)
   {
        comrade_fail_ie(downloadUrl);
   }
}

function start_download_click(downloadUrl) 
{
    var launchedOk = 1;
    if (navigator.appName.match("Microsoft") == "Microsoft")
    {
        //Check version of IE
        if (navigator.appVersion.match("MSIE 7.0") == "MSIE 7.0" || navigator.appVersion.match("MSIE 8.0") == "MSIE 8.0")
        {
            comradePopup.location.replace(downloadUrl);
            
            //We need to let the window open and render.  1 second should be long enough.
            setTimeout(function(){
                check_open_window(downloadUrl);
            }, 1000);
        }
        else
        {
            window.frames["dlfrm"].location = downloadUrl;
            //We need to let the window re-layout.
            setTimeout(function(){
                check_open_iframe(downloadUrl);
            }, 1000);
        }
    }
    else
    {
        var hasPlugin =  mimeTypeExists();
        
        if ( hasPlugin )
        {
            try
            {
                // Dummy fast loading web site.
                comradePopup.location.replace(downloadUrl);
            }
            catch(err)
            {
                launchedOk = 0;
            }

            if (launchedOk == 0)
            {
                comrade_fail_ff(downloadUrl);
            }
            else
            {
               comrade_popup_close();
            }
        }
        else
        {
            comrade_fail_ff(downloadUrl);
        }
    }
}

function comrade_popup_open(popupUrl) 
{
    //Change the size of the window depending on the browser version.
    if (navigator.appName.match("Microsoft") == "Microsoft")
    {
        comradePopup = window.open(popupUrl, "comradeDL", "location=no, menubar=no, height=505, width=600, status=no, resizable=no, scrollbar=no");
    }
    else
    {
        comradePopup = window.open(popupUrl, "comradeDL", "location=no, menubar=no, height=460, width=600, status=no, resizable=no, scrollbar=no");
    }    
}

var comrade_timer = -1;
function comrade_popup_close() 
{
    //Sometimes the web browser will pop-up a dialog for the user.  When this happens, the close() is not processed.
    //So we loop until the popup is closed.
	if((typeof(comradePopup) != "undefined") && (comradePopup.closed == false))
	{
       comradePopup.close();
       if (comrade_timer == -1)
       {
           comrade_timer = setInterval("comrade_popup_close()", 1000);
           //Close overlay
           tb_remove();
       }
    }
    else
    {
        clearInterval(comrade_timer);
        comrade_timer = -1;	
    }
}

function comrade_fail_ie(downloadUrl)
{
    //Clear out the error on the page with an empty page before we bring the window forward for the user.
    comrade_popup_open(EmptyPageUrl);
    tb_remove(true);
    comrade_popup_open(dlLink);
    comradePopup.focus();}

function comrade_fail_ff(downloadUrl)
{
    //The popup has been closed, so do nothing.
	if((typeof(comradePopup) != "undefined") && (comradePopup.closed == true))
	{
        tb_remove();
        return;
    }
    
    tb_remove(true);
    downloadProductDLM();
}

//Fallback to DLM overlay
function downloadProductDLM()
{
    comradePopup.location.replace(dlLink);
    comradePopup.focus();
    window.blur();
}

function mimeTypeExists()
{
    navigator.plugins.refresh();
    if (navigator.mimeTypes["application/x-comrade"])
    {
        return true;
    }
    else
    {
        return false;
    }
}