Ext.onReady(function()
{
        Ext.form.Field.prototype.msgTarget = 'side';
        Ext.QuickTips.init();        
                    
            var win = new Ext.Window({
                modal   :true,
                closeAction: 'hide',
                resizable: false,
                width   : 800,
                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_ARTICLE_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_ARTICLE')});
            
            function loadWindow(value,active,msgtargetid){
                if(msgtargetid){
                    Ext.get(msgtargetid).hide();
                }
                
                myMask.show();
                if(!tb){
                    tb = new brodos.fulltext.Tbrowser({
                        localization: localization,
                        subject     : 'ARTICLEVLIST',
                        width       : 780,
                        hiddenColumns : ["DISTRIBARTICLEID"],
                        columnAdditions: {'DESCRIPTION':{width:300},"DISTARTICLENUMBER":{width:150},"MATCHCODE":{width:150},'PRICE':{width:80},'PRICEARTKEY':{width:80}},
                        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="DISTRIBARTICLEID" type="CHAR"/><column name="MATCHCODE" type="CHAR"/><column name="DISTARTICLENUMBER" type="CHAR"/><column name="DESCRIPTION" type="CHAR"/><column name="PRICE" type="CHAR"/><column name="PRICEARTKEY" 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'){
                    var filters = [{value:value,key:'fulltextsearchterm'},{value:active,key:'active'}];                    
                    tb.setFilters(filters);
                    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("DISTARTICLENUMBER");
                message = brodos.translation.get('_MASK_WANT_RESTORE_ARTICLE_PRETEXT') +'<b> ' + message + '</b> ' + brodos.translation.get('_MASK_WANT_RESTORE_ARTICLE_POSTTEXT');
                Ext.MessageBox.confirm(brodos.translation.get('_MASK_WANT_RESTORE_ARTICLE_HEADTEXT'),message,function(e){
                    if(e == "yes"){
                        Ext.Ajax.request({
                           url: brodos.config.get('WDIR') + "/service.php/article/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_ARTICLE'));
                                }
                           },
                           failure: function (){Ext.Msg.alert(brodos.translation.get('_MASK_ERROR'),brodos.translation.get('_MASK_ERROR_ON_RESTORE_ARTICLE'));},
                           params: {partnertype:'aricle',aritcleno: grid.store.getAt(rowIndex).get("DISTARTICLENUMBER") ,systemid : brodos.config.get('SYSTEMID'), action: 'activate'}
                        });
                    }
                });
            };
            
            selectedSubmit = function (grid,rowIndex){
                if(grid.getEl())
                {
                var mask = new Ext.LoadMask(grid.getEl()).show();
                }
                var showArticle = document.getElementById('searcharticleno');
                showArticle.value = grid.store.getAt(rowIndex).get('DISTARTICLENUMBER');
                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("_MASK_ARTICLENO"),
                                                    id:'searcharticlenofieldid',
                                                    maxLength:40,
                                                    maxLengthText: brodos.translation.get("_MASK_MAX_ARTICLE_VALUE_LENGTH")
                                                },{ 
                                                    fieldLabel: brodos.translation.get("_MASK_SEARCHTERM"),
                                                    id:'searcharticlematchcode',
                                                    maxLength:20,
                                                    maxLengthText: brodos.translation.get("_MSG_ARTICLE_SEARCHTERM_VALUE_LENGTH")
                                                }],
                                        keys:{ key: Ext.EventObject.ENTER,
                                                scope: this,
                                                fn:function(el)
                                                {   
                                                    var artnosearch = Ext.get('searcharticlenofieldid').getValue().replace(/^\s+|\s+$/g,"");
                                                    var machcode = Ext.get('searcharticlematchcode').getValue().replace(/^\s+|\s+$/g,"");
                                                    var filter = [];
                                                    if (artnosearch != "") 
                                                    {
                                                    	filter.push({value:artnosearch,key:'artno'});
                                                    }
                                                    if(machcode != "")
                                                    {
                                                    	filter.push({value:machcode,key:'searchterm'});
                                                    }
                                                    if(Ext.getCmp('searcharticlenofieldid').isValid() && Ext.getCmp('searcharticlematchcode').isValid())
                                                    {
                                                        loadWindow(filter,1,'NOFOUNDMSG');                                                                                
                                                    }
                                                }
                                        }
                                    }],
                                buttons:[new brodos.button.Search({
                                            listeners:  {"click":{fn:function()//onClickevent auf "suche" Button
                                                                    {                                                                                            
                                                                       	var artnosearch = Ext.get('searcharticlenofieldid').getValue().replace(/^\s+|\s+$/g,'');
                                                                        var machcode = Ext.get('searcharticlematchcode').getValue().replace(/^\s+|\s+$/g,'');
                                                                        var filter = [];
                                                                        if (artnosearch != "") 
                                                                        {
                                                                        	filter.push({value:artnosearch,key:'artno'});
                                                                        }
                                                                        if(machcode != "")
                                                                        {
                                                                        	filter.push({value:machcode,key:'searchterm'});
                                                                        }
                                                                        if(Ext.getCmp('searcharticlenofieldid').isValid() && Ext.getCmp('searcharticlematchcode').isValid())
                                                                        {
                                                                            loadWindow(filter,1,'NOFOUNDMSG');                                                                                
                                                                        }
                                                                    }
                                                                }
                                                        }
                                        })]
                                
                            }]
                }]
            });  
    var deleted = false;
    var deletedWindow = false;
    if(Ext.get('deletedArticle'))
    {
        deleted = Ext.get('deletedArticle');
        
        deletedWindow = false;
        
        var deletedsearch = new brodos.fulltext.Searchpanel({descriptionLabel:brodos.translation.get('_MASK_SEARCHCOLUMNS_ARTICLE')});
        
        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_ARTICLE_FOUND") + '</div>'
        });
        
        deletedWindow = new Ext.Window({
            layout  :'form',
            resizable : false,
            bodyStyle: 'backgroundColor : fff;',
            title   : brodos.translation.get('_MASK_HEAD_SEARCH_DELETED_ARTICLE'),
            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);
        });
    }
});
