// To determine if the browser is IE or anything else, please place the following script BEFORE the <script/> include
// in the calling page. If dlmCheckIE is not set, this code will fall back to explicity checking userAgent strings, which
// some browsers, like Opera, love to spoof.
//
//<script type="text/javascript">var dlmCheckIE = 0;</script>
//<!--[if IE]>
//<script type="text/javascript">dlmCheckIE = 1;</script>
//<![endif]-->
//


// Main Functions
/////////////////

// gets the DLM Version for any supported browser. Returns "" if the plugin isn't found
function GetDLMVersion()
{   
    if ( IsWinIE() )
        return GetDLMActiveXVersion();
    else
        return GetDLMNetscapeVersion();     
}

// returns true if the DLM is installed for this browser, false otherwise
function IsDLMInstalled()
{    
    var version = GetDLMVersion();    
    return ( version.length > 0 );
}

// returns true if the installed version of the DLM can acquire a transfer URL on its own, false otherwise
// NOTE: If run from an unsupported browser (such as Opera), this will return false
function CanDLMAcquireUrl()
{
    // version 2.2.0 and above can all acquire transfer urls
    return ( CompareVersion("2.2.0", GetDLMVersion()) >= 0 );
}


// Other DLM functions
//////////////////////

// gets the installed DLM Netscape plugin version. If the plugin is not found, "" is returned
function GetDLMNetscapeVersion()
{
    // TFS bug 4158
    // first force the browser to refresh all plugins, just incase we patched with the browser open
    navigator.plugins.refresh(false);
    
    // enumerate all installed plugins & check for DLM
    var num = navigator.plugins.length 
    for( i = 0; i < num; i++)
    {
        if ( navigator.plugins[i].description.indexOf("DLM Netscape Plugin") > -1  )
            return ParseDLM_NPVersion(navigator.plugins[i].description);
    }
    
    return "";
}

// gets the installed DLM ActiveX control version. If the control is not found, "" is returned
// NOTE: Only call under IE on Windows
function GetDLMActiveXVersion()
{
    if ( !IsWinIE() )
        return "";
        
    // simply create the control & return the version
    try
    {
        var FPDC = new ActiveXObject("DLMControl.DLMControlImp.1");                
        return FPDC.GetDLMVersion();
    }
    catch (err)
    {
        return "";
    }
}

// returns the DLM petscaplugin version from the description string
function ParseDLM_NPVersion(desc)
{
    var parsed = desc.split("(");
    if ( parsed.length < 2 )
        return "";
       
    var parsed2 = parsed[1].split(")");
    return parsed2[0];
}


// Misc Helper Functions
////////////////////////

// Compares two version strings. The version string must be delimited with '.' Ex: 2.1.2.76
//
// returns -1 if the left version is greater than the right
// returns 0 if the two versions are equal
// returns 1 if the right version is greater than the left
// Note: this function treats trailing 0's as nothing. So, CompareVersion("2.1.0", "2.1") will return 0
function CompareVersion(left, right)
{        
    var lhs = left.split(".");
    var rhs = right.split(".");
    
    // determine if either have a major / minor / build / etc. number different
    for ( index = 0; index < lhs.length && index < rhs.length; index++ )
    {
        if ( lhs[index] > rhs[index] )
            return -1;
        else if ( lhs[index] < rhs[index] )
            return 1;
    }
    
    // if here, strings are equal to length of shortest
    if ( lhs.length < rhs.length )
    {
        // account for extra 0's
        for ( ; index < rhs.length; index++ )
        {
            if ( rhs[index] != 0 )
                return 1;
        }
    }
    else if ( lhs.length > rhs.length )
    {
        // account for extra 0's
        for ( ; index < lhs.length; index++ )
        {
            if ( lhs[index] != 0 )
                return -1;
        }        
    }
    
    // same version
    return 0;
}

// returns true if Windows IE, false other wise (only works with Windows IE 5.0+)
function IsWinIE()
{    
    // opera likes to say it is IE.. so we need to special case it out
    if ( !isset('dlmCheckIE') )
        return ( (navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1) && (navigator.userAgent.indexOf('Opera') == -1) );
 
    // changed to this sweet Windows IE 5.0+ (MS hating standards) check to avoid Opera and others from spoofing as IE
    return dlmCheckIE;
}

// pass in a string representing the variable name to determine if the variable is set
function isset(varname)
{
    try
    {
        if ( eval(varname) ) {}        
    }
    catch(err)
    {
        return false;
    }
    
    return true;   
}

