﻿//Used by Advanced.aspx
function OnSplitterLoadHandler(sender, eventArgs) {
    var spl = RadSplitter1;
    window.parent.parent.InnerSplitter = spl;
}
//--------------------------------------------------
var oid = 0;
var oiEditedBy = '';
var currentUser = '';
var currentUserProfile = '';
var applicationPath = "";
var radMenu = null;
var radMenuId = null;
var addDialoguePath = "";
var isFullScreen = false;

function OnMouseOver(sender, eventArgs) {
    LogEvent("Mouse over: " + eventArgs.Item.Text);
}
function OnMouseOut(sender, eventArgs) {
    LogEvent("Mouse out: " + eventArgs.Item.Text);
}
function OnClicking(sender, eventArgs) {
    LogEvent("On clicking: " + eventArgs.Item.Text);
}
function OnClicked(sender, eventArgs) {
    LogEvent("On clicked: " + eventArgs.Item.Text);
}
function OnOpen(sender, eventArgs) {
    LogEvent("On open: " + eventArgs.Item.Text);
}
function OnClose(sender, eventArgs) {
    LogEvent("On close: " + eventArgs.Item.Text);
}
function OnFocus(sender, eventArgs) {
    LogEvent("On focus: " + eventArgs.Item.Text);
}
function OnBlur(sender, eventArgs) {
    LogEvent("On blur: " + eventArgs.Item.Text);
}
function LogEvent(text) {
    /*
    var log = document.getElementById("LogEvent");
    log.value += text + "\n";
    log.scrollTop = log.scrollHeight;
    */
}
//-------------------------------------------------------------------------
function menuOutHandler(e) {
    // alert(radMenuId);
    //var MenuInstance = RadMenuNamespace.GetStandardMenu(radMenuId);     
    //MenuInstance.HideMenu();
}
//-------------------------------------------------------------------------
var curTables = [];
function IsObjectInArray(elem, array) {
    if (!array || !array.length) return false;
    for (var i = 0; i < array.length; i++) {
        if (elem == array[i]) return true;
    }
    return false;
}
//-------------------------------------------------------------------------
function OnClientCommandExecuting(editor, commandName, oTool) {
    if ("InsertTable" == commandName) {
        var cArea = editor.GetContentArea();
        var tables = cArea.getElementsByTagName("TABLE");
        for (var i = 0; i < tables.length; i++) {
            curTables[curTables.length] = tables[i];
        }
    }
}
//-------------------------------------------------------------------------
var shouldHide = false;
var timerID;
//-------------------------------------------------------------------------
function OnMouseOver() {
    shouldHide = false;
}
//-------------------------------------------------------------------------
function SetHideTimer() {
    if (radMenu == null) {
        radMenu = RadMenuNamespace.GetStandardMenu(radMenuId);
    }
    shouldHide = true;
    timerID = window.setInterval(HideTimer, 2000);
}
//-------------------------------------------------------------------------
function HideTimer() {
    if (shouldHide) {
        radMenu.HideMenu();
        window.clearInterval(timerID);
    }
}

