var accounts = {};

$(document).ready(function() { 

    $('#Accounts-grid-filter').change(function(){
    	accounts.grid.reload({params:{start:0,limit:$(this).val()}});
    });

	accounts.grid = new RichHTML.grid({
        el: 'Accounts-grid',
        pagingEl: 'Accounts-grid-pagination',
        buttonsEl: 'Accounts-grid-buttons',
        metaEl: 'Accounts-grid-metadata',
        url: 'index.php?fuse=clients&action=getPendingOrdersList',
        root: 'data',
        totalProperty: 'totalcount',
        baseParams: { sort: 'pendingpackagedate', limit: 5, dir: 'asc', limit: clientexec.records_per_view},
        
        columns: [{
                dataIndex:  "pendingpackageid",                
                xtype:      "checkbox"
            },
            {
                id:         'date',
                text:     "Date",
                width:      100,
                sortable: true,
                dataIndex:  "pendingpackagedate",
                renderer: 	function(text,row) {
                	var date = text.split(" ");
                	return date[0]+"<br/><span style='font-size:smaller;'>"+date[1]+"</span>";
                },
                align:      "right"
            },
            {
                id:         'customername',
                text:     "Name",
                renderer:   function(text,row) {
                	return String.format('<a href="index.php?fuse=clients&view=Admin_Profile_Contact&frmClientID={0}">{1}</a><br/><span style="font-size:smaller;">{2}</span>', row.pendingpackageuserid, text, row.pendingpackagereference);                	
                },
                hidden:     false,
                dataIndex:  "pendingpackagecustomername"
            }
            ,
            {
                id:         'name',
                text:     "Pkg Name",
                width:      150,
                dataIndex:  "pendingpackagename",
                align:      "center"
            },
            {
                id:         'status',
                text:     "Status",
                dataIndex:  "packagepaidstatus",
                width:      130,
                align:      "center"
            }

        ]
    });
    accounts.grid.render();
    
    // **** listeners to grid
    $(accounts.grid).bind({    
	    "load" : function(event,data) {
        	$('#activateAccount').attr('disabled','disabled');	      
        	$('#delAccount').attr('disabled','disabled');        	
	    },
	    "rowselect": function(event,data) {
	        if (data.totalSelected > 0) {
	        	$('#activateAccount').removeAttr('disabled');
	        	$('#delAccount').removeAttr('disabled');	        	
	        } else {
	        	$('#activateAccount').attr('disabled','disabled');
	        	$('#delAccount').attr('disabled','disabled');	        	
	        }
	    }
    });
    
    // **** lets bind our buttons    
    $('#activateAccount').click(function () { 
        if ($(this).attr('disabled')) { return false; } 
        var html = lang('Are you sure you want to activate the selected account(s)');
        html += "<br/><div style='padding-top:8px;'><input type='checkbox' name='useplugin' id='useplugin' checked='checked'/> <span style='border-bottom: 1px solid #DFDFDF;cursor:help;' qtip='Activate the selected accounts then trigger the create action from their respective plugin.  Warning: This will also register any domains you are activating.'>Use their respective plugins?</span></div>";
        RichHTML.msgBox(html, {
            type:"prompt",
            buttons: {  button1:{text:"yes"}, button2:{text:"no"} }
        }, function(result) {
            var usepackageplugin = 0;
            var useregistrarplugin = 0;
            if (typeof(result.elements.useplugin) !== "undefined") {
                usepackageplugin = 1; 
                useregistrarplugin = 1;
            }
            if(result.btn === "yes") {
                $.post("index.php?action=ActivateClientPackages&fuse=clients", {
                    domainids: accounts.grid.getSelectedRowIds(),
                    usepackageplugin: usepackageplugin,
                    useregistrarplugin: useregistrarplugin
                },
                function(data){
                    if ( data.error == true ) {
                        RichHTML.msgBox(data.message, { type: 'error'});
                    }
                    accounts.grid.reload({params:{start:0}});
                }, 'json');
            }
        });
    });
    
    $('#delAccount').click(function () { 
        if ($(this).attr('disabled')) { return false; }
        var html = lang('Are you sure you want to delete the selected account(s)');
        RichHTML.msgBox(html, {
            type:"prompt",
            buttons: {  button1:{text:"yes"}, button2:{text:"no"} }
        }, function(result) {
            if(result.btn === "yes") {
                $.post("index.php?action=DeleteClientPackages&fuse=clients", {domainids:accounts.grid.getSelectedRowIds()}, function(data){
                    if ( data.error == true ) {
                        RichHTML.msgBox(data.message, { type: 'error'});
                    }
                    accounts.grid.reload({params:{start:0}});
                }, 'json');
            }
        });
    });
});