﻿function ValidateRequiredCheckBoxList(source, args) {
    if( source.controltovalidate2 != null && source.controltovalidate2 != '' )
    {
        var controls = document.getElementById(source.controltovalidate2).getElementsByTagName('INPUT');
        for(i = 0; i<controls.length; i++ )
        {
            if( controls[i].type == 'checkbox' )
            {
                if( controls[i].checked )
                {
                    args.IsValid = true;                    
                    return;
                }
            }
        }
    }    
    args.IsValid = false;
    return;
}
function centerProgressDivs(containerId, left, top) {
    var prog;
    var x = 0;
    var y = 0;
    if (arguments.length == 0) {
        prog = $("div[id*='_divProgress']");
        x = ((document.body.offsetWidth / 2));
        y = ((document.body.offsetHeight / 2) - 38);
    }
    else {
        prog = $("#" + containerId).find("div[id*='_divProgress']");
        x = left;
        y = top;
    }
    prog.css({ 'position': 'fixed', 'left': (x - (prog.width() / 2)) + "px", 'top': (y - (prog.height() / 2)) + "px" });
}
function setProgressMessage(msg) {
    $('div[id*="_divProgress"] > span').html(msg);
}
function showProgressBar(containerId) {
    if (arguments.length == 0) $('div[id*="_divProgress"]').show();
    else $("#" + containerId).find("div[id*='_divProgress']").show();
}
function hideProgressBar(containerId) {
    if (arguments.length == 0) $('div[id*="_divProgress"]').hide();
    else $("#" + containerId).find("div[id*='_divProgress']").hide();
}
function resetProgressMessage() {
    $('div[id*="_divProgress"] > span').html("Loading...");
}
function findParentDiv(callerId) {
    return $("#" + callerId).parents("div.progress_BG");    
}
function EnableUI(state, callerId) {
    var obj = findParentDiv(callerId);
    //alert(callerId + obj.length.toString());
    if (obj.length == 0) {
        centerProgressDivs();
        return;
    }

    if (!state) {
        createProgressBackground(obj.attr("id"));

        var pos = Array();
        pos[0] = obj.offset().left;
        pos[1] = obj.offset().top;
        var offsetY = typeof (window.pageYOffset) == 'number' ? (pos[1] - window.pageYOffset) : (pos[1] - document.documentElement.scrollTop);

        $("#" + obj.attr("progBGId"))
                .css({ "display": "", "position": "fixed", "left": pos[0] + "px", "top": offsetY + "px" })
                .width(obj.width() + "px")
                .height(obj.height() + "px")
                .css("zIndex", 10000)
                .addClass("modalBackground");

        showProgressBar(obj.attr("id"));
        centerProgressDivs(obj.attr("id"), pos[0] + (obj.width() / 2), offsetY + (obj.height() / 2));        
    }
    else {
        hideProgressBar(obj.attr("id"));
        $("#" + obj.attr("progBGId")).css("display", "none");
    }
}
function createProgressBackground(containerId) {
    var obj = $("#" + containerId);
    
    // if not, create, if so do nothing
    var _bgId = obj.attr("progBGId");
    var oBG;
    
    if( _bgId != null && _bgId != '' )
        oBG = $("#" + _bgId);
    else
        _bgId = '';
    
    if (_bgId == '' || oBG.length == 0) {
        var progBGS = $("div[id*='divProgressBackgroundElement_']");
        var id = 'divProgressBackgroundElement_' + progBGS.length;
        obj.attr("progBGId", id);

        var _bgElement = document.createElement("div");
        _bgElement.id = id;

        obj.append(_bgElement);;
    }
}
/*
** Sets the caret (cursor) position of the specified text field.
** Valid positions are 0-oField.length.
*/
function setCaretPosition(elem, caretPos) {
    if (elem != null) {
        if (elem.createTextRange) {
            var range = elem.createTextRange();
            range.move('character', caretPos);
            range.select();
        }
        else {
            if (elem.selectionStart) {
                elem.focus();
                elem.setSelectionRange(caretPos, caretPos);
            }
            else
                elem.focus();
        }
    }

}
/*
** Returns the caret (cursor) position of the specified text field.
** Return value range is 0-oField.length.
*/
function getCaretPosition(oField) {
    // Initialize
    var iCaretPos = 0;

    // IE Support
    if (document.selection) {

        // Set focus on the element
        try { oField.focus(); } catch(e) { }

        // To get cursor position, get empty selection range
        var oSel = document.selection.createRange();

        // Move selection start to 0 position
        oSel.moveStart('character', -oField.value.length);

        // The caret position is selection length
        iCaretPos = oSel.text.length;
    }

    // Firefox support
    else if (oField.selectionStart || oField.selectionStart == '0')
        iCaretPos = oField.selectionStart;

    // Return results
    return (iCaretPos);
}
function setLoginFocus() {
    $($telerik.findElement(document, "txtUsername")).focus();
}
window.blockConfirm = function(text, mozEvent, oWidth, oHeight, callerObj, oTitle, menuItem) {
    var ev = mozEvent ? mozEvent : window.event; //Moz support requires passing the event argument manually 
    //Cancel the event 
    ev.cancelBubble = true;
    ev.returnValue = false;
    if (ev.stopPropagation) ev.stopPropagation();
    if (ev.preventDefault) ev.preventDefault();

    //Determine who is the caller 
    var callerObj = ev.srcElement ? ev.srcElement : ev.target;

    //Call the original radconfirm and pass it all necessary parameters 
    if (callerObj) {
        //Show the confirm, then when it is closing, if returned value was true, automatically call the caller's click method again. 
        var callBackFn = function(arg) {
            if (arg) {
                confirmedClick = true;
                _confirmedClick = true;
                callerObj["onclick"] = "";

                if (callerObj.click) $(callerObj).click(); //Works fine every time in IE, but does not work for links in Moz 
                else if (callerObj.tagName == "A") //We assume it is a link button! 
                {
                    try {
                        eval(callerObj.href)
                    }
                    catch (e) { }
                }
            }
        }

        radconfirm(text, callBackFn, oWidth, oHeight, callerObj, oTitle);
    }
    return false;
}
