﻿function GetQueryString(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

function ReplaceUrlParam(url, name, value) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(url);
    if (results == null)
        return url;
    else {
        return url.replace(results[0], results[0].replace(/=(.*)/, '=' + value));
    }
}

function OpenEditWindow(managerId, pagesPath, windowTitle, editControlVirtualPath, containerPageTypeName, parentModuleName, parentControlTypeName, settingsControlId, editMode, itemId, customerType, view, publicAccess) {
    var manager = $find(managerId);
    var wnd = manager.Open(pagesPath + "/Edit.aspx?WindowTitle=" + windowTitle + "&EditControlVirtualPath=" + editControlVirtualPath + "&ContainerPageTypeName=" + containerPageTypeName + "&ParentModuleName=" + parentModuleName + "&ParentControlTypeName=" + parentControlTypeName + "&SettingsControlId=" + settingsControlId + "&EditMode=" + editMode + "&ItemId=" + itemId + "&CustomerType=" + customerType + "&View=" + view + "&PublicAccess=" + publicAccess, "RadWindow1");
}

function GetRadWindow() {
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow;
    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
    return oWindow;
}

function CloseRadWindow() {
    var oWindow = GetRadWindow();
    oWindow.argument = null;
    oWindow.close();
} 

function EditWindowClosing(wnd, args) {
    if (args.get_argument() != null) {
        if (args.get_argument().action == 'save') {
            // Check if we should close the parent (only if it's a pop-up)
            if (args.get_argument().closeParent != null && args.get_argument().closeParent.length > 0 && args.get_argument().closeParent == 'Yes') {
                CloseWindow(args.get_argument().action, args.get_argument().view);
            }
            else {
                // Check if the Edit Page is returning a View we should return to
                if (args.get_argument().view != null && args.get_argument().view.length > 0) {
                    // Check if the View parameter was already part of the URL
                    if (GetQueryString('View', null).length > 0) {
                        // If so, replace it with the new value
                        window.location.href = ReplaceUrlParam(window.location.href, 'View', args.get_argument().view);
                    }
                    else {
                        // If not, add it to the URL
                        if (window.location.href.indexOf('?') != -1) {
                            // Not the first parameter
                            window.location.href = window.location.href + "&View=" + args.get_argument().view;
                        }
                        else {
                            // The first parameter
                            window.location.href = window.location.href + "?View=" + args.get_argument().view;
                        }
                    }
                }
                else {
                    window.location.href = window.location.href;
                }
            }
        }
    }
}

function OpenConfigurationWindow(managerId, pagesPath, displayTitleEnabled, showDisplayTitle, pageTypeName, controlVirtualPath, controlId, editModeEnabled, securityByModeEnabled, basicViewChildControlsVirtualPaths, advancedViewControlVirtualPath, basicEditChildControlsVirtualPaths, advancedEditControlVirtualPath, module, component) {
    var manager = $find(managerId);
    var wnd = manager.Open(pagesPath + "/Configuration.aspx?DisplayTitleEnabled=" + displayTitleEnabled + "&ShowDisplayTitle=" + showDisplayTitle + "&PageTypeName=" + pageTypeName + "&ControlVirtualPath=" + controlVirtualPath + "&ControlId=" + controlId + "&EditModeEnabled=" + editModeEnabled + "&SecurityByModeEnabled=" + securityByModeEnabled + "&BasicViewChildControlsVirtualPaths=" + basicViewChildControlsVirtualPaths + "&AdvancedViewControlVirtualPath=" + advancedViewControlVirtualPath + "&BasicEditChildControlsVirtualPaths=" + basicEditChildControlsVirtualPaths + "&AdvancedEditControlVirtualPath=" + advancedEditControlVirtualPath + "&Module=" + module + "&Component=" + component, "RadWindow1");
}

function ConfigurationWindowClosing(wnd, args) {
    if (args.get_argument() != null) {
        if (args.get_argument().action == 'save') {
            window.location.href = window.location.href;
        }
    }
}

function GetWindow() {
    var wnd = null;
    if (window.radWindow) wnd = window.radWindow;
    else if (window.frameElement.radWindow) wnd = window.frameElement.radWindow;
    return wnd;
}

function MailTo(email, subject) {
    location.href = "mailto:" + email + "?subject=" + subject;
}

function ShowConfirmDialog(managerId, callBackFunctionName, dialogText, dialogTitle, width, height) {
    var callBackFunction = window[callBackFunctionName];
    var manager = $find(managerId);
    manager.radconfirm(dialogText, callBackFunction, width, height, null, dialogTitle);
}

function ShowAlertDialog(managerId, callBackFunctionName, dialogText, dialogTitle, width, height) {
    var callBackFunction = window[callBackFunctionName];
    var manager = $find(managerId);
    manager.radalert(dialogText, width, height, dialogTitle, callBackFunction);
}

/* Accounts */

function OnClientNodeClicking(sender, args) {
    var node = args.get_node();
    var nodeUrl = node.get_navigateUrl();
    if (nodeUrl != null) {
        location.replace(nodeUrl);
    }
}

/* End Accounts */

/* Form */

