/* because of perfect code portability! */
document.forms.EditView = document.forms.DetailView;

/* Pk */
addEvent(
    window,
    'load',
    function () {
        //initialize table
        N = new MyTable('pkTable');

        N.onRefreshRowIndex = function(row) {
            var data = new Object();
            data['index'] = (row.index+1).toString();
            row.cells.item(0).setData(data);
        }

        N.onCreateRow = function(row) {
            row.newPos = false;

            row.ondblclick = function() {
                this.newPos = !this.newPos;
                var img = this.cells.item(1).getElementsByTagName('img');

                if(!this.newPos)
                    img[0].src = "modules/EcmServices/images/edit.gif";
                else
                    img[0].src = "modules/EcmServices/images/editset.gif";

                for(var i=0; i<this.myTable.colCount(); i++)
                    this.cells.item(i).change(!this.newPos);
            }

            row.onSelect = function() {
                for(var i=0; i<this.myTable.colCount(); i++) {
                    this.cells.item(i).style.height = OPT['row_item_height_selected'];
                    this.cells.item(i).change(!this.newPos);
                }
            }

            row.onDeselect = function() {
                for(var i=0; i<this.myTable.colCount(); i++) {
                    this.cells.item(i).style.height = OPT['row_item_height'];
                    this.cells.item(i).change(false);
                }
            }

            row.calculateTotal = function() {
                var data = new Object();
                this.cells.item(3).getData(data,true);
                this.cells.item(4).getData(data,true);
                this.cells.item(5).getData(data,true);

                this.cells.item(6).getData(data,true);
                this.cells.item(7).getData(data,true);
                this.cells.item(8).getData(data,true);

                if(data.quantity && data.price)
                    data.total = data.quantity*data.price+data.quantity*data.price*data.discount/100;
                else
                    data.total = 0;

                this.cells.item(9).setData(data);

                calculateTotal();
            }
        }

        N.onCreateCell = function(cell) {
            var i = cell.index;

            cell.change = function(select) {
            //
            };

            cell.style.height = OPT['row_item_height'];
            cell.noSelect = true;

            // Index.
            if(i == 0) {
                cell.setData = function(data) {
                    if(data.index)
                        cell.firstChild.value = data.index;
                };

                cell.getData = function(data) {
                    data.index = cell.firstChild.value;
                }

                cell.select = function() {
                    this.selectNext();
                }

                var edit = document.createElement('input');
                edit.setAttribute('type','text');
                edit.setAttribute('readOnly','readonly');
                edit.setAttribute('tabIndex',1);
                edit.className = 'inputs';

                cell.appendChild(edit);
            }

            // Code.
            if(i == 1) {
                cell.getData = function(data) {
                    var cn = this.getElementsByTagName('input');

                    data.code = cn[0].value;
                    data.id = cn[1].value;
                    data.unit_id = cn[2].value;
                    data.vat_id = cn[3].value;
                    data.category_id = cn[4].value;
                }

                cell.setData = function(data) {
                    var cn = this.getElementsByTagName('input');

                    if(data.code)
                        cn[0].value = data.code;

                    if(data.id)
                        cn[1].value = data.id;

                    if(data.unit_id)
                        cn[2].value = data.unit_id;

                    if(data.vat_id)
                        cn[3].value = data.vat_id;

                    if(data.category_id)
                        cn[4].value = data.category_id;
                }

                var edit = '<input type="text" readonly="readonly" tabindex="1" class="inputs" autocomplete="off" >';
                cell.innerHTML = edit;
                cell.align = 'right';

                var id = document.createElement('input');
                id.setAttribute('type','hidden');
                cell.appendChild(id);

                var unit_id = document.createElement('input');
                unit_id.setAttribute('type','hidden');
                unit_id.setAttribute('value',OPT['default_unit']);
                cell.appendChild(unit_id);

                var vat_id = document.createElement('input');
                vat_id.setAttribute('type','hidden');
                vat_id.setAttribute('value',OPT['default_vat']);
                cell.appendChild(vat_id);

                var category_id = document.createElement('input');
                category_id.setAttribute('type','hidden');
                category_id.setAttribute('value',OPT['default_category']);

                cell.appendChild(category_id);
            }

            // Name.
            if(i == 2) {
                cell.getData = function(data) {
                    var cn = this.getElementsByTagName('textarea');
                    data.name = cn[0].value;
                }

                cell.setData = function(data) {
                    var cn = this.getElementsByTagName('textarea');

                    if(data.name)
                        cn[0].value = data.name;
                }

                var textarea = '<textarea tabindex="1" readonly="readonly" style="height:100%"; class="inputs"></textarea>';

                cell.innerHTML = textarea;
            }

            // Quantity.
            if(i == 3) {
                cell.getData = function(data,noAlert) {
                    var tmp = UserFormatNumberToNumber(this.firstChild.value);

                    if(tmp == -1) {
                        ERROR = true;
                        data.quantity = 0;
                        this.firstChild.style.color = 'red';
                    } else {
                        data.quantity = tmp;
                        this.firstChild.style.color = 'black';
                    }
                }

                cell.setData = function(data) {
                    if(data.quantity)
                        this.firstChild.value = NumberToUserFormatNumber(data.quantity);
                    else
                        this.firstChild.value = NumberToUserFormatNumber(0);
                }

                var edit = '<input type="text" readonly="readonly" tabindex="1" style="text-align:right;" autocomplete="off" class="inputs" value="'+NumberToUserFormatNumber(1)+'">';
                cell.innerHTML = edit;
            }

            // Rezerwacja.
            if(i == 4) {
                cell.getData = function(data,noAlert) {
                    var tmp = UserFormatNumberToNumber(this.firstChild.value);

                    if(tmp == -1) {
                        ERROR = true;
                        data.rq = 0;
                        this.firstChild.style.color = 'red';
                    } else {
                        data.rq = tmp;
                        this.firstChild.style.color = 'black';
                    }
                }

                cell.setData = function(data) {
                    if(data.rq)
                        this.firstChild.value = NumberToUserFormatNumber(data.rq);
                    else
                        this.firstChild.value = NumberToUserFormatNumber(0);
                }

                var edit = '<input type="text" readonly="readonly" tabindex="1" style="text-align:right;" autocomplete="off" class="inputs" value="'+NumberToUserFormatNumber(0)+'">';
                cell.innerHTML = edit;
            }

            // Price.
            if(i == 5) {
                cell.getData = function(data,noAlert) {
                    var tmp = UserFormatNumberToNumber(this.firstChild.value);

                    if(tmp == -1) {
                        ERROR = true;

                        if(!noAlert)
                            alert(MOD['LBL_FORMAT_NUMBER_ERROR']+' ('+this.firstChild.value+')');

                        data.price = 0;
                        this.firstChild.style.color = 'red';
                    } else {
                        data.price = tmp;
                        this.firstChild.style.color = 'black';
                    }
                }

                cell.setData = function(data) {
                    if(data.price)
                        this.firstChild.value = NumberToUserFormatNumber(data.price);
                    else
                        this.firstChild.value = NumberToUserFormatNumber(0);
                }

                var edit = '<input type="text" readonly="readonly" tabindex="1" class="inputs" style="text-align:right;" autocomplete="off" value="'+NumberToUserFormatNumber(0)+'">';

                cell.innerHTML = edit;
            }

            // Discount
            if(i == 6) {
                cell.getData = function(data,noAlert) {
                    var tmp = UserFormatNumberToNumber(this.firstChild.value,'%');

                    if(tmp == -1) {
                        ERROR = true;

                        if(!noAlert)
                            alert(MOD['LBL_FORMAT_NUMBER_ERROR']+' ('+this.firstChild.value+')');

                        data.discount = 0;
                        this.firstChild.style.color = 'red';
                    } else {
                        data.discount = tmp;
                        this.firstChild.style.color = 'black';
                    }
                }

                cell.setData = function(data) {
                    if(data.discount)
                        this.firstChild.value = NumberToUserFormatNumber(data.discount,'%');
                    else
                        this.firstChild.value = NumberToUserFormatNumber(0,'%');
                }

                cell.selectNext = function() {
                    var row = this.parentNode.selectNext();
                    row.select();
                    row.cells.item(0).select();
                };

                var edit = '<input type="text" readonly="readonly" tabindex="1" class="inputs" style="text-align:right;" autocomplete="off" value="'+NumberToUserFormatNumber(0,'%')+'">';

                cell.innerHTML = edit;
            }

            // Price2.
            if(i == 7) {
                cell.getData = function(data,noAlert) {
                    var tmp = UserFormatNumberToNumber(this.firstChild.value);

                    if(tmp == -1) {
                        ERROR = true;

                        if(!noAlert)
                            alert(MOD['LBL_FORMAT_NUMBER_ERROR']+' ('+this.firstChild.value+')');

                        data.price2 = 0;
                        this.firstChild.style.color = 'red';
                    } else {
                        data.price2 = tmp;
                        this.firstChild.style.color = 'black';
                    }
                }

                cell.setData = function(data) {
                    if(data.price2)
                        this.firstChild.value = NumberToUserFormatNumber(data.price2);
                    else
                        this.firstChild.value = NumberToUserFormatNumber(0);
                }

                var edit = '<input type="text" readonly="readonly" tabindex="1" class="inputs" style="text-align:right;" autocomplete="off" value="'+NumberToUserFormatNumber(0)+'">';

                cell.innerHTML = edit;
            }

            // Vat.
            if(i == 8) {
                cell.getData = function(data,noAlert) {
                    var cn = this.getElementsByTagName('input');
                    data.vat_value = cn[0].value;
                    data.vat_id = cn[1].value;

                    if(VAT[cn[1].value])
                        data.vat_name = VAT[cn[1].value].name;
                }

                cell.setData = function(data) {
                    if(data.vat_value)
                        this.getElementsByTagName('input')[0].value = data.vat_value;

                    if(data.vat_id)
                        this.getElementsByTagName('input')[1].value = data.vat_id;

                    if(data.vat_name)
                        this.getElementsByTagName('input')[2].value = data.vat_name;
                }

                cell.onDeselect = function() {
                    ERROR = false;
                    var data = new Object();
                    this.getData(data);

                    if(!ERROR) {
                        data.vat_id = data.vat_id;
                        this.setData(data);
                    }
                }

                var inp=document.createElement('input');
                inp.setAttribute('type','hidden');
                cell.appendChild(inp);

                var inp=document.createElement('input');
                inp.setAttribute('type','hidden');
                cell.appendChild(inp);

                var inp;
                inp = document.createElement('input');
                inp.setAttribute('type','text');
                inp.className = 'inputs';
                inp.style.textAlign = "right";
                inp.cell = cell;

                cell.appendChild(inp);
            }

            // Unit.
            if(i == 9) {
                cell.getData = function(data,noAlert) {
                    data.unit_name = this.firstChild.value;
                }

                cell.setData = function(data) {
                    this.firstChild.value = data.unit_name;
                }

                var edit = '<input type="text" readonly="readonly" tabindex="1" class="inputs" style="text-align:right;" autocomplete="off" value="">';

                cell.innerHTML = edit;
            }

            // Total
            if(i == 10) {
                //cell.select = function() {  };

                cell.getData = function(data) {
                    var cn = this.getElementsByTagName('input');
                    data.total = UserFormatNumberToNumber(this.firstChild.value);
                }

                cell.setData = function(data) {
                    var cn = this.getElementsByTagName('input');

                    if(data.total)
                        this.firstChild.value = NumberToUserFormatNumber(data.total);
                    else
                        this.firstChild.value = NumberToUserFormatNumber(0);
                }

                var edit = document.createElement('input');
                edit.setAttribute('type','text');
                edit.setAttribute('value',NumberToUserFormatNumber(0));
                edit.setAttribute('readOnly','readonly');
                edit.className = 'inputs';
                edit.style.textAlign = "right";

                cell.appendChild(edit);
            }

            // Stock.
            if(i == 11) {
                cell.getData = function(data,noAlert) {
                    var tmp = UserFormatNumberToNumber(this.firstChild.value);

                    if(tmp == -1) {
                        ERROR = true;
                        data.stock = 0;
                        this.firstChild.style.color = 'red';
                    }
                    else {
                        data.stock = tmp;
                        this.firstChild.style.color = 'black';
                    }
                }

                cell.setData = function(data) {
                    var dat = cell.parentNode.getData();

                    if (dat.type=='service') {
                        this.firstChild.value='usługa';
                        return;
                    }

                    if(data.stock)
                        this.firstChild.value = NumberToUserFormatNumber(data.stock);
                    else
                        this.firstChild.value = NumberToUserFormatNumber(0);
                }

                var stock;

                stock = document.createElement('input');
                stock.setAttribute('type', 'text');
                stock.setAttribute('readOnly', 'readonly');
                stock.className = 'inputs';
                stock.style.textAlign = "right";
                stock.cell = cell;

                cell.appendChild(stock);
            }
        }

        N.onSetCellData = function(row,cell,data) {
            if(cell.innerHTML == '')
                cell.innerHTML = '&nbsp;';
        }

        var pl = document.getElementById('pk_list').value;

        if(pl && pl != '') {
            try {
                pl = eval(pl);
                for(x in pl) {
                    if(typeof(pl[x].code) != "undefined") {
                        var pl_row = pl[x];
                        N.addRow().setData(pl_row);
                    }
                }
            } catch(err) {
                pl = null;
            };
        }

        //if(N.rowCount() == 0)
        //    N.addRow();

        calculateTotal = function() {
            var vats = new Object();
            var subtotal = 0;
            var total = 0;
            //var cbm_total = 0;

            for(var i=0; i<N.rowCount(); i++) {
                var data = N.row(i).getData();

                if(OPT.to_is_vat_free == 0) {
                    if(data.vat_id && data.name != '') {
                        if(typeof(vats[data.vat_id]) != "object") {
                            vats[data.vat_id] = new Object();
                            vats[data.vat_id]['vat'] = 0;
                        }
                        vats[data.vat_id]['vat'] += data.total;
                    }
                }

                subtotal += data.total;
            //cbm_total += data.cbm;
            }

            total = subtotal;

            var rt = document.getElementById('position_result_table');

            for(var i=1; i<rt.rows.length-1; i++) {
                if(!vats[rt.rows.item(i).id]) {
                    rt.deleteRow(i);
                    --i;
                } else {
                    vats[rt.rows.item(i).id]['node'] = rt.rows.item(i);
                }
            }

            for(var x in vats) {
                if(VAT[x]){
                    vats[x]['vat'] = vats[x]['vat']*(parseFloat(VAT[x].value)/100);
                    total += vats[x]['vat'];

                    var txL = MOD['LBL_VAT']+' ('+VAT[x].name+')';
                    var txF = NumberToUserFormatNumber(vats[x]['vat']);
                }

                if(vats[x]['node']) {
                    vats[x]['node'].id = x;
                    vats[x]['node'].cells.item(0).innerHTML = txL;
                    vats[x]['node'].cells.item(1).innerHTML = "<input type='text' readonly='readonly' style='border:0px;font-weight:900;width:100%;text-align:right;' value='"+txF+"'>";
                } else {
                    rt.insertRow(1);
                    rt.rows.item(1).id = x;
                    rt.rows.item(1).insertCell(0);
                    rt.rows.item(1).cells.item(0).className = 'positionsLabel';
                    rt.rows.item(1).cells.item(0).innerHTML = txL;
                    rt.rows.item(1).insertCell(1);
                    rt.rows.item(1).cells.item(1).className = 'positionsField';
                    rt.rows.item(1).cells.item(1).innerHTML = "<input type='text' readonly='readonly' style='border:0px;font-weight:900;width:100%;text-align:right;' value='"+txF+"'>";
                }
            }

            var total2 = total;
            var discount;

            if((discount = CheckDiscount(true)) == -1)
                discount = 0;

            total2 = ((OPT['type'] == "correct")?'-':'')+NumberToUserFormatNumber(total2).toString();
            total = ((OPT['type'] == "correct")?'-':'')+NumberToUserFormatNumber(total).toString();

            document.getElementById('articles_subtotal').value = NumberToUserFormatNumber(subtotal);

            if(document.getElementById('articles_total'))
                document.getElementById('articles_total').value = total2;
        }

        calculateTotal();

        function HideLoadingView() {
            var slv = document.getElementById('ShowLoadingView');

            if(slv)
                slv.style.display = 'none';
        }

        ItemListSave = function(json) {
            var data = '';

            for(var i=0; i < N.rowCount(); i++) {
                data = data + '&p_' + i.toString() + '=' + JSON.stringifyNoSecurity(N.row(i).getData());
            }

            return data;
        }

        ItemsList = function(json) {
            var data = '';

            for(var i=0; i < N.rowCount(); i++) {
                data = data + '||||' + JSON.stringifyNoSecurity(N.row(i).getData());
            }

            return data;
        }

        /*
        setInterval(function() {
              doRequest('index.php',"module=EcmServices&action=subpanels&to_pdf=1&record="+document.forms.DetailView.record.value,
                function(result) {
                  if(result != '' && document.getElementById('subpanels_div')) document.getElementById('subpanels_div').innerHTML = result;
                }
              );
          },
          10000
        );
        */

        //quick view
        var main = document.getElementById('main');

        if(main) {
            var h2 = main.getElementsByTagName('h2')[0];

            if(h2) {
                h2.style.display = 'inline';
                var quickInfoH2 = document.getElementById('quickInfoH2');

                if(quickInfoH2) {
                    h2.parentNode.appendChild(quickInfoH2);
                    quickInfoH2.style.display = '';
                }
            }
        }

        if(OPT['setTab'] && OPT['setTab'] != '') {
            if(OPT['setTab'] == 'EMAIL')
                setEMAIL();
        }
    }
    );
