﻿try { Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndEntityRequest); }
catch (x) { }

function EndEntityRequest() {
    //SelectFindInput();
}

function OnCompleteAddComment(arg) {
    if (!OnComplete(arg)) {
        document.getElementById("buttonAddComment").disabled = false;
    }
}

function OnCompleteAddAnswer(arg) {
    if (!OnComplete(arg)) {
        document.getElementById("buttonAddAnswer").disabled = false;
    }
}

function OnCompleteAddNotebookNote(arg) {
    if (!OnComplete(arg)) {
        document.getElementById("buttonAddNotebookNote").disabled = false;
    }
}

function OnCompleteUpdateNotebookNote(arg) {
    if (!OnComplete(arg)) {
        document.getElementById("buttonUpdateNotebookNote").disabled = false;
    }
}

var lang = "en";

function Initialize() {
    //SelectFindInput();
}

/*function SelectFindInput() {
    var inputFind = document.getElementById('inputFind');
    if (inputFind != undefined) {
        var l = inputFind.value.length;
        if (l > 0) {
            inputFind.focus();
            inputFind.select();
        }
    }
}*/

function ShowCommentDiv() {
    document.getElementById("uiCommentDiv").style.display = "block";
}

function AddComment(userName, token, targetId, targetRev, message) {
    MarkupStation.Web.EntityService.AddComment(userName, token, targetId, targetRev, message, OnCompleteAddComment);
}

function RemoveComment(accountId, stateId, commentId) {
    if (confirm("Remove?")) {
        MarkupStation.Web.EntityService.RemoveComment(accountId, stateId, commentId, OnComplete);
    }
}

function AnswerComment(stateId, commentId, response) {
    MarkupStation.Web.EntityService.AnswerComment(stateId, commentId, response, OnCompleteAddAnswer);
}

function AddNotebookNote(stateId, nbId, title, text, props) {
    MarkupStation.Web.EntityService.AddNotebookNote(stateId, nbId, title, text, props, OnCompleteAddNotebookNote);
}

function UpdateNotebookNote(stateId, noteId, title, text, props) {
    MarkupStation.Web.EntityService.UpdateNotebookNote(stateId, noteId, title, text, props, OnCompleteUpdateNotebookNote);
}

function RemoveEntity(stateId, entityId) {
    if (confirm("Remove this entity?")) {
        MarkupStation.Web.EntityService.RemoveEntity(stateId, entityId, OnComplete);
    }
}

function BeginEditCurrentNotebookNote(numberOfCommonProps) {
    /*document.getElementById("noteId").value = document.getElementById("CurrentNotebookNoteId").firstChild.data;
    document.getElementById("noteTitle").value = document.getElementById("CurrentNotebookNoteTitle").firstChild.data;
    document.getElementById("noteText").value = document.getElementById("CurrentNotebookNoteText").firstChild.data;

    for (i = 0; i < numberOfCommonProps; ++i) {
    var p = i + 1;
    document.getElementById("noteProp" + p).value = document.getElementById("CurrentNotebookNoteProp" + p).firstChild.data;
    }

    document.getElementById("noteTitle").focus();*/
    //document.getElementById("noteTitle").select();
}

function OnAddNotebookNote(stateId, nbId, numberOfCommonProps) {
    var noteTitle = document.getElementById('uiNewNoteTitle').value;
    var noteText = document.getElementById('uiNewNoteText').value;
    var noteProps = new Array();

    for (i = 0; i < numberOfCommonProps; ++i) {
        var p = i + 1;
        noteProps[i] = document.getElementById("uiNewNoteProp" + p).value;
    }

    AddNotebookNote(stateId, nbId, noteTitle, noteText, noteProps);
}

function OnUpdateNotebookNote(stateId, noteId, numberOfProps) {
    var noteTitle = document.getElementById('uiSelectedNoteTitle').value;
    var noteText = document.getElementById('uiSelectedNoteText').value;
    var noteProps = new Array();

    for (i = 0; i < numberOfProps; ++i) {
        var p = i + 1;
        noteProps[i] = document.getElementById("uiSelectedNoteProp" + p).value;
    }

    UpdateNotebookNote(stateId, noteId, noteTitle, noteText, noteProps);
}

function FindKeyDown(e) {
    if (e.keyCode == 13) {
        document.getElementById("buttonFind").click();
        return false;
    }
    return true;
}

function OnBeginEdit() {
    document.getElementById('uiEditDiv').style.display = 'block';
    document.getElementById('uiEditDiv').scrollIntoView(false);
}

function googleTranslateElementInit() {
    new google.translate.TranslateElement({
        pageLanguage: 'sv'
    }, 'google_translate_element');
}
