Ext.onReady(function(){

	// Create the add button
    var addServer = new Ext.Button({
                id:"addServer",
                renderTo:"addserver",
                text: lang('Add Server'),
                iconAlign: 'left',
                handler:function(){
                        closing = false;
                        update = false;
                        addServers();
                }
            }
        );

	
    
    
    // Get and process the JSON data
    servers_store = new Ext.data.JsonStore({
        id: "servers_store",
        root: 'data',
        idProperty: 'id',
        remoteSort: true,
        fields: [
            'id','name','hostname', 'sharedip', 'plugin', 'domains_quota','isdefault'
        ],
        proxy: new Ext.data.HttpProxy({
            url: 'index.php?fuse=admin&action=GetServers'
        })
    });
    servers_store.setDefaultSort('name', 'asc');
    servers_store.load();

    
    
    
    
    // Function to put together the link for the open window
    function renderServerName(value,p,record){
        var link = "<a href='javascript:editServer(" + record.data.id + ");'>" + record.data.name + "</a>";
        return link;
    }
    
    
    var sm = new Ext.grid.CheckboxSelectionModel({
        id:'tld-sm',
        width: 30,
        listeners:{
             'rowdeselect': function(model, rowIndex, record){
                    toggleButtons();
              },
              'rowselect': function(model, rowIndex, record){
                    toggleButtons();
              }
        }
    });
    
    // Create the grid
    var servergrid = new Ext.grid.GridPanel({
    	id: 'servergrid',
    	store: servers_store,
        renderTo: 'servers-list',
        autoExpandColumn: 'name',
        autoHeight:true,
        emptyMsg: lang("There are no Servers available. Use the button above to add one."),
        stripeRows: true,
        columns: [sm,
                    {
                        id: 'name',
                        header: 'Server Name',
                        dataIndex: 'name',
                        width: 99,
                        renderer: renderServerName,
                        menuDisabled:true
                    },{
                        header: 'Hostname',
                        dataIndex: 'hostname',
                        align: 'left',
                        menuDisabled:true,
                        width: 250
                    },{
                        header: 'Shared IP',
                        dataIndex: 'sharedip',
                        align: 'center',
                        menuDisabled:true,
                        width: 130
                    },{
                        header: 'Plugin',
                        dataIndex: 'plugin',
                        align: 'center',
                        menuDisabled:true,
                        width: 120
                    },{
                        header: 'Quota',
                        dataIndex: 'domains_quota',
                        align: 'center',
                        menuDisabled:true,
                        width: 100
                    }

                ],
       sm: sm,
       
       buttons:[deletebutton = new Ext.Button({
           id:'actionbuttons',
           autoHeight:true, 
           xtype:'button',
           text:'Delete',
           disabled: true,
           onClick: function(record){
                  if(!deletebutton.disabled){
                      requesturl = "index.php?fuse=admin&action=DeleteServer";
                      ProcessAction(requesturl, 'delete');
                  }
              }
           }), defaultbutton = new Ext.Button({
           id:'defaultbutton',
           autoHeight:true,
           xtype:'button',
           text:'Make Default',
           disabled: true,
           onClick: function(record){
                  if(!defaultbutton.disabled){
                      requesturl = "index.php?fuse=admin&action=DefaultServer";
                      ProcessAction(requesturl, 'default');
                  }
              }
           })
       ],
       buttonAlign:'left'
    });
    
    // option button code
    function toggleButtons(count){
        if(count == undefined){
            count = sm.getCount();
        }
        if(count>0){
            deletebutton.setDisabled(false);
        }else{
            deletebutton.setDisabled(true);
        }

        if(count == 1) {
            defaultbutton.setDisabled(false);
        } else {
            defaultbutton.setDisabled(true);
        }
    }
      
    function GetSelectedRowIds(){
        var arrayofids = new Array();
        rows = sm.getSelections();
	    for(x=0;x<rows.length;x++){
	       arrayofids[x] = rows[x].data.id;
	    }
	    return arrayofids;
    }

    function ProcessAction(requesturl, action){
    	servergrid.disable();
	    Ext.Ajax.request({
           url: requesturl,
           success: function(xhr) {
                //refreshgrid
	    		servergrid.enable();
                servers_store.load({params:{start:0}});
                toggleButtons(0);
                
                var data = Ext.util.JSON.decode(xhr.responseText);
                if(data.message){
                    msg(data.message,"");
                }else{
                    if(action == 'delete') {
                        msg(lang("The selected Servers have been removed."),"");
                    } else {
                        msg(lang("The selected Server has been made default."),"");
                    }
                } 
           },
           failure: function() {
        	   tldgrid.enable();
                msg(lang("There was an error performing this action"),"");
           },
           params: { 
            ids: Ext.util.JSON.encode(GetSelectedRowIds())
         }
       });
	    
    }


    
    
    
    
    
    
});

