var gridstore;

Ext.onReady(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();
              }
        }
    });

    var grid = new Ext.grid.GridPanel({
        el:'domains-grid',
		autoHeight:true,
        store: gridstore,
        trackMouseOver:true,
        disableSelection:true,
        loadMask: true,

        // grid columns
        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)
    {
        
        var arrIds = GetSelectedRowIds();
        var strItems = "";
        
        for (x=0; x <= arrIds.length; x++) {
            if (arrIds[x] != undefined)
              strItems += arrIds[x] + " ";
        }
        //setStatus('Importing...');
    	var opt = {
    		method: 'post',
    		postBody: 'domains='+strItems+'&registrar='+$('selectRegistrar').options[$('selectRegistrar').selectedIndex].value,
    		onSuccess: importDomainsSuccess
    	}		
    	new Ajax.Request('index.php?fuse=domains&action=ImportDomain', opt);
    }
    

});

function importDomainsSuccess(t)
{
    $('domains_results').innerHTML = t.responseText;
    unsetStatus('', 1);
}