﻿function getNavigatorAgent(){
    return navigator.userAgent;
}

function is_ie(){
    var agent = getNavigatorAgent();
    var msieShift = agent.indexOf("MSIE");
    var msie = agent.indexOf("msie");
    return (( msieShift != -1 ) || ( msie != -1));
}

function is_ie8() {
    var agent = getNavigatorAgent();
    var msieShift = agent.indexOf("MSIE 8.0");
    var msie = agent.indexOf("msie 8.0");
    var msie7 = agent.indexOf("msie 7.0");
    return ((msieShift != -1) || (msie != -1));
}

function is_mozilla(){
    var agent = getNavigatorAgent();
    return (agent.indexOf("Mozilla") != -1);
}

function showtable(controlId)
    {
        var tabla = document.getElementById("tblContent_"+controlId);
        if( tabla ){
            tabla.style.display = "";
            var toolTipImage = document.getElementById("tooltip_image_"+controlId);
            var toolTipSpan = document.getElementById("tooltip_span_"+controlId);
            if(is_ie()){
                if(toolTipImage){
                    toolTipImage.style.display = "none";
                }
            }
            else{
                if(toolTipSpan){
                    toolTipSpan.style.display = "none";
                }
            }
        }
    }
function FCKUpdateLinkedField(id)
{
    try
    {
        if(typeof(FCKeditorAPI) == "object")
        {
            FCKeditorAPI.GetInstance(id).UpdateLinkedField();
        }
    }
    catch(err) {

    }
}
function openSendMessageWindow(root, idReceiver){
    window.open(root + 'users/sendmessage/' + idReceiver, 'sendMail', 'status=Yes,toolbar=No,resizable=Disallow, width=640, height=540, scrollbars=No');
}
function ShowHideElement(divId,hiddenFieldId){
    var div = document.getElementById(divId);
    var hiddenField = document.getElementById(hiddenFieldId);
    
    if(div && hiddenField)
    {
        if (div.style.display == "")
        {
            div.style.display = "none";
            hiddenField.value = "display:none;";
        }
        else
        {
            if (div.style.display == "none")
            {
                div.style.display = "";
                hiddenField.value = "display:";
            }
        }
    }
}
function ShowHideElement(divId) {
    var div = document.getElementById(divId);

    if (div) {
        if (div.style.display == "") {
            div.style.display = "none";
        }
        else {
            if (div.style.display == "none") {
                div.style.display = "";
            }
        }
    }
}
function changeClass(element, className){
    element.className = className;
}
function ValidateDocumentExtension(fileUploadId, notValidExtensions, validatorTextContainerId) {
    var fileupload = document.getElementById(fileUploadId);
    var validatorTextContainer = document.getElementById(validatorTextContainerId);
    if (fileupload && fileupload.value != "") {
        var splitedName = fileupload.value.split(".");
        var extension = splitedName[splitedName.length - 1];
        if (notValidExtensions.indexOf(extension) != -1) {
            Page_IsValid = false;
            validatorTextContainer.style.display = "";
            return false;
        } else {
        validatorTextContainer.style.display = "none";
            return true;
        }
    }
}
function ValidateDocumentExtensionForConversion(fileUploadId, popupId, hidpopupId, validExtensions, okButtonUniqueId, isVirtualRoomEnabled, ajaxLoaderPath) {
    var fileupload = document.getElementById(fileUploadId);
    var popup = $('#'+popupId);
    var hidpopup = $('#' + hidpopupId);
    if (Page_IsValid == true && fileupload && fileupload.value != "") {
        if (isVirtualRoomEnabled == "True") {
            var splitedName = fileupload.value.split(".");
            var extension = splitedName[splitedName.length - 1];
            if (extension && extension != "") {
                if (validExtensions.indexOf(extension) != -1) {
                    uploadDocument(ajaxLoaderPath);
                    __doPostBack(okButtonUniqueId, '')
                    return true;
                } else {
                    if (hidpopup && hidpopup != null) {
                        hidpopup.hide();
                    }
                    if (popup && popup != null) {
                        popupStatus = 0;
                        showPopup(popup);
                    }
                    
                    return false;
                }
            }
        } else {
            uploadDocument(ajaxLoaderPath);
            __doPostBack(okButtonUniqueId, '')
            return true;
        }
    }
}
function ValidateContentExtension(fileUploadId, validExtensions, validatorTextContainerId) {
    var fileupload = document.getElementById(fileUploadId);
    var validatorTextContainer = document.getElementById(validatorTextContainerId);
    if (fileupload && fileupload.value != "") {
        var splitedName = fileupload.value.split(".");
        var extension = splitedName[splitedName.length - 1];
        if (validExtensions.indexOf(extension.toLowerCase()) == -1) {
            Page_IsValid = false;
            validatorTextContainer.style.display = "";
            return false;
        } else {
            validatorTextContainer.style.display = "none";
            return true;
        }
    }
}
function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent)
        while (1) {
        curleft += obj.offsetLeft;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent)
        while (1) {
        curtop += obj.offsetTop;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}


