function getSettings(separator) {
    var app = App();
    if (app == null) return "";
    if (separator == null) separator = ";\n";
    SetParams(false);
    var x,y;
    return "N=" + parseInt(document.FmMain.TNeedObjCount.value)
        + separator + "Mode="+document.FmMain.DPackingKind.options.selectedIndex
        + separator + "Bun="+(app.getPackInBunker()?1:0)
        + separator + "BunXYZ=\""+parseFloat(document.FmMain.TBunX.value)
            +"x"+parseFloat(document.FmMain.TBunY.value)
            +"x"+parseFloat(document.FmMain.TBunZ.value)+"\""
        + separator + "BunC=\""+document.FmMain.TBunColors.value+"\""
        + separator + "L="+parseInt(document.FmMain.TNearestFrom.value)
        + separator + "C="+parseInt(document.FmMain.TCompactnessFactor.value)
        + separator + "RndRot="+(document.FmMain.CRandomRotating.checked?1:0)
        + separator + "R=\""+app.getMeanR()+"+"+app.getDR()+":"+document.FmMain.DRandomRadiusModel.selectedIndex+"\""
        + separator + "r=\""+app.getMeanR2()+"+"+app.getDR2()+":"+document.FmMain.DRandomRadiusModel2.selectedIndex+"\""
        + separator + "RI=\""+app.getMeanRI()+"+"+app.getDRI()+":"+document.FmMain.DRandomRadiusIncrementModel.selectedIndex+"\""
        + separator + "Kinds=\""+app.getSpherePolyhedraKinds()+"\""
        + separator + "Colors=\""+app.getUsedColors()+"\""
        + separator + "Inv=\""+document.FmMain.TInvisibleColors.value+"\""
        + separator + "Frac=\""+app.getFractionsOfKinds()+"\""
        + separator + "VFrac="+(app.isVolumeFractionsMode()?1:0)
        + separator + "PrDir=\""+app.getPreferredDirectionsCount()+"|"
            +app.getPreferredDirections(",",", ")+"|"
            +app.getPreferredDirectionsDFi("; ")+"\""
        +(isNaN(x=parseFloat(document.FmMain.TPreferredStartX.value))?"":separator+"PrX="+x)
        +(isNaN(x) || isNaN(v=parseFloat(document.FmMain.TPreferredStartDX.value))?"":separator+"PrDX="+v)
        +(isNaN(y=parseFloat(document.FmMain.TPreferredStartY.value))?"":separator+"PrY="+y)
        +(isNaN(y) || isNaN(v=parseFloat(document.FmMain.TPreferredStartDY.value))?"":separator+"PrDY="+v)
        + separator + "ReqN=\""+document.FmMain.TRequiredNeighbourIndex.value+"\""
        + separator + "SameCN="+document.FmMain.DSameColorNeighboursCount.selectedIndex
        + separator + "MaxInvN="+(3-document.FmMain.DMaxInvisibleNeighboursCount.selectedIndex)
        + separator + "NonReN="+parseInt(document.FmMain.TNonRestrictedObjCount.value)
        + separator + "MaxD="+app.getRestrictedMaxD()
        + separator + "WarnWC="+(document.FmMain.CWarningWhenCannotPack.checked?1:0)
        + separator + "TimeOut="+parseFloat(document.FmMain.TPackTimeOut.value)
        + separator + "AllPos="+(document.FmMain.CCheckAllPositions.checked?1:0)
        + separator + "MaxN="+parseInt(document.FmMain.TMaxObjCount.value)
        + separator + "MaxGenN="+parseFloat(document.FmMain.TMaxAverageGeneratrixSegmentsCount.value)
        + separator + "Grid="+parseInt(document.FmMain.TGridDim.value)
        + separator + "GrStep="+parseFloat(document.FmMain.TGridStep.value)
        + separator + "Repr="+(document.FmMain.CReproducibleResults.checked?1:0)
        + separator + "Sc="+parseFloat(document.FmPaint.TPaintScale.value)
        + separator;
}

