var mrOK = 1;
var mrCancel = 2;
var mrYes = 3;
var mrNo = 4;
var mrCustom = 100;


var __dialogsizes = null;
var __activedialog = null;
function Dialog(dlgid) {
    var frame = null;
    var background = null;
    var form = null;
    var formenvelope = null;
    var callback = null;
    var _visible = false;
    var scrollpos = null;

    this.Init = function(iframeid, layerid) {
        var el = document.getElementById(iframeid);
        if (el == 'undefined') el = null;
        this.frame = el;
        el = document.getElementById(layerid + '_background');
        if (el == 'undefined') el = null;
        this.background = el;
        el = document.getElementById(layerid + '_area');
        if (el == 'undefined') el = null;
        this.form = el;
        el = document.getElementById(layerid + '_area_format');
        if (el == 'undefined') el = null;
        this.formenvelope = el;
    }

    this.Show = function(url, callbackfcn, width, height) {
        if (this.frame != null) {
            this._showdlg(width, height);
            this.frame.src = url;
            this.callback = callbackfcn;
            window.onscroll = dialoglockScrolling;
            window.onresize = dialogsetScrollingInfo;
            __activedialog = this;
        }
    }

    this.lockScrolling = function() {
        //window.setTimeout("window.scroll(" + __dialogsizes[2] + "," + __dialogsizes[3] + ");", 10);
        this._showdlg(__dialogsizes[2][6], __dialogsizes[2][7]);
    }

    this.setScrollingInfo = function() {
        this._showdlg(__dialogsizes[2][6], __dialogsizes[2][7]);
    }

    this.Close = function(data) {
        if (this._visible) {
            this._hidedlg();
            window.onscroll = null;
            __activedialog = null;
        }
    }

    this.Submit = function(data) {
        if (this._visible) {
            if (typeof (this.callback) == 'function') {
                this.callback(data);
            }
            this.Close();
        }
    }

    this._showdlg = function(width, height) {
        if ((this.frame != null) && (this.form != null)) {
            if (width > -1) this.frame.width = width;
            if (height > -1) this.frame.height = height;
            __dialogsizes = this._getSize();
            if (this.background != null) {
                this.background.style.width = __dialogsizes[0];
                this.background.style.height = __dialogsizes[1];
                this.background.style.left = __dialogsizes[2] + 'px';
                this.background.style.top = __dialogsizes[3] + 'px';
                this.background.style.display = 'block';
            }
            this.form.style.width = __dialogsizes[0];
            this.form.style.height = __dialogsizes[1];
            this.form.style.top = __dialogsizes[3]+'px';
            this.form.style.left = __dialogsizes[2] + 'px';

            this.form.style.display = 'block';
            if (this.formenvelope != null) {
                this.formenvelope.style.width = __dialogsizes[0];
                this.formenvelope.style.height = __dialogsizes[1];
            }

            this._visible = true;
        }
    }

    this._hidedlg = function() {
        if (this._visible) {
            this.background.style.display = 'none';
            this.form.style.display = 'none';
        }
    }

    this._getSize = function(sizeX, sizeY) {
        var myWidth = 0, myHeight = 0; var myYpos = 0; var myXpos; var docHeight; var docWidth;
        if (typeof (window.innerWidth) == 'number') {
            //Non-IE
            myWidth = window.innerWidth;
            myHeight = window.innerHeight;
            myYpos = document.documentElement.scrollTop;
        } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
            //IE 6+ in 'standards compliant mode'
            myWidth = document.documentElement.clientWidth;
            myHeight = document.documentElement.clientHeight;
            myYPos = document.documentElement.scrollTop;
        } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
            //IE 4 compatible
            myWidth = document.body.clientWidth;
            myHeight = document.body.clientHeight;
            myYPos = document.body.scrollTop;
        };
        myYpos = window.pageYOffset ? window.pageYOffset : 0;
        if (myYpos == 0) myYpos = document.documentElement ? document.documentElement.scrollTop : 0;
        if (myYpos == 0) myYpos = document.body ? document.body.scrollTop : 0;
        myXpos = window.pageXOffset ? window.pageXOffset : 0;
        if (myXpos == 0) myXpos = document.documentElement ? document.documentElement.scrollLeft : 0;
        if (myXpos == 0) myXpos = document.body ? document.body.scrollLeft : 0;
        docHeight = document.body.offsetHeight;
        docWidth = document.body.offsetWidth;

        return new Array(myWidth, myHeight, myXpos, myYpos, docWidth, docHeight, sizeX, sizeY);
    }

    this.Init(dlgid + "_iframe", dlgid);
}

function dialoglockScrolling() {
    if (__activedialog != null)
        __activedialog.lockScrolling();
}
function dialogsetScrollingInfo() {
    if (__activedialog != null)
        __activedialog.setScrollingInfo();
}

function DialogReturnValue(modalResult, value) {
    window.returnValue = value;
    if ((window.parent != null) && (window.parent.dialog != null))
        window.parent.dialog.Submit({ result: modalResult, value: value });
}