
// Legacy support (SiteMaker3-)
function CoreCommand(cmd, arg) {
    var legacyEvent = arguments[2] || window.event || { button: 0, ctrlKey: false };

    switch (cmd) {
        case "Login":
            if (legacyEvent.button == 2 && legacyEvent.ctrlKey)
                document.location.href = "Login.aspx";
            break;
        case "Logout":
            document.location.href = "Login.aspx?Logout=1";
            break;
        case "GetDocumentTitle":
            return _CoreDocumentTitle;
            break;
        case "GetDocumentID":
            return _CoreDocumentID;
            break;
        case "LoadDocument":
            var target = (arg.length ? arg[1].toString().toLowerCase() : "_self");
            var anchor = arg.length && arg.length == 3 ? '#' + arg[2] : '';
            if (target == "_self") {
                window.location.href = _CoreUrl + "?id=" + (arg.length ? arg[0] : arg) + anchor;
            } else if (target == '_blankanchor') {
                window.open(_CoreUrl + '?id=' + arg[0] + anchor);
            } else if (target == '_anchor') {
                window.location.href = _CoreUrl + '?id=' + arg[0] + anchor;
            } else {
                window.open(_CoreUrl + (arg.length ? "?Popup=1&" : "?") + "id=" + (arg.length ? arg[0] : arg), target, (arg.length ? arg[2] : null));
            }
            break;
        case "LoadHref":
            window.open(arg[0], arg[1], arg[2]);
            break;
        case "RefreshDocument":
            document.forms[0].submit();
            break;
        case "RefreshStyleSheet":
            if (document.getElementById)
                document.getElementById('StyleSheet').href = (arg != null) ? arg : document.getElementById('StyleSheet').href;
            break;
        case "CloseWindow":
            window.top.close();
            break;
        case "GoBack":
            history.back();
            break;
        case "GetPermission":
            return _CorePermissions;
            break;
        case "WebObjectRemoveParameter":
            var w, cache = _CoreWebObjectParameterCache;
            if (w = cache['_' + arg[0]]) {
                for (var i = 0; i < w.length; i++) {
                    if (w[i].name == arg[1]) {
                        w.splice(i, 1);
                        return;
                    }
                }
            }
            break;
        case "WebObjectAddParameter":
            var cache = _CoreWebObjectParameterCache;
            var payload = { name: arg[1], value: arg[2] };

            if (!cache['_' + arg[0]])
                cache['_' + arg[0]] = [payload];
            else
                cache['_' + arg[0]].push(payload);
            break;
        case "SetWindowStatus":
            window.status = arg;
            return true;
            break;
        default:
            //alert("Unknown command: " + cmd);
            break;
    }
    return false;
}
function WebObjectParameterRemove(instanceId, name) {
    CoreCommand("WebObjectRemoveParameter", [instanceId, name]);
}
function WebObjectParameterAdd(instanceId, name, value) {
    CoreCommand("WebObjectAddParameter", [instanceId, name, value]);
}
function WebObjectSubmit(hash) {
    // if hash is set, add it to the forms action
    if (typeof hash == 'string')
        document.forms[0].action += '#' + hash;

    var cache = _CoreWebObjectParameterCache;
    var data = '';
    var value, p, i;

    for (p in cache) {
        if (!cache.hasOwnProperty(p))
            continue;
        data += '&' + p + '=';
        for (value = '', i = 0; i < cache[p].length; i++)
            value += '&' + cache[p][i].name + '=' + encodeURIComponent(cache[p][i].value);

        data += encodeURIComponent(value.substr(1));
    }

    document.forms[0].elements['WebObjectData'].value = data.substr(1);
    CoreCommand("RefreshDocument");
}
function WebObjectSubmitGet(hash) {
    var cache = _CoreWebObjectParameterCache;
    var data = '';
    var value, p, i;

    for (p in cache) {
        if (!cache.hasOwnProperty(p))
            continue;
        data += '&' + p + '=';
        for (value = '', i = 0; i < cache[p].length; i++) {
            if (cache[p][i].name.indexOf('__ViewState') == -1)
                value += '&' + cache[p][i].name + '=' + encodeURIComponent(cache[p][i].value);
        }

        data += encodeURIComponent(value.substr(1));
    }

    if (hash)
        data += '#' + hash;

    window.location = '/?id=' + _CoreDocumentID + data;
}

var _CoreWebObjectParameterCache = {};
