

var bDocReady = false;
var bInsObj   = false;
var isLinux   = (navigator.userAgent.indexOf("Linux") >= 0);
var isMacFF   = (navigator.userAgent.indexOf("Firefox") >= 0 && navigator.userAgent.indexOf("Mac") >= 0);
var isSafari  = (navigator.userAgent.indexOf("Safari") >= 0);
var isSolaris = (navigator.userAgent.indexOf("Sun") >= 0);
var isWinFF   = (navigator.userAgent.indexOf("Firefox") >= 0 && navigator.userAgent.indexOf("Windows") >= 0);
var isIE7     = (navigator.userAgent.indexOf("MSIE 7") >= 0);
var isIE8     = (navigator.userAgent.indexOf("MSIE 8") >= 0);			
var bdmIsReady = false;
var bDMStarted = false;
var bDMFailed  = false;
var bShutdown  = false;
var startTries = 0;

function doLoad() {
    bDocReady = true;
    return;
}

function closeIt() {
    if (isIE7) {
            return;
    }
    if (isIE8) {
            return;
    }				

    if (bDMStarted && !bShutdown) {
        event.returnValue = "The Download Manager is still running.\n" +
            "Pressing 'OK' will stop any active downloads and close the Download Manager.";
    }
}

// Initiate shutdown
function doDLMShutdown() {
    if (bShutdown) {
        return;
    }

    bShutdown = true;
    window.opener = null;
	//window.close();
}

// Initiate the download
function doStart() {
    startTries++;
    if (startTries > 120) {
        bDMFailed = true;
        return;
    }

    try {
        var dm = document.getElementById("dm");
        if (dm == null) {
            bDMFailed = true;
            return;
        }
    
        dm.detachEvent("DLMShutdown", doDLMShutdown);
        dm.attachEvent("DLMShutdown", doDLMShutdown);                    
        
        dm.StartDownload();
        
        bDMStarted = true;
    } catch (e) {
        bDMStarted = false;
        if (e.description != "object Error") {
            bDMFailed = true;
        }
    }
}

// Start the DLM
function startDLM() {
    if (bDocReady) {
        insertObj(arguments[0], arguments[1]);
        if (bdmIsReady) {
            doStart();
        }
        else{
        }
        
    }

    if (bDMFailed) {
        // Don't try to go direct, since this happens by
        // default on XP SP2 and above.
        return;
    }

    if (!bDMStarted) {
        setTimeout("startDLM('"+arguments[0]+"', '"+arguments[1]+"');", 500);
    }
}

// Check if the DM object is fully loaded
function dmReady() {
    var dm = document.getElementById("dm");
    if (dm == null) {
        bDMFailed = true;
        return;
    }

    if (dm.readyState == 4) {
        bdmIsReady = true;
    }
}


// Check if the document is fully loaded
function docReady() {
    if (document.readyState == "complete") {
        bDocReady = true;
    } else {
        bDocReady = false;
    }
}

// Insert the code to create the DM object
function insertObj() {
    // Only insert the object once
//    if (!bInsObj) {
    if (true) {
        bInsObj = true;
        var userLang = (navigator.language) ? navigator.language : navigator.userLanguage;

        //Get the URL
        var s = location.href;
        var i = s.indexOf("download_item_");
        s = s.substring(0,i);

        // Create object tag
        var sObjHTML = "<object id=\"dm\" classid=\"CLSID:4871A87A-BFDD-4106-8153-FFDE2BAC2967\" CODEBASE=\"http://dlm.tools.akamai.com/dlmanager/versions/activex/dlm-activex-2.2.5.7.cab#Version=2,2,5,7\" width=1 height=1> " +
            "    <PARAM name=\"version\" value=\"2.2.5.7\"/> " +
            "    <PARAM name=\"URL\" value=\"" + arguments[0] + "\"/> " +
            "    <PARAM name=\"recoveryURL\" value=\"" + s + "download_item_dlm.aspx?file=" + arguments[0] + "&SLanguage=" + arguments[1]+ "\"/> " +
            "    <PARAM name=\"language\" value=\""+userLang+"\"/> " +
            "    <PARAM name=\"providerName\" value=\"ASUSTek Computer Inc. \"/> " +
            "    <PARAM name=\"maxCon\" value=\"6\"/> " +
            "    <PARAM name=\"maxConn\" value=\"6\"/> " +
            "    <PARAM name=\"requestSize\" value=\"1024\"/> " +
            "    <PARAM name=\"flushSize\" value=\"32\"/> " +
            "    <PARAM name=\"initialView\" value=\"single\"/> " + //or value="summary"
            "    <PARAM name=\"referer\" value=\"http://support.asus.com/\"/> " +
            "    <PARAM name=\"icon\" value=\"true\"/> " +
            "    <PARAM name=\"logoURL\" value=\"http://support.asus.com/images/LOGO-asus-dlm2.gif\"/> " +
            "    <PARAM name=\"launch\" value=\"no\"/> " +
            "    <PARAM name=\"closeWhenDone\" value=\"no\"/> " +
            "    <PARAM name=\"useStateReporting\" value=\"true\"/> " +
            "    <PARAM name=\"stateReportingVersion\" value=\"1\"/> " +
            "</object> ";
            
        objdiv = document.getElementById("objectDIV");
        if (objdiv == null) {     
            document.getElementById("ifr_download").src = arguments[0];
            return;
        }

        objdiv.innerHTML = sObjHTML;
        var dm = document.getElementById("dm");

        if (dm == null) {
            bDMFailed = true;
        }

        // Set up handler for DM readystate change
        dm.onreadystatechange = dmReady;
        dmReady();
    }
}
document.onreadystatechange = docReady;
