﻿/// <reference name="MicrosoftAjax.js" />



//
//
// Sample:
//<div id='containerUniqId' style='height: 100%'>
//	<div id='headerUniqId' />text1</div>
//	<div id='contentUniqId' style='overflow: auto'>text2</div>
//</div>
//<script language='javascript' type='text/javascript'>
//	ExpandElementToFillContainerByHeight('containerUniqId', 'headerUniqId', 'contentUniqId');
//</script>
function ExpandElementToFillContainerByHeight(containerId, fixedElementId, flexibleElementId) {
    Sys.Application.add_load(function() {
        function collapseElement(flexibleElement) {
            //flexibleElement.style.height = '0px';
            flexibleElement.style.display = 'none';
        }
        function expandElement(e) {
            //e.fixedElement.style.whiteSpace = 'nowrap';
            var restHeight = e.container.clientHeight - e.fixedElement.offsetHeight;
            if (restHeight < 0)
                restHeight = 0

            if (e.lastHeight != restHeight) {
                e.lastHeight = restHeight
                e.flexibleElement.style.height = restHeight.toString() + 'px';
            }

            if (e.flexibleElement.style.removeAttribute)
                e.flexibleElement.style.removeAttribute('display');
            else
                e.flexibleElement.style.removeProperty('display');
        }

        function expand() {
            if (!window._expandElements)
                return;

            for (var k in window._expandElements)
                collapseElement(window._expandElements[k].flexibleElement);

            for (var k in window._expandElements)
                expandElement(window._expandElements[k]);
        }
        if (!window._expandElements) {
            window._expandElements = new Array();
            $addHandler(window, 'resize', expand);
        }

        var container = $get(containerId);
        var fixedElement = $get(fixedElementId);
        var flexibleElement = $get(flexibleElementId);
        $addHandler(container, 'resize', expand);
        $addHandler(fixedElement, 'resize', expand);
        $addHandler(flexibleElement, 'resize', expand);

        window._expandElements[containerId] = {
            container: container,
            fixedElement: fixedElement,
            flexibleElement: flexibleElement,
            lastHeight: -1
        };
        expand();
    });
}



//
// Clears default value in textbox.
//
function ClearTextBoxDefaultValue(tb, defaultValue) {
    if (tb.value == defaultValue)
        tb.value = '';
}

//
// Posts page with specified command when ENTER pressed.
//
function PostBackCommandByEnter(event, controlUniqueId, postBackCommand) {
    if (event.keyCode != 13)
        return true;

    var src = event.srcElement || event.target;
    if (!src || (src.tagName.toLowerCase() === 'textarea'))
        return true;

    DoPagePostBack(controlUniqueId, postBackCommand);
    stopEvent(event);
    return false;
}

//
// Posts page from specified control with specified command.
//
function DoPagePostBack(controlUniqueId, postBackCommand) {
    Sys.WebForms.PageRequestManager.getInstance()
		._doPostBack(controlUniqueId, postBackCommand);
}



//
// Stops event propagation and bubbling.
//
function stopEvent(event) {
    if (event) {
        event.cancelBubble = true;

        if (typeof (event.stopPropagation) === 'function')
            event.stopPropagation();
    }
}
//
// Stops event propagation and bubbling.
//
function stopEventAndPreventDefault(event) {
    if (event) {
        stopEvent(event);

        if (typeof (event.preventDefault) === 'function')
            event.preventDefault();
    }
}



//
// Clicks specified button when ESC pressed.
//
function FireButtonClickByEsc(event, buttonUniqueId) {
    if (event.keyCode != 27)
        return true;

    $get(buttonUniqueId).click();
    stopEvent(event);
    return false;
}
//
// Clicks specified button when ENTER pressed.
//
function FireButtonClickByEnter(event, buttonUniqueId) {
    if (event.keyCode != 13)
        return true;

    var src = event.srcElement || event.target;
    if (!src || (src.tagName.toLowerCase() === 'textarea'))
        return true;

    $get(buttonUniqueId).click();
    stopEvent(event);
    return false;
}



