/*!
 * Ext JS Library 3.0+
 * Copyright(c) 2006-2009 Ext JS, LLC
 * licensing@extjs.com
 * http://www.extjs.com/license
 */

var AccountsWin;

function ShowEditAccountsWindow(id){
        AccountsWin.CreateWindow(true);
        AccountsWin.PopulateFields(id);
}

Ext.onReady(function(){
    var win;

    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: 330},
        //defaultType: 'textfield',

        items: [{
                fieldLabel: 'Id',
                name: 'id',
                xtype: "hidden"
            },{
                fieldLabel: lang('E-mail'),
                type: 'textfield',
                xtype: "textfield",
                name: 'email',
                vtype:'email',
                anchor:'100%',
                allowBlank:false
            },
//            {
//                xtype: 'radiogroup',
//                itemCls: 'x-check-group-alt',
//                fieldLabel: 'Options',
//                allowBlank: false,
//                items: [{
//                    columnWidth: '.4',
//                    items: [
//                        {xtype: 'label', text: lang('Notifications'), cls:'x-form-check-group-label', anchor:'-15'},
//                        {boxLabel: lang('Yes'), name: 'optionnotification', id:'optionnotificationyes', inputValue: 1, checked: true},
//                        {boxLabel: lang('No'), name: 'optionnotification', id:'optionnotificationno', inputValue: 0}
//                    ]
//                },{
//                    columnWidth: '.3',
//                    items: [
//                        {xtype: 'label', text: lang('Invoices'), cls:'x-form-check-group-label', anchor:'-15'},
//                        {boxLabel: lang('Yes'), name: 'optioninvoice', id:'optioninvoiceyes', inputValue: 1, checked: true},
//                        {boxLabel: lang('No'), name: 'optioninvoice', id:'optioninvoiceno', inputValue: 0}
//                    ]
//                },{
//                    columnWidth: '.3',
//                    items: [
//                        {xtype: 'label', text: lang('Support'), cls:'x-form-check-group-label', anchor:'-15'},
//                        {boxLabel: lang('Yes'), name: 'optionsupport', id:'optionsupportyes', inputValue: 1, checked: true},
//                        {boxLabel: lang('No'), name: 'optionsupport', id:'optionsupportno', inputValue: 0}
//                    ]
//                }]
//            },



            {
                layout:     'column',
                border:     false,
                labelAlign: 'left',
                items:  [
                    {
                        columnWidth: .4,
                        layout:       'form',
                        border:       false,
                        items: [
                            {
                                fieldLabel: lang('Notifications'),
                                hideLabel:        true,
                                boxLabel:         lang('Notifications'),
                                id:'optionnotification',
                                name: 'optionnotification',
                                xtype: "checkbox",
                                allowBlank:true,
                                anchor:'100%'
                            }
                        ]
                    },{
                        columnWidth: .3,
                        layout:       'form',
                        border:       false,
                        items: [
                            {
                                fieldLabel: lang('Invoices'),
                                hideLabel:        true,
                                boxLabel:         lang('Invoices'),
                                id:'optioninvoice',
                                name: 'optioninvoice',
                                xtype: "checkbox",
                                allowBlank:true,
                                anchor:'100%'
                            }
                        ]
                    },{
                        columnWidth: .3,
                        layout:       'form',
                        border:       false,
                        items: [
                            {
                                fieldLabel: lang('Support'),
                                hideLabel:        true,
                                boxLabel:         lang('Support'),
                                id:'optionsupport',
                                name: 'optionsupport',
                                xtype: "checkbox",
                                allowBlank:true,
                                anchor:'100%'
                            }
                        ]
                    }
                ]
            },{
                fieldLabel: lang('Migrate support tickets from guest account'),
                hideLabel:        true,
                boxLabel:         lang('Migrate support tickets from guest account'),
                id:'optionmigrate',
                name: 'optionmigrate',
                xtype: "checkbox",
                allowBlank:true,
                anchor:'100%'
            }
        ]
    });

    function TableTypeWindow(update){
        if(this instanceof TableTypeWindow){
            this.update = update;
            this.CreateWindow = createWindow;
            this.PopulateFields = populateFields;
        }else{
            return new TableTypeWindow();
        }
    }
    AccountsWin = new TableTypeWindow();

    function populateFields(id){
        geturl = 'index.php?fuse=clients&action=Admin_GetClientAlternateAccounts&id=' + id;
        simpleForm.getForm().load({
            url:      geturl,
            waitMsg:  'Loading'
        });
    }

    function createWindow(update){
        if(update){
            requesturl = 'index.php?fuse=clients&action=Admin_UpdateProfileAccounts';
            buttonText = 'Update';
            titleText = '&nbsp;&nbsp;'+lang('Edit Alternate Account');
        }else{
            requesturl = 'index.php?fuse=clients&action=Admin_AddProfileAccount';
            buttonText = 'Save';
            titleText = '&nbsp;&nbsp;'+lang('Add Alternate Account');
        }

        // 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:160,
                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,
                    success: function(form, action) {
                       if(action != null && action.result != null && action.result.message != null){
                          Ext.Msg.alert('Success', action.result.message);
                       }
                       win.hide();
                       //get the grid if there is one
                       //if we are in the billingtype view
                       //if not then skip
                       grid = Ext.getCmp("accountsgrid");
                       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.message);
                               break;
                       }
                    }

                });
            });
        //}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("deletebutton");

            if(count>0){
                if(deletebutton!=null){
                    deletebutton.setDisabled(false);
                }
            }else{
                if(deletebutton!=null){
                    deletebutton.setDisabled(true);
                }
            }
        }
    }
});