function InitializeForm() {
    var allInputs = document.getElementsByTagName("input");

    // Sections
    for (var i = 0; i < allInputs.length; i++) {
        if (allInputs[i].getAttribute('TabName') != null) {
            var tabName = allInputs[i].getAttribute('TabName');

            if (allInputs[i].checked) {
                EnableSections(tabName, true);
            }
        }
    }

    // Fields
    for (var i = 0; i < allInputs.length; i++) {
        if (allInputs[i].getAttribute('SectionName') != null) {
            var tabName = allInputs[i].getAttribute('ParentTabName');
            var sectionName = allInputs[i].getAttribute('SectionName');

            if (allInputs[i].checked) {
                EnableFields(tabName, sectionName, true);
            }
        }
    }
}

function EnableSections(tabName, enabled) {
    var allInputs = document.getElementsByTagName("input");
    for (var i = 0; i < allInputs.length; i++) {
        if (allInputs[i].getAttribute('SectionName') != null && allInputs[i].getAttribute('ParentTabName') == tabName) {
            allInputs[i].disabled = !enabled;
        }
    }
}

function EnableFields(tabName, sectionName, enabled) {
    // Inputs
    var allInputs = document.getElementsByTagName("input");
    for (var i = 0; i < allInputs.length; i++) {
        if (allInputs[i].getAttribute('ParentTabName') == tabName && allInputs[i].getAttribute('ParentSectionName') == sectionName) {
            allInputs[i].disabled = !enabled;
        }
    }

    // Select
    var allSelects = document.getElementsByTagName("select");
    for (var i = 0; i < allSelects.length; i++) {
        if (allSelects[i].getAttribute('ParentTabName') == tabName && allSelects[i].getAttribute('ParentSectionName') == sectionName) {
            allSelects[i].disabled = !enabled;
        }
    }
}

/* End Form */

/* Lookup */

function OpenLookupWindow(managerId, pagesPath, entity, fieldValue, fieldText, pageSize, skin, multiSelect) {
    var manager = $find(managerId);
    var pageUrl = "";

    var values = escape(document.getElementById(fieldValue).value);
    var texts = escape(document.getElementById(fieldText).value);

    pageUrl = pagesPath + "/Lookup.aspx?entity=" + entity + "&pageSize=" + pageSize + "&skin=" + skin + "&values=" + values + "&texts=" + texts + "&multiselect=" + multiSelect.toString(), "RadWindow1";

    var wnd = manager.Open(pageUrl);

    var args = new Object();
    args.fieldValue = fieldValue;
    args.fieldText = fieldText;

    wnd.argument = args;
}

function LookupWindowClosing(wnd, args) {
    if (args.get_argument() != null) {
        document.getElementById(wnd.argument.fieldValue).value = args.get_argument().selectedValue;
        document.getElementById(wnd.argument.fieldText).value = args.get_argument().selectedText;
    }
}

/* End Lookup */

/* Access Lookup */

function OpenAccessLookupWindow(managerId, pagesPath, windowTitle, displayTitle, fieldValueId, fieldLevelCutoffId, fieldTextId) {
    var manager = $find(managerId);
    var fieldValue = document.getElementById(fieldValueId).value;
    var fieldLevelCutoff = document.getElementById(fieldLevelCutoffId).value;
    var wnd = manager.Open(pagesPath + "/AccessLookup.aspx?WindowTitle=" + windowTitle + "&DisplayTitle=" + displayTitle + "&FieldValue=" + fieldValue + "&FieldLevelCutoff=" + fieldLevelCutoff, "RadWindow1");

    var args = new Object();
    args.fieldValueId = fieldValueId;
    args.fieldLevelCutoffId = fieldLevelCutoffId;
    args.fieldTextId = fieldTextId;

    wnd.argument = args;
}

function AccessLookupWindowClosing(wnd, args) {
    if (args.get_argument() != null) {
        document.getElementById(wnd.argument.fieldValueId).value = args.get_argument().selectedValue;
        document.getElementById(wnd.argument.fieldLevelCutoffId).value = args.get_argument().selectedLevelCutoff;
        document.getElementById(wnd.argument.fieldTextId).value = args.get_argument().selectedText;
    }
}

/* End Access Lookup */

/* Select Sessions */

function selectSession(check, textbox) {
    if (check.disabled)
        return;

    textbox = (typeof textbox == 'string') ? document.getElementById(textbox) : textbox;

    SetRowState(check, textbox);

    if (check.checked) {
        if (textbox.value == "" || textbox.value == "0")
            textbox.value = "1";
    } else {
        textbox.value = "0";
    }
}

function SetRowState(check, textbox) {
    textbox.disabled = !check.checked;
}

/* End Select Sessions */

/* Status Bar */

function ProcessWarningAnswer(arg) {
    if (arg) {
        __doPostBack("FilterAndContinueToShippingCart", "");
    }
}

/* End Status Bar */

/* Navigation */

function OpenNavigationPanelConfigWindow(managerId, pagesPath, name, selectedEventTypes, someoneElseRegistration) {
    var manager = $find(managerId);
    var wnd = manager.Open(pagesPath + "/NavigationConfig.aspx?Name=" + name + "&EventTypes=" + selectedEventTypes + "&WindowTitle=Config. Navigation&DisplayTitle=Edit Navigation&SomeoneElseRegistration=" + someoneElseRegistration, "RadWindow1");
}

/* End Navigation */
