   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   : 870,
                buttons:[new brodos.button.Done({
                    listeners:{click:{fn:function(){win.hide();}}}
                })]
            });
            
            var tb = false;
            
            myMask = new Ext.LoadMask(Ext.getBody(),{msg:brodos.translation.get((!brodos.config.get('EMPLOYEEMODE')?'_MSG_PERSON_DATA_SEARCH':'_MSG_EMPLOYEE_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((!brodos.config.get('EMPLOYEEMODE')?'_MASK_SEARCHCOLUMNS_PERSON':'_MASK_SEARCHCOLUMNS_EMPLOYEE'))});
            
            function loadWindow(value,active,msgtargetid){
                if(msgtargetid){
                    Ext.get(msgtargetid).hide();
                }
                
                myMask.show();
                if(!tb)
                {
                    tb = new brodos.fulltext.Tbrowser({
                        localization: localization,
                        subject     : 'PERSONLIST',
                        width       : 855,
                        hiddenColumns : ["HOUSENO","STREET"],
                        columnAdditions: {
                            "URL"           :{width:200},
                            "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;
                                                                }
                                            },
                            "ISACTIVE":{width: 50, renderer: function(val,meta,rec,rind,cind,store)
                                                                {
                                                                    if (val == 0) return '<img src="'+brodos.config.get('WDIR')+'/images/cross.png" border="0">';
                                                                    else return '<img src="'+brodos.config.get('WDIR')+'/images/tick.png" border="0">';
                                                                }
                                            }
                          },
                        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="PERSONNO" type="CHAR" /><column name="FIRSTNAME" type="CHAR" /><column name="LASTNAME" type="CHAR" /><column name="STREET" 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" /><column name="ISACTIVE" type="CHAR" /></columns></query-definition></metadata><status code="0"></status> </result>'
                    });
                    
                    tb.on('tbrowserloaded', function(browser){
                           var countofresults = browser.grid.store.getCount();
                           tb.columnModel.setHidden(9,true);
                           if(countofresults > 0 )
                           {                                
                                if(deletedWindow)                                        
                                {
                                    if(!deletedWindow.isVisible() && countofresults == 1 && browser.grid.bottomToolbar.currentPage == 1)
                                    {
                                        selectedSubmit(browser.grid,0);
                                        return true;
                                    }
                                    else if(!deletedWindow.isVisible())
                                    {
                                        tb.columnModel.setHidden(9,false);
                                    }
                                }
                                    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'){
                    var filters = [{value:value,key:'fulltextsearchterm'},{value:active,key:'active'}];                    
                    if(brodos.config.get('EMPLOYEEMODE'))
                    {                 
                    if( Ext.get("EMPLOYEETYPE"))filters.push({value:Ext.get("EMPLOYEETYPE").getValue(),key:'EMPLOYEETYPE'});
                    if( Ext.get("EMPLOYERNO"))filters.push({value:Ext.get("EMPLOYERNO").getValue(),key:'EMPLOYERNO'});
                    }
                    tb.setFilters(filters);
                    tb.reload();
                }
                else if(typeof value == 'object'){                    
                    if(brodos.config.get('EMPLOYEEMODE'))
                    {                 
	                    if( Ext.get("EMPLOYEETYPE"))value.push({value:Ext.get("EMPLOYEETYPE").getValue(),key:'EMPLOYEETYPE'});
	                    if( Ext.get("EMPLOYERNO"))value.push({value:Ext.get("EMPLOYERNO").getValue(),key:'EMPLOYERNO'});
                    }
                    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("FIRSTNAME") + ' ' + grid.store.getAt(rowIndex).get("LASTNAME") ;
                message = brodos.translation.get('_MASK_WANT_RESTORE_PERSON_PRETEXT') +'<b> ' + message + '</b> ' + brodos.translation.get('_MASK_WANT_RESTORE_PERSON_POSTTEXT');
                Ext.MessageBox.confirm(brodos.translation.get('_MASK_WANT_RESTORE_PERSON_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_PERSON'));
            }
                           },
                           failure: function (){Ext.Msg.alert(brodos.translation.get('_MASK_ERROR'),brodos.translation.get('_MASK_ERROR_ON_RESTORE_PERSON'));},
                           params: {partnertype:'person',personno: grid.store.getAt(rowIndex).get('PERSONNO') ,systemid : brodos.config.get('SYSTEMID'), action: 'activate'}
                        });
         }
                });
            };
            
            selectedSubmit = function (grid,rowIndex){
                if(grid.getEl())
                {
                var mask = new Ext.LoadMask(grid.getEl()).show();
                }
                var showPerson = document.getElementById('searchpersonno');
                showPerson.value = grid.store.getAt(rowIndex).id;
                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},
                                        items:[{
                                                    fieldLabel: brodos.translation.get((!brodos.config.get('EMPLOYEEMODE')?"_MASK_PERSONNO":"_MASK_EMPLOYEENO")),
                                                    id:'searchpersonnofieldid',
                                                    maxLength:10,
                                                    minLength:1,
                                                    maxLengthText: brodos.translation.get("_MASK_MAX_PERSON_VALUE_LENGTH")
                                                },{ 
                                                    fieldLabel: brodos.translation.get("_MASK_PERSONBMNO"),
                                                    id:'searchbmnofieldid',
                                                    maxLength:16,
                                                    minLength:16, 
                                                    maxLengthText: brodos.translation.get("_BMNO_VALUE_LENGTH"),
                                                    minLengthText: brodos.translation.get("_BMNO_VALUE_LENGTH")
                                                },{
                                                    store: new Ext.data.ArrayStore({
                                                                                    fields:['1','2'], 
                                                                                    data:[  
                                                                                            ['NOCHOICE', brodos.translation.get("_MASK_NOCHOICE")],
                                                                                            ['NOTASSIGNED', brodos.translation.get("_MASK_NOTASSIGNED_PERSON")],
                                                                                            ['NOCASHUPASSIGNED', brodos.translation.get("_MASK_NOTASSIGNED_PERSON_WITH_CARD")]
                                                                                        ]
                                                                                    }) ,
                                                    value: 'NOCHOICE',
                                                    forceSelection:true,
                                                    fieldLabel: !brodos.config.get('EMPLOYEEMODE') ? brodos.translation.get('_MASK_NOTASSIGNED'):'',
                                                    id:'clauseselect',
                                                    xtype:'combo',
                                                    triggerAction:'all',
                                                    mode:'local',
                                                    displayField:'2',
                                                	hidden: brodos.config.get('EMPLOYEEMODE'),
                                                    valueField:'1'
                                                }],
                                        keys:{ key: Ext.EventObject.ENTER,
                                                scope: this,
                                                fn:function(el)
                                                                            {   
                                                                                var perssearch = Ext.get('searchpersonnofieldid').getValue().replace(/^\s+|\s+$/g,"");
                                                                                var bmnosearch = Ext.get('searchbmnofieldid').getValue().replace(/^\s+|\s+$/g,"");
                                                    var clauseselect = Ext.getCmp('clauseselect').getValue().replace(/^\s+|\s+$/g,"");
                                                    
                                                    var filter = [];
                                                    if(brodos.config.get('EMPLOYEEMODE'))
                                                    {
                                                    if( Ext.get("EMPLOYEETYPE"))filter.push({value:Ext.get("EMPLOYEETYPE").getValue(),key:'EMPLOYEETYPE'});
                                                    if( Ext.get("EMPLOYERNO"))filter.push({value:Ext.get("EMPLOYERNO").getValue(),key:'EMPLOYERNO'});
                                                    }
                                                    if (perssearch != "") filter.push({value:perssearch,key:'PERSONNO'});
                                                    if(bmnosearch != "") filter.push({value:bmnosearch,key:'BMNO'});
                                                    if(clauseselect != "NOCHOICE") filter.push({value:clauseselect,key:'USERCASE'});
                                                    if(Ext.getCmp('searchpersonnofieldid').isValid() && Ext.getCmp('searchbmnofieldid').isValid())
                                                        loadWindow(filter,1,'NOFOUNDMSG');                                                                                
                                                                                }
                                                                            }
                                    }],
                                buttons:[new brodos.button.Search({
                                            listeners:{"click":{fn:function()//onClickevent auf "suche" Button
                                                                    {                                                                                            
                                                                        var perssearch = Ext.get('searchpersonnofieldid').getValue().replace(/^\s+|\s+$/g,"");
                                                                        var bmnosearch = Ext.get('searchbmnofieldid').getValue().replace(/^\s+|\s+$/g,"");
                                                                        var clauseselect = Ext.getCmp('clauseselect').getValue().replace(/^\s+|\s+$/g,"");
                                                                        
                                                                        var filter = [];
                                                                        if(brodos.config.get('EMPLOYEEMODE'))
                                                                        {
                                                                        if( Ext.get("EMPLOYEETYPE"))filter.push({value:Ext.get("EMPLOYEETYPE").getValue(),key:'EMPLOYEETYPE'});
                                                                        if( Ext.get("EMPLOYERNO"))filter.push({value:Ext.get("EMPLOYERNO").getValue(),key:'EMPLOYERNO'});
                                                                        }
                                                                        if (perssearch != "") filter.push({value:perssearch,key:'PERSONNO'});
                                                                        if(bmnosearch != "") filter.push({value:bmnosearch,key:'BMNO'});
                                                                        if(clauseselect != "NOCHOICE") filter.push({value:clauseselect,key:'USERCASE'});
                                                                        if(Ext.getCmp('searchpersonnofieldid').isValid() && Ext.getCmp('searchbmnofieldid').isValid())
                                                                            loadWindow(filter,1,'NOFOUNDMSG');              
                                                                }
                                                            }
                                                    }
                                        })]
                                
                            }]
                }]
            });  
    var deleted = false;
    var deletedWindow = false;
    if(Ext.get('deletedPersons'))
                                                                            {
        deleted = Ext.get('deletedPersons');
        
        deletedWindow = false;
        
        var deletedsearch = new brodos.fulltext.Searchpanel({descriptionLabel:brodos.translation.get(!brodos.config.get('EMPLOYEEMODE')?'_MASK_SEARCHCOLUMNS_PERSON':'_MASK_SEARCHCOLUMNS_EMPLOYEE')});
        
        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((!brodos.config.get('EMPLOYEEMODE')?"_ERROR_NO_PERSONS_FOUND":"_ERROR_NO_EMPLOYEE_FOUND")) + '</div>'
        });
                                        
        deletedWindow = new Ext.Window({
            layout  :'form',
            resizable : false,
            bodyStyle: 'backgroundColor : #fff;',
            title   : brodos.translation.get((!brodos.config.get('EMPLOYEEMODE')?'_MASK_HEAD_SEARCH_DELETED_PERSONS':'_MASK_HEAD_SEARCH_DELETED_EMPLOYEE')),
            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);
                });
            }
});
