function Dialog(type, title, message, value, button1, button2, button3) {
    this.type = type;
    this.title = title;
    this.message = message;
    this.value = value;
    this.button1 = button1;
    this.button2 = button2;
    this.button3 = button3;
    this.defaultButton = 1;
    this.top = null;
    this.form = {action: "#", method: "post", target: "", enctype: "application/x-www-form-urlencoded"};
    this.setTop = function(top) {
        this.top = top;
    };
    this.getTop = function() {
        return this.top;
    };
    //var buttonIds = new Array(document.uniqueID, document.uniqueID, document.uniqueID);
    var div;
    if (!this.button1 && !this.button2 && !this.button3) {
        if (this.type == Dialog.TYPE_MESSAGE) {
            this.button1 = "OK";
        } else {
            this.button1 = "OK";
            this.button2 = "Отмена";
        }
    }
    this.onDone = null;
    this.result = null;
    this.divId = "DIALOG_" + (++Dialog.LAST_ID);
    this.buildHtml = function() {
        var buttonsHtml = "";
        var buildButtonHtml = function(id, value) {
            return "<input type=\"button\" value=\"" + value + "\" onclick=\"Dialog.CURRENT_DIALOG.result=" + id + ";if(this.form.value)Dialog.CURRENT_DIALOG.value=this.form.value.value;Dialog.CURRENT_DIALOG.close();\"/>";
        };
        if (this.button1) buttonsHtml += buildButtonHtml(1, this.button1);
        if (this.button2) buttonsHtml += " " + buildButtonHtml(2, this.button2);
        if (this.button3) buttonsHtml += " " + buildButtonHtml(3, this.button3);
        var ret = Dialog.TEMPLATE;
        ret = ret.replace(/\$\{action\}/g, this.form.action);
        ret = ret.replace(/\$\{method\}/g, this.form.method);
        ret = ret.replace(/\$\{target\}/g, this.form.target);
        ret = ret.replace(/\$\{enctype\}/g, this.form.enctype);
        ret = ret.replace(/\$\{width\}/g, 200);
        ret = ret.replace(/\$\{title\}/g, this.title);
        ret = ret.replace(/\$\{message\}/g, this.message);
        ret = ret.replace(/\$\{buttons\}/g, buttonsHtml);
        return ret;
    };
    this.show = function() {
        if (Dialog.CURRENT_DIALOG != null) throw new Error("Another dialog already opened!");
        Dialog.CURRENT_DIALOG = this;
        //$lockDocument("");
        div = document.createElement("DIV");
        div.style.position = "absolute";
        div.style.left = 0;
        div.style.top = this.top ? this.top : document.body.scrollTop + 50;
        div.style.width = "100%";
        //div.style.height = "100%";
        div.style.textAlign = "center";
        div.style.verticalAlign = "middle";
        div.innerHTML = this.buildHtml();
        document.body.appendChild(div);
    };
    this.close = function() {
        document.body.removeChild(div);
        div = null;
        //$unlockDocument();
        Dialog.CURRENT_DIALOG = null;
        if (this.onDone) this.onDone();
    };
}

Dialog.TYPE_MESSAGE = 1;
Dialog.TYPE_CUSTOM = 3;

Dialog.LAST_ID = 0;

Dialog.CURRENT_DIALOG = null;

Dialog.LAST_UNIQUE_ID = 1;
Dialog.generateUniqueId = function() {
    return "net_integrio_scripting_ajax_Dialog_UNIQUE_ID" + Dialog.LAST_UNIQUE_ID++;
};
/*
function $createMessageDialog(message, button1, button2, button3) {
    return new Dialog(Dialog.TYPE_MESSAGE, document.title, message, null, button1, button2, button3);
}

function $createConfirmDialog(message, button1, button2) {
    return $createMessageDialog(message, button1 ? button1 : "Да", button2 ? button2 : "Нет");
}

function $createPromptDialog(message, value, button1, button2, button3) {
    return new Dialog(Dialog.TYPE_PROMPT, document.title, message, value, button1, button2, button3);
}

function $createCustomDialog(html, button1, button2, button3) {
    return new Dialog(Dialog.TYPE_CUSTOM, document.title, html, null, button1, button2, button3);
}

function $showMessage(message) {
    $createMessageDialog(message).show();
}
*/
Dialog.TEMPLATE = "<form action=\"${action}\" method=\"${method}\" enctype=\"${enctype}\" target=\"${target}\" onsubmit=\"return false;\">" +
                  "<table class=\"net_integrio_scripting_ajax_Dialog_dialog\" width=\"${width}\" align=\"center\">" +
                  "<tr><th>${title}</th></tr>" +
                  "<tr><td>${message}</td></tr>" +
                  "<tr><td class=\"net_integrio_scripting_ajax_Dialog_buttons\">" +
                  "${buttons}" +
                  "</td></tr>" +
                  "</table>" +
                  "</form>";