<script language="javascript" type="text/javascript">
// <![CDATA[

    //keep track of what customer's old value for paypal subscription
    //was so that we can show the proper warning when updating
    var old_subscriptionyes=false;
    var selectedplugin='{selectedpluginname}';
    var selectedpluginrealname='{selectedpluginrealname}';


    function DeleteCC(){
        //calls the ajax action that returns the fields
        //then call category with edit
        setStatus(lang("Deleting Credit Card"));

        var post;

        new Ajax.Request(   'index.php?fuse=clients&action=DeleteCCNumber',
            {
                postBody    : post,
                onSuccess   : function(t,json)
                {
                   unsetStatus('', 1);
                   if(json.alertmessage!=""){
                       alert(json.alertmessage);
                   }

                   window.location = "index.php?fuse=clients&view=ViewProfile_Billing";
                }
            }
        );

    }


    function SubscriptionConfirmation(new_subscriptionvalue,paymentmethodchanged){
        //scenario one

        var msg;
        if( (old_subscriptionyes) && (new_subscriptionvalue==0) && (!paymentmethodchanged) ){
            msg = lang("Click ok to confirm canceling your subscription through "+selectedpluginrealname+". Submit a support ticket if you wish to cancel your service with us.");
            a=confirm(msg);
            if(a) return true;
        }else if( (old_subscriptionyes) && (paymentmethodchanged) ){
            msg = lang("Changing payment method will cancel your subscription through "+selectedpluginrealname+". Click ok to confirm.");
            a=confirm(msg);
            if(a) return true;
        }else if( (!old_subscriptionyes) && (new_subscriptionvalue==1) ){
            msg = lang("Click ok to confirm. Your subscription with "+selectedpluginrealname+" will be initiated after paying your next invoice.");
            a=confirm(msg);
            if(a) return true;
        }
        return false;
    }

    function SaveBillingInformation(){
        //save billing information
        

        var post="dummy=dummy";
        var paymentmethodchanged=false;

                
        if($('paymenttype')!=null){
            post += "&paymenttype="+$('paymenttype').value;
            if($('paymenttype').value!=selectedplugin) paymentmethodchanged=true;
        }else{
            post += "&paymenttype="+selectedplugin;
        }


        if($('ccyear')!=null){
            post += "&ccyear="+$('ccyear').value;
        }
        if($('ccmonth')!=null){
            post += "&ccmonth="+$('ccmonth').value;
        }

        if($('newccnumber')!=null){
            if( ($('ccnumber')!=null) && ($('ccnumber').value!="") ){
                post += "&ccnumber="+$('ccnumber').value;
            }else{
                post += "&ccnumber="+$('newccnumber').value;
            }
        }

        if(old_subscriptionyes){
            post += "&old_subscription=1";
        }else{
            post += "&old_subscription=0";
        }

        post += "&old_selectedplugin="+selectedplugin;
        var new_subscriptionvalue = $RF("billingform","recurring_paypal");

        //only check if we are not showing cc information
        if( (new_subscriptionvalue!=null) && ($('creditcardinfo').style.display=="none") ){

            post += "&new_subscription="+new_subscriptionvalue;
            if(old_subscriptionyes!=new_subscriptionvalue){
                if(!SubscriptionConfirmation(new_subscriptionvalue,paymentmethodchanged)){
                    return false;
                };
            }
        }else{

            post += "&new_subscription=0";
            if( (old_subscriptionyes) && (paymentmethodchanged) ){
                if(!SubscriptionConfirmation(new_subscriptionvalue,paymentmethodchanged)){
                    return false;
                };
            }
        }

        setStatus(lang("Update billing information"));

        new Ajax.Request(   'index.php?fuse=clients&action=UpdateBilling',
            {
                postBody    : post,
                onSuccess   : function(t,json)
                {
                   unsetStatus('', 1);
                   if(json.error!=""){
                       alert(json.error);
                       return false;
                   }
                   
                   window.location = "index.php?fuse=clients&view=ViewProfile_Billing";

                }
            }
        );
    }

     function GetCCInformation(selObj){


        setStatus(lang("Retrieving Credit Card Information"));

        var post="plugin="+selObj;
        new Ajax.Request(   'index.php?fuse=clients&action=GetPaymentPluginDetails',
            {
                postBody    : post,
                onSuccess   : function(t,json){

                    unsetStatus('', 1);

                    if(json.alertmessage!=""){
                        alert(json.alertmessage);
                        return;
                    }

                    
                    //show subscription options
                    if(json.showsubscriptionoptions){
                        
                        $('use_recurring').style.display = 'block';
                        
                        if(json.subscriptionyes){
                            $('subscriptionyes').checked = true;
                            old_subscriptionyes = true;
                        }else{
                            old_subscriptionyes = false;
                        }
                    }else{
                        $('use_recurring').style.display = 'none';
                    }

                    //show cc info
                    if(json.showccoptions){
                        $('creditcardinfo').style.display = 'block';
                    }else{
                        $('creditcardinfo').style.display = 'none';
                        return;
                        //return because the remaining options are only for cc
                    }


                    if(json.awaitingvalidation!=""){
                        $('awaitingvalidation').innerHTML = json.awaitingvalidation;
                        $('awaitingvalidationspan').style.display = '';
                        $('ccnumberspan').style.display = 'none';
                        $('deleteccspan').style.display = 'none';
                        $('newccspan').style.display = "none";
                    }else{
                        if(json.last4!=""){
                            $('newccspan').style.display = "";
                            $('ccnumberspan').style.display = 'none';
                            $('lastfourspan').style.display = '';
                            $('lastfour').innerHTML = "xxxxxxxxxxxx"+json.last4;
                            $('deleteccspan').style.display = '';  
                        }else{
                            $('newccspan').style.display = "none";
                            $('ccnumberspan').style.display = '';
                            $('lastfourspan').style.display = 'none';
                            $('lastfour').innerHTML = "";
                            $('deleteccspan').style.display = 'none';
                            
                        }
                        $('awaitingvalidationspan').style.display = 'none';
                    }
                    
                    //if cc then which cards do we show
                    $('visa_logo').style.display = json.visastyle;
                    $('mastercard_logo').style.display = json.mastercardstyle;
                    $('americanexpress_logo').style.display = json.americanexpressstyle;
                    $('discover_logo').style.display = json.discoverstyle;
                    //for card validation
                    $('validccbits').value= json.visabit+''+json.mcbit+''+json.amexbit+''+json.discbit;
                }
            }
        );
     }