//-------------------------------------------------------------------------   
function menuEventHandler(e) {
    shouldHide = false;
    try {
        var theTarget
        if (e.target == null) {
            var theTarget = e.srcElement;
        }
        else {
            var theTarget = e.target;
        }
        var attributes = document.getElementById(theTarget.parentNode.parentNode.id + "_Attributes");
        var objectId = '';
        if (attributes != null) {
            theTarget = attributes.attributes["category"].value;
            oiEditedBy = '';
            currentUser = '';
            currentUserProfile = '';

            if (attributes.attributes["editedBy"] != null)
                oiEditedBy = attributes.attributes["editedBy"].value;
            if (attributes.attributes["currentUser"] != null)
                currentUser = attributes.attributes["currentUser"].value;
            if (attributes.attributes["currentUserProfile"] != null)
                currentUserProfile = attributes.attributes["currentUserProfile"].value;
            if (attributes.attributes["oid"] != null)
                objectId = attributes.attributes["oid"].value;
        }
        else {
            attributes = document.getElementById(theTarget.parentNode.id + "_Attributes");
            if (attributes != null) {
                oiEditedBy = '';
                currentUser = '';
                currentUserProfile = '';

                if (attributes.attributes["editedBy"] != null)
                    oiEditedBy = attributes.attributes["editedBy"].value;
                if (attributes.attributes["currentUser"] != null)
                    currentUser = attributes.attributes["currentUser"].value;
                if (attributes.attributes["currentUserProfile"] != null)
                    currentUserProfile = attributes.attributes["currentUserProfile"].value;
                if (attributes.attributes["oid"] != null)
                    objectId = attributes.attributes["oid"].value;

                while ((theTarget != null) && (theTarget.toString().indexOf("object") > -1)) {
                    theTarget = theTarget.parentNode;
                }
                if (theTarget == null) {
                    return false;
                }
            }
            else {
                attributes = document.getElementById(theTarget.id + "_Attributes");
                if (attributes != null) {
                    oiEditedBy = '';
                    currentUser = '';
                    currentUserProfile = '';

                    if (attributes.attributes["editedBy"] != null)
                        oiEditedBy = attributes.attributes["editedBy"].value;
                    if (attributes.attributes["currentUser"] != null)
                        currentUser = attributes.attributes["currentUser"].value;
                    if (attributes.attributes["currentUserProfile"] != null)
                        currentUserProfile = attributes.attributes["currentUserProfile"].value;
                    if (attributes.attributes["oid"] != null)
                        objectId = attributes.attributes["oid"].value;

                    while ((theTarget != null) && (theTarget.toString().indexOf("object") > -1)) {
                        theTarget = theTarget.parentNode;
                    }
                    if (theTarget == null) {
                        return false;
                    }
                }
            }
        }
        oid = objectId;
        if ((oid.length != "") && (isNaN(oid))) {
            //alert("Invalid Object Instance ID : " + oid);
        }
        objectId = null;
    }
    catch (e) {
        //alert(e);
        return false;
    }
}
//-------------------------------------------------------------------------
function menuMouseOut(sender, eventArgs) {
    eventArgs.Item.ParentMenu.HideMenu();
}
//-------------------------------------------------------------------------
function PageMoveUp() {
    //item.ParentMenu.HideMenu();
    launchWin(applicationRoot + "admin/PagePriorityChange.aspx?pos=up&oid=" + oid, 130, 70, "", true);
}
function PageMoveUpAdv() {
    //item.ParentMenu.HideMenu();
    launchWin(applicationRoot + "admin/PagePriorityChange.aspx?pos=up&oid=" + oid, 130, 70, "", true);
    RefreshMenu(oid, "Page management");
}
//-------------------------------------------------------------------------
function PageMoveDown() {
    launchWin(applicationRoot + "admin/PagePriorityChange.aspx?pos=down&oid=" + oid, 130, 70, "", true);
}
function PageMoveDownAdv() {
    launchWin(applicationRoot + "admin/PagePriorityChange.aspx?pos=down&oid=" + oid, 130, 70, "", true);
    RefreshMenu(oid, "Page management");
}
//-------------------------------------------------------------------------
function NewPageSame() {
    launchWin(applicationRoot + "admin/PageEdit.aspx?pos=same&oid=" + oid, 500, 400);
}
function NewPageSameAdv() {
    launchWin(applicationRoot + "admin/PageEdit.aspx?pos=same&oid=" + oid, 500, 400);
}
//-------------------------------------------------------------------------
function NewPageSub() {
    launchWin(applicationRoot + "admin/PageEdit.aspx?pos=sub&oid=" + oid, 500, 400);
}
function NewPageSubAdv() {
    launchWin(applicationRoot + "admin/PageEdit.aspx?pos=sub&oid=" + oid, 500, 400);
}
//-------------------------------------------------------------------------
function PageProp() {
    if (oiEditedBy != '' && currentUser != '' && currentUserProfile != '') {
        if (oiEditedBy != '00000000-0000-0000-0000-000000000000' && oiEditedBy != currentUser) {
            alert("This page is being edited by \"" + currentUserProfile + "\".\nPlease try again later.\nRemember to refresh the page.");
            return false;
        }
    }
    launchWin(applicationRoot + "admin/PageEdit.aspx?edit=true&oid=" + oid, 500, 400);
}
function PagePropAdv() {
    if (oiEditedBy != '' && currentUser != '' && currentUserProfile != '') {
        if (oiEditedBy != '00000000-0000-0000-0000-000000000000' && oiEditedBy != currentUser) {
            alert("This page is being edited by \"" + currentUserProfile + "\".\nPlease try again later.\nRemember to refresh the page.");
            return false;
        }
    }
    launchWin(applicationRoot + "admin/PageEdit.aspx?edit=true&oid=" + oid, 500, 400);
}
function PageDelete() {
    if (oiEditedBy != '' && currentUser != '' && currentUserProfile != '') {
        if (oiEditedBy != '00000000-0000-0000-0000-000000000000' && oiEditedBy != currentUser) {
            alert("This page is being edited by \"" + currentUserProfile + "\".\nPlease try again later.\nRemember to refresh the page.");
            return false;
        }
    }
    if (confirm("Are you sure you want to delete this page?")) {
        //launchWin(applicationRoot + "PageEdit.aspx?edit=true&oid=" + oid, 500, 400);
    }
}
//-------------------------------------------------------------------------
function PageMenuClick(sender, eventArgs) {
    //alert(eventArgs.Item.Value);
    if (oid != 0 && !isNaN(oid)) {
        switch (eventArgs.Item.Value) {
            case "PageUp":
                {
                    PageMoveUp();
                    break;
                }
            case "PageUpAdv":
                {
                    PageMoveUpAdv();
                    break;
                }
            case "PageDown":
                {
                    PageMoveDown();
                    break;
                }
            case "PageDownAdv":
                {
                    PageMoveDownAdv();
                    break;
                }
            case "NewSame":
                {
                    NewPageSame();
                    break;
                }
            case "NewSameAdv":
                {
                    NewPageSameAdv();
                    break;
                }
            case "NewSub":
                {
                    NewPageSub();
                    break;
                }
            case "NewSubAdv":
                {
                    NewPageSubAdv();
                    break;
                }
            case "PageProp":
                {
                    PageProp();
                    break;
                }
            case "PagePropAdv":
                {
                    PagePropAdv();
                    break;
                }
            case " PageDelete":
                {
                    PageDelete();
                    break;
                }
        }
    }
}
//-------------------------------------------------------------------------
function launchWin(url, w, h, windowName, NeverOpenInScreen) {
    if ((windowName == null) || (windowName == ""))
        windowName = "RadWindow1";

    var ContextMenuOpenInScreen = document.getElementById("ContextMenuOpenInScreen");
    if ((ContextMenuOpenInScreen == null) || (NeverOpenInScreen == true)) {
        windowName = windowName + "";

        if (windowName == "undefined")
            windowName = "RadWindow1";

        var oManager;
        var flagPannel = false;
        if (typeof GetRadWindowManager != "undefined") {
            oManager = GetRadWindowManager();
        }
        else {
            oManager = parent.GetRadWindowManager();
            flagPannel = true;
        }

        //Success. Getting existing window DialogWindow using GetWindowByName
        var oWnd = oManager.GetWindowByName(windowName);

        if (oWnd != null) {
            if (flagPannel == true)
                url += "&panelNav=true";

            oWnd.WrapperElement.style.zIndex = '60000';

            //oWnd.MoveTo(250,220);
            oWnd.SetSize(w, h);
            oWnd.SetUrl(url);

            //Success. Opening window
            oWnd.Show();
            oWnd.OnClientClose = "OnClientClose";
            //oWnd.Reload();

            oWnd.argument = this;
        }
        oManager = null;
        oWnd = null;
        return false;
    }
    else {
        if (url.indexOf("?") == -1) {
            url += "?";
        }
        else {
            url += "&";
        }
        url += "contentOnPage=true";
        setUrl(url);
    }
}

