﻿var registeredMPEObjects = new Array();
var openedMPEObjects = new Array();
function sure(item) {
    return window.confirm("Are you sure you want to delete the " + item + "?");
}
function findMPE(mpeId, l) {
    for (var i = 0; i < l.length; i++)
        if (l[i].mpeId == mpeId)
        return l[i];
    return null;
}
function findMPEIndex(mpeId, l) {
    for (var i = l.length - 1; i >= 0; i--)
        if (l[i].mpeId == mpeId)
        return i;
    return -1;
}
function findRMPE(mpeId) {
    return findMPE(mpeId, registeredMPEObjects);
}
function findOMPE(mpeId) {
    return findMPE(mpeId, openedMPEObjects);
}
function layoutMPEd(mpeId) {
    //	var mpe = findOMPE(mpeId);
    //	if (mpe && !mpe.layoutInterval)
    //	{
    //		mpe.layoutInterval = window.setInterval("layoutMPE('"+mpeId+"')", 100);
    //	}
}
function layoutMPE(mpeId) {
    var mpe = findRMPE(mpeId);
    if (mpe) {
        //		var rmpe = $find(mpe.behaviorId);
        //var o=rmpe.targetControlId?document.getElementById(rmpe.targetControlId):null;
        //window.status = 'window.screen.availHeight:'+window.screen.availHeight+';o.clientHeight='+o.clientHeight;
        //if (!o || window.screen.availHeight-100<o.clientHeight)
        //{
        $find(mpe.behaviorId)._layout();
        //}
    }
}
function registerMPE(mpeId, behaviorId, showButtonId, hideButtonId, itemHiddenId, showMPE, onShowScript, onHideScript, targetControl, encodedKey, modalPopupUrl, modalPopupWidth, modalPopupHeight) {
    if (findRMPE(mpeId))
        window.alert("The modal popup extender '" + mpeId + "' is already registered!");
    var m = new Object();
    m.mpeId = mpeId
    m.behaviorId = behaviorId;
    m.onShowScript = onShowScript;
    m.onHideScript = onHideScript;
    m.showButtonId = showButtonId;
    m.hideButtonId = hideButtonId;
    m.itemHiddenId = itemHiddenId;
    m.showMPE = showMPE;
    m.targetControl = targetControl;
    m.encodedKey = encodedKey;
    m.modalPopupUrl = modalPopupUrl;
    m.modalPopupWidth = modalPopupWidth; ;
    m.modalPopupHeight = modalPopupHeight;
    registeredMPEObjects[registeredMPEObjects.length] = m;
}


function addLoadEvent(func) {
    if (window.addEventListener)
        window.addEventListener('load', func, false);
    else if (window.attachEvent)
        window.attachEvent('onload', func);
}
function initUpdatePanelEndRequest() {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function(sender, e) {
        if (openedMPEObjects.length > 0) {
            var mpe = openedMPEObjects[openedMPEObjects.length - 1];
            if (mpe) {
                window.setTimeout("preparePngs();", 50);

                var m = $find(mpe.behaviorId);
                m.disableTab();
                m._layout();
                var o = document.getElementById($find(mpe.behaviorId)._PopupControlID);
                if (o) {
                    if (o.offsetTop < 0)
                        if (o.runtimeStyle)
                        o.runtimeStyle.top = "10px";
                    else o.style.top = "10px";
                }
            }
        }
    });
}
addLoadEvent(initUpdatePanelEndRequest);

