// miscellaneous Mars functions

// load top frame if a detail page was loaded instead
function checkTopFrame() {
    if (self == top && window.location.href.indexOf("/index.jsp") == -1) {
        getTop().location.href = getBase() + "index.jsp";
    }
}

function getTop() {
    var frame = window;
    var result = top;
    while (frame.parent != null) {
        try {
            if (frame.portalContainerFrame) {
                result = frame;
            }
            if (frame == top) {
                break;
            }
            frame = frame.parent;
        } catch (e) {
            break;
        }
    }
    return result;
}
// open screen view/detail view
function openScreenView(oid, view) {
    var location = getBase() + "instance/load_mo.jsp?action=instance.load&id=" + oid + "&view="
        + (view != null ? view : "screenview");
    openScreenViewWindow(location, "screenview" + oid);
}

function openScreenViewWindow(location, name, width, height) {
    if (!width || width == null) width = 850;
    if (!height || height == null) height = 685;
    window.open(location, name, "width="+width+",height="+height+",resizable=yes,menubar=no,toolbar=no,status=no");
}


// returns the window height in standards compliance mode
function getWindowHeight(document) {
    var windowHeight = 0;
    if (document.documentElement && document.documentElement.clientHeight) {
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        windowHeight = trimPx(document.body.clientHeight);
    }
    if (window.innerHeight) {
        windowHeight = window.innerHeight;
    }
    return windowHeight;
}

//allow inline download based on infotype id
//id and ver are just provided for eventual later enhancement based on mimetype of the finedata
function allowInlineDownload(infoTypeId,id,ver) {
    switch( parseInt(infoTypeId) ) {
        case 175:   // text documents
            return true;
        default:
            return false;
    }
}

/**
 * Return the allowed page sizes for the given thumbnail size.
 */
function getAllowedPageSizes(thumbScaleIdx) {
    return getTop().RESULT_PAGESIZES[thumbScaleIdx];
}

function populatePageSizeSelect(elementId, thumbScaleIdx, selectedIndex) {
    var element = document.getElementById(elementId);
    if (selectedIndex == null) {
        // default: preserve current selection index
        selectedIndex = element.selectedIndex;
    }
    for (var i = 0; i < element.options.length; i++) {
        element.options[i] = null;
    }
    var sizes = getAllowedPageSizes(thumbScaleIdx);
    for (var i = 0; i < sizes.length; i++) {
        var option = document.createElement("option");
        option.value = i;
        option.text = getTop().msg.RESULTSIZE.replace("[1]", sizes[i]);
        element.options[i] = option;
        if (i == selectedIndex) {
            element.options[i].selected = true;
        }
    }
}

// from selfhtml
function decode_utf8(utftext)
{
    var plaintext = "";
    var i = 0;
    var c = c1 = c2 = 0;
    // while-Schleife, weil einige Zeichen uebersprungen werden
    while (i < utftext.length)
    {
        c = utftext.charCodeAt(i);
        if (c < 128) {
            plaintext += String.fromCharCode(c);
            i++;
        }
        else if ((c > 191) && (c < 224)) {
            c2 = utftext.charCodeAt(i + 1);
            plaintext += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
        }
        else {
            c2 = utftext.charCodeAt(i + 1);
            c3 = utftext.charCodeAt(i + 2);
            plaintext += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
    return plaintext;
}
