function ObjCount() {
    var app = App(true);
    if (app == null) return 0;
    return app.getObjCount();
}

function WaitFor(count) {
    var app = App(true);
    if (app == null) return;
    app.waitFor(count);
}

function GetUserName() {
    var app = App(true);
    if (app == null) return null;
    return app.getUserName();
}

function GetRemainingPacks() {
    var app = App(true);
    if (app == null) return 0;
    return app.getRemainingPacks();
}

function App(notShowMessage) {
    if (!canWork(notShowMessage)) return null;
    var app = getApp("AppletPackingMink",notShowMessage);
    return app;
}

function StopDemoRotate() {
    var app = getApp("PackingObjectsForDesign",true);
    if (app == null) return;
    app.disableRotateAll();
}

function density(R,infix,postfix) {
    var app = App();
    if (app == null) return "";
    if (infix==null) infix= "&plusmn;";
    var min = app.getDensity(R,-1);
    var max = app.getDensity(R,1);
    if (min!=max)
        return app.sDouble(0.5*(min+max),3)+infix+app.sDouble(0.5*(max-min),3);
    else if (min!=0.0)
        return app.sDouble(min,5)+(postfix==null?"":postfix);
    else
        return "";
}

function callOpenerOnAppletLoad() {
    var p = location.search.indexOf("_CALLOPENER_");
    if (p == -1) return;
    if (window.opener != null && !opener.closed && opener.pmOnLoad != null) {
        var app = App(true);
        if (app == null) {
            setTimeout("callOpenerOnAppletLoad()",1000);
            return;
        }
        opener.pmOnLoad(window,app);
    }
}

function bodyOnBeforeUnload() {
    var app = App(true);
    if (app == null) return null;
    // Netscape 3 requires that a method always returns something or never return anything
    if (msieversion()<6) return null;
    // MSIE 4 has problems with calling applet method at this point
    // MSIE 5 incorrectly generates this event when we click on #xxxx links
    if (app.getObjCount()>app.getSpecialObjCount()) {
        return 'You are about to leave the "Packing Sphere-Polyhedra" executable page. '
            + 'The current pack of sphere-polyhedra, which you built here ('
            + app.getObjCount() + ' objects), will be permanently lost.\n\n'
            + 'To save the pack, you may use the "Build detailed report" button in the "Report" section.\n'
            + 'To save the pack scenario, you may use "Saving/restoring scenario" section.';
    }
}

