/*  Javascript functions for Invoice Module
 *  12/22/2005 Initial creation
/*--------------------------------------------------------------------------*/

/*********************************/
/* Used in sectionInvoices.tpl   */
/*********************************/
var invoicesSelected = 0;
var selectedInvoiceId = 0;
var lastSelectedInvoiceList = "";



function RefreshPage(t){
    location.href = location.href;
}

function onInvoiceActionSuccess(t){

    if(t.responseText=="") return;
    unsetStatus('',1); //hide working message on top right

    var msg = t.responseText.split('|')[0];
    if(msg!=="") alert(msg);
    window.location.reload(true);
}

//General function to shoot out the async function performed on invoices
function PerformAsyncActionOnInvoices(bolShowStatus,invoices,fuse,action,successfunc,extraargs){
    var successFunction = "onInvoiceActionSuccess";

    if (bolShowStatus){
        setStatus(lang('Working ...'));
        //DisableInvoiceButtons();
    }

    if (successfunc != null){
        successFunction = successfunc;
    }

    var args = 'invoices='+invoices;
    if(extraargs!="") args = args+"&"+extraargs;

    var opt = {
         method: 'post',
         postBody: args,
         onSuccess: eval(successFunction)
    }
    new Ajax.Request('index.php?fuse='+fuse+'&action='+action, opt);
}

function PayInvoice(invoice,action,fuse)
{

    if (pluginArrCC[$RF('paymentmethodform', 'paymentmethod')] == null) {
        // 3rd party plugin -- forward customer
        location.href = "index.php?fuse="+fuse+"&action="+action+"&invoiceid="+invoice+"&plugin="+$RF('paymentmethodform', 'paymentmethod');
    } else {
        if ($('ccnumber').value == '') {
            $('errMessages').innerHTML = '<font class="redtext"><b>'+lang('Missing credit card information')+'</b></font>';
            return;
        }
        if (pluginArrCVV[$RF('paymentmethodform', 'paymentmethod')] == 1 && $('cvv2').value == '') {
            $('errMessages').innerHTML = '<font class="redtext"><b>'+lang('Missing CVV2 information')+'</b></font>';
            return;
        }
        // Merchant plugin -- async process payment
        args = 'invoiceid='+invoice+'&ccnumber='+$('ccnumber').value+'&ccyear='+$('ccyear').value+'&ccmonth='+$('ccmonth').value+'&cvv2='+$('cvv2').value;
        PerformAsyncActionOnInvoices(true,invoice,'billing','PayInvoice_Async&plugin='+$RF('paymentmethodform', 'paymentmethod'),null,args);
    }
}

function refreshCCSection(objvalue)
{
    var paymentmethod = objvalue;

    //hide all other cc elements that might be open
    var els=document.getElementsByName("ccinfo");
    for(var x=0;x<els.length;x++){
        els[x].style.display = "none";
    }

    if (pluginArrCC[paymentmethod] == null) {
        cleanUpCCSection();
    } else {
        if (pluginArrCVV[paymentmethod] == 1) {
            $('cvv2info').style.display = '';
        } else {
            $('cvv2').value = '';
            $('cvv2info').style.display = 'none';
        }

        //$('ccinfo').style.display = '';
        $('ccinfo_'+paymentmethod).innerHTML = $('ccinfo').innerHTML;
        $('ccinfo_'+paymentmethod).style.display = '';
    }
}

function cleanUpCCSection()
{
    $('ccinfo').style.display = 'none';
}
