function getCookieValue(name) {
    var value = "; " + document.cookie;
    var parts = value.split("; " + name + "=");
    if (parts.length == 2) return parts.pop().split(";").shift();
}

function RemoveDiv(divName) {
    var selectedDiv = document.getElementById(divName);

    if (selectedDiv != null) selectedDiv.parentNode.removeChild(selectedDiv);
}

function ConfirmNavigation(navigationLocation) {
    if (confirm("Are you sure?")) document.location = navigationLocation;
}

function MakeDivVisible(divName) {
    document.getElementById(divName).style.display = "block";
}

function MakeDivInvisible(divName) {
    document.getElementById(divName).style.display = "none";
}

function AuthenticationTypeSelected(accessType) {
    var revit_tooltip = document.getElementById("revit_tooltip");
    var clarity_tooltip = document.getElementById("clarity_tooltip");

    if (accessType.options[accessType.selectedIndex].value.toLowerCase() == "revit") {
        revit_tooltip.style.display = "block";
        clarity_tooltip.style.display = "none";
    }
    else if (accessType.options[accessType.selectedIndex].value.toLowerCase() == "clarity") {
        revit_tooltip.style.display = "none";
        clarity_tooltip.style.display = "block";
    }
}

function OpenHelpFile(type) {
    switch (type.toLowerCase()) {
        case "roomdatasheetediting":
            window.open("https://s3.amazonaws.com/IMAGINiTSoftware/Documentation/Clarity/2026/index.htm#t=Admin_%26_Coordinators_Guide%2FData_Sheets%2FRoom_Datasheet_Editing.htm", "help");
            break;
        case "areadatasheetediting":
            window.open("https://s3.amazonaws.com/IMAGINiTSoftware/Documentation/Clarity/2026/index.htm#t=Admin_%26_Coordinators_Guide%2FData_Sheets%2FArea_Datasheet_Editing.htm", "help");
            break;
        case "spacedatasheetediting":
            window.open("https://s3.amazonaws.com/IMAGINiTSoftware/Documentation/Clarity/2026/index.htm#t=Admin_%26_Coordinators_Guide%2FData_Sheets%2FSpace_Datasheet_Editing.htm", "help");
            break;
        case "genericdatasheetediting":
            window.open("https://s3.amazonaws.com/IMAGINiTSoftware/Documentation/Clarity/2026/index.htm#t=Admin_%26_Coordinators_Guide%2FData_Sheets%2FItem_Datasheet_Editing.htm", "help");
            break;
    }
}

function ScrollToPageTop() {
    window.scroll(0, 0);
}

function showModelDialog(modalOverlay) {
    if (modalOverlay) $(modalOverlay).show();
    else $("#modal-overlay").show();
}

function hideModelDialog(modalOverlay) {
    if (modalOverlay) {
        $(modalOverlay).empty();
        $(modalOverlay).hide();
    }
    else {
        $("#modal-overlay").empty();
        $("#modal-overlay").hide();
    }
}

function fullscreenDiv(dialogId, onComplete, onRemove) {
    $("#" + dialogId).addClass("fullscreen");

    if (onRemove) $("#" + dialogId).append("<img id=\"fullscreen-button\" src=\"" + applicationPath + "/Images/Icons/close.png\" onclick=\"removefullscreenDiv('" + dialogId + "', " + onRemove.toString() + ")\" border=\"0\">");
    else $("#" + dialogId).append("<img id=\"fullscreen-button\" src=\"" + applicationPath + "/Images/Icons/close.png\" onclick=\"removefullscreenDiv('" + dialogId + "')\" border=\"0\">");

    if (onComplete) onComplete();
}

function removefullscreenDiv(dialogId, onComplete) {
    $("#" + dialogId).removeClass("fullscreen");
    $("#fullscreen-button").remove();

    if (onComplete) onComplete();
}

function generateUUID() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

function copyToClipboard(textToCopy) {
    var textArea = document.createElement("textarea");

    // Attempting to hide to appended textbox
    textArea.style.position = "fixed";
    textArea.style.top = 0;
    textArea.style.left = 0;
    textArea.style.width = "2em";
    textArea.style.height = "2em";
    textArea.style.padding = 0;
    textArea.style.border = "none";
    textArea.style.outline = "none";
    textArea.style.boxShadow = "none";
    textArea.style.background = "transparent";

    textArea.value = textToCopy;

    document.body.appendChild(textArea);

    textArea.select();

    var successful = false;

    try {
        successful = document.execCommand("copy");
    }
    catch (err) {
        console.log(err)
    }

    document.body.removeChild(textArea);

    return successful;
}

function displayAlertMessage(text, callback) {
    var alert = document.createElement("div")
    alert.id = "alertContainer";
    var message = document.createElement("p");
    message.innerText = text;
    alert.appendChild(message);
    alert.appendChild(document.createElement("br"));
    var buttonContainer = document.createElement('div');
    buttonContainer.classList.add("right-floating-panel");
    var button = document.createElement("input");
    button.type = "button";
    button.classList.add("button");
    button.value = "Ok";
    button.onclick = function () {
        if (callback && typeof callback === "function")
            callback();
        RemoveDiv('alertContainer');
    };
    buttonContainer.appendChild(button);
    alert.appendChild(buttonContainer);
    document.body.appendChild(alert);
}