/*
fixed in net35sp1?

Sys.WebForms.PageRequestManager.prototype._fireDefaultButton =
function(event, target) {
if (event.keyCode === 13) {
var src = event.srcElement || event.target;
if (!src || (src.tagName.toLowerCase() !== 'textarea')) {
var defaultButton = document.getElementById(target);
if (defaultButton && (typeof (defaultButton.click) !== 'undefined')) {
this._activeDefaultButton = defaultButton;
this._activeDefaultButtonClicked = false;
try {
defaultButton.click();
}
finally {
this._activeDefaultButton = null;
}

stopEvent(event);
return false;
}
}
}
return true;
};




/*

//
// Fixes bug in FireFox for default button handing on pressing 'Enter' key.
//
function WebForm_FireDefaultButton (event, target)
{
if (event.keyCode == 13) 
{
if ( !event.srcElement || (event.srcElement.tagName.toLowerCase() !== 'textarea'))
{
// get defaultButton
var defaultButton;
if (__nonMSDOMBrowser)
defaultButton = document.getElementById(target);
else
defaultButton = document.all[target];
   
if (defaultButton && typeof(defaultButton.click) !== 'undefined') 
{
defaultButton.click();
stopEvent(event);
return false;
}
else
{
if (emulateClick (defaultButton))
{
stopEvent(event);
return false;
}
}
}
}
return true;
}

//
// returns true if succeeded
//
function emulateClick (ctrl)
{
if( !ctrl || typeof(ctrl.click) != 'undefined' )
return false;

if (typeof (document.createEvent) != 'undefined')
{
// FF
var evt = ctrl.ownerDocument.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, ctrl.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
ctrl.dispatchEvent(evt);
}
else
{
// IE
ctrl.click();
}
	
return true;
}
/*
*/





//
// Shows ajax exception in messagebox.
//
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(ajaxErrorHandler);
function ajaxErrorHandler(sender, args) {
    if (args.get_error() != undefined) {
        var m = args.get_error().message;
        m = m.substr(m.indexOf(':') + 1).trim();
        alert(m);
        args.set_errorHandled(true);
    }
}


//
// Fixes the bug of several nested modal popups.
//
Sys.Application.add_init(initModalPopupBehavior);
function initModalPopupBehavior() {
    if (window.AjaxControlToolkit && AjaxControlToolkit.ModalPopupBehavior) {
        var mpb = AjaxControlToolkit.ModalPopupBehavior;
        if (!mpb._modalZIndexBugFixed) {
            mpb._modalZIndexBugFixed = true;    // prevents recursion when the script is loaded in main window and in its iframe
            mpb.curModalZIndex = 10000;
            mpb.originalMPEInit = mpb.prototype.initialize;
            mpb.prototype.initialize = function() {
                mpb.originalMPEInit.call(this);
                this._backgroundElement.style.zIndex = mpb.curModalZIndex++;
                this._foregroundElement.style.zIndex = mpb.curModalZIndex++;
            };
        }
    }
}
//Sys.Application.add_load(initCreatedModalPopups);
//function initCreatedModalPopups() {
//    if (window.AjaxControlToolkit && AjaxControlToolkit.ModalPopupBehavior) {
//        setTimeout(function() {
//            //			alert ($get ('ctl00_ctl00_mainPlaceHolder_zoneLeft_ctl00_moduleBlock_3687_ctl00_lnkConfig').style.zIndex);
//        }, 1000);
//    }
//}



//
// Changes animation's fps of CollapsiblePanel.
//
//Sys.Application.add_init(initCollapsiblePanelBehavior);
//function initCollapsiblePanelBehavior() {
//    if (window.AjaxControlToolkit && AjaxControlToolkit.CollapsiblePanelBehavior) {
//        var cpb = AjaxControlToolkit.CollapsiblePanelBehavior;
//        cpb.originalInitialize = cpb.prototype.initialize;
//        cpb.prototype.initialize = function() {
//            cpb.originalInitialize.call(this);
//            this._animation._fps = 25;
//        };
//    }
//}
Sys.Application.add_load(initCreatedCollapsiblePanels);
function initCreatedCollapsiblePanels() {
    if (window.AjaxControlToolkit && AjaxControlToolkit.CollapsiblePanelBehavior) {
        var c = Sys.Application.getComponents();
        for (var i = 0; i < c.length; ++i)
            if (c[i] instanceof AjaxControlToolkit.CollapsiblePanelBehavior)
            c[i]._animation._fps = 25;
    }
}






//
// Reports end of script file to ajax.
//
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
