/*  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;
    
    Ext.Ajax.request({
        url: 'index.php?fuse='+fuse+'&action='+action,
        success: eval(successFunction),
        params: args,
        method:'POST'
    });
}

function PayInvoice(invoice,action,fuse)
{

    var radioGrp = document['forms']['paymentmethodform']['paymentmethod'];

    if(radioGrp.length == undefined){
        if (radioGrp.checked == true) {
            var radioValue = radioGrp.value;
        }else{
            document.getElementById('errMessages').innerHTML = '<font class="redtext"><b>'+lang('You need to select a payment method')+'</b></font>';
            return;
        }
    }else{
        for(i=0; i < radioGrp.length; i++){
            if (radioGrp[i].checked == true) {
                var radioValue = radioGrp[i].value;
            }
        }
    }

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

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

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