var uninvoicedlist = {};

$(document).ready(function() {

           
    uninvoicedlist.renderDescription = function (value, record, el) {
    	var html, text = "";
    	    	
    	html  = "<b>[#"+record.invoiceentryid+']</b> <a href="javascript:ShowEditInvoiceEntryWindow('+record.invoiceentryid+',false, false, '+record.clientid+');" id="'+record.invoiceentryid+'" qtip="Edit this entry"><b>'+record.descriptionplain+'</b></a>';
    	customerName = String.format("<a href='index.php?fuse=clients&view=Admin_Profile_Contact&frmClientID={1}' style='text-shadow: none;color:#BD5A35'>{0}</a>",record.customername,record.clientid);    	
    	
	    if(record.includeinnextpayment==1){
            text = " <img style='position: relative;top: 3px;cursor: pointer;' src='../templates/admin/images/invoices/date_next.png' alt='attachment' qtip-s='This entry will be included in the next automatically generated invoice for package #"+record.appliestoid+"' />";
        }

    	html += "<br/><span>"+text+" for "+customerName;
    	 
    	if (record.appliestoid > 0) {
    		html += " charge applies to <a style='color:#BD5A35;' href='index.php?fuse=clients&frmClientID="+record.clientid+"&view=Admin_Profile_Products&packageid="+record.appliestoid+"'>product: #"+record.appliestoid+"</a>";
    	}
    	
    	html += "</span>"
    	    		  
    	return html;
    }
           
    $('#uninvoicedgrid-filter').change(function(){
    	uninvoicedlist.grid.reload({params:{start:0,limit:$(this).val()}});
    });
	           
           
	uninvoicedlist.renderDueDate = function (value, record, el) {
	    var html;
	    html  = "Due: <b>"+record.billdate+"</b>";
	    html += "<Br/>Price: "+record.billbalancedue;
	    
		return html;
	}           
           
    uninvoicedlist.grid = new RichHTML.grid({
        el: 'uninvoicedgrid',
        pagingEl: 'uninvoicedgrid-pagination',
        buttonsEl: 'uninvoicedgrid-buttons',
        metaEl: 'uninvoicedgrid-metadata',
        url: 'index.php?fuse=billing&action=GetUnInvoicedList',
        root: 'uninvoiced',
        totalProperty: 'totalcount',
		baseParams: { limit: 5, dir: 'asc', limit: clientexec.records_per_view},		
        columns: [
            {
                id:         "cb",
                dataIndex:  "invoiceentryid",                
                xtype:      "checkbox"
            },{
                id:         "description",
                text:     	lang("Description"),
                align:		"left",
                renderer:   uninvoicedlist.renderDescription,
                dataIndex:  "id",
                sortable: 	true
            }, {
	            id: 'billdate',
	            text: lang("Due Date"),
	            renderer: uninvoicedlist.renderDueDate,
	            dataIndex : "date",
	            width: 110,
	            align:"left",
                sortable: 	true
            }           
        ]
    });
    
    // **** listeners to grid
    $(uninvoicedlist.grid).bind({
	    "load" : function(event,data) {
        	$('#btnDelCharges').attr('disabled','disabled');
	    },
	    "rowselect": function(event,data) {
	        if (data.totalSelected > 0) {
	        	$('#btnDelCharges').removeAttr('disabled');
	        } else {
	        	$('#btnDelCharges').attr('disabled','disabled');
	        }
	    }
    });
    
    // **** lets bind our buttons
    $('#btnDelCharges').click(function () { 
    	if ($(this).attr('disabled')) { return false; }
    	RichHTML.msgBox(lang('Are you sure you want to delete the selected charges(s)'),
		{
			type:"prompt",
			buttons: {  button1:{text:"yes"}, button2:{text:"no"}, button3:{text:"cancel",type:"cancel"} }
		}, function(result) {
			if(result.btn === "yes") {
		    	uninvoicedlist.grid.mask();			
				$.post("index.php?fuse=billing&action=Admin_ActOnInvoices", {itemstype:'uninvoiced',actionbutton:'deletebutton',items:uninvoicedlist.grid.getSelectedRowIds()},
				   function(data){
   						uninvoicedlist.grid.reload({params:{start:0}});
				});
			}
		});
    });
    
    
    uninvoicedlist.grid.render();

});