﻿function openPageSearch()
{
    pageSearch.show();
}

function openSubjectPopup(idSubject)
{
    window.open("SubjectPopup.aspx?idSubject=" + idSubject, "subjectPopup", "status=1,width=800,height=600,resizable=1", "");
}

function openReageerPopup(idReageer)
{
    window.open("ReageerPopup.aspx?id=" + idReageer, "reageerPopup", "status=1,width=800,height=600,resizable=1", "");
}

function openCopyrightPopup()
{
    window.open("CopyrightPopup.aspx", "copyrightPopup", "status=1,width=800,height=600,resizable=1", "");
}

function openEditDocumentPopup(idParentDocument, idDocument)
{
    var queryString = "";
    if (idParentDocument !== null)
    {
        queryString += "?idParentDocument=" + idParentDocument;
    }
    else if (idDocument !== null)
    {
        queryString += "?idDocument=" + idDocument;
    }
    window.open("EditDocumentPopup.aspx" + queryString, "editDocumentPopup", "status=1,width=800,height=600,resizable=1", "");
}

function openBestelPopup(id)
{
    var url = "BestelPopup.aspx";
    if (id !== undefined)
    {
        url += "?id=" + id;
    }
    window.open(url, "bestelPopup", "status=1,width=800,height=600,resizable=1", "");
}

function doSearch(elementId, idUitgave, idTypeUitgave, jaargang)
{
    var txt = document.getElementById(elementId);

    if (txt)
    {
        var searchword = txt.value.trim();
        
        if (searchword != "")
        {
            var queryString = "&q=" + searchword;;

            if (idUitgave !== undefined)
            {
                queryString += "&idUitgave=" + idUitgave;
            }
            
            if (idTypeUitgave !== undefined)
            {
                queryString += "&idTypeUitgave=" + idTypeUitgave;
            }
            
            if (jaargang !== undefined)
            {
                queryString += "&jaargang=" + jaargang;
            }

            window.location.href = "zoekresultaatpage.aspx?idHoofddeel=2" + queryString;
        }
    }
}

function openAfdrukkenPopup(idTekst, tabType, idtoc)
{
    var url = "PrintDocumentPopup.aspx?id=" + idTekst;
    if (tabType !== undefined)
    {
        url += "&tabType=" + tabType
    }
    if (idtoc !== undefined) {
        url += "&idToc=" + idtoc
    }
    window.open(url, "simpleDocumentPopup", "status=1,width=800,height=600,resizable=1,scrollbars=1,menubar=1", "");
}

function copyToClipboard(text)
{
    if (confirm("Als u op 'Ok' klikt wordt de volgende friendly link op het clipboard geplaatst:\n\n" + text + "\n\nAls u dit niet wilt klikt u op 'Annuleren'."))
    {
        if (window.clipboardData)
        {
            window.clipboardData.setData("text", text);
        }
        else
        {
            alert("Deze functionaliteit werkt alleen in Internet Explorer.");
        }
    }
}

function saveToKlembord(idDocument)
{
    Hene.Proeftuin.Web.NDSZService.SaveToKlembord
    (
        idDocument,
        function onSucceeded(result, context, methodName)
        {
            //console.log("Opslaan gelukt");
        },
        function onFailed(result, context, methodName)
        {
            //console.log("Opslaan ging fout");
            alert("Fout bij toevoegen aan klembord.");
        }
    );
}

function contentWrapper_OnUpdating()
{
    $("#content-wrapper-updating").show();
}

function contentWrapper_OnUpdated()
{
    $("#content-wrapper-updating").hide();
}

function EndRequestHandler(sender, e)
{
    if (e.get_error() !== null)
    {
        var errorMessage;

        if (e.get_response().get_statusCode() === 200)
        {
            //console.log(e.get_error());
            errorMessage = e.get_error().message;
        }
        else
        {
            // Error occurred somewhere other than the server page.
            errorMessage = "Er heeft een onbekende fout opgetreden.";
        }

        // Hide de updating div (hoeft niet altijd zichtbaar te zijn maar voor de zekerheid altijd doen)
        contentWrapper_OnUpdated();

        e.set_errorHandled(true);

        alert(errorMessage);
    }
}