Entity = Class.create(ModalEditable, {

    data: {status: 'pj'},
    validFields: [],
    validations: {
        name:    [Validate.Presence],
        cui:     [Validate.Presence],
        reg_com: [Validate.Presence],
        iban:    [Validate.Presence, Validate.IBAN],
        bank:    [Validate.Presence],
        address: [Validate.Presence]
    },

    template: new Template(
       '<dt id="invoice-#{type}-name"><strong>#{name}</strong></dt> \
        <dd id="invoice-#{type}-cui" class="field-pj"><strong>CUI / CIF:</strong> <span>#{cui}</span></dd> \
        <dd id="invoice-#{type}-reg_com" class="field-pj"><strong>Nr. Registrul Comerţului:</strong> <span>#{reg_com}</span></dd> \
        <dd id="invoice-#{type}-cnp" style="display: none;" class="field-pf"><strong>CNP:</strong> <span>#{cnp}</span></dd> \
        <dd id="invoice-#{type}-ci" style="display: none;" class="field-pf"><strong>B.I./C.I.:</strong> <span>#{ci}</span></dd> \
        <dd id="invoice-#{type}-iban"><strong>IBAN:</strong> <span>#{iban}</span></dd> \
        <dd id="invoice-#{type}-bank"><strong>Banca:</strong> <span>#{bank}</span></dd> \
        <dd id="invoice-#{type}-address"><strong>Sediu:</strong> <span>#{address}</span></dd> \
        <dd id="invoice-#{type}-telephone"><strong>Telefon:</strong> <span>#{telephone}</span></dd> \
        <dd id="invoice-#{type}-email"><strong>Email:</strong> <span><a href="mailto:#{email}" title="#{email}">#{email}</a></span></dd> \
        <dd id="invoice-#{type}-url"><strong>Web:</strong> <span><a href="#{url}" title="#{url}">#{url}</a></span></dd>'
    ),

    initialize: function ($super, type) {
        this.type = type;
        this.name = type;

        this.attachDialog();

        $super();
    },

    attachDialog: function () {
        this.dialog = new Dialog({
            handle: '#edit-'+ this.type + '-handle',
            title: $('edit-'+ this.type + '-handle').title,
            target: {
                id: 'edit-' + this.type,
                auto: true
            },

            /**
             * Destroy validation instances
             */
            beforeClose: (function () {
                this.destroyValidations();
            }).bind(this),

            /**
             * Attach validation instances
             */
            afterOpen: (function () {
                this.form  = $('dialog-content').down('#invoice-'+ this.type +'-form');

                this.addValidations();
                this.populateForm();

                if (typeof(this.afterDialogOpen) == 'function') {
                    this.afterDialogOpen();
                }
            }).bind(this)
        });
    },

    update: function ($super, data) {
        this.data.type = this.type;
        $super(data);       

        var container = $('invoice-' + this.type).down('dl.details');
        container.update(this.template.evaluate(data));

        container.select('dd[class*="field-p"]').invoke('hide');
        container.select('dd[class*="field-'+ this.data.status +'"]').invoke('show');

        for (var field in this.data) {
            if (field == 'type') {
                continue;
            }
            if (!this.data[field]) {
                var container = $('invoice-' + this.type + '-' + field);
                if (container) {
                    container.addClassName('empty');
                }                
            }

            $(this.type + '_' + field).value = this.data[field];
        }
    }
});

Supplier = Class.create(Entity, {
    validFields: [],    
    initialize: function ($super) {
        $super('supplier');
    }
});

Customer = Class.create(Entity, {
    validations: {
        name:    [Validate.Presence],
        cnp:     [Validate.Presence, Validate.CNP],
        ci:      [Validate.Presence],
        cui:     [Validate.Presence],
        reg_com: [Validate.Presence],
        iban:    [Validate.IBAN],
        address: [Validate.Presence]
    },
    validFields: [],
    
    initialize: function ($super) {
        $super('customer');
    },

    afterDialogOpen: function () {
        if (this.data.status) {
            this.setStatus(this.data.status);
        }

        this.tabs = this.form.down('ul.tabs').select('a');
        this.tabs.invoke('observe', 'click', (function (event) {
            var element = event.findElement('a');
            this.setStatus(element.rel);
        }).bind(this));
    },

    setStatus: function (status) {
        this.data.status = status;
        var parent = this;

        this.form.select('li[class*="field-p"]').invoke('hide').each(function (item) {
            var field = item.down('input');
            if (field.validation) {
                field.validation.destroy();
            }
        });

        this.form.select('li[class="field-'+ status +'"]').invoke('show').each(function (item) {
            var field = item.down('input');
            parent.addFieldValidations(field);
        });
        
        this.form.down('ul.tabs').select('li').invoke('removeClassName', 'selected').find(function (item) {
            return item.down('a').rel == status;
        }).addClassName('selected');

    }
});