// ]]>
</script>

<div id="kb" class="block">
<div class="block_title">
<h3><b><a href="#">[l]Support Center[/l]</a> &raquo; <a href="#">[l]Profile[/l]</a></b></h3>
</div>
<ul>
<li>
<h1>[l]Client Payment Options[/l]</h1>
<p class="kb st" style="background: url(templates/{TEMPLATE}/images/public/icon_submit_ticket.jpg) no-repeat 0 8px;">[l]Please keep your profile up to date.  You can use this section to update your billing preferences, alternatate contact information and update your password.[/l]</p>
</li>
<li>

    <div id="cip">
        <div class="nav_1">
            <ul>
                <li>
                    <a href="index.php?fuse=clients&amp;view=ShowCustomerData"><span>[l]Profile[/l]</span></a>
                </li>
                <li>
                    <a href="index.php?fuse=clients&amp;view=ViewProfile_AlternateEmails"><span>[l]Alternate Emails[/l]</span></a>
                </li>
                <li class="selected">
                    <a href="#"><span>[l]Billing[/l]</span></a>
                </li>
                <li>
                    <a href="index.php?fuse=clients&amp;view=ViewProfile_Password"><span>[l]Password[/l]</span></a>
                </li>
		<li>
                <a href="index.php?fuse=clients&amp;view=ViewProfile_StaffNotes"><span>[l]Notes[/l]</span></a>
              </li>

            </ul>
        </div>
        <br /><br />
    </div>