function displayConfirmMessage(id, title, message, acceptCallback, cancelCallback) {

    const modalDiv = document.getElementById("modal-overlay");
    const alert = document.createElement("div");
    alert.className = "main_popup_container";
    if (id) {
        alert.id = id;
    } else {
        alert.id = "confirmContainer";
    }

    if (title) {
        const titleDiv = document.createElement("div");
        titleDiv.innerText = title;
        titleDiv.className = "main_popup_header";
        alert.appendChild(titleDiv);
        alert.appendChild(document.createElement("br"));
    }

    const messageDiv = document.createElement("div");
    messageDiv.innerText = message;
    alert.appendChild(messageDiv);
    alert.appendChild(document.createElement("br"));

    const buttonContainer = document.createElement('div');
    buttonContainer.classList.add("right-floating-panel");

    const buttonOk = document.createElement("input");
    buttonOk.type = "button";
    buttonOk.classList.add("button");
    buttonOk.value = "OK";
    buttonOk.className = "button confirmation";
    buttonOk.onclick = function () {
        if (acceptCallback && typeof acceptCallback === "function")
            acceptCallback();
        RemoveDiv('confirmContainer');
    };
    buttonContainer.appendChild(buttonOk);

    var buttonCancel = document.createElement("input");
    buttonCancel.type = "button";
    buttonCancel.classList.add("button");
    buttonCancel.value = "Cancel";
    buttonCancel.className = "button confirmation";
    buttonCancel.onclick = function () {
        if (cancelCallback && typeof cancelCallback === "function")
            cancelCallback();
        RemoveDiv('confirmContainer');
        hideModelDialog(modalDiv);
    };
    buttonContainer.appendChild(buttonCancel);

    alert.appendChild(buttonContainer);

    modalDiv.appendChild(alert);

    showModelDialog(modalDiv);

}

function displayConfirmElement(id, title, element, acceptCallback, cancelCallback) {

    const modalDiv = document.getElementById("modal-overlay");
    const alert = document.createElement("div");
    alert.className = "main_popup_container";
    if (id) {
        alert.id = id;
    } else {
        alert.id = "confirmContainer";
    }

    if (title) {
        const titleDiv = document.createElement("div");
        titleDiv.innerText = title;
        titleDiv.className = "main_popup_header";
        alert.appendChild(titleDiv);
        alert.appendChild(document.createElement("br"));
    }

    alert.appendChild(element);
    alert.appendChild(document.createElement("br"));

    const buttonContainer = document.createElement('div');
    buttonContainer.classList.add("right-floating-panel");

    const buttonOk = document.createElement("input");
    buttonOk.type = "button";
    buttonOk.classList.add("button");
    buttonOk.value = "OK";
    buttonOk.className = "button confirmation";
    buttonOk.onclick = function () {
        if (acceptCallback && typeof acceptCallback === "function")
            acceptCallback();
        RemoveDiv('confirmContainer');
    };
    buttonContainer.appendChild(buttonOk);

    var buttonCancel = document.createElement("input");
    buttonCancel.type = "button";
    buttonCancel.classList.add("button");
    buttonCancel.value = "Cancel";
    buttonCancel.className = "button confirmation";
    buttonCancel.onclick = function () {
        if (cancelCallback && typeof cancelCallback === "function")
            cancelCallback();
        RemoveDiv('confirmContainer');
        hideModelDialog(modalDiv);
    };
    buttonContainer.appendChild(buttonCancel);

    alert.appendChild(buttonContainer);

    modalDiv.appendChild(alert);

    showModelDialog(modalDiv);

}

function clearWebTriggersessionStorage() {
    const keys = ["webTriggerId", "skyscraperProjectName", "webtrigger-selectedHubName", "folder", "webtrigger-filters-model", "webtrigger-model",
        "webtrigger-selectedHub", "externalProjectId", "generaltrigger-Url", "skyscraperProjectId", "isProjectSpecific", "folderId", "metadata", "webtrigger-filters"];
    keys.forEach(k => {
        sessionStorage.removeItem(k);
    });
}

function bootstrapAlert(message, alertType, title, timeout) {
    const container = document.getElementById("content");
    const alert = document.createElement("div");
    alert.className = `alert alert-${alertType} alert-dismissible fade show`;
    alert.role = "alert";
    if (title) {
        alertTitle = document.createElement("h4");
        alertTitle.className = "alert-heading";
        alertTitle.innerText = title;
        alert.appendChild(alertTitle);
    }

    alertMessage = document.createElement("p");
    alertMessage.innerText = message;
    alert.appendChild(alertMessage);
    const button = document.createElement("button");
    button.type = "button";
    button.className = "btn-close";
    button.setAttribute("data-bs-dismiss", "alert");
    button.setAttribute("aria-label", "Close");
    const span = document.createElement("span");
    span.setAttribute("aria-hidden", "true");
    span.innerHTML = "&times;";
    button.appendChild(span);
    alert.appendChild(button);
    document.body.appendChild(alert);
    if (timeout) {
        setTimeout(() => {
            alert.remove();
        }, timeout);
    }

    container.insertBefore(alert, container.firstChild);
}
