function setImageById(pImageId, pUrl, pWidth, pHeight, pAlt) {
    var image = document.getElementById(pImageId);
    if (!image) {
        return;
    }
    image.src = pUrl;
    if (pWidth && pWidth > 0) {
        image.width = pWidth;
    }
    if (pHeight && pHeight > 0) {
        image.height = pHeight;
    }
    if (pAlt) {
        image.alt = pAlt;
    }
}

function setImage(pImage, pUrl) {
    alert("pImage: " + pImage);
    alert("pImage.id: " + pImage.id);
    alert("pImage.name: " + pImage.name);
    if (pImage) {
        pImage.src = pUrl;
    }
}

function setDivClassById(pDivId, pClass) {
    var div = document.getElementById(pDivId);
    if (!div) {
        return;
    }
    div.className = pClass;
}

function setDivDisplayStyleById(pDivId, pDisplayStyle) {
    var div = document.getElementById(pDivId);
    if (!div) {
        return;
    }
    div.style.display = pDisplayStyle;
}

function setClassByImageAlt(pImageId, pClass) {
    var image = document.getElementById(pImageId);
    if (!image) {
        return;
    }
    var alt = image.alt;
    var div = document.getElementById(alt);
    if (!div) {
        return;
    }
    div.className = pClass;
}

function submitForm(elem){
    while (elem && elem.tagName && elem.tagName.toLowerCase() !== "form"){
        elem = elem.parentNode;
    }

    if (elem) {
        elem.submit();
    }
    
    return false;
}

function getSpanText(elem) {
    if (!elem) {
        return null;
    }
    
    var spanElements = elem.getElementsByTagName("span");

    for (var i=0; i<spanElements.length; i++) {
        var spanElement = spanElements[i];
        if (spanElement && spanElement.innerHTML) {
            return spanElement.innerHTML;
        }
    }
    
    return null;
}
    
function setHidden(hiddenId, theText) {
    var hidden = document.getElementById(hiddenId);
    
    if (hidden) {
        hidden.value = theText;
        return hidden;
    }
    
    return null;
}
    
function setHiddenAndSubmitForm(hiddenId, theText) {
    var hidden = setHidden(hiddenId, theText);
    
    if (hidden) {
        submitForm(hidden);
    }
    
    return false;
}

function selectField(theFieldId) {
    if (theFieldId) {
        var theField = document.getElementById(theFieldId);

        if (theField) {
            theField.focus();
            setCaretToEnd(theField);
        }
    }
}

function clearField(theField) {
    if (theField && theField.value === ' ') {
        theField.value = '';
    }
}

function setupZoomGallery() {
    Galleria.run('#zoom_gallery');
}
    
function setupDefiningGallery() {
    Galleria.run('#defining_gallery', {
        autoplay: true,
        transition: 'fade',
        thumbnails: false
    });
}
    
function initPage(theFocusField) {
    if (!window.location.hash) {
        selectField(theFocusField);
    }
    
    clearField(document.getElementById("comment_text"));
    
    var themeUrl = '../js/galleria/themes/classic/galleria.classic.min.js';
    
    if (document.getElementById("zoom_gallery")) {
        Galleria.loadTheme(themeUrl);
        setupZoomGallery();
    }
    
    if (document.getElementById("defining_gallery")) {
        Galleria.loadTheme(themeUrl);
        setupDefiningGallery();
    }
}

function setCaretToEnd(theField) {
    if (theField.setSelectionRange) {
        theField.setSelectionRange(theField.value.length, theField.value.length);
    }
    else if (theField.createTextRange) {
        var range = theField.createTextRange();
        range.moveStart('character', theField.value.length);
        range.select();
    }
}

function findNode(source, node) {
    if (!node || !source) {
        return null;
    }
    
    var children = node.childNodes;
    var selectedValue = source.options[source.selectedIndex].value;
    
    for (var i = 0; i < children.length; i++) {
        var childNode = children[i];
        
        if (childNode.nodeType !== 1) {
            continue;
        }
        
        var text = childNode.firstChild.nodeValue;

        if (selectedValue === text) {
            return childNode;
        }
    }
    
    return null;
}