if (navigator.userAgent.indexOf('WebKit/') > -1) {
    Sys.Browser.WebKit = {};
    Sys.Browser.agent = Sys.Browser.WebKit;
    Sys.Browser.version = parseFloat(navigator.userAgent.match(/WebKit\/(\d+(\.\d+)?)/)[1]);
    Sys.Browser.name = 'WebKit';
}

function showPopup(targetPopup) {
    activePopup = targetPopup;
    loadPopup();
    centerPopup();
}
$(window).scroll(function() {
    if (popupStatus == 1) {
        centerPopup();
    }
});
$(window).resize(function() {
    if (popupStatus == 1) {
        centerPopup();
    }
});
//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var activePopup;
//loading popup with jQuery magic!
function getBackgroundPopup() {
    return $("#backgroundPopup").first();
}
function loadPopup() {
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        getBackgroundPopup().css({
            "opacity": "0.7"
        });
        getBackgroundPopup().fadeIn("fast");
        activePopup.fadeIn("fast");
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function hidePopup() {
    //disables popup only if it is enabled
    //$(this).preventDefault();
    if (popupStatus == 1 || popupStatus == 2) {
        getBackgroundPopup().fadeOut("fast");
        activePopup.fadeOut("fast");
        popupStatus = 0;
    }
}

//centering popup
function centerPopup() {
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = activePopup.height();
    var popupWidth = activePopup.outerWidth();
    if (is_IE()) {
        //IE CANNOT ANIMATE USING ATTRIBUTE POSITION
        activePopup.css({
            "position": "absolute",
            "display":"block",
            "top": (windowHeight / 2 - popupHeight / 2) + $(window).scrollTop(),
            "left": windowWidth / 2 - popupWidth / 2,
            "z-index": 50
        });
        //only need force for IE6
        getBackgroundPopup().css({
            "position": "absolute",
            "display":"block",
            "top": $(window).scrollTop(),
            "left": 0,
            "width": windowWidth,
            "height": windowHeight
        });
    } else {
        activePopup.css({
            "position": "fixed",
            "top": (windowHeight / 2 - popupHeight / 2),
            "left": windowWidth / 2 - popupWidth / 2,
            "z-index": 50
        });
        getBackgroundPopup().css({
            "position": "absolute",
            "display": "block",
            "top": $(window).scrollTop(),
            "width": windowWidth,
            "height": windowHeight
        });
    }
}
function showPopupHelp(trigger,popup) {
    var top = $(trigger).offset().top - 30;
    var left = $(trigger).offset().left + 20;
    activePopup = popup;
    loadPopup(popup);
    popupStatus = 2;
    $(popup).css({
        "display": "block",
        "position":"absolute",
        "top": top,
        "left": left,
        "z-index":9999
    });
}
$(document).ready(function() {
    extendDOMObjects();
    $(document).keypress(function(e) {
        //Press Enter event!
        if (e.keyCode == 13 && popupStatus == 1) {
            var defaultId = activePopup.attr("DefaultButton");
            if (defaultId && defaultId != "") {
                firePostBackEvent(defaultId);
            }
        }
    });
});

function is_IE() {
    if ($.browser.name == "msie") {
        return true;
    }
    return false;
}

function changeClass(element, className) {
    element.className = className;
}

function firePostBackEvent(controlId) {
    __doPostBack(controlId, '');
}
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, args) {
    extendDOMObjects();
}
function extendDOMObjects() {
    createMaskedObjects();
    createCalendarObjects();
    createTooltipObjects();
    createPasswordStrengthObjects();
    ReBindTooltip();
    createHelpTooltipObjects();
}

function createMaskedObjects() {
    $(".ui-extend-time-mask").unmask();
    $(".ui-extend-date-mask").unmask();

    $(".ui-extend-time-mask").mask("99:99");
    $(".ui-extend-date-mask").mask("99/99/9999");
}

function createCalendarObjects() {
    $.datepicker.setDefaults($.extend({ showMonthAfterYear: false }, $.datepicker.regional[document.getElementById('__CALENDARREGIONALIZATION').value]));
    $(".ui-extend-datepicker").datepicker('destroy');
    $(".ui-extend-datepicker").datepicker({ changeMonth: true, changeYear: true, yearRange: '1910:2100',onSelect: function() { } });

    $(".ui-extend-datepicker-button").each(function() {
    $(this).datepicker({ showOn: 'button', buttonImage: $(this).attr("ImageUrl"), buttonImageOnly: true, changeMonth: true, changeYear: true, yearRange: '1910:2100' });
    }); 
}

