var gridstore;

$(document).ready(function() {

    gridstore = new Ext.data.JsonStore({
        id: "gridstore",
        root: 'domains',
        totalProperty: 'totalcount',
        idProperty: 'id',
        remoteSort: false,
        baseParams: {
            registrar: "--",
            page: "1"
        },
        fields: [
        'id','domain','expiry'
        ],
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
            url: 'index.php?fuse=domains&action=GetDomainList'
        })
    });
	
    var gridbb = new Ext.PagingToolbar({
        store: gridstore,
        displayInfo: true,
        displayMsg: lang('Displaying domains') +' {0} - {1}' + ' of ' + '{2}',
        emptyMsg: lang("No domains to display"),
        listeners:{
            'change': function(bb,data){
                if(data.total == 0) {
                    var addBy = 0;
                } else {
                    var addBy = 1;
                }
                from = (data.activePage * bb.pageSize) - bb.pageSize + addBy;
                to = from+ bb.pageSize - 1;
                if(to>data.total) to = data.total;
            }
        }
    });

    var sm = new Ext.grid.CheckboxSelectionModel({
        listeners:{
            'rowdeselect': function(model, rowIndex, record){
                toggleButtons();
            },
            'rowselect': function(model, rowIndex, record){
                toggleButtons();
            }
        }
    });

    grid = new Ext.grid.GridPanel({
        el:'domains-grid',
        autoHeight:true,
        store: gridstore,
        trackMouseOver:true,
        disableSelection:true,
        loadMask: true,
        columns:[
        sm,
        {
            id: 'id', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
            header: lang("Id"),
            width: 50,
            dataIndex : "id",
            sortable: false,
            hidden: true,
            align:"left"
        },{
            id: 'domain', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
            header: lang("Domain"),
            width: 320,	
            dataIndex : "domain",
            sortable: false
        },{
            id: 'expiry', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
            header: lang("Expiry Date"),
            width: 100,	
            dataIndex : "expiry",
            sortable: false
        }
        ],
        stripeRows: true,
        // customize view config
        viewConfig: {
            forceFit:true,
            enableRowBody:true,
            emptyText: lang("There are currently no domains to view"),
            scrollOffset:1
        },
        sm: sm,
        // inline buttons
        buttons:[actionbuttons = new Ext.Button({
            id:'actionbuttons',
            autoHeight:true,
            arrowAlign:'right',
            xtype:'button',
            text:'Import',
            disabled: true,
            handler: importDomains
        })
        ],
        buttonAlign:'left',
        // paging bar on the bottom
        bbar: gridbb
    });
    
    // render it
    grid.render();
    gridbb.pageSize = 25;
    gridstore.setBaseParam('limit', 25);
    gridstore.load({
        params:{
            start:0
        }
    });
    
    
// option button code
function toggleButtons(count){
    if(sm.getCount()>0){
        actionbuttons.setDisabled(false);
    }else{
        actionbuttons.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 importDomains(button, event)
{
    // Disable the grid
    grid.setDisabled(true);
    
    var arrIds = GetSelectedRowIds();
    var strItems = "";
        
    for (x=0; x <= arrIds.length; x++) {
        if (arrIds[x] != undefined)
            strItems += arrIds[x] + " ";
    }
    
    Ext.Ajax.request({
        url: 'index.php?fuse=domains&action=ImportDomain&domains='+strItems+'&registrar='+document.getElementById('selectRegistrar').options[document.getElementById('selectRegistrar').selectedIndex].value,
        success: importDomainsSuccess,
        failure: function() {
            Ext.Msg.show({
                title: lang('Error'),
                msg: lang('There was an error trying to import those domains.'),
                buttons: Ext.Msg.OK,
                icon: Ext.MessageBox.WARNING
            });
            grid.setDisabled(false);
        }
    });
}
    

});

function importDomainsSuccess(response)
{
    document.getElementById('domains_results').innerHTML = response.responseText;

    gridstore.load();
    grid.setDisabled(false);

    //unsetStatus('', 1);
}