﻿// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// EDIT WINDOW HELPER ROUTINES
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

// this function will open the correct RAD window based upon
// the client id, with the given URL
function openPopupEditor(url, clientId) {
    var windowObj = $find(clientId);
    windowObj.setUrl(url);
    windowObj.show();
}

// this function is called upon the rad editor window
// being closed... if requested, reload the current page
function onPopupEditWindowClose(sender, eventArgs) {
    var reload = sender.argument;
    if (reload == true)
        window.location.reload();
}


// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// ADMIN CONTEXT MENU ROUTINES
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

// this function will show a context menu over the element that triggered
// the event; the RAD context menu client ID should be specified on the
// element as an attribute of "radcontextmenuid"
var lastRADContextMenuTarget = null;
function showRADContextMenu(e) {
    // retrieve the element that caused the event and the context menu
    // associated with that element
    var target = e.target;
    var contextMenuId = target.getAttribute('radcontextmenuid');

    // show the context menu
    $find(contextMenuId).showAt(e.clientX, e.clientY);
    lastRADContextMenuTarget = target;

    // stop the propogation of the event
    e.stopPropagation();
}

// this routine will return the target of the last RAD context menu that
// was clicked; it is intended to provide that information to a specific
// context menu event handler
function getLastRADContextMenuTarget() {
    return lastRADContextMenuTarget;
}


function handleRADContextMenuClick(sender, args) {
    // retrieve the individual context menu item
    // that the user executed (clicked) on
    var clickedItem = args.get_item();
    var contextTarget = getLastRADContextMenuTarget();

    // handle standard (known) commands in a pre-defined way
    switch (clickedItem.get_value()) {
        case 'edit':
            // the user is asking to edit an item... the URL for the editor will be contained within
            // an attribute of the target of the context menu
            var editurl = contextTarget.getAttribute('editurl');
            var editwindow = contextTarget.getAttribute('editwindowid');
            openPopupEditor(editurl, editwindow);
            break;
            
        case 'delete':
            // the user is asking to delete an item... the deletion window (confirm) URL will be
            // stored as an attribute on the target of the context menu
            var editurl = contextTarget.getAttribute('deleteurl');
            var editwindow = contextTarget.getAttribute('deletewindowid');
            openPopupEditor(editurl, editwindow);
            break;
    }
}
