 Ext.onReady(function()
   {
        Ext.form.Field.prototype.msgTarget = 'side';
        Ext.QuickTips.init();

           localization = new brodos.localization.Localization
            ({
                translationArray: js_translation_array,
                scope: this
            });
            localization.load();
          
                    
            var win = new Ext.Window({
                modal   :true,
                closeAction: 'hide',
                width   : 1015,
                buttons:[new brodos.button.Done({
                    listeners:{click:{fn:function(){win.hide();}}}
                })]
            });
            
            var tb = false;
            
            myMask = new Ext.LoadMask(Ext.getBody(),{msg:brodos.translation.get('_MSG_KREDITOR_DATA_SEARCH')});

            Ext.getBody().on('keydown',function(Event, Element, Object)
        {
                if(Event.ESC == Event.getKey())
            {
                    myMask.hide();
                    if(Ext.get('NOFOUNDMSG'))Ext.get('NOFOUNDMSG').hide();
                    if(Ext.get('NOFOUNDMSGINNER'))Ext.get('NOFOUNDMSGINNER').hide();
                    Ext.Ajax.abort();
            }
            });
            
            var fulsearchpan = new brodos.fulltext.Searchpanel({descriptionLabel:brodos.translation.get('_MASK_SEARCHCOLUMNS_KREDITOR')});
            
            function loadWindow(value,active,msgtargetid){
                if(msgtargetid){
                    Ext.get(msgtargetid).hide();
         }
         
                myMask.show();
                if(!tb){
                    tb = new brodos.fulltext.Tbrowser({
                        localization: localization,
                        subject     : 'SUPLIERLIST',
                        width       : 1000,
                        hiddenColumns : ["HOUSENO","STREET"],
                        columnAdditions: {
                            "CITY"      :{width: 100},
                            "ZIPCODE"   :{width: 100},
                            "NAME2"     :{width: 140},
                            "NAME1"     :{width: 140},
                            "PHONE"     :{width: 100},
                            "URL"       :{width: 160},
                            "SEARCHTERM":{width: 120,sortable: true},
                            "SUPPLIERNO":{width: 80,sortable: true},
                            "STREET_HOUSENO":{width: 150, renderer: function(val,meta,rec,rind,cind,store)
                                                                {
                                                                    var srt = rec.get("STREET");
                                                                    var hno = rec.get("HOUSENO");
                                                                    if(hno == "" && srt != "") return srt;
                                                                    if(srt == "" && hno != "") return hno;
                                                                    if(srt == "" && hno == "") return "";
                                                                    return srt + " " + hno;
                                                                }
                                            }
                          },
                        staticMetaData: '<'+'?xml version="1.0" encoding="UTF-8" ?'+'><result xmlns="http://brodos.net/schemas/dataStorage/2008/01"><metadata><query-definition name="Liste" key="query1"><columns><column name="SUPPLIERNO" type="CHAR" /><column name="SEARCHTERM" type="CHAR" /><column name="NAME1" type="CHAR" /><column name="NAME2" type="CHAR" /><column name="STREET" type="CHAR" /><column name="STREET_HOUSENO" type="CHAR" /><column name="HOUSENO" type="CHAR" /><column name="ZIPCODE" type="CHAR" /><column name="CITY" type="CHAR" /><column name="URL" type="CHAR" /><column name="PHONE" type="CHAR" /></columns></query-definition></metadata><status code="0"></status> </result>'
                    });
                    tb.on('tbrowserloaded', function(browser){
                           var countofresults = browser.grid.store.getCount();
                           if(countofresults > 0 )
                           {                                
                                if(deletedWindow)                                        
                                {
                                    if(!deletedWindow.isVisible() && countofresults == 1 && browser.grid.bottomToolbar.currentPage == 1)
                                    {
                                        selectedSubmit(browser.grid,0);
                                        return true;
                                    }
                                    }
                                        myMask.hide();
                                        win.show();
                                        win.focus();
                                    }
                                else{                                
                                    myMask.hide();
                                if(msgtargetid)
                                {
                                    if(deletedWindow)                                        
                                    {
                                        if(deletedWindow.isVisible())
                                   {
                                            Ext.get('NOFOUNDMSGINNER').show();
                                   }
                                   else
                                   { 
                                            Ext.get('NOFOUNDMSG').show();
                                        }  
                                       } 
                                       else
                                       {
                                        Ext.get('NOFOUNDMSG').show();
                                       }                                                                                                             
                                   }
                                }
                    });  
                    win.add(tb);
                }
                        
                tb.queryDefinition.paging.skip = 0
                tb.grid.bottomToolbar.currentPage = 1;
                if(typeof value === 'string')value = trim(value);
                if ((value.length <1  || value.length >= 3) && typeof value === 'string'){
                    tb.setFilters([{value:value,key:'fulltextsearchterm'},{value:active,key:'active'}]);
                    tb.reload();
                }
                else if(typeof value == 'object'){
                    value.push({value:active,key:'active'});
                    tb.setFilters(value);
                    tb.reload();
                }
                
                if (active == 1){   
                    tb.grid.on('rowclick', selectedSubmit);
                    tb.grid.un('rowclick',ShowDecitionWindow);
                }
                else{
                    tb.grid.on('rowclick',ShowDecitionWindow );
                    tb.grid.un('rowclick',selectedSubmit);                    
                                    }
                                }
            
            fulsearchpan.FulltextSearchButton.on('click',function(){
                if(fulsearchpan.fulltextInputField.isValid())
                    loadWindow(fulsearchpan.fulltextInputField.getValue(),"1",'NOFOUNDMSG');
            });
            
            ShowDecitionWindow = function  (grid,rowIndex){
                var message = grid.store.getAt(rowIndex).get("NAME1") + ' ' + grid.store.getAt(rowIndex).get("NAME2") ;
                message = brodos.translation.get('_MASK_WANT_RESTORE_KREDITOR_PRETEXT') +'<b> ' + message + '</b> ' + brodos.translation.get('_MASK_WANT_RESTORE_KREDITOR_POSTTEXT');
                Ext.MessageBox.confirm(brodos.translation.get('_MASK_WANT_RESTORE_KREDITOR_HEADTEXT'),message,function(e){
                    if(e == "yes"){
                        Ext.Ajax.request({
                           url: brodos.config.get('WDIR') + "/service.php/partner/activate",
                           success: function (response){
                                var success = Ext.decode (response.responseText)
                                if(success.SUCCESS)
                                        {
                                    selectedSubmit(grid,rowIndex);
                                        }
                                        else
                                        { 
                                    Ext.Msg.alert(brodos.translation.get('_MASK_ERROR'),brodos.translation.get('_MASK_ERROR_ON_RESTORE_KREDITOR'));
                                            } 
                           },
                           failure: function (){Ext.Msg.alert(brodos.translation.get('_MASK_ERROR'),brodos.translation.get('_MASK_ERROR_ON_RESTORE_KREDITOR'));},
                           params: {partnertype:'kreditor',supplierno: grid.store.getAt(rowIndex).get("SUPPLIERNO"), systemid : brodos.config.get('SYSTEMID'), action: 'activate'}
                        });
                                            }                                                                                                             
                });
            };
            
            selectedSubmit = function (grid,rowIndex){
                if(grid.getEl())
                {
                var mask = new Ext.LoadMask(grid.getEl()).show();
                }
                var showCustomer = document.getElementById('searchsupplierno');
                showCustomer.value = grid.store.getAt(rowIndex).get("SUPPLIERNO");
                document.forms[0].submit();
            };
            
            var pan = new Ext.Panel({
                renderTo:'buttons'
               ,autoWidth:true
               ,height:Ext.isIE?190:'auto'
               ,layout:'fit'
               ,border:false
               ,cls:'masktxt'
               ,items:{
                    id:'sendsearchterm',
                    xtype:'form',
                    layout:'column',
                    labelWidth:80,
                    border:false,
                    frame:false,
                    cls:'masktxt',
                    defaults:{
                        columnWidth:0.5,
                        layout:'form',
                        border:false
                                        }
                   ,items:[{
                                xtype   : 'fieldset',
                                layout  : 'form',
                                autoHeight:true,
                                defaults:{style   : 'padding-top:10px;'},
                                title   : brodos.translation.get("_MASK_FULLTEXTSEARCH_HEAD"),
                                keys:{key: Ext.EventObject.ENTER,scope:this,fn:function(el){
                                        if(fulsearchpan.fulltextInputField.isValid())
                                            loadWindow(fulsearchpan.fulltextInputField.getValue(),"1",'NOFOUNDMSG'); 
                                }
                                },
                                items   : [fulsearchpan]
                                    
                           },{buttonAlign:"right",style:' border-left:2px solid gray',cls:'masktxt',
                                 defaults:{xtype:'fieldset', layout:'form', anchor:'100%',cls:'masktxt', autoHeight:true,border:false,labelWidth:110}
                                 ,items:[{title: brodos.translation.get("_MASK_SIMPLE_SEARCH_HEAD"),defaultType:'textfield',ctCls:'masktxt',defaults:{ anchor:'-20', allowBlank:true,border:true}
                                         ,items:[{fieldLabel: brodos.translation.get("_MASK_SUPPLIERNO"),id:'searchsupliernofieldid',name:'searchsupliernofieldid', value :searchsuplierno,maxLength:10,maxLengthText: brodos.translation.get("_MASK_MAX_VALUE_SUPPLIER_NO_LENGTH")}
                                                ,{fieldLabel: brodos.translation.get("_MASK_SEARCHSTRING"),id:'searchsuchfieldid',name:'searchsuchfieldid',maxLength:15, value :searchsuch,maxLengthText: brodos.translation.get("_MASK_MAX_VALUE_SUPPLIER_SEARCH_LENGTH")}]
                                                ,keys:{key: Ext.EventObject.ENTER
                                                            ,scope: this,fn:function(el)
                                                                            {   
                                                                                var searchsupplierno = Ext.get('searchsupliernofieldid').getValue().replace(/^\s+|\s+$/g,"");                                                                                  
                                                                                var searchsuch = Ext.get('searchsuchfieldid').getValue().replace(/^\s+|\s+$/g,"");
                                                                                    var filter = [];
                                                                                if (searchsupplierno != "") filter.push({value:searchsupplierno,key:'SUPLIERNO'});
                                                                                if(searchsuch != "") filter.push({value:searchsuch,key:'SEARCHTERM'});
                                                                                if(Ext.getCmp('searchsupliernofieldid').isValid() && Ext.getCmp('searchsuchfieldid').isValid())
                                                                                    loadWindow(filter,1,'NOFOUNDMSG');
                                                                                }
                                                                            
                                                        }
                                        }]
                                 ,buttons:[new brodos.button.Search({listeners:{"click":{fn:function()//onClickevent auf "suche" Button
                                                                    {         
                                                                        var searchsupplierno = Ext.get('searchsupliernofieldid').getValue().replace(/^\s+|\s+$/g,"");                                                                                  
                                                                        var searchsuch = Ext.get('searchsuchfieldid').getValue().replace(/^\s+|\s+$/g,"");                                                                                  
                                                                                    var filter = [];
                                                                                    if (searchsupplierno != "") filter.push({value:searchsupplierno,key:'SUPLIERNO'});
                                                                                    if(searchsuch != "") filter.push({value:searchsuch,key:'SEARCHTERM'});
                                                                                    if(Ext.getCmp('searchsupliernofieldid').isValid() && Ext.getCmp('searchsuchfieldid').isValid())
                                                                                        loadWindow(filter,1,'NOFOUNDMSG');
                                                                    }
                                                                  }
                                                               }
                                            })]
                                }
                        ]}
            });  
    var deleted = false;
    var deletedWindow = false;
    if(Ext.get('deletedKreditors'))
    {          
        deleted = Ext.get('deletedKreditors');
        var deletedsearch = new brodos.fulltext.Searchpanel({descriptionLabel:brodos.translation.get('_MASK_SEARCHCOLUMNS_KREDITOR')});
        deletedsearch.FulltextSearchButton.on('click',function(){
            loadWindow(deletedsearch.fulltextInputField.getValue(),"0",'NOFOUNDMSGINNER');
        });
        deletedsearchpanel  = new Ext.form.FieldSet({
            layout  : 'form',
            border:false,
            autoHeight:true,
            width: 450,
            style   : 'margin-top:20px;',
            title   : brodos.translation.get("_MASK_FULLTEXTSEARCH_HEAD"),
            keys:[{key: Ext.EventObject.ENTER,fn:function(el){
                        if(deletedsearch.fulltextInputField.isValid())
                            loadWindow(deletedsearch.fulltextInputField.getValue(),"0",'NOFOUNDMSGINNER'); 
                    }
                }],
            items:[deletedsearch],
            html: '<br ><div id="NOFOUNDMSGINNER" style="display:none;" class="errortxt" align="center">'+ brodos.translation.get("_ERROR_NO_KREDITOR_FOUND") + '</div>'
        });
        
        deletedWindow = new Ext.Window({
            layout  :'form',
            bodyStyle: 'backgroundColor : fff;',
            title   : brodos.translation.get('_MASK_HEAD_SEARCH_DELETED_KREDITOR'),
            modal   :true,
            closeAction:'hide',
            items	: [deletedsearchpanel],
            width	: 450,
            buttons :[new brodos.button.Done({
                listeners:{
                    click:{fn:function(){
                                deletedWindow.hide();
                                Ext.get('NOFOUNDMSGINNER').hide();
                                myMask.hide();
                                Ext.Ajax.abort();
                            }
                                                    } 
                                                                            }
            })]
                                                    });
                    
        deleted.on('click',function(){
                deletedWindow.show();
                Ext.get('NOFOUNDMSGINNER').hide();
                deletedWindow.setZIndex(10);
        });
                                           }
                });
