
var client_staffNotes;
var clientNotesWin;
var showtoClients;
var associatedWithTickets;
var ticketTypes;

CE.prototype.showClientNotes = function(){
    client_staffNotes.reset();
    //defineTicketTypes();
    showtoClients.reset();
    associatedWithTickets.reset();
    ticketTypes.hide();
    clientNotesWin.show();
    clientNotesWin.buttons[0].setDisabled(false);
    client_staffNotes.focus('',300);
}

Ext.onReady(function(){
    //staff notes
    client_staffNotes = new Ext.form.TextArea({
        id:         "ce-client_staffNotes",
        border:     false,
        hideLabel:  true,
        anchor:     '100%',
        height:180,
        width:480,
        name:       'ce-client_staffNotes'
    });

    showtoClients = new Ext.form.Checkbox({
        id: 'showToClients',
        boxLabel : lang('Visible to customer'),
        name: 'showToClients'
    });

    associatedWithTickets = new Ext.form.Checkbox({
        id: 'associatedWithTickets',
        boxLabel : lang('Associated with tickets'),
        name: 'associatedWithTickets',
        listeners : {
            check : function(){
                if(associatedWithTickets.checked == true){
                    ticketTypes.show();
                    ticketTypes.addSelectAllItem = true;
                    ticketTypes.allSelected = false;
                }else{
                    ticketTypes.hide();
                }
            }
        }
    });
    
    ticketTypeStore = new Ext.data.JsonStore( {
        storeId: 'comboSubstrate_store',
        root: 'groups',
        url: 'index.php?fuse=support&action=GetTicketTypeList&addAll=1',
        idProperty: 'ticketTypeId',
        fields:  [ 'ticketTypeId', 'ticketTypeName']
    });

    ticketTypes = new Ext.ux.form.LovCombo({
         id:'tickettypelovcombo',
         hideOnSelect:true,
         hidden : true,
         maxHeight:200,
         editable:false,
         value: "ALL",
         store: ticketTypeStore
        ,displayField:'ticketTypeName'
        ,valueField:'ticketTypeId'
        ,triggerAction:'all'
        ,mode:'remote',
        beforeBlur : Ext.emptyFn
    });//ticketTypes.initComponent();

    //show different window if loading from Notes.js or the sitewide

    clientNotesWin = new Ext.Window({
        id: "clientNotesWindow",
        width: 500,
        height:300,
        modal: true,
        title:"Add Staff Note",
        closeAction: 'hide',
        plain: true,
        items : [
                client_staffNotes,
                {
                    layout: 'column',
                    plain:true,
                    items:[{
                        border:false,
                        columnWidth:.5,
                        style: "padding-left:10px;",
                        height:100,
                        items:[
                            associatedWithTickets,
                            ticketTypes
                        ]
                    },{
                        border:false,
                        columnWidth:.5,
                        items:[showtoClients]
                    }]
                }
                
            ],
        buttons: [{
            text : 'Add',
            disabled:true,
            handler:function(){
                updateClientNotes();
                ticketTypes.setValue("");
            }
        },{
            text: 'Close',
            handler:function(){
                clientNotesWin.setTitle("Add Staff Note");
                clientNotesWin.buttons[0].setDisabled(true);
                clientNotesWin.hide();
                ticketTypes.setValue("");
            }
        }]
    });

    function updateClientNotes(){
        if(client_staffNotes.getValue().trim() == ""){
            Ext.Msg.alert("Please fill the notes");
            return;
        }
        var ticketTypesArr = Ext.getCmp('tickettypelovcombo').getValue().split(",");
        var visibleClients = "false";
        if(showtoClients.getValue()){
            visibleClients = showtoClients.getValue();
        }
        Ext.Ajax.request({
           url: 'index.php?fuse=clients&action=saveClientNote',
           success: function(xhr) {
                        if(typeof(notes_gridstore) != "undefined"){
                            notes_gridstore.load({params:{start:0}});
                        }
                        msg("Note was added");
                    },
           failure: function() { },
           params: {
                    customerGroup:0,
                    noteContent : client_staffNotes.getValue(),
                    visibleCustomer : visibleClients,
                    associateTicketTypes : associatedWithTickets.getValue(),
                    encoded : true,
                    ticketTypes : Ext.util.JSON.encode(ticketTypesArr)},
           method:'post'
        });
        showtoClients.reset();
        associatedWithTickets.reset();
        clientNotesWin.setTitle("Add Staff Note");
        clientNotesWin.buttons[0].setDisabled(true);
        clientNotesWin.hide();
    }

});