// Downloading a large applet can take some time, and also requires the Java
// virtual machine to be loaded.  MPEx is a signed applet, which will require
// user authorization.  The idea here is to not impose all this on a visitor
// when the page is visited, but to wait until the applet is explicitly sought.
//
// The parameters should be pretty self-explanatory. oldNodeId should represent a
// string which will identify and html element with document.getElementById().
//
// This code should be the same as the function in mpexJSLib.js

var replacedTheAppletAlready = false;

function replaceWithApplet(oldNodeId, appletClass, appletCodebase, appletArchive, appletHeight, appletWidth, appletTextElem) {
    if (replacedTheAppletAlready) return;
    document.onclick = null;

    // Structure of replacement code based on what HtmlConverter does
    // to applet tags (see below):
    var text = "<applet code='" + appletClass + "' \n";
    text += "           codebase='" + appletCodebase + "' \n";
    text += "           archive='" + appletArchive + "' \n";
    text += "           width='" + appletWidth + "' \n";
    text += "           height='" + appletHeight + "'>\n";
    text += "           <h2>\n";
    text += "               <em>'Start MPEx' button<br />(Java not enabled)</em>.\n";
    text += "           </h2>\n";
    text += "</applet>";

    var newChild = document.createElement("div");
    newChild.innerHTML += text;
    newChild.style.border = "0";

    // Can't just replace the old child, because that's where the
    // call to this function originated, so just hide it after
    // adding the new one.
    var oldChild = document.getElementById(oldNodeId);
    oldChild.parentNode.insertBefore(newChild, oldChild);
    oldChild.style.display = "none";  // Make the original image disappear.

    replacedTheAppletAlready = true;
}
