Ext.onReady(function(){
    
    
});

var passphrase;
var processDueInvoices;
var nonautopayment;
var autopayment;
var initial = 0;

function startProcessingInvoices(tPassphrase, tProcessDueInvoices, tNonAutopayment, tAutopayment){
    Ext.MessageBox.show({
       title: 'Please wait',
       msg: 'Processing Invoices...',
       progressText: 'Initializing...',
       width:300,
       progress:true,
       closable:false,
       animEl: 'mb6'
   });

   //set variables for this transaction
   passphrase = tPassphrase;
   processDueInvoices = tProcessDueInvoices;
   nonautopayment = tNonAutopayment;
   autopayment = tAutopayment;

   pollingInvoiceGenerator();
}

function goCompleted(){
    this.location="index.php?fuse=admin&view=ProcessInvoices&phase=CompletedRunningRecurringActions";
    Ext.MessageBox.hide();
}

function pollingInvoiceGenerator(){
    //start ajax call until I get back a reason to quit
    Ext.Ajax.request({
       url: "index.php?fuse=admin&action=ProcessInvoice",
       success: function(xhr) {
           var res = Ext.util.JSON.decode(xhr.responseText);
           //alert(dump(res));
           //do we need to update global vars
           percentfraction = res.percentage;
           res.percentage = percentfraction*100;
           if (res.doaction == "close") {
              Ext.MessageBox.updateText("<span style='font-size:10px;'>Completed</span>");
              Ext.MessageBox.updateProgress(percentfraction, parseInt(res.percentage)+'% completed');
              timerID = setTimeout("goCompleted();",res.timer);
           } else if (res.doaction == "gonext") {
              initial = res.initial;
              Ext.MessageBox.updateText("<span style='font-size:10px;'>"+res.statusline + " invoice(s):"+res.invoice+"</span>");
              Ext.MessageBox.updateProgress(percentfraction, parseInt(res.percentage)+'% completed');
              timerID = setTimeout("pollingInvoiceGenerator();",res.timer);
           } else {
               ce.log(1, "returned from ProcessInvoice with invalide doaction")
           }
       },
       failure: function() {
           msg(lang("There was an error performing this action"),"");
       },
       params: {
           autopayment: autopayment,
           nonautopayment: nonautopayment,
           initialnum: initial,
           passphrase:passphrase,
           processDueInvoices:processDueInvoices
       }
   });
}

function ShowPassPhrase(){
    if (!document.getElementById)  return
    which = document.getElementById('passphrasediv');
    which.style.display="";
}

function HidePassPhrase(){
    if (!document.getElementById)  return
    which = document.getElementById('passphrasediv');
    which.style.display="none";
}

function SubmitForm(){
        document.forms['generateinvoice'].submit();
}