function OnClientClose(radWindow, returnValue) {
    radWindow.SetUrl("");
}
//-------------------------------------------------------------------------
function GetRadWindow() {
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog
    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow; //IE (and Moz az well)
    return oWindow;
}
//-------------------------------------------------------------------------
function CloseRadWindow() {
    if (GetRadWindow() != null) {
        GetRadWindow().Close();
    }
}
//-------------------------------------------------------------------------
function RefreshParentPage() {
    GetRadWindow().BrowserWindow.location.reload();
}
//-------------------------------------------------------------------------
function RedirectParentPage(newUrl) {
    GetRadWindow().BrowserWindow.document.location.href = newUrl;
}
//-------------------------------------------------------------------------
function hideWin() {
    hideWin("RadWindow1");
}
function hideWin(windowName) {
    windowName = windowName + "";
    if (windowName == "undefined")
        windowName = "RadWindow1";

    var oManager = GetRadWindowManager();
    var oWnd = oManager.GetWindowByName(windowName);
    oWnd.Hide();
    oManager = null;
    oWnd = null;
}
//-------------------------------------------------------------------------
function openAddDialogue(url) {
    addDialoguePath = url;
    launchWin(url, 450, 400);
    //window.open(url, "addDialogue", "directories=no,height=350,width=390,location=no,menubar=no,resizable=yes,status=no,titlebar=no,toolbar=no,scrollbars=yes");
    return false;
}
//-------------------------------------------------------------------------
function closeAddDialogue() {
    closeAddDialogue("");
}
function closeAddDialogue(addDialoguePath) {
    addDialoguePath = addDialoguePath + "";
    if (addDialoguePath != "" && addDialoguePath != "undefined")
        document.location.href = addDialoguePath;
    else
        CloseRadWindow();
    return false;
}
//-------------------------------------------------------------------------
function textCounter(field, maxlimit) {
    if (field.value.length > maxlimit) {
        field.value = field.value.substring(0, maxlimit);
    }
}
//-------------------------------------------------------------------------
function scrollableWindowOpen(url, width, height) {
    var winl = (screen.width - width) / 2;
    var wint = (screen.height - height) / 2;
    window.open(url, '_blank', 'directories=no, fullscreen=no, height=' + height + ', width=' + width + ', location=no, menubar=no, resizable=no, scrollbars=yes, status=no, titlebar=no, toolbar=no, top=' + wint + ', left=' + winl + '', 'bReplace=false');
}
//-------------------------------------------------------------------------
