   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',
                resizable	: false,
                width   	: 968,
                layout		: 'fit',
                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_CUSTOMER_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_DEBITOR')});
            
            function loadWindow(value,active,msgtargetid){
                if(msgtargetid){
                    Ext.get(msgtargetid).hide();
                }
                
                myMask.show();
                if(!tb){
                    tb = new brodos.fulltext.Tbrowser({
                        localization: localization,
                        subject     : 'CUSTOMERLIST',
                        width       : 970,
                        height		: 380,
                        hiddenColumns : ["HOUSENO","STREET",'NAME1'
                        					//,'NAME2'
                        					],
                        columnAdditions: {
                            "CITY"      :{width: 100},
                            "ZIPCODE"   :{width: 100},
                            "NAME2"     :{width: 140,header:brodos.translation.get('_MASK_NAME1_NAME2'),renderer: function(val,meta,rec,rind,cind,store)
                                                                {
                                                                    var n1 = rec.get("NAME1") || '';
                                                                    var n2 = rec.get("NAME2") || '';
                                                                 //   var n3 = rec.get("NAME3") || '';
                                                                    
                                                                    return n1 + ' ' +  n2;// + ' ' + n3;
                                                                }},
                            "PHONE"     :{width: 100},
                            "URL"       :{width: 160},
                            "SEARCHTERM":{width: 120,sortable: true},
                            "CUSTOMERNO":{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="CUSTOMERNO" type="CHAR" /><column name="SEARCHTERM" type="CHAR" /><column name="NAME" 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("NAME");// + ' ' + grid.store.getAt(rowIndex).get("NAME2") ;
                message = brodos.translation.get('_MASK_WANT_RESTORE_CUSTOMER_PRETEXT') +'<b> ' + message + '</b> ' + brodos.translation.get('_MASK_WANT_RESTORE_CUSTOMER_POSTTEXT');
                Ext.MessageBox.confirm(brodos.translation.get('_MASK_WANT_RESTORE_CUSTOMER_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_DEBITOR'));
         }
                           },
                           failure: function (){Ext.Msg.alert(brodos.translation.get('_MASK_ERROR'),brodos.translation.get('_MASK_ERROR_ON_RESTORE_DEBITOR'));},
                           params: {partnertype:'customer',customerno: grid.store.getAt(rowIndex).get("CUSTOMERNO"),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('sended_customerno');
                showCustomer.value = grid.store.getAt(rowIndex).get("CUSTOMERNO");
                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:100}
                                 ,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_CUSTOMERNO"),id:'searchcustomernofieldid',name:'searchcustomernofieldid', value : searchcustomerno,maxLength:10,maxLengthText: brodos.translation.get("_MASK_MAX_VALUE_LENGTH_CUSTNO_DEB")}
                                                ,{fieldLabel: brodos.translation.get("_MASK_SEARCHSTRING"),id:'searchsuchfieldid',name:'searchsuchfieldid',maxLength:15, value : searchsuch,maxLengthText: brodos.translation.get("_MASK_MAX_VALUE_LENGTH_SEARCH_DEB")}
                                                ,{fieldLabel: brodos.translation.get("_MASK_VONO"),id:'searchvonofieldid',name:'searchvonofieldid',maxLength:20, value : searchvono,maxLengthText: brodos.translation.get("_MASK_MAX_VALUE_LENGTH_VONO_DEB")}
                                                ,{fieldLabel: brodos.translation.get("_MASK_PHONE"),id:'searchphonefieldid',name:'searchphonefieldid',maxLength:20, value : searchphone,maxLengthText: brodos.translation.get("_MASK_MAX_VALUE_LENGTH_PHONE_DEB")}]
                                                ,keys:{key: Ext.EventObject.ENTER
                                                            ,fn:function(el){
                                                                                var searchcustomerno = Ext.get('searchcustomernofieldid').getValue().replace(/^\s+|\s+$/g,"");
                                                                                var searchvono = Ext.get('searchvonofieldid').getValue().replace(/^\s+|\s+$/g,"");
                                                                                var searchphone =  Ext.get('searchphonefieldid').getValue().replace(/^\s+|\s+$/g,"");
                                                                                var searchsuch = Ext.get('searchsuchfieldid').getValue().replace(/^\s+|\s+$/g,"");
                                                                                var filter = [];
                                                                                if (searchcustomerno != "") filter.push({value:searchcustomerno,key:'CUSTOMERNO'});
                                                                                if(searchvono != "") filter.push({value:searchvono,key:'VONO'});
                                                                                if(searchphone != "") filter.push({value:searchphone,key:'TELNUMBER'});
                                                                                if(searchsuch != "")  filter.push({value:searchsuch,key:'SEARCHTERM'});
                                                                                if(Ext.getCmp('searchsuchfieldid').isValid() && Ext.getCmp('searchvonofieldid').isValid() && Ext.getCmp('searchphonefieldid').isValid() && Ext.getCmp('searchcustomernofieldid').isValid())
                                                                                    loadWindow(filter,1,'NOFOUNDMSG');
                                                                                }
                                                        }
                                        }]
                                 ,buttons:[new brodos.button.Search({listeners:{"click":function(){                                                                                            
                                                                        var searchcustomerno = Ext.get('searchcustomernofieldid').getValue().replace(/^\s+|\s+$/g,"");
                                                                        var searchvono = Ext.get('searchvonofieldid').getValue().replace(/^\s+|\s+$/g,"");
                                                                        var searchphone =  Ext.get('searchphonefieldid').getValue().replace(/^\s+|\s+$/g,"");
                                                                        var searchsuch = Ext.get('searchsuchfieldid').getValue().replace(/^\s+|\s+$/g,"");
                                                                        var filter = [];
                                                                        if (searchcustomerno != "") filter.push({value:searchcustomerno,key:'CUSTOMERNO'});
                                                                        if(searchvono != "") filter.push({value:searchvono,key:'VONO'});
                                                                        if(searchphone != "") filter.push({value:searchphone,key:'TELNUMBER'});
                                                                        if(searchsuch != "")  filter.push({value:searchsuch,key:'SEARCHTERM'});
                                                                        if(Ext.getCmp('searchsuchfieldid').isValid() && Ext.getCmp('searchvonofieldid').isValid() && Ext.getCmp('searchphonefieldid').isValid() && Ext.getCmp('searchcustomernofieldid').isValid())
                                                                            loadWindow(filter,1,'NOFOUNDMSG');
                                                                    }
                                                               }
                                            }),new brodos.button.AdvancedSearch({listeners:{"click":function(){
                                                                                            var showadvance = document.getElementById('show_advance_search');
                                                                                            showadvance.value = 1;
                                                                                            document.forms[0].submit();
                                                                                        }
                                                                                   }
                                              })]
                                }
                        ]}
            });  
    var deleted = false;
    var deletedWindow = false;
    
    if(Ext.get('deletedCustomers'))
                                                                            {
        deleted = Ext.get('deletedCustomers');
        
        deletedWindow = false;
        
        var deletedsearch = new brodos.fulltext.Searchpanel({descriptionLabel:brodos.translation.get('_MASK_SEARCHCOLUMNS_DEBITOR')});
        
        deletedsearch.FulltextSearchButton.on('click',function(){
            loadWindow(deletedsearch.fulltextInputField.getValue(),"0",'NOFOUNDMSGINNER');
        });
        
        deletedsearchpanel  = new Ext.form.FieldSet({
            layout  : 'form',
            border:false,
            autoHeight:true,
           // width: 400,
            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_DATA") + '</div>'
                                                    });
                   
        deletedWindow = new Ext.Window({
            layout  :'form',
            bodyStyle: 'backgroundColor : fff;',
            title   : brodos.translation.get('_MASK_HEAD_SEARCH_DELETED_CUSTOMER'),
            modal   :true,
            closeAction:'hide',
            width: 450,
            items	: [deletedsearchpanel],
            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);
        });
            }
});