function nodeTreeSize(node) {
    if (!node) {
        return 0;
    }
    
    var children = node.childNodes;
    var count = 1;
    
    for (var i = 0; i < children.length; i++) {
        var childNode = children[i];
        
        if (childNode.nodeType !== 1) {
            continue;
        }
        
        count += nodeTreeSize(childNode);
    }
    
    return count;
}

function repopulate(sourceName, targetName, index, node) {
    var source = document.getElementById(sourceName + "_" + index);
    
    if (!source) {
        return;
    }
    
    var target = document.getElementById(targetName + "_" + index);
    
    if (!target) {
        return;
    }
    
    target.options.length = 0;
    
    if (node) {
        var childNode = findNode(source, node);

        if (childNode) {
            buildMenu(childNode, target);
        }
    }
    
    if (target.options.length === 0) {
        var noOption = new Option("-", "");
        target.options[0] = noOption;
        target.disabled = true;
    }
    else {
        target.disabled = false;
    }
    
    var currentOption = source.options[source.selectedIndex];
    var title = null;
    
    if (currentOption && currentOption.title) {
        title = currentOption.title;
    }
    
    source.title = title;
}

function buildMenu(node, target) {
    var optionCount = 0;
    var children = node.childNodes;
    
    for (i = 0; i < children.length; i++) {
        var childNode = children[i];
        
        if (childNode.nodeType !== 1) {
            continue;
        }

        var text = childNode.firstChild.nodeValue;
        
        var option = new Option(text, text);
        option.selected = false;
        
        if (childNode.title) {
            option.title = childNode.title;
        }
        
        target.options[optionCount] = option;
        optionCount++;
    }


    var subCount = nodeTreeSize(node) - 1;
    
    var onlyOption = null;
    var allOption = null;
    
    if (subCount > 0) {
        if (node.id) {
            onlyOption = new Option("[Enbart]", "-");
            $(onlyOption).html(onlyOption.text);    // IE fix
            target.insertBefore(onlyOption, target.options[0]);
        }

        if (subCount > 1 || node.id) {
            allOption = new Option("[Alla]", "*");
            $(allOption).html(allOption.text);    // IE fix
            target.insertBefore(allOption, target.options[0]);
        }
    }
    
    var preferredOption = null;
    
    if (onlyOption) {
        preferredOption = onlyOption;
    }
    else if (allOption) {
        preferredOption = allOption;
    }
    else if (optionCount > 0) {
        preferredOption = target.options[0];
    }
    
    if (preferredOption) {
        preferredOption.selected = true;
    }
}

function repopulate_spe(index) {
    var rootNode = document.getElementById("data_element_catalog");
    
    if (!rootNode) {
        return;
    }
    
    repopulate("spe", "obj", index, rootNode);
    repopulate_obj(index);
}

function repopulate_obj(index) {
    var rootNode = document.getElementById("data_element_catalog");
    
    if (!rootNode) {
        return;
    }
    
    var spe = document.getElementById("spe_" + index);
    
    if (!spe) {
        return;
    }
    
    var objNode = findNode(spe, rootNode);
    
    repopulate("obj", "asc", index, objNode);
}

function showHideOpTextField(sourceName, targetName) {
    var source = $("#"+sourceName);
    
    if (!source.length) {
        return;
    }
    
    // Get operand of source:
    //
    var value = source.val();
    
    if (!value) {
        return;
    }

    // Add/remove corresponding class:
    //
    var target = $("#" + targetName);
    
    if (value.includes("EMPTY") || value.includes("EXISTS")) {
        target.hide();
    }
    else {
        target.show();
    }
}
    
function closeDialog(hiddenId) {
    var dialog = document.getElementById(hiddenId);
    
    if (dialog) {
        dialog.style.display = 'none';
    }
}

function setValueAndDisable(id,newValue) {
    var item = document.getElementById(id);
    
    if (item) {
        if (newValue) {
            item.value = newValue;
        }
        
        item.disabled = 'disabled';
    }
    
    return true;
}