/*!
 * Ext JS Library 3.0+
 * Copyright(c) 2006-2009 Ext JS, LLC
 * licensing@extjs.com
 * http://www.extjs.com/license
 */
 
var BillingTypeWin; 
 
function ShowEditBillingTypeWindow(id){
        BillingTypeWin.CreateWindow(true);
        BillingTypeWin.PopulateFields(id);        
} 
 
Ext.onReady(function(){
    var win;

     new Ext.Button({
            id:"addbilling",
            renderTo:"addbillingtype",
            text: lang('Add Billing Type'),
            iconAlign: 'left',
            handler:function(){
                 BillingTypeWin.CreateWindow(false,0);
                 simpleForm.getForm().reset();
            }
    });
    

    //alert(editlinks.length);
    var simpleForm = new Ext.FormPanel({
        labelWidth: 75, // label settings here cascade unless overridden
        frame:true,
        border:false,
        hideBorders:true,
        bodyStyle:'padding:8px 15px 0',
        width: 350,
        defaults: {width: 230},
        defaultType: 'textfield',

        items: [{
                fieldLabel: 'Id',
                name: 'id',
                xtype: "hidden"
            },{
                fieldLabel: 'Name',
                name: 'name',
                allowBlank:false
            },{
                fieldLabel: 'Description',
                name: 'description',
                allowBlank:false
            },{
                fieldLabel: 'Details',
                name: 'detail',
                xtype: "textarea",
                height: 110
            },{
                fieldLabel: 'Price',
                name: 'price',
                width:70
            }
        ]
    });

    function TableTypeWindow(update){
        if(this instanceof TableTypeWindow){
            this.update = update;
            this.CreateWindow = createWindow; 
            this.PopulateFields = populateFields;
        }else{
            return new TableTypeWindow();
        }
    }
    BillingTypeWin = new TableTypeWindow();
    
    function populateFields(id){
        geturl = 'index.php?fuse=admin&action=GetBillingTypes&id=' + id;
        simpleForm.getForm().load({url:geturl, waitMsg:'Loading'});
    }
    
    function createWindow(update){
        if(update){
            requesturl = 'index.php?fuse=billing&action=UpdateBillingType';
            buttonText = 'Update';
            titleText = '&nbsp;&nbsp;'+lang('Edit Billing Type');
        }else{
            requesturl = 'index.php?fuse=billing&action=AddBillingType';
            buttonText = 'Save';
            titleText = '&nbsp;&nbsp;'+lang('Add Billing Type');
        }
    
        // create the window on the first click and reuse on subsequent clicks
        //if(!win){
            win = new Ext.Window({
                layout:'fit',
                title:titleText,
                width:380,
                height:300,
                modal:true,
                closeAction:'hide',
                plain: true,
                items: [simpleForm],
                buttons: [
                    {
                    text: 'Close',
                    handler: function(){
                        win.hide();
                    }
                }]
            });
            
            // simple button save (update)
            win.addButton(buttonText, function(){
                simpleForm.getForm().submit({
                    clientValidation: true,
                    url: requesturl,
                    params: {
                        newStatus: 'delivered'
                    },
                    success: function(form, action) {
                       win.hide();
                       //get the grid if there is one
                       //if we are in the billingtype view
                       //if not then skip
                       grid = Ext.getCmp("billingtype-grid");
                       if(grid!=null){
                            grid.getStore().load({params:{start:0}}); 
                            toggleButtons(0);
                       }
                       //we might want to check to see if we are showing a dropdown
                       //in the event we are creating an invoice
                       //do that code here and update the dropdown
                       
                    },
                    failure: function(form, action) {
                        switch (action.failureType) {
                            case Ext.form.Action.CLIENT_INVALID:
                                Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
                                break;
                            case Ext.form.Action.CONNECT_FAILURE:
                                Ext.Msg.alert('Failure', 'Ajax communication failed');
                                break;
                            case Ext.form.Action.SERVER_INVALID:
                               Ext.Msg.alert('Failure', action.result.msg);
                       }
                    }
                    
                });
            });
        //}else{
        //    simpleForm.getForm().reset();
        //}
                
        win.show(this);
    }
    
    // option button code
    function toggleButtons(count){
        if(count == undefined){
            //get the sm if there is one
            //if we are in the currency view
            //if not then skip
            sm = Ext.getCmp("billingtype-sm");
            if(sm!=null){
                count = sm.getCount();
            }
        }
        if(count != undefined){
            //get the deletebutton if there is one
            //if we are in the currency view
            //if not then skip
            deletebutton = Ext.getCmp("actionbuttons");
            
            //get the setdefaultbutton if there is one
            //if we are in the currency view
            //if not then skip
            archivebutton = Ext.getCmp("statusbuttons");
            
            if(count>0){
                if(deletebutton!=null){
                    deletebutton.setDisabled(false);
                }
                
                if(archivebutton!=null){
                    archivebutton.setDisabled(false);
                }
            }else{
                if(deletebutton!=null){
                    deletebutton.setDisabled(true);
                }
                
                if(archivebutton!=null){
                    archivebutton.setDisabled(true);
                }
            }
        }
    }
});