function doRequest(where, post, doFunction, error) {
    this.Display = function(result) {
        doFunction(result.responseText);
    }

    this.Fail = function(result) {
        if (error)
            alert(error);
    }

    YAHOO.util.Connect.asyncRequest('POST', where, {success: this.Display, failure: this.Fail}, post);
}

function changeValidateRequired(formname, name, required) {
    for (var i = 0; i < validate[formname].length; i++)
        if (validate[formname][i][0] == name) {
            validate[formname][i][2] = required;
            break;
        }
}

//function set_focus() { document.getElementById('name').focus(); }

function my_popup(module, field_array, call_back_function, form_name) {
    if (!call_back_function)
        call_back_function = "set_return";

    if (!form_name)
        form_name = "EditView";

    return open_popup(module, 600, 400, "", true, false, {"call_back_function": call_back_function, "form_name": form_name, "field_to_name_array": field_array});
}

function addEvent(object, eventName, do_function) {
    if (typeof(object) == "string")
        object = document.getElementById(object);

    if (!object) {
        alert('No object in function addEvent!');
        return;
    }

    if (object.addEventListener) {
        object.addEventListener(eventName, do_function, false);
    } else {
        object.attachEvent('on' + eventName, do_function);
    }
}

var tbody_;
var ItemListSave;
var ItemListClear;
var ItemListFil;
var FillText;

addEvent(
        window,
        'load',
        function() {
            var PDFLanguagesTable = new PDFLanguagesMT('PDFLanguagesTable');

            PDFLanguagesTable.onRefreshRowIndex = function(row) {
                var data = new Object();

                data['index'] = (row.index + 1).toString();

                row.cells.item(0).setData(data);
            };

            PDFLanguagesTable.onCreateRow = function(row) {
                row.newPos = false;
                row.noAddNew = true;

                row.ondblclick = function() {
                };

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

                row.onDeselect = function() {
                    for (var i = 0; i < this.myTable.colCount(); i++)
                        this.cells.item(i).change(false);
                };
            };

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

                cell.change = function(select) {
                };

                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);
                }
                ;

                if (i == 1) {
                    cell.change = function(select) {
                    };

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

                        data.label = cn[0].value;
                    };

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

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

                    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);
                }
                ;

                if (i == 2) {
                    cell.noNewAdd = true;

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

                        data.translation = cn[0].value;
                    };

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

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

                    var edit = '<input type="text" onFocus="this.parentNode.select();" tabindex="1" class="inputs" style="height:16px;" autocomplete="off" onKeyDown="return this.parentNode.myTable.KeyPressed(event,this.parentNode);">';

                    cell.innerHTML = edit;
                }
                ;

            };

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

            ItemListSave = function(json, el) {
                var data = new Object();
                var tmp;
                var ecmlanguage = document.getElementById('ecmlanguage').value;

                if (typeof(el) == "string")
                    ecmlanguage = el;

                if (!PDFLanguagesOptions.ecmlanguage)
                    PDFLanguagesOptions.ecmlanguage = new Object();

                for (var i = 0; i < PDFLanguagesTable.rowCount(); i++) {
                    data[i.toString()] = PDFLanguagesTable.row(i).getData();
                }

                PDFLanguagesOptions['ecmlanguage'][ecmlanguage]['labels'] = data;

                FillText('header', document.getElementById('header_parent'));
                FillText('footer', document.getElementById('footer_parent'));
                FillText('ads', document.getElementById('ads_parent'));

                if (json) {
                    var r = JSON.stringifyNoSecurity(PDFLanguagesOptions['ecmlanguage']);

                    doRequest(
                            'index.php',
                            'module=EcmInvoiceOutOlds&action=PDFLanguagesSave&to_pdf=1&ecmlanguage=' + document.getElementById('ecmlanguage').value + '&pdflanguagelist=' + r,
                            function(result) {
                                if (result.indexOf("Saved") >= 0)
                                    alert("Saving done!");
                                else
                                    alert("Error with saving!");

                            }
                    );
                }
            };

            ItemListClear = function(noNew, save) {
                if (typeof(save) == "string")
                    ItemListSave(null, save);

                while (PDFLanguagesTable.rowCount() > 0)
                    PDFLanguagesTable.row(0).deleteRow(noNew);
            };

            ItemListFill = function() {
                var ecmlanguage = document.getElementById('ecmlanguage').value;
                var pl;

                if (PDFLanguagesOptions && PDFLanguagesOptions.ecmlanguage && PDFLanguagesOptions['ecmlanguage'][ecmlanguage]) {
                    pl = PDFLanguagesOptions['ecmlanguage'][ecmlanguage]['labels'];
                }
	
                console.log(ecmlanguage);
                console.log(pl);
				
                FillText('header', document.getElementById('header_parent'));
                FillText('footer', document.getElementById('footer_parent'));
                FillText('ads', document.getElementById('ads_parent'));

                if (pl && pl != '') {
                    try {
                        pl = eval(pl);

                        for (x in pl) {
                            var pl_row = pl[x];
                            PDFLanguagesTable.addRow().setData(pl_row);
                        }
                    } catch (err) {
                        pl = null;
                    }
                }

                document.getElementById('ecmlanguage').lastSelected = document.getElementById('ecmlanguage').value;
                //if(PDFLanguagesTable.rowCount() == 0) PDFLanguagesTable.addRow();    
            };

            FillText = function(name, parent) {
                var text = document.getElementById(name + '_text');
                var el = document.getElementById('ecmlanguage');
                
                if (!parent.lastSelected) {
                    parent.lastSelected = parent.value;
                    parent.lastSelectedEcmLanguage = el.value;
                } else {
                    //PDFLanguagesOptions['ecmlanguage'][parent.lastSelectedEcmLanguage]['texts'][parent.lastSelected][name + '_text'] = text.value;
                }                    
                
                //text.value = PDFLanguagesOptions['ecmlanguage'][el.value]['texts'][parent.value][name + '_text'];
                
                parent.lastSelected = parent.value;
                parent.lastSelectedEcmLanguage = el.value;
            };

            //PDFLanguagesTable.addRow();
            ItemListFill();
        }
);