function setSettings(s) {
    if (s == null || (s = trim(s," \t\r\n")).length == 0) return false;
    for (var p = 0; true; ) {
        if (p >= s.length) {
            top.scenario = null;
            break;
        }

        var q = s.indexOf("\n",p);
        if (q == -1) q = s.indexOf("\r",p);
        if (q == -1) q = s.indexOf("&",p);
        if (q == -1) q = s.length;
        var param = trim(s.substring(p,q)," \t\r\n;");
        p = q+1;

        q = param.indexOf("****");
        if (q >= 0 && q <= 3) {
            top.scenario = s.substring(p);
            break;
        }

        var r = param.indexOf("=");
        if (r == -1) continue;
        var key = trim(param.substring(0,r));
        var v = trim(param.substring(r+1));
        if (v.length > 1 && v.charAt(0) == "\"" && v.charAt(v.length-1) == "\"")
            v = v.substring(1,v.length-1);
        var iv = parseInt(v);
        if (key=="N") {
            document.FmMain.TNeedObjCount.value = v;
        } else if (key=="Mode") {
            document.FmMain.DPackingKind.options.selectedIndex = iv;
        } else if (key=="Bun") {
            document.FmMain.CPackInBunker.checked = iv>0;
        } else if (key=="BunXYZ") {
            var a = split(v,"x");
            if (a.length > 0) document.FmMain.TBunX.value = a[0];
            if (a.length > 1) document.FmMain.TBunY.value = a[1];
            if (a.length > 2) document.FmMain.TBunZ.value = a[2];
        } else if (key=="BunC") {
            document.FmMain.TBunColors.value = v;
        } else if (key=="L") {
            document.FmMain.TNearestFrom.value = iv;
        } else if (key=="C") {
            document.FmMain.TCompactnessFactor.value = v;
        } else if (key=="RndRot") {
            document.FmMain.CRandomRotating.checked = iv>0;
        } else if (key=="R") {
            var a = split(v,"+:");
            if (a.length > 0) document.FmMain.TMeanR.value = a[0];
            if (a.length > 1) document.FmMain.TDR.value = a[1];
            if (a.length > 2) document.FmMain.DRandomRadiusModel.selectedIndex = parseInt(a[2]);
        } else if (key=="r") {
            var a = split(v,"+:");
            if (a.length > 0) document.FmMain.TMeanR2.value = a[0];
            if (a.length > 1) document.FmMain.TDR2.value = a[1];
            if (a.length > 2) document.FmMain.DRandomRadiusModel2.selectedIndex = parseInt(a[2]);
        } else if (key=="RI") {
            var a = split(v,"+:");
            if (a.length > 0) document.FmMain.TMeanRI.value = a[0];
            if (a.length > 1) document.FmMain.TDRI.value = a[1];
            if (a.length > 2) document.FmMain.DRandomRadiusIncrementModel.selectedIndex = parseInt(a[2]);
        } else if (key=="Kinds") {
            document.FmMain.TSpherePolyhedraKinds.value = v;
        } else if (key=="Colors") {
            document.FmMain.TUsedColors.value = v;
        } else if (key=="Inv") {
            document.FmMain.TInvisibleColors.value = v;
        } else if (key=="Frac") {
            document.FmMain.TFractionsOfKinds.value = v;
        } else if (key=="VFrac") {
            document.FmMain.CVolumeFractionsMode.checked = iv>0;
        } else if (key=="PrDir") {
            var a = split(v,"|");
            if (a.length > 0) document.FmMain.TPreferredDirectionsCount.value = a[0];
            if (a.length > 1) document.FmMain.TPreferredDirections.value = a[1];
            if (a.length > 2) document.FmMain.TPreferredDirectionsDFi.value = a[2];
        } else if (key=="PrX") {
            document.FmMain.TPreferredStartX.value = v;
        } else if (key=="PrDX") {
            document.FmMain.TPreferredStartDX.value = v;
        } else if (key=="PrY") {
            document.FmMain.TPreferredStartY.value = v;
        } else if (key=="PrDY") {
            document.FmMain.TPreferredStartDY.value = v;
        } else if (key=="ReqN") {
            document.FmMain.TRequiredNeighbourIndex.value = v;
        } else if (key=="SameCN") {
            document.FmMain.DSameColorNeighboursCount.selectedIndex = iv;
        } else if (key=="MaxInvN") {
            document.FmMain.DMaxInvisibleNeighboursCount.selectedIndex = 3-iv;
        } else if (key=="NonReN") {
            document.FmMain.TNonRestrictedObjCount.value = v;
        } else if (key=="MaxD") {
            document.FmMain.TRestrictedMaxD.value = v;
        } else if (key=="WarnWC") {
            document.FmMain.CWarningWhenCannotPack.checked = iv>0;
        } else if (key=="TimeOut") {
            document.FmMain.TPackTimeOut.value = v;
        } else if (key=="AllPos") {
            document.FmMain.CCheckAllPositions.checked = iv>0;
        } else if (key=="MaxN") {
            document.FmMain.TMaxObjCount.value = v;
        } else if (key=="MaxGenN") {
            document.FmMain.TMaxAverageGeneratrixSegmentsCount.value = v;
        } else if (key=="Grid") {
            document.FmMain.TGridDim.value = v;
        } else if (key=="GrStep") {
            document.FmMain.TGridStep.value = v;
        } else if (key=="Repr") {
            document.FmMain.CReproducibleResults.checked = iv>0;
        } else if (key=="Sc") {
            document.FmPaint.TPaintScale.value = v;
        }
    }
    SetParams(false);
    SetPaintParams();
    return true;
}

