function initialize() {
    var s = location.pathname + "";
    top.pageUrl = s;
    var p = Math.max(s.lastIndexOf("/"),s.lastIndexOf("\\")) + 1;
    top.directoryUrl = s.substring(0,p);
    // Confirm function can be called from another directory ("dialogs/")
    // It can lead to a problem under Opera, that can be solved by directoryUrl variable
}
initialize();

function checkVersion(app) {
    var info = app.getAppletInfo()+"";
    var p = info.indexOf("\nVersion: ");
    top.appBuild = info;
    if (p != -1) {
        p += "\nVersion: ".length;
        var q = info.indexOf("\n",p);
        if (q == -1) q = info.length;
        top.appBuild = info.substring(p,q);
    }
    if (top.build != top.appBuild) {
        top.open(top.directoryUrl+"dialogs/versionmismatch.html",
            "PackingMinkVersionMismatch",
            "width=700"
            +",height=500"
            +",left="+(scrW()-700)/2
            +",top="+(scrH()-500)/7
            +",scrollbars=1,menubar=0,resizable=1").focus();
        return false;
    }
    return true;
}

function FreeWelcome() {
    if (location.hostname.indexOf("195.") == 0 || location.hostname == "localhost")
        return;
    var url = top.directoryUrl+"dialogs/freewelcome.html";
    url += "?referrer=" + escape(document.referrer)
        + "&NeedObjCount=" + escape(document.FmMain.TNeedObjCount.value)
        + "&PackingKind=" + document.FmMain.DPackingKind.options.selectedIndex
        + "&PackInBunker=" + document.FmMain.CPackInBunker.checked
        + "&SpherePolyhedraKinds=" + escape(document.FmMain.TSpherePolyhedraKinds.value);
    window.open(url,
        "PackingMinkFreeWelcome",
        "width=390"
        +",height=250"
        +(window.screen==null? "":
            ",left="+(scrW()-390)/2
            +",top="+(scrH()-250)/3)
        +",scrollbars=0,menubar=0,resizable=1").focus();
}

window.requiredExpressionForEvaluationAfterAuthorization = null;
function Authorize(expressionForEvaluationAfterAuthorization) {
    window.requiredExpressionForEvaluationAfterAuthorization = expressionForEvaluationAfterAuthorization;
    window.open(top.directoryUrl+"dialogs/authorize.html",
        "PackingMinkAuthorize",
        "width=390"
        +",height=250"
        +(window.screen==null? "":
            ",left="+(scrW()-390)/2
            +",top="+(scrH()-250)/3)
        +",scrollbars=0,menubar=1,resizable=1").focus();
}

function DoAuthorize(registrationCode,wnd) {
    var app = App();
    if (app == null) {
        return false;
    }
    if (!app.authorize(registrationCode+"")) {
        var s = app.getConnectionError()+"";
        if (s=="")
            alert("Wrong registration code");
        else
            alert("Cannot connect to autorization Web server:\n"+s
                +"\n\nPlease check your internet connection. "
                +"You must be connected to internet to pack more than 100 objects.");
        return false;
    }
    if (!window.requiredExpressionForEvaluationAfterAuthorization) {
        alert('Welcome, '+GetUserName()+'!\n'
            +'You can build '+GetRemainingPacks()+' packs containing >100 objects.\n\n'
            +'Now, please click "Execute" or "Apply" button to build a new pack.');
//    ConfirmPack(true);
    } else {
        // the following checks allows working with Windows XP SP2+ popup blocker
        if (window.requiredExpressionForEvaluationAfterAuthorization == "PageExecute(true)") {
            PageExecute(true,wnd);
        } else if (window.requiredExpressionForEvaluationAfterAuthorization == "SetNeedObjCount(true)") {
            SetNeedObjCount(true,wnd);
        } else // the following code doesn't work with some popup blockers
            eval(window.requiredExpressionForEvaluationAfterAuthorization);
    }
    return true;
}

function ConfirmPack() {
    if (confirm("Welcome, " + GetUserName() + "!\n\n"
        + "You have chosen to build a new pack contaning more than 100 objects.\n"
        + "This action IS NOT FREE: you can build only restricted number of such packs.\n\n"
        + "Now, the left number of packs is: \t" + GetRemainingPacks() + "\n"
        + "After this pack, the left number will be: \t" + (GetRemainingPacks()-1) + "\n\n"
        + "Do you want to continue?")) {
        var app = App();
        if (app == null) return false;
        app.setAuthorized(true);
        return true;
    } else {
        return false;
    }
}

/* The following solution can be incompatible with popup blockers
function ConfirmPack(isWelcome,wnd) {
    // isWelcome is never true in this version
    if (wnd==null) wnd= window;
    wnd.open(top.directoryUrl+"dialogs/confirmation.html",
        "PackingMink"+(isWelcome?"Welcome":"Confirm"),
        "width=500"
        +",height="+(isWelcome?200:300)
        +(window.screen==null? "":
            ",left="+(scrW()-500)/2
            +",top="+(scrH()-300)/3)
        +",scrollbars=0,menubar=0,resizable=1").focus();
}
function DoConfirmPack() {
    var app = App();
    if (app == null) return false;
    app.setAuthorized(true);
    window.focus();
    return true;
}
*/
top.firstFreeWelcome = true;
function AskAuthorizationOrConfirmationForLargePack(expressionForEvaluationAfterAuthorization,wnd) {
    var app = App();
    if (app == null) return false;
    var needcount = parseInt(document.FmMain.TNeedObjCount.value);
    if (needcount > 100) {
        if (app.getRemainingPacks()<=0) {
//            alert("Authorization required to pack > 100 objects.\nAuthorize, please!");
            Authorize(expressionForEvaluationAfterAuthorization);
            return false;
        } else {
            if (!ConfirmPack())
                return false;
        }
    } else {
        if (top.firstFreeWelcome)
            FreeWelcome();
    }
    top.firstFreeWelcome = false;
    return true;
}

function ShowHelp(url) {
    window.open(url,"PackingMinkHelp",
        (window.screen==null? "":
            "width="+scrW()*2/3
            +",height="+scrH()*2/3
            +",left="+scrW()*3/10
            +",top="+scrH()/10)
        +",scrollbars=1,menubar=1,location=1,resizable=1,toolbar=1,status=1").focus();
    return false;
}

