// Event class

function Event(){
    // Event constructor
}

Event.events = [];

Event.prototype = {
    attachEvent : function (sEventType, mEvent) {
        if(!(mEvent instanceof Object))
            return;

        if(!Event.events[sEventType])
            Event.events[sEventType] = [];

        Event.events[sEventType].push(mEvent);
    },

    notify : function (sEventType) {
        if(!Event.events[sEventType])
            return;

        for(var i = 0, cEvent = Event.events[sEventType], eLen = cEvent.length; i < eLen; i++)
        {
            if(cEvent[i] instanceof Function)
            {
                cEvent[i](this);
            }
        }
    }
}

Object.extend = function (source, destination) {
    for (var property in source)
        destination[property] = source[property];
    return destination;
}

// Web page class

function Webpage()
{
    this._startup = [];  // Startup functions
}

Webpage.prototype = Object.extend(Event.prototype, {

    registerStartup : function (Function) {

        this._startup.push(Function);
    },

    runStartup : function () {

        for(var i = 0, eLen = this._startup.length; i < eLen; i++)
        {
            if(this._startup[i] instanceof Function)
                this._startup[i]();
        }
    },

    block : function () {

        var elem = document.getElementById("blocker");

        if(!elem)
            return;

        elem.style.display = "block";
    },

    unblock : function () {

        var elem = document.getElementById("blocker");
        
        if(!elem)
            return;

        elem.style.display = "none";
    }
});

// Form class
function Form(name)
{
    this._pointer = document.forms[name];
}

Form.prototype = {

    getElement : function(name) {

        return this._pointer.elements[name];
    },

    razmer : function() {

        return this._pointer.elements.length;
    },

    submit : function() {

        this._pointer.submit();
    },

    select_all : function(elem) {

        var checked = elem.checked;

        for(var i = 0, len = this._pointer.elements.length; i < len; i++)
        {
            elem = this._pointer.elements[i];

            if(elem.type == "checkbox" && elem.name.search(/^id(\[\]){0,1}$/) != -1)
            {
                elem.checked = checked;
            }
        }
    },

    list_delete : function () {

        if(!this._pointer) return;

        var is_checked = false;

        for(var i = 0, len = this._pointer.elements.length; i < len; i++)
        {
            elem = this._pointer.elements[i];

            if(elem.name.search(/^ids[\[\]]*$/) != -1 && elem.checked)
            {
                is_checked = true;
                break;
            }
        }

        if(!is_checked)
        {
            alert("Не выбраны записи для удаления");
        }
        else if(confirm("Вы уверены, что хотите удалить? "))
        {
            this.submit();
        }
    },

    delete_confirm : function () {

        if(confirm("Вы уверены, что хотите удалить? "))
            document.location.href = "?action=delete&id=" + this._pointer.id.value + (this._pointer.method_id ? "&method_id=" + this._pointer.method_id.value : "");
    }
};


// Dialog class

function Dialog()
{
    this.document = null;
}

Dialog.prototype = Object.extend(Webpage.prototype, {

    open : function(path, width, height, no_control) {

        this.close();
        this.document = window.open(path, "", "width=" + width + ", height=" + height + ", location=no, resizable=yes, alwaysRaised=yes, dependent=yes, scrollbars=yes");

        if(!no_control)
        {
            this.notify("DIALOG_OPEN");
            this.checkForClose();
        }
    },

    checkForClose : function() {

        if(this.document && this.document.closed)
            this.notify("DIALOG_CLOSE");
        else
            setTimeout("dialog.checkForClose()", 500);
    },

    close : function() {

        if(this.document)
        {
            try
            {
                this.document.close();
            }
            catch(err)
            {
                this.document = null;
            }
        }
    },

    getElementById : function(name) {

        return this.document.document.getElementById(name);
    }
});


// Menu class

function Menu()
{
    this._pointer = null;
}

Menu.prototype = Object.extend(Webpage.prototype, {

    display : function (id) {

        var menu = document.getElementById(id);

        if(!menu) return;

        menu.style.display = (menu.style.display == "block" ? "none" : "block");

        if(this._pointer && menu.id != this._pointer.id)
            this._pointer.style.display = "none";

        this._pointer = menu;
    },

    moveTo : function (x, y) {

        this._pointer.style.left = x + "px";
        this._pointer.style.top  = y + "px";
    },

    is_open : function (id) {

        var menu = document.getElementById(id);

        if(!menu || menu.style.display.search(/^(none|)$/) == 0)
            return false;

        return true;
    }
});

function image_preload(imgNames)
{
    var img = new Array();

    for(var i = 0; i < imgNames.length; i++)
    {
        img[i] = new Image();
        img[i].src = imgNames[i];
    }

    return img;
}

var dialog  = new Dialog();
var webpage = new Webpage();

function form_delete(oElem)
{
    while(oElem.nodeName != "FORM")
    {
        if(!oElem)
            return false;

        oElem = oElem.parentNode;
    }

    var query = "";

    if(confirm("Вы уверены, что хотите удалить? "))
    {
        for(var i = 0, len = oElem.elements.length; i < len; i++)
        {
            elem = oElem.elements[i];

            if(elem.name.search(/^id/) != -1
               && (((elem.type == "checkbox" || elem.type == "radio") && elem.checked) || elem.type == "hidden"))
                query += (query != "" ? "&" : "") + elem.name + "=" + elem.value;
        }

        if(query == "")
        {
            alert("Не выбраны записи для удаления");
        }
        else
        {
            document.location.href = (oElem.action != "" ? oElem.action : "") + "?action=delete&" + query;
        }
    }

    return true;
}

function select_all(oCheckbox)
{
    var checked = oCheckbox.checked;

    while(oCheckbox.nodeName != "FORM")
    {
        if(!oCheckbox)
            return false;

        oCheckbox = oCheckbox.parentNode;
    }

    for(var i = 0, len = oCheckbox.elements.length; i < len; i++)
    {
        elem = oCheckbox.elements[i];

        if(elem.type == "checkbox" && elem.name.search(/^id/) != -1)
            elem.checked = checked;
    }
}

function delete_confirm(oLink)
{
    if(confirm("Вы уверены, что хотите удалить?     "))
        document.location.href = oLink.href;

    return false;
}

