﻿function FixTabContainerParent(parentPanelName) {
    try {
        setTimeout('SetControlHeight("' + parentPanelName + '")', 100);
    }
    catch (e)
    { }
}

function SetControlHeight(controlID) {
    try {
        var control = document.getElementById(controlID);

        if (control) {
            control.style.height = "auto";
        }
    }
    catch (e)
    { }
}

// Sets the display of an element to none
function HideElement(containerName) {
    try {
        var control = document.getElementById(containerName);

        if (control) {
            control.style.display = 'none';
        }
    }
    catch (e)
    { }
}

// COOKIES ////////////////////////////////////////

// Add cookie
function setCookie(c_name, value, expiredays) {
    try {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
    }
    catch (e)
    { }
}

// Return cookie value
function getCookie(c_name) {
    try {
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=");
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1) c_end = document.cookie.length;
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
        return "";
    }
    catch (e)
    { }
}

// Check if the cookie has a specific value
function checkCookie(c_name, value) {
    try {
        collapsibleBehavior = getCookie(c_name);
        if (collapsibleBehavior != null && collapsibleBehavior != "") {
            if (collapsibleBehavior == value) {
                return false;
            }
            else {
                return true;
            }
        }
    }
    catch (e)
    { }
}

// TEXT ////////////////////////////////////

// Check if the key is a special key
function CheckSpecialKeys(e) {
    try {
        if (e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40) {
            return false;
        }
        else {
            return true;
        }
    }
    catch (e)
    { }
}

function LimitCharacters(multilineTextbox, maxLength) {
    var txt = $(multilineTextbox);

    if (txt.val().length > maxLength) {
        txt.val(txt.val().substring(0, maxLength));
        return false;
    }
}

/*
DevExpress:
Hides or shows Field Chooser window of a devexpress grid
*/
function ShowHideColumnCustomization(grid) {
    try {
        if (grid.IsCustomizationWindowVisible()) {
            grid.HideCustomizationWindow();
        }
        else {
            grid.ShowCustomizationWindow();
        }

        UpdateButtonText();
    }
    catch (e)
    { }
}

function UpdateButtonText() {

}

/*
A checkbox enables disables a textbox based on its checked state
Usage: $('#<%=chkSendToMyself.ClientID %>').click(function () { CheckboxEnableDisable('<%=chkSendToMyself.ClientID %>', '<%=txtMyEmail.ClientID %>'); });
*/
function CheckboxEnableDisable(checkbox, textbox) {
    var chk = $('#' + checkbox);
    var txt = $('#' + textbox);

    if (chk.is(':checked')) {
        txt.removeAttr('disabled');
        txt.focus();
    }
    else {
        txt.val('');
        txt.attr('disabled', 'disabled');
    }
}

// Arrays

// Searches a 2 dimension named array for the first element that has the specified key value 
function GetElementFromArray(array2Search, keyName, searchValue) {
    for (var i = 0; i < array2Search.length; i++) {
        if (array2Search[i][keyName] == searchValue) {
            return (array2Search[i]);
        }
    }

    return null;
}

// Searches a 2 dimension named array for elements that have the specified key value 
function GetElementsFromArray(array2Search, keyName, searchValue) {
    var resultArray = new Array();

    for (var i = 0; i < array2Search.length; i++) {
        if (array2Search[i][keyName] == searchValue) {
            resultArray.push(array2Search[i]);
        }
    }

    return resultArray;
}

function GetElementsFromArray2Parameters(array2Search, keyName1, searchValue1, keyName2, searchValue2) {
    var resultArray = new Array();

    for (var i = 0; i < array2Search.length; i++) {
        if (array2Search[i][keyName1] == searchValue1 && array2Search[i][keyName2] == searchValue2) {
            resultArray.push(array2Search[i]);
        }
    }

    return resultArray;
}

// Sets the disabled state of button with the given client ID to the checked state of the given chk
// E.g. if the checkbox's checked state is true, the button is enabled
// This is used in pages where an 'I accept' checkbox is required in order to enable a submit button(DeleteAccount, ...)
function SetButtonEnabledState(buttonClientID, checkbox) {
    if (checkbox == null || checkbox == undefined) {
        return;
    }

    var submitBtn = document.getElementById(buttonClientID);
    if (submitBtn != null && submitBtn != undefined) {
        $(submitBtn).attr('disabled', !$(checkbox).attr('checked'));
    }
}

/////////////////////////////////////////////
// Used in ChartControl for legend selection:

// Called when check all is clicked
function SelectUnselectAll(chk) {
    $("input[type='checkbox'][id^='channel_']").each(function () {
        if ($(this).attr('checked') != chk.checked) {
            $(this).attr('checked', chk.checked);
        }
    });

    SelectUnselect();
}

// Called when one of the checkboxes in legend rows are clicked
function SelectUnselect() {
    // encodedString will contain the list of selected channel IDs, separated
    // by spaces
    var encodedString = '';
    var checkBoxList = $("input[type='checkbox'][id^='channel_']");

    // Add to encodedString the channel ID of the selected rows
    $(checkBoxList).filter(":checked").each(function () {
        if (encodedString.length > 0) {
            encodedString += ' ';
        }

        encodedString += $(this).attr('id');
    });

    // If at least one item is unchecked, check all should be unchecked
    // If all items are checked, check all should be checked
    if ($(checkBoxList).not(":checked").length > 0) {
        $("#chkSelectAll").attr('checked', false);
    }
    else {
        $("#chkSelectAll").attr('checked', true);
    }

    $('#hiddenLegendSelections').val(encodedString);
}
/////////////////////////////////////////////
