<script>

function PerformAsyncActionOnCoupons(bolShowStatus,coupons,fuse,action,successfunc,extraargs){
    
    var successFunction = "onInvoiceActionSuccess";
    var couponsChecked = '';
    
    selItems = coupons.split(',');
    
    for(i=0;i<(selItems.length);i++){
        a = selItems[i];
        if(document.getElementById(a).checked){
            if(couponsChecked == ''){
                couponsChecked = a;
            }else{
                couponsChecked = couponsChecked+','+a;
            }
        }
    }
    
    if (bolShowStatus){
        setStatus('Working ...', 1000);
    }

    if (successfunc != null){
        successFunction = successfunc;
    }
    
    var args = 'couponsId='+couponsChecked;
    if(extraargs!="") args = args+"&"+extraargs;
    
    var opt = {
         method: 'post',
         postBody: args,
         onSuccess: eval(successFunction),
         on404: on404,
         onFailure: onFailure
    }
    if (action == 'DeleteCoupons'){
    
        var answer=confirm(lang('Deleting coupons will remove all related coupon history. \nPlease archive coupons if you wish to retain this information. \nDo you really want to delete the coupon? (OK = YES, CANCEL = NO)'));
        if (answer){
            new Ajax.Request('index.php?fuse='+fuse+'&action='+action, opt);
        }
    } else if (action == 'ArchiveCoupons_Async' || action == 'UnArchiveCoupons_Async'){
        new Ajax.Request('index.php?fuse='+fuse+'&action='+action, opt);
    }
}

function RemoveInvoicesSuccess(t){

    var msg = t.responseText.split('|')[0];
    var remove=true;
    
    if(msg.indexOf('**')>=0){
        msg = msg.substring(2);
        remove=false;
    }
    
    if(remove){
 
      var coupons = t.responseText.split('|')[2]; 
      var arrCoupons=coupons.split(',');
      var coupon_num=0;
      while (coupon_num < arrCoupons.length)
      {  
        em = $('coupon_'+arrCoupons[coupon_num]);  
        em.parentNode.removeChild(em);;  
        coupon_num+=1;  
      }
    }
    unsetStatus('',1);
    StatusMessageThenFade(msg, 5.0); //show what action was completed

    return;
}

function StatusMessageThenFade(strMessage, durationTime){
    if(bolUseEffects ){
        $('statusMsg').innerHTML=strMessage;
        new Effect.Appear($('statusContainer'));
        new Effect.Fade($('statusContainer'), {duration: durationTime,transition: Effect.Transitions.linear, from: 1.0, to: 0.0} );
    } else {
        $('errMessages').innerHTML = '<font class="red"><b>'+strMessage+'</b></font>';
    }
}

</script>

<form name="coupons_actions" method="POST">
    <table width=100% cellpadding=0 border=0 cellspacing=0>
        <tr>
            <td class="snapshot-header-left"></td>
            <td class="snapshot-header">
                <font class=snapshot-header-text>[l]Coupons list[/l]</font>
            </td>
            <td class="snapshot-header-right"></td>
        </tr>
    </table>
    <table width="l00%" class="sort-table" id="table-1" border="0" cellpadding="0" cellspacing="0">
        <col style="text-align: right" />
        <col />
        <col />
        <col />
        <col />
        <col />
        <col />
        <col />
        <col />
        <col />
        <col />
        <thead>
            <tr>
                <!-- BEGIN DYNAMIC BLOCK: couponHeaderCheckbox -->
                <td width="1%"></td>
                <!-- END DYNAMIC BLOCK: couponHeaderCheckbox -->
                <td align=center width="60" nowrap="true">[l]Name[/l]</td>
                <td align=left nowrap="true">[l]Description[/l]</td>
                <td align=left nowrap="true">[l]Code[/l]</td>
                <td align=left nowrap="true">[l]In Effect[/l]</td>
                <td align=left nowrap="true">[l]Discount[/l]</td>
                <td align=left nowrap="true">[l]Recurring[/l]</td>
                <td align=left nowrap="true">[l]Duration[/l]</td>
                <td align=left nowrap="true">[l]Applies to[/l]</td>
                <td align=left nowrap="true">[l]Billing Cycles[/l]</td>
                <td align=left nowrap="true">[l]Taxable[/l]</td>
            </tr>
        </thead>
        <tbody>
            <!-- BEGIN DYNAMIC BLOCK: coupon -->
                <tr id="coupon_{couponId}" class="{trClass}">
                    <!-- BEGIN DYNAMIC BLOCK: couponCheckbox -->
                    <td width="5"><input type="checkbox" style="position:relative;top:1px;" id="{couponId}" name="{couponId}" value="{couponId}" onClick="checkCheckBox(this);" /></td>
                    <!-- END DYNAMIC BLOCK: couponCheckbox -->
                    <td>{couponLabel}</td>
                    <td>{couponDescription}</td>
                    <td>{couponCode}</td>
                    <td>{couponInEffect}</td>
                    <td>{couponDiscount}</td>
                    <td>{couponRecurring}</td>
                    <td>{couponRecurringTime}</td>
                    <td>
                        <!-- BEGIN DYNAMIC BLOCK: appliesTo -->
                            {packageName}<br />
                        <!-- END DYNAMIC BLOCK: appliesTo -->
                    </td>
                    <td>{couponBillingCycles}</td>
                    <td>{couponTaxable}</td>
                </tr>
            <!-- END DYNAMIC BLOCK: coupon -->
        </tbody>
    </table>
    {NOCOUPONS}

    <!-- BEGIN DYNAMIC BLOCK: deleteBtn -->
    <br />
    <input type="checkbox" name="toggleAllChk" onclick="toggleAll(this.form);"/>
    <b>&nbsp;&nbsp;&nbsp;[l]Toggle All[/l]</b>
    <br /><br />
    <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr class="actionfooter">
            <td valign="middle" align="left" width="53" nowrap>
                <input id=btnDelete type="submit" class="button_medium" value="[lh]delete[/lh]" style="filter:alpha(opacity=50); -moz-opacity:0.4" disabled="true" onclick="{DELETE}" /> 
                <input id=btnDelete type="submit" class="button_large" value="{BUTTON_NAME}" style="filter:alpha(opacity=50); -moz-opacity:0.4" disabled="true" onclick="{COUPONS}" />
            </td>
        </tr>
    </table>
    <!-- END DYNAMIC BLOCK: deleteBtn -->
</form>
