﻿//---------------------------------------------------------------------------
function RunDetection(detectionXml, resultsElementId, numberOfRetries)
{
    if (!numberOfRetries)
    {
        numberOfRetries = 0;
    }

    try
    {
        toggleDetectionLayout(true);

        var detectionApplet = GetDetectionApplet();
    
        //If we didn't find the applet attempt to run detection again after waiting a few seconds.
        //This is mainly for Safari Mac.
        if ((typeof (detectionApplet) == 'undefined' || detectionApplet == null 
            || typeof (detectionApplet.sendAppletInput) == 'undefined')
			&& numberOfRetries > 0)
        {
            setTimeout(function() { RunDetection(detectionXml, resultsElementId, --numberOfRetries); }, 2000);
        }
        else
        {    
            resultsElement = document.getElementById(resultsElementId)
            var isSuccessful = RunDetection_Applet(detectionXml, resultsElement, detectionApplet);
            if (isSuccessful)
            {
                document.frmDetection.submit();
            }
            else
            {
                toggleDetectionLayout(false);
            }
        }
    }
    catch (err)
    {
        toggleDetectionLayout(false);
    }
}
function toggleDetectionLayout(fromErrorToDetection)
{
    detectionRunningElement = document.getElementById("detectionRunning");
    detectionErrorElement = document.getElementById("detectionError");
    if (fromErrorToDetection)
    {
        detectionRunningElement.style.display = "block";
        detectionErrorElement.style.display = "none";
    }
    else
    {
        detectionRunningElement.style.display = "none";
        detectionErrorElement.style.display = "block";
    }
}

////---------------------------------------------------------------------------
//function RunDetection_AX(detectionXml, resultsElement)
//{
//    if ((typeof (_ApperangAX) != "undefined") && (_ApperangAX !== null) && (_ApperangAX.object !== null))
//    {
//        if (_ApperangAX.RunDetection(detectionXml))
//        {
//            var result = _ApperangAX.GetDetectionResult();
//            if ((result !== null) && (result != ''))
//            {
//                if (resultsElement)
//                {
//                    resultsElement.value = HTMLEncode(result);
//                }
//            }
//        }
//    }
//}

//---------------------------------------------------------------------------
function RunDetection_Applet(detectionXml, resultsElement, detectionApplet)
{
    if(detectionApplet != null)
    {
        var result = detectionApplet.sendAppletInput(detectionXml, false, null, null, null);
        //var url = _apperangRestUri + _saveDetectionResultsFunc + "/Put"
        //var result = detectionApplet.sendAppletInput(_detectionXml, true, url, _appUserId, _appUserSessionId);

        if ((result !== null) && (result != ''))
        {
            if (resultsElement)
            {
                resultsElement.value = HTMLEncode(result);
                return true;
            }
        }
    }
    return false;
}

//---------------------------------------------------------------------------
function GetDetectionApplet()
{
    var detectionApplet = document.DetectionApplet

    //If we didn't find the applet by name then look in the applets collection.
    //This is mainly for Chrome and Opera.
    if ((detectionApplet == undefined || detectionApplet == null))
    {
        if (document.getElementById('DetectionApplet') != null)
        {
            detectionApplet = document.getElementById('DetectionApplet');
        }
        else if (document.applets != null)
        {
            detectionApplet = document.applets[0];
        }
        else if (document.embeds != null && document.embeds['DetectionApplet'] != null)
        {
            detectionApplet = document.embeds['DetectionApplet'];
        }
    }

    return detectionApplet;
}

//---------------------------------------------------------------------------
function OnLoadDetection()
{
    RunDetection(_detectionXml, _resultsElementId, 5);
}

//---------------------------------------------------------------------------
function RunCloseDetection(appUserId, appUserSessionId, apperangAX, detectionApplet, apperangRestUri, saveDetectionResultsFunc)
{
    //    if ((appUserId !== null) && (appUserSessionId !== null)
    //		        && (typeof (apperangAX) != "undefined") && (apperangAX !== null) && (apperangAX.object !== null))
    //    {
    //        _ApperangAX.SetProperty("baseApiUrl", apperangRestUri);
    //        _ApperangAX.SendResults(saveDetectionResultsFunc, appUserId, appUserSessionId);
    //    }
}

//---------------------------------------------------------------------------
function OnUnloadDetection()
{
    RunCloseDetection(_appUserId, _appUserSessionId, _apperangAX, _detectionApplet, _apperangRestUri, _saveDetectionResultsFunc)
}

//---------------------------------------------------------------------------
function HTMLEncode(value)
{
    var div = document.createElement("div");
    var text = document.createTextNode(value);
    div.appendChild(text);
    return div.innerHTML;
}

//---------------------------------------------------------------------------
// Need to includ the following tag on the page for this function to work.
// <script type="text/javascript" src="http://java.com/js/deployJava.js"></script>
function IsJavaInstalled()
{
    if ((deployJava.getJREs() === null) || (deployJava.getJREs() == ''))
    {
        return false;
    }

    return true;
}

//---------------------------------------------------------------------------
function IsBrowserIE()
{
    if (navigator.userAgent.match(/MSIE/) != null)
    {
        return true;
    }

    return false;
}

//---------------------------------------------------------------------------
function UpdateProgress(progress) {
	//alert("Progress: " + progress);
	var progressbarInnerWidth = (progress * 3.6) + "px";
	$("#progressbarInner").css("width", progressbarInnerWidth);
}