function GrabSettings() {
    document.FmMain.TAllSettings.value = getSettings();
}

function GrabAndAppendSettings() {
    var s = document.FmMain.TAllSettings.value;
    if (s == null || (s = trim(s," \t\r\n")).length == 0)
        document.FmMain.TAllSettings.value = getSettings();
    else
        document.FmMain.TAllSettings.value = s + "\n\n/********/\n" + getSettings();
}

function RestoreSettings() {
    if (!setSettings(document.FmMain.TAllSettings.value))
        alert("Nothing to restore!");
}

function SaveSettingsInUrl() {
    var s = document.FmMain.TAllSettings.value;
    if (s == null || (s = trim(s," \t\r\n")).length == 0) {
        alert("Nothing to save in the input area!\n\n"
            + "Before pressing \"Save in URL\", "
            + "please grab all settings into the input area "
            + "by \"Grab\" button");
        return;
    }
    sNew = replace(s,"\n","&");
    if (sNew == s) sNew = replace(s,"\r","&");
    sNew = replace(sNew,"\n","");
    sNew = replace(sNew,"\r","");
    sNew = escape(sNew);
    sNew = replace(sNew,"%3D","=");
    sNew = replace(sNew,"%26","&");
    sNew = replace(sNew,"%3B",";");
    if (sNew.length >= 1600) {
        alert("Sorry, the scenario is too long (" + sNew.length + " characters) to be saved in an URL.\n"
            + "You may save the scenario above by Clipboard and a text editor.");
        return;
    }
    window.open(top.directoryUrl
        + "appletpackingmink.html?_SCENARIO_&" + sNew + "#BeginOfExecutable",
        "_blank").focus();
    window.infoRequired = true;
}

function RestoreSettingsFromURL() {
    var p = location.search.indexOf("_SCENARIO_&");
    if (p != -1) {
        if (App(true) == null) {
            setTimeout("RestoreSettingsFromURL()",1000);
            return;
        }
        var s = unescape(location.search.substring(p+"_SCENARIO_&".length));
        s = replace(s,"&","\n");
        setSettings(s);
        document.FmMain.TAllSettings.value = s;
        if (window.opener != null && !opener.closed && opener.infoRequired) {
            opener.infoRequired = false;
            alert("The packing settings are saved in the URL of the new page (after \"?\" character).\n\n"
                +"You may save the URL of this page in your \"Favorites\". "
                +"Every time you will open this link from the \"Favorites\" menu, "
                +"you will see the packing settings identical to the saved ones.");
        }
    }
}