/* /Pk */

/* Registry */
addEvent(
    window,
    'load',
    function () {
        //initialize table
        N2 = new MyTable('registryTable');

        N2.onRefreshRowIndex = function(row) {
            var data = new Object();
            data['index'] = (row.index+1).toString();
            row.cells.item(0).setData(data);
        }

        N2.onCreateRow = function(row) {
            row.newPos = false;

            row.ondblclick = function() {
                this.newPos = !this.newPos;
                var img = this.cells.item(1).getElementsByTagName('img');

                if(!this.newPos)
                    img[0].src = "modules/EcmServices/images/edit.gif";
                else
                    img[0].src = "modules/EcmServices/images/editset.gif";

                for(var i=0; i<this.myTable.colCount(); i++)
                    this.cells.item(i).change(!this.newPos);
            }

            row.onSelect = function() {
                for(var i=0; i<this.myTable.colCount(); i++) {
                    this.cells.item(i).style.height = OPT['row_item_height_selected'];
                    this.cells.item(i).change(!this.newPos);
                }
            }

            row.onDeselect = function() {
                for(var i=0; i<this.myTable.colCount(); i++) {
                    this.cells.item(i).style.height = OPT['row_item_height'];
                    this.cells.item(i).change(false);
                }
            }

            row.calculateTotal = function() {
                var data = new Object();
                this.cells.item(3).getData(data,true);
                this.cells.item(4).getData(data,true);
                this.cells.item(5).getData(data,true);

                this.cells.item(6).getData(data,true);
                this.cells.item(7).getData(data,true);
                this.cells.item(8).getData(data,true);

                if(data.quantity && data.price)
                    data.total = data.quantity*data.price+data.quantity*data.price*data.discount/100;
                else
                    data.total = 0;

                this.cells.item(9).setData(data);

                calculateTotal();
            }
        }

        N2.onCreateCell = function(cell) {
            var i = cell.index;

            cell.change = function(select) {
            //
            };

            cell.style.height = OPT['row_item_height'];
            cell.noSelect = true;

            // Index.
            if(i == 0) {
                cell.setData = function(data) {
                    if(data.index)
                        cell.firstChild.value = data.index;
                };

                cell.getData = function(data) {
                    data.index = cell.firstChild.value;
                }

                cell.select = function() {
                    this.selectNext();
                }

                var edit = document.createElement('input');
                edit.setAttribute('type','text');
                edit.setAttribute('readOnly','readonly');
                edit.setAttribute('tabIndex',1);
                edit.className = 'inputs';

                cell.appendChild(edit);
            }

            // Code.
            if(i == 1) {
                cell.getData = function(data) {
                    var cn = this.getElementsByTagName('input');

                    data.code = cn[0].value;
                    data.id = cn[1].value;
                    data.unit_id = cn[2].value;
                    data.vat_id = cn[3].value;
                    data.category_id = cn[4].value;
                }

                cell.setData = function(data) {
                    var cn = this.getElementsByTagName('input');

                    if(data.code)
                        cn[0].value = data.code;

                    if(data.id)
                        cn[1].value = data.id;

                    if(data.unit_id)
                        cn[2].value = data.unit_id;

                    if(data.vat_id)
                        cn[3].value = data.vat_id;

                    if(data.category_id)
                        cn[4].value = data.category_id;
                }

                var edit = '<input type="text" readonly="readonly" tabindex="1" class="inputs" autocomplete="off" >';
                cell.innerHTML = edit;
                cell.align = 'right';

                var id = document.createElement('input');
                id.setAttribute('type','hidden');
                cell.appendChild(id);

                var unit_id = document.createElement('input');
                unit_id.setAttribute('type','hidden');
                unit_id.setAttribute('value',OPT['default_unit']);
                cell.appendChild(unit_id);

                var vat_id = document.createElement('input');
                vat_id.setAttribute('type','hidden');
                vat_id.setAttribute('value',OPT['default_vat']);
                cell.appendChild(vat_id);

                var category_id = document.createElement('input');
                category_id.setAttribute('type','hidden');
                category_id.setAttribute('value',OPT['default_category']);

                cell.appendChild(category_id);
            }

            // Name.
            if(i == 2) {
                cell.getData = function(data) {
                    var cn = this.getElementsByTagName('textarea');
                    data.name = cn[0].value;
                }

                cell.setData = function(data) {
                    var cn = this.getElementsByTagName('textarea');

                    if(data.name)
                        cn[0].value = data.name;
                }

                var textarea = '<textarea tabindex="1" readonly="readonly" style="height:100%"; class="inputs"></textarea>';

                cell.innerHTML = textarea;
            }

            // Quantity.
            if(i == 3) {
                cell.getData = function(data,noAlert) {
                    var tmp = UserFormatNumberToNumber(this.firstChild.value);

                    if(tmp == -1) {
                        ERROR = true;
                        data.quantity = 0;
                        this.firstChild.style.color = 'red';
                    } else {
                        data.quantity = tmp;
                        this.firstChild.style.color = 'black';
                    }
                }

                cell.setData = function(data) {
                    if(data.quantity)
                        this.firstChild.value = NumberToUserFormatNumber(data.quantity);
                    else
                        this.firstChild.value = NumberToUserFormatNumber(0);
                }

                var edit = '<input type="text" readonly="readonly" tabindex="1" style="text-align:right;" autocomplete="off" class="inputs" value="'+NumberToUserFormatNumber(1)+'">';
                cell.innerHTML = edit;
            }

            // Rezerwacja.
            /*
            if(i == 4) {
                cell.getData = function(data,noAlert) {
                    var tmp = UserFormatNumberToNumber(this.firstChild.value);

                    if(tmp == -1) {
                        ERROR = true;
                        data.rq = 0;
                        this.firstChild.style.color = 'red';
                    } else {
                        data.rq = tmp;
                        this.firstChild.style.color = 'black';
                    }
                }

                cell.setData = function(data) {
                    if(data.rq)
                        this.firstChild.value = NumberToUserFormatNumber(data.rq);
                    else
                        this.firstChild.value = NumberToUserFormatNumber(0);
                }

                var edit = '<input type="text" readonly="readonly" tabindex="1" style="text-align:right;" autocomplete="off" class="inputs" value="'+NumberToUserFormatNumber(0)+'">';
                cell.innerHTML = edit;
            }
            */

            // Price.
            if(i == 4) {
                cell.getData = function(data,noAlert) {
                    var tmp = UserFormatNumberToNumber(this.firstChild.value);

                    if(tmp == -1) {
                        ERROR = true;

                        if(!noAlert)
                            alert(MOD['LBL_FORMAT_NUMBER_ERROR']+' ('+this.firstChild.value+')');

                        data.price = 0;
                        this.firstChild.style.color = 'red';
                    } else {
                        data.price = tmp;
                        this.firstChild.style.color = 'black';
                    }
                }

                cell.setData = function(data) {
                    if(data.price)
                        this.firstChild.value = NumberToUserFormatNumber(data.price);
                    else
                        this.firstChild.value = NumberToUserFormatNumber(0);
                }

                var edit = '<input type="text" readonly="readonly" tabindex="1" class="inputs" style="text-align:right;" autocomplete="off" value="'+NumberToUserFormatNumber(0)+'">';

                cell.innerHTML = edit;
            }

            // Discount
            if(i == 5) {
                cell.getData = function(data,noAlert) {
                    var tmp = UserFormatNumberToNumber(this.firstChild.value,'%');

                    if(tmp == -1) {
                        ERROR = true;

                        if(!noAlert)
                            alert(MOD['LBL_FORMAT_NUMBER_ERROR']+' ('+this.firstChild.value+')');

                        data.discount = 0;
                        this.firstChild.style.color = 'red';
                    } else {
                        data.discount = tmp;
                        this.firstChild.style.color = 'black';
                    }
                }

                cell.setData = function(data) {
                    if(data.discount)
                        this.firstChild.value = NumberToUserFormatNumber(data.discount,'%');
                    else
                        this.firstChild.value = NumberToUserFormatNumber(0,'%');
                }

                cell.selectNext = function() {
                    var row = this.parentNode.selectNext();
                    row.select();
                    row.cells.item(0).select();
                };

                var edit = '<input type="text" readonly="readonly" tabindex="1" class="inputs" style="text-align:right;" autocomplete="off" value="'+NumberToUserFormatNumber(0,'%')+'">';

                cell.innerHTML = edit;
            }

            // Price2.
            if(i == 6) {
                cell.getData = function(data,noAlert) {
                    var tmp = UserFormatNumberToNumber(this.firstChild.value);

                    if(tmp == -1) {
                        ERROR = true;

                        if(!noAlert)
                            alert(MOD['LBL_FORMAT_NUMBER_ERROR']+' ('+this.firstChild.value+')');

                        data.price2 = 0;
                        this.firstChild.style.color = 'red';
                    } else {
                        data.price2 = tmp;
                        this.firstChild.style.color = 'black';
                    }
                }

                cell.setData = function(data) {
                    if(data.price2)
                        this.firstChild.value = NumberToUserFormatNumber(data.price2);
                    else
                        this.firstChild.value = NumberToUserFormatNumber(0);
                }

                var edit = '<input type="text" readonly="readonly" tabindex="1" class="inputs" style="text-align:right;" autocomplete="off" value="'+NumberToUserFormatNumber(0)+'">';

                cell.innerHTML = edit;
            }

            // Vat.
            if(i == 7) {
                cell.getData = function(data,noAlert) {
                    var cn = this.getElementsByTagName('input');
                    data.vat_value = cn[0].value;
                    data.vat_id = cn[1].value;

                    if(VAT[cn[1].value])
                        data.vat_name = VAT[cn[1].value].name;
                }

                cell.setData = function(data) {
                    if(data.vat_value)
                        this.getElementsByTagName('input')[0].value = data.vat_value;

                    if(data.vat_id)
                        this.getElementsByTagName('input')[1].value = data.vat_id;

                    if(data.vat_name)
                        this.getElementsByTagName('input')[2].value = data.vat_name;
                }

                cell.onDeselect = function() {
                    ERROR = false;
                    var data = new Object();
                    this.getData(data);

                    if(!ERROR) {
                        data.vat_id = data.vat_id;
                        this.setData(data);
                    }
                }

                var inp=document.createElement('input');
                inp.setAttribute('type','hidden');
                cell.appendChild(inp);

                var inp=document.createElement('input');
                inp.setAttribute('type','hidden');
                cell.appendChild(inp);

                var inp;
                inp = document.createElement('input');
                inp.setAttribute('type','text');
                inp.className = 'inputs';
                inp.style.textAlign = "right";
                inp.cell = cell;

                cell.appendChild(inp);
            }

            // Unit.
            /*
            if(i == 9) {
                cell.getData = function(data,noAlert) {
                    data.unit_name = this.firstChild.value;
                }

                cell.setData = function(data) {
                    this.firstChild.value = data.unit_name;
                }

                var edit = '<input type="text" readonly="readonly" tabindex="1" class="inputs" style="text-align:right;" autocomplete="off" value="">';

                cell.innerHTML = edit;
            }
            */

            // Total
            if(i == 8) {
                //cell.select = function() {  };

                cell.getData = function(data) {
                    var cn = this.getElementsByTagName('input');
                    data.total = UserFormatNumberToNumber(this.firstChild.value);
                }

                cell.setData = function(data) {
                    var cn = this.getElementsByTagName('input');

                    if(data.total)
                        this.firstChild.value = NumberToUserFormatNumber(data.total);
                    else
                        this.firstChild.value = NumberToUserFormatNumber(0);
                }

                var edit = document.createElement('input');
                edit.setAttribute('type','text');
                edit.setAttribute('value',NumberToUserFormatNumber(0));
                edit.setAttribute('readOnly','readonly');
                edit.className = 'inputs';
                edit.style.textAlign = "right";

                cell.appendChild(edit);
            }

            // Stock.
            /*
            if(i == 11) {
                cell.getData = function(data,noAlert) {
                    var tmp = UserFormatNumberToNumber(this.firstChild.value);

                    if(tmp == -1) {
                        ERROR = true;
                        data.stock = 0;
                        this.firstChild.style.color = 'red';
                    }
                    else {
                        data.stock = tmp;
                        this.firstChild.style.color = 'black';
                    }
                }

                cell.setData = function(data) {
                    var dat = cell.parentNode.getData();

                    if (dat.type=='service') {
                        this.firstChild.value='usługa';
                        return;
                    }

                    if(data.stock)
                        this.firstChild.value = NumberToUserFormatNumber(data.stock);
                    else
                        this.firstChild.value = NumberToUserFormatNumber(0);
                }

                var stock;

                stock = document.createElement('input');
                stock.setAttribute('type', 'text');
                stock.setAttribute('readOnly', 'readonly');
                stock.className = 'inputs';
                stock.style.textAlign = "right";
                stock.cell = cell;

                cell.appendChild(stock);
            }
            */
        }

        N2.onSetCellData = function(row,cell,data) {
            if(cell.innerHTML == '')
                cell.innerHTML = '&nbsp;';
        }

        var pl = document.getElementById('registry_list').value;

        if(pl && pl != '') {
            try {
                pl = eval(pl);
                for(x in pl) {
                    if(typeof(pl[x].code) != "undefined") {
                        var pl_row = pl[x];
                        N2.addRow().setData(pl_row);
                    }
                }
            } catch(err) {
                pl = null;
            };
        }

        //if(N2.rowCount() == 0)
        //    N2.addRow();

        calculateTotal = function() {
            var vats = new Object();
            var subtotal = 0;
            var total = 0;

            //var cbm_total = 0;

            for(var i=0; i<N2.rowCount(); i++) {
                var data = N2.row(i).getData();

                if(OPT.to_is_vat_free == 0) {
                    if(data.vat_id && data.name != '') {
                        if(typeof(vats[data.vat_id]) != "object") {
                            vats[data.vat_id] = new Object();
                            vats[data.vat_id]['vat'] = 0;
                        }
                        vats[data.vat_id]['vat'] += data.total;
                    }
                }
                subtotal += data.total;
            //cbm_total += data.cbm;
            }

            total = subtotal;

            var rt = document.getElementById('services_result_table');

            for(var i=1; i<rt.rows.length-1; i++) {
                if(!vats[rt.rows.item(i).id]) {
                    rt.deleteRow(i);
                    --i;
                } else {
                    vats[rt.rows.item(i).id]['node'] = rt.rows.item(i);
                }
            }

            for(var x in vats) {
                if(VAT[x]){
                    vats[x]['vat'] = vats[x]['vat']*(parseFloat(VAT[x].value)/100);
                    total += vats[x]['vat'];

                    var txL = MOD['LBL_VAT']+' ('+VAT[x].name+')';
                    var txF = NumberToUserFormatNumber(vats[x]['vat']);
                }

                if(vats[x]['node']) {
                    vats[x]['node'].id = x;
                    vats[x]['node'].cells.item(0).innerHTML = txL;
                    vats[x]['node'].cells.item(1).innerHTML = "<input type='text' readonly='readonly' style='border:0px;font-weight:900;width:100%;text-align:right;' value='"+txF+"'>";
                } else {
                    rt.insertRow(1);
                    rt.rows.item(1).id = x;
                    rt.rows.item(1).insertCell(0);
                    rt.rows.item(1).cells.item(0).className = 'positionsLabel';
                    rt.rows.item(1).cells.item(0).innerHTML = txL;
                    rt.rows.item(1).insertCell(1);
                    rt.rows.item(1).cells.item(1).className = 'positionsField';
                    rt.rows.item(1).cells.item(1).innerHTML = "<input type='text' readonly='readonly' style='border:0px;font-weight:900;width:100%;text-align:right;' value='"+txF+"'>";
                }
            }

            var total2 = total;
            var discount;

            if((discount = CheckDiscount(true)) == -1)
                discount = 0;

            total2 = ((OPT['type'] == "correct")?'-':'')+NumberToUserFormatNumber(total2).toString();
            total = ((OPT['type'] == "correct")?'-':'')+NumberToUserFormatNumber(total).toString();

            document.getElementById('services_subtotal').value = NumberToUserFormatNumber(subtotal);

            if(document.getElementById('services_total'))
                document.getElementById('services_total').value = total2;
        }

        calculateTotal();

        function HideLoadingView() {
            var slv = document.getElementById('ShowLoadingView');

            if(slv)
                slv.style.display = 'none';
        }

        ServiceListSave = function(json) {
            var data = '';

            for(var i=0; i < N2.rowCount(); i++) {
                data = data + '&s_' + i.toString() + '=' + JSON.stringifyNoSecurity(N2.row(i).getData());
            }

            return data;
        }

        ServicesList = function(json) {
            var data = '';

            for(var i=0; i < N2.rowCount(); i++) {
                data = data + '||||' + JSON.stringifyNoSecurity(N2.row(i).getData());
            }

            return data;
        }

        /*
        setInterval(function() {
              doRequest('index.php',"module=EcmServices&action=subpanels&to_pdf=1&record="+document.forms.DetailView.record.value,
                function(result) {
                  if(result != '' && document.getElementById('subpanels_div')) document.getElementById('subpanels_div').innerHTML = result;
                }
              );
          },
          10000
        );
        */

        //quick view
        var main = document.getElementById('main');

        if(main) {
            var h2 = main.getElementsByTagName('h2')[0];

            if(h2) {
                h2.style.display = 'inline';
                var quickInfoH2 = document.getElementById('quickInfoH2');

                if(quickInfoH2) {
                    h2.parentNode.appendChild(quickInfoH2);
                    quickInfoH2.style.display = '';
                }
            }
        }

        if(OPT['setTab'] && OPT['setTab'] != '') {
            if(OPT['setTab'] == 'EMAIL')
                setEMAIL();
        }
    }
    );
/* /Registry */

/* PDF */
addEvent(
    window,
    'load',
    function () {
        setPREVIEW_INCOME = function() {
            if(CheckDiscount() != -1) {
                /*
                calculateTotalIncome();
                calculateTotalItems();
                calculateTotalServices();
                */
                
                sendFormPostToPdf('income', true);
            }
        }

        setPREVIEW_VAL = function() {
            if(CheckDiscount() != -1) {
                /*
                calculateTotalIncome();
                calculateTotalItems();
                calculateTotalServices();
                */
                
                sendFormPostToPdf('val', true);
            }
        }

        setPREVIEW_QTY = function() {
            if(CheckDiscount() != -1) {
                /*
                calculateTotalIncome();
                calculateTotalItems();
                calculateTotalServices();
                */
                
                sendFormPostToPdf('qty', true);
            }
        }

        setPREVIEW_WARRANTY = function() {
            if(CheckDiscount() != -1) {
                /*
                calculateTotalIncome();
                calculateTotalItems();
                calculateTotalServices();
                */
                
                sendFormPostToPdf('warranty', true);
            }
        }
    }
    );
/* /PDF */