function createTooltipObjects() {
    var elements = $(".ui-extend-tooltip");
    elements.each(function(i) {
        var element = $(this);
        $('#' + element.attr('id'))
            .tooltip
            ({
                tip: '#' + element.attr('id') + '_tooltip',
                opacity: '1',
                effect: 'fade',
                position: 'bottom center',
                delay: 250,
                events: {
                    def: "click,mouseout",
                    input: "focus,blur",
                    widget: "focus mouseover,blur mouseout",
                    tooltip: "mouseover,mouseout"
                }
            });
    });

    var txtelements = $(".ui-extend-tooltip-textbox");
    txtelements.each(function(i) {
        var element = $(this);
        $('#' + element.attr('id'))
            .tooltip
            ({
                tip: '#' + element.attr('id') + '_tooltip',
                opacity: '1',
                effect: 'slide',
                position: 'top center',
                delay: 250,
                offset: [8, 0],
                events: {
                    def: "",
                    input: "click,blur",
                    widget: "focus mouseover,blur mouseout",
                    tooltip: ""
                }
            });
    });
}
var passwordStrengthTexts = "";
function createPasswordStrengthObjects() {
    if (passwordStrengthTexts != "") {
        var text = passwordStrengthTexts.split(";");
        $('.passwordStrength').password_strength(
        {
            'container': null,
            'minLength': 6,
            'texts': {
                1: text[0],
                2: text[1],
                3: text[2],
                4: text[3],
                5: text[4]
            }
        });
    }
}
function ReBindTooltip() { $('.categoryTooltip').each(function(index, value) { $(this).qtip({ position: { corner: { target: 'topMiddle', tooltip: 'bottomMiddle'} }, style: { background: '', width: 35, height: 30, padding: 0, color: 'black', textAlign: 'center', margin: 0, border: { width: 0, radius: 0, color: '#FFFFFF' }, name: 'light' }, content: '<div class=\"mediaLibraryTooltip ' + $(value).attr("imageurl") + '\"></div>' }); }); }
//Lists
function showDefaultSearchMessage() {
    popupStatus = 0;
    showPopup($('#SetAsDefaultMessagePanel'));
}
function showMessageById(id) {
    popupStatus = 0;
    showPopup($('#' + id));
}
function hideOverlayById(id) 
{
    hidePopup(('#' + id));
}
var hdCpeFilterCollapsedState = "";
var pnlFiltersId = "";
var filtersText = $('#filtersText');
function BindFiltersBox(id, hdCpeFilterCollapsedStateId, collapsedStateValue) {
    hdCpeFilterCollapsedState = hdCpeFilterCollapsedStateId;
    pnlFiltersId = id;
    UpdateCollapsedStateText(collapsedStateValue);
    $('#pnlFiltersTitle').unbind('click').click(function() {
            var filtersContainer = $('#' + id);
            var textLabelContainer = $('#filtersText');
            if (filtersContainer.css('display') == 'none') {
                showFilters(filtersContainer, textLabelContainer);
            } else {
                hideFilters(filtersContainer, textLabelContainer);
            }
    });
}
function UpdateCollapsedStateText(collapsedStateValue) {
    if (document.getElementById(hdCpeFilterCollapsedState) != null) {
        var state = document.getElementById(hdCpeFilterCollapsedState).value;
        if (state == "True") {
            hideFilters($('#' + pnlFiltersId), $('#filtersText'));
        } else {
            showFilters($('#' + pnlFiltersId), $('#filtersText'));
        }
    }
}
function hideFilters(filtersContainer, textLabelContainer) {
    filtersContainer.slideUp();
    textLabelContainer.text(textClickToExpand);
    $('.filtersImage').removeClass('collapse').addClass('expand');
    document.getElementById(hdCpeFilterCollapsedState).value = true;
}
function showFilters(filtersContainer, textLabelContainer) {
    filtersContainer.slideDown();
    textLabelContainer.text(textClickToCollapse);
    $('.filtersImage').removeClass('expand').addClass('collapse');
    document.getElementById(hdCpeFilterCollapsedState).value = false;
}
function showFiltersFromBehind(show) {
    if (pnlFiltersId != "") {
        var filtersContainer = $('#' + pnlFiltersId);
        var textLabelContainer = $('#filtersText');
        if (show == 'True') {
            showFilters(filtersContainer, textLabelContainer);
        } else {
            hideFilters(filtersContainer, textLabelContainer);
        }
    }
}
var helpButtonClicked = false;
function hideHelps() {
    $('.helpContainer:visible').hide();
}
function createHelpTooltipObjects() {
    var elements = $(".help-tooltip");
    elements.each(function(i) {
        var element = $(this);
        var tipPosition = "bottom center";
        if (element.attr('orientation') == "Left") {
            tipPosition = "bottom left";
        }
        else if (element.attr('orientation') == "Right") {
            tipPosition = "bottom right";
        }
        var isRelative = false;
        if (element.attr('type') == "popup") {
            isRelative = true;
        }
        $('#' + element.attr('id')).tooltip({
            tip: '#' + element.attr('tooltipreference'),
            opacity: '1',
            effect: 'fade',
            position: tipPosition,
            delay: 250,
            events: {
                def: "click,mouseout",
                input: "focus,blur",
                widget: "focus mouseover,blur mouseout",
                tooltip: "mouseover,mouseout"
            },
            relative: isRelative
        });
    });
}
