﻿function onSilverlightError(sender, args) {

    var appSource = "";
    if (sender != null && sender != 0) {
        appSource = sender.getHost().Source;
    } 
    var errorType = args.ErrorType;
    var iErrorCode = args.ErrorCode;
    
    var errMsg = "Unhandled Error in Silverlight 2 Application " +  appSource + "\n" ;

    errMsg += "Code: "+ iErrorCode + "    \n";
    errMsg += "Category: " + errorType + "       \n";
    errMsg += "Message: " + args.ErrorMessage + "     \n";

    if (errorType == "ParserError")
    {
        errMsg += "File: " + args.xamlFile + "     \n";
        errMsg += "Line: " + args.lineNumber + "     \n";
        errMsg += "Position: " + args.charPosition + "     \n";
    }
    else if (errorType == "RuntimeError")
    {           
        if (args.lineNumber != 0)
        {
            errMsg += "Line: " + args.lineNumber + "     \n";
            errMsg += "Position: " +  args.charPosition + "     \n";
        }
        errMsg += "MethodName: " + args.methodName + "     \n";
    }

    throw new Error(errMsg);
}



WaitForInstallCompletion = function()
{
    try
    {
        //This forces Firefox/Safari to refresh their 
        //list of known plugins.
        navigator.plugins.refresh();
    }
    catch(e)
    {
        //IE does not support the method, so an 
        //exception will be thrown.
    }
    if ( isSilverlightInstalled() )
    {
        //Silverlight is installed. Refresh the page.
        window.location.reload(false);
    }
    else
    {   
        //Wait 3 seconds and try again
        setTimeout(WaitForInstallCompletion, 3000);
    }    
};

onLoad = function()
{
    //This only works if we are performing a clean install, 
    //not an upgrade.
    if ( !isSilverlightInstalled() )
    {
        //Silverlight is not installed. Try to refresh 
        //the page when it is installed.
        WaitForInstallCompletion();
    }
}

function isSilverlightInstalled()
{
    var isSilverlightInstalled = false;
    
    try
    {
        //check on IE
        try
        {
            var slControl = new ActiveXObject('AgControl.AgControl');
            isSilverlightInstalled = true;
        }
        catch (e)
        {
            //either not installed or not IE. Check Firefox
            if ( navigator.plugins["Silverlight Plug-In"] )
            {
                isSilverlightInstalled = true;
            }
        }
    }
    catch (e)
    {
        //we don't want to leak exceptions. However, you may want
        //to add exception tracking code here.
    }
    return isSilverlightInstalled;
}
