﻿function updateDateRange() {
    $("#dateRange").text(getStartDatePicker().value().toString("MMMM d, yyyy") + " - " + getEndDatePicker().value().toString("MMMM d, yyyy"));
    $("#dateRange").animate({ color: "#F57601" }, 750);
    $("#dateRange").animate({ color: "#444444" }, 750);
}

var dialog;

function createNote(source, parent) {
    dialog = $.telerik.window.create({
        title: "Add Note",
        name: "CreateNote",
        contentUrl: '/notes/create/?sourceId=' + source + '&parentId=' + parent,
        modal: true,
        resizable: false,
        draggable: true,
        visible: false,
        width: 600,
        height: 350,
        onClose: function () { window.location.reload; }
    })
    .data('tWindow').center().open();
}

function saveNote() {
    var sourceId = $("#SourceId").val();
    var parentId = $("#ParentId").val();
    var noteBody = $("#noteBody").data('tEditor').encodedValue();
    $.post('/notes/savenote', 
        {sourceId: sourceId, parentId: parentId, noteBody: noteBody},
        function (data) {
            $("#saveResult").html(data);
        });
}


function viewNote(noteId) {
    dialog = $.telerik.window.create({
        title: "View/Edit Note",
        contentUrl: '/notes/edit/' + noteId,
        modal: true,
        resizable: false,
        draggable: true,
        visible: false,
        width: 600,
        height: 400
    })
    .data('tWindow').center().open();
}

function deleteNote(noteId) {
    if (confirm("Are you sure you want to delete this note?")) {
        $.ajax({
            url: '/notes/delete/' + noteId,
            type: 'DELETE'
        });
        closeNote();
    } 
}

function closeNote() {
    if (dialog != null) {
        dialog.close();
        dialog = null;
    }
    var url = window.location.href.replace("#", "");
    window.location.href = url;
}