function setVisibilityForMPE(mpeId, dataKeys, visible) {
    try {

        //window.alert(q.offsetWidth+"/"+q.clientWidth+"/"+q.scrollWidth);
        var mpe = findRMPE(mpeId);

        if (mpe) {
            if (mpe.showMPE) {
                //DisableScroll=visible;
                var index = findMPEIndex(mpeId, openedMPEObjects);
                if (visible) {
                    if (index == -1) {
                        if (document.all && mpe.modalPopupUrl) {
                            var left = (window.screen.availWidth - mpe.modalPopupWidth) / 2;
                            var top = (window.screen.availHeight - mpe.modalPopupHeight) / 2;
                            window.open(mpe.modalPopupUrl + "DataKey=" + encodeURIComponent(dataKeys) + "&key=" + encodeURIComponent(mpe.encodedKey),
						"", "left=" + left + "; top=" + top + "; width=" + mpe.modalPopupWidth + "; height=" + mpe.modalPopupHeight + ";titlebar=no; resizable=yes; scrollbars=yes; toolbar=no; menubar=no; location=no; center=yes;");
                        }
                        else {
                            if (mpe.onShowScript)
                                window.eval(mpe.onShowScript);
                            openedMPEObjects[openedMPEObjects.length] = mpe;

                            var popupIndex = openedMPEObjects.length;
                            var mpeObject = $find(mpe.behaviorId);
                            mpeObject._backgroundElement.style.zIndex = 10000 + popupIndex;
                            mpeObject._foregroundElement.style.zIndex = 10000 + popupIndex + 1;
                            mpeObject.show();
                            layoutMPEd(mpeId);
                        }
                    }
                    else {
                        window.alert("The modal popup extender '" + mpeId + "' is alerady opened!");
                        return false;
                    }
                }
                else {

                    if (index != -1) {
                        if (mpe.onHideScript)
                            window.eval(mpe.onHideScript);
                        var last = true;
                        if (index != -1 && index != openedMPEObjects.length - 1)
                            last = false;
                        openedMPEObjects.splice(index, 1); //openedMPEObjects.length-index);
                        window.clearInterval(mpe.layoutInterval);
                        mpe.layoutInterval = 0;
                        var aeId = mpe.behaviorId + "_AE";
                        var ae = $find(aeId);
                        if (ae) {
                            ae._hiddenHandler();
                            //ae._shown._animation.play();
                            window.setTimeout("$find('" + mpe.behaviorId + "').hide()", 500);
                        }
                        else $find(mpe.behaviorId).hide();
                        //window.alert(mpe.hideButtonId);
                        //window.setTimeout("window.alert(document.getElementById('"+mpe.hideButtonId+"').style.display);",1000);
                        if (!last) {
                            //window.setTimeout("hideMPE(openedMPEObjects["+index+"].mpeId);",100);
                        }
                    }
                    else {
                        window.alert("The modal popup extender '" + mpeId + "' is not opened!");
                        return false;
                    }
                }
            }
            else {
                var o = mpe.targetControl ? document.getElementById(mpe.targetControl) : null;

                if (visible) {
                    if (mpe.onShowScript)
                        window.eval(mpe.onShowScript);

                    if (o) {
                        o.style.display = 'block';
                        o.style.visibility = 'visible';
                    }
                    if (openedMPEObjects.length > 0 && openedMPEObjects[openedMPEObjects.length - 1].mpeId != mpeId)
                        openedMPEObjects[openedMPEObjects.length] = mpe;
                }
                else {
                    if (mpe.onHideScript) {
                        window.eval(mpe.onHideScript);
                    }
                    if (o) {
                        o.style.display = 'none1';
                        o.style.visibility = 'hidden';
                    }
                    var index = findMPEIndex(mpeId, openedMPEObjects);
                    var last = true;
                    if (index != -1 && index != openedMPEObjects.length - 1)
                        last = false;
                    if (index != -1)
                        openedMPEObjects.splice(index, 1); //openedMPEObjects.length-index);
                    if (!last) {
                        window.setTimeout("hideMPE(openedMPEObjects[" + index + "].mpeId);", 10);
                    }
                }
            }
            if (dataKeys) {
                if (document.getElementById(mpe.itemHiddenId))
                    document.getElementById(mpe.itemHiddenId).value = dataKeys;
            }
            //		if (visible&&document.getElementById(mpe.hideButtonId))
            //		{
            //			document.getElementById(mpe.hideButtonId).disabled='';
            //			document.getElementById(mpe.hideButtonId).style.display='';
            //		}
            //		window.alert("__doPostBack('m$FC$l$btnShowForm','') || __doPostBack('"+document.getElementById(mpe.showButtonId).name+"','')");
            //window.alert(document.getElementById(mpe.hideButtonId).style.display);
            if (document.getElementById(mpe.showButtonId) && visible || document.getElementById(mpe.hideButtonId) && !visible)
                __doPostBack(visible ? document.getElementById(mpe.showButtonId).name : document.getElementById(mpe.hideButtonId).name, '');
            //layoutMPE(mpeId);
        }
        else window.alert("The modal popup extender '" + mpeId + "' was not registered!");
        return false;
    }
    catch (ex) {
        window.alert("Utility.js: Exception has been thrown: \n" + ex.message);
        throw ex;
    }
}

function showMPE(mpeId, dataKeys) {
    setVisibilityForMPE(mpeId, dataKeys, true);
    return false;
}
function hideMPE(mpeId, dataKeys) {
    setVisibilityForMPE(mpeId, dataKeys, false);
    return false;
}


function closeMPE(value) {
    if (closePopupTimeOut != 0) {
        window.clearTimeout(closePopupTimeOut);
    }
    if (openedMPEObjects.length > 0) {

        if (value && typeof value != undefined)
            value = value.toString().replace("\'", "''");
        else value = '';

        //close the latest popup
        closePopupTimeOut = window.setTimeout("closePopupTimeOut=0;hideMPE(openedMPEObjects[openedMPEObjects.length-1].mpeId, '" + value + "');", 200);
    }
    return false;
}

function closeModalDDL(value, ddlId, separator) {
    var ddl = $('#' + ddlId);
    var length = ddl.find('option[@value$="' + value + '"]').length;
    if (length > 0) {
        ddl[0].value = value;
        if (typeof separator == 'undefined')
            separator = ',';
        value += separator + length;
    }
    return closeMPE(value);
}

function closeAllOpenedMPEs() {
    if (closePopupTimeOut != 0) {
        window.clearTimeout(closePopupTimeOut);
    }

    for (i = openedMPEObjects.length - 1; i >= 0; i = i - 1) {
        closePopupTimeOut = window.setTimeout("closePopupTimeOut=0;hideMPE(openedMPEObjects[" + i + "].mpeId);", 100);
    }
}

var closePopupTimeOut = 0;
function OnWindowLoad() {
    document.onkeydown = function(e) {
        if (!e && window.event)
            e = window.event;
        if (e.which == 27 || e.keyCode == 27) {
            closeMPE();
        }
    }
}
//bind key press event when loading
if (window.addEventListener)
    window.addEventListener('load', OnWindowLoad, false);
else if (window.attachEvent)
    window.attachEvent('onload', OnWindowLoad);



if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();