<!-- BEGIN DYNAMIC BLOCK: billing -->
<div style="padding-left: 15px;">

    <!-- BEGIN DYNAMIC BLOCK: caneditpaymenttype -->
    <span class="label130">[l]Payment Type[/l]:</span>
    <select name="paymenttype" id="paymenttype" onchange="GetCCInformation(this.options[this.selectedIndex].value); return false;">
        <!-- BEGIN DYNAMIC BLOCK: paymentTypeOption -->
        <option value="{paymentTypeOptionValue}" {paymentTypeOptionSelected} >{paymentTypeOptionLabel}</option>
        <!-- END DYNAMIC BLOCK: paymentTypeOption -->
    </select>
    <!-- END DYNAMIC BLOCK: caneditpaymenttype -->
    <!-- BEGIN DYNAMIC BLOCK: cannoteditpaymenttype -->
    <span class="label130">[l]Payment Type[/l]:</span>
    {paymentTypeOptionLabel}
    <!-- END DYNAMIC BLOCK: cannoteditpaymenttype -->
    

    <form method="post" id="billingform" name="billingform" action="index.php?fuse=clients&amp;action=UpdateBilling">

    <div id="use_recurring" style="display:none;">
        <span class="label130">[l]Use Subscription[/l]:</span>
        <span>
        <input type="radio" id="subscriptionno" name="recurring_paypal" value="0" checked="checked" />[l]No[/l]&nbsp;&nbsp;
        <input type="radio" id="subscriptionyes" name="recurring_paypal" value="1" />[l]Yes[/l]&nbsp;&nbsp;
        </span>
    </div>
    
    <div id="creditcardinfo" style="display:none;">
        <span class="label130">[l]Credit Card Number[/l]:</span>


        <span id="lastfourspan">
            <span id="lastfour"></span>
        </span>

        <span id="deleteccspan" style="padding-left:10px;">
            <input type="button" id="deleteccbutton" value="[l]Delete Credit Card[/l]" onclick="DeleteCC();return false;" />
            <br />
        </span>

        <span id="ccnumberspan">
            <input type="text" id="ccnumber" name="ccnumber" size="30" />
            <br />
        </span>

        <span id="awaitingvalidationspan">
            <span id="awaitingvalidation"></span>
            <br />
        </span>

        
        <span id="newccspan">
            <span class="label130">[l]New Card Number[/l]:</span>
            <input type="text" id="newccnumber" name="newccnumber" />&nbsp;&nbsp;{helpCCChange}
            <br/>
        </span>


        <span class="label130">[l]Expires[/l]:</span>
        <select id="ccmonth" name="ccmonth">
            <!-- BEGIN DYNAMIC BLOCK: monthOptions -->
            <option value="{monthOptionsValue}" {monthOptionsSelected} >{monthOptionsLabel}</option>
            <!-- END DYNAMIC BLOCK: monthOptions -->
        </select>

        <select id="ccyear" name="ccyear">
            <!-- BEGIN DYNAMIC BLOCK: yearOptions -->
            <option value="{yearOptionsValue}" {yearOptionsSelected} >{yearOptionsLabel}</option>
            <!-- END DYNAMIC BLOCK: yearOptions -->
        </select>
        <br />

        <span class="label130">[l]Accepting[/l]:</span>
        <span>
            <img src="images/visa_logo.gif" style="display:{visaStyle}" id="visa_logo" alt="" />
            <img src="images/mastercard_logo.gif" style="display:{mastercardStyle}" id="mastercard_logo" alt="" />
            <img src="images/americanexpress_logo.gif" style="display:{americanexpressStyle}" id="americanexpress_logo" alt="" />
            <img src="images/discover_logo.gif" style="display:{discoverStyle}" id="discover_logo" alt="" />
        </span>
        <input type="hidden" name="validcc" id="validccbits" value='{visabit}{mcbit}{amexbit}{discbit}' />
    </div>

    <br />
    <input type="submit" value='[l]update[/l]' onclick='SaveBillingInformation(); return false;' class="body" />
    </form>
</div>

<script type="">
    GetCCInformation(selectedplugin);
</script>

<!-- END DYNAMIC BLOCK: billing -->

<br />
</li>
</ul>
</div>
