﻿function getDialogFeatures(dialogWidth, dialogHeight, resizable)
{
    w = document.documentElement.clientWidth;
    h = document.documentElement.clientHeight;
    
    leftPos = window.screenLeft + (w - dialogWidth) / 2
    //alert("screentop: " + window.screenTop + " h: " + h + " dialogHeight: " + dialogHeight);
    topPos = window.screenTop + (h - dialogHeight) / 2;
    //alert("topPos: " + topPos);
    //alert(w + " x " + h);
    
    return "dialogLeft: " + leftPos + "px;dialogTop: " + topPos + "px;dialogWidth: " + dialogWidth + "px;dialogHeight: " + dialogHeight + "px;status: no;unadorned: yes;scroll: no;help: no;" + (resizable ? "resizable: yes;" : "");
}

function getWindowFeatures(windowWidth, windowHeight, resizable, scrollbars)
{
    if (document.all) 
    {
        w = document.documentElement.clientWidth;
        h = document.documentElement.clientHeight;
        leftPos = window.screenLeft + (w-windowWidth)/2
        topPos = window.screenTop + (h-windowHeight)/2;
    }
    else 
    {
       w = window.innerWidth;
       h = window.innerHeight;
       leftPos = window.screenX + (w-windowWidth)/2;
       topPos = window.screenY + (h-windowHeight)/2;
    }
    var resizableVal;
    if (resizable)
    {
        resizableVal = ",resizable=1";
    }
    else
    {
        resizableVal = ",resizable=0";
    }
    var scrollbarsVal;
    if (scrollbars)
    {
        scrollbarsVal = ",scrollbars=1";
    }
    else
    {
        scrollbarsVal = ",scrollbars=0";
    }
    return "top=" + topPos + ",left=" + leftPos + ",width=" + windowWidth + ",height=" + windowHeight + resizableVal + scrollbarsVal + ",toolbar=0,status=0,menubar=0,location=0";
}

var fileDialog = '';
function showDialog(url, width, height, resizable, scrollbars, modal, popupControlId, postToWindow)
{
    if (window.showModalDialog && modal)
    {
        var value = window.showModalDialog(url, '', getDialogFeatures(width, height, resizable));
        if (value)
        {
            __doPostBack(popupControlId, 'PopupClosed|' + value);
        }
    }
    else
    {
        if (!fileDialog.closed && fileDialog.location) 
        {
	        fileDialog.location.href = url;
        }
        else 
        {
            if (!postToWindow)
            {
                fileDialog = window.open(url, '', getWindowFeatures(width, height, resizable, scrollbars), true);
            }
            else
            {
                fileDialog = window.open('', 'popupWindow', getWindowFeatures(width, height, resizable, scrollbars), true);
                theForm.target = 'popupWindow';
                __doPostBack(popupControlId, 'Opening');
                theForm.target = '';
            }
        }
        if (window.focus)
        {
            fileDialog.focus();
        }
    }
}