<script data-cfasync="false" type="text/javascript">
    // --- Existing JavaScript preserved exactly ---
    function CheckForCCDisplay(selObj){
        var inarray = 0;
        validpluginsforcc = new Array(<?php echo $this->pluginsList;?>);
        <?php echo $this->availableCCArray;?>

        for (x = 0; x < <?php echo $this->arraycount;?>; x++){
            if (selObj == validpluginsforcc[x]) inarray = 1;
        }

        visabit=mcbit=amexbit=discoverbit=laserbit=dinersbit=switchbit=0;

        function safeSetDisplay(id, show) {
            var el = document.getElementById(id);
            if (el) el.style.display = show ? "" : "none";
        }

        if (inarray == 1) {
            visabit     = pluginsforcc[selObj][<?php echo $this->cCREDITVISA;?>]     == 1 ? 1 : 0;
            mcbit       = pluginsforcc[selObj][<?php echo $this->cCREDITMC;?>]       == 1 ? 1 : 0;
            amexbit     = pluginsforcc[selObj][<?php echo $this->cCREDITAMEX;?>]     == 1 ? 1 : 0;
            discoverbit = pluginsforcc[selObj][<?php echo $this->cCREDITDISC;?>]     == 1 ? 1 : 0;
            laserbit    = pluginsforcc[selObj][<?php echo $this->cCREDITLASER;?>]    == 1 ? 1 : 0;
            dinersbit   = pluginsforcc[selObj][<?php echo $this->cCREDITDINERS;?>]   == 1 ? 1 : 0;
            switchbit   = pluginsforcc[selObj][<?php echo $this->cCREDITSWITCH;?>]   == 1 ? 1 : 0;

            safeSetDisplay('visa_logo', visabit);
            safeSetDisplay('mastercard_logo', mcbit);
            safeSetDisplay('americanexpress_logo', amexbit);
            safeSetDisplay('discover_logo', discoverbit);
            safeSetDisplay('lasercard_logo', laserbit);
            safeSetDisplay('dinersclub_logo', dinersbit);
            safeSetDisplay('switch_logo', switchbit);

            document.getElementById('creditcardinfo')?.style && (document.getElementById('creditcardinfo').style.display = "");
        } else {
            const form = document.forms['customerdata'];
            if (form?.elements['c_ccnumber']) form.elements['c_ccnumber'].value = '';
            if (form?.elements['r_ccnumber']) form.elements['r_ccnumber'].value = '';
            document.getElementById('creditcardinfo')?.style && (document.getElementById('creditcardinfo').style.display = "none");
        }

        const form = document.forms['customerdata'];
        if (form && form.elements['validcc']) {
            form.elements['validcc'].value =
                '' + visabit + mcbit + amexbit + discoverbit + laserbit + dinersbit + switchbit + '';
        }
    }

    function replaceString(oldS,newS,fullS){
        for (var i=0;i<fullS.length;i++){
            if (fullS.substring(i,i+oldS.length)==oldS)
                fullS=fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length);
        }
        return fullS;
    }

    function fixaddress(myfield){
        myfield.value=replaceString(" ","",myfield.value).toLowerCase();
    }

    function checkCreditBalance(form){
        if ( typeof(form.balance)!=="undefined" && typeof(form.balance.value)!=="undefined" ){
            if (isNaN(form.balance.value)||form.balance.value<0){
                alert(lang("Your credit balance has a wrong format or is not valid"));
                return false;
            }
        }
        return true;
    }

    function showHideRecurring(value){
        usingRecurring=document.getElementById('use_recurring');
        usingRecurring.style.display=(value=='paypal')?'block':'none';
    }

    function showHideProfileID(value){
        if ($('.selected_paymenttype').length>0)
            $('.selected_paymenttype')[0].value=value;
        if ($('.DivBillingProfileID').length>0) $('.DivBillingProfileID').hide();
        if ($('.BillingProfileID').length>0) $('.BillingProfileID').hide();
        if ($('.'+value+'BillingProfileID').length>0){
            $('.'+value+'BillingProfileID').show();
            $('.DivBillingProfileID').show();
        }
    }

    function showHidePaymentInformation(value){
        if ($('.PaymentInformation').length>0) $('.PaymentInformation').hide();
        if ($('.PaymentIframe').length>0) $('.PaymentIframe').attr('src','');
        if ($('.'+value+'PaymentInformation').length>0) $('.'+value+'PaymentInformation').show();
        if ($('.'+value+'PaymentIframe').length>0)
            $('.'+value+'PaymentIframe').attr('src','index.php?fuse=admin&view=viewpluginurl&plugintoshow='+value);
    }

    if (document.images){
        pic1=new Image(167,14);
        pic1.src="<?php echo $this->gRelativePath;?>images/loading.gif";
    }
</script>

<?php echo $this->profileheader($this->customer);?>
<div class="resp-tabs-container tab_identifier_parent no-padding">
    <div>
        <div class="client-contact-wrapper" id="profile-fields">
            <div class="form-wrapper">
                <form method="post" id="customerdata" name="customerdata" class="w-100">
                    <dl class="row">
                        <?php if ($this->canEditCustomer) { ?>

                            <!-- Invoice PDF Template -->
                            <dt class="col-sm-3 col-form-label"><?php echo $this->user->lang("Invoice PDF Template");?></dt>
                            <dd class="col-sm-9">
                                <?php if ($this->clients_edit_payment_type) { ?>
                                    <select name="invoice_template" class="form-select">
                                        <option value="0" <?php echo ($this->selected_invoice_template == $template) ? 'selected' : ''; ?>>
                                            <?php echo $this->user->lang("Use system defined template");?>
                                        </option>
                                        <?php foreach($this->invoice_templates as $template) { ?>
                                            <option value="<?php echo $template;?>" <?php echo ($this->selected_invoice_template == $template)?'selected':''; ?>>
                                                <?php echo $template;?>
                                            </option>
                                        <?php } ?>
                                    </select>
                                <?php } else { ?>
                                    <div class="form-text">
                                        <?php echo ($this->selected_invoice_template == "") ? $this->user->lang("Use system defined template") : $this->selected_invoice_template; ?>
                                    </div>
                                <?php } ?>
                            </dd>

                            <!-- Credit Balance (editable) -->
                            <?php if ($this->billingEditCredit) { ?>
                                <dt class="col-sm-3 col-form-label"><?php echo $this->user->lang("Credit Balance");?></dt>
                                <dd class="col-sm-9">
                                    <?php echo $this->unescape($this->userBalance); ?>
                                    <div id="credit-buttons" class="mt-2">
                                        <button class="cm-btns lined-blue" id="add-credit-btn">Add Credit</button>
                                        <button class="cm-btns lined-grey" id="remove-credit-btn">Remove Credit</button>
                                        <button class="cm-btns lined-cyan" id="credit-history-btn">Credit History</button>
                                    </div>
                                </dd>
                            <?php } ?>

                            <!-- Credit Balance (read-only) -->
                            <?php if ($this->canSeeCredit) { ?>
                                <dt class="col-sm-3 col-form-label"><?php echo $this->user->lang("Credit Balance");?></dt>
                                <dd class="col-sm-9"><?php echo $this->creditBalance;?></dd>
                            <?php } ?>

                            <!-- Taxable -->
                            <dt class="col-sm-3 col-form-label"><?php echo $this->user->lang("Taxable");?></dt>
                            <dd class="col-sm-9">
                                <?php if ($this->clients_edit_payment_type) { ?>
                                    <select name="taxable" class="form-select">
                                        <option value="1" <?php echo $this->taxableYesSelected;?>><?php echo $this->user->lang("Yes");?></option>
                                        <option value="0" <?php echo $this->taxableNoSelected;?>><?php echo $this->user->lang("No");?></option>
                                    </select>
                                <?php } else { echo $this->taxableText; } ?>
                            </dd>

                            <!-- Currency -->
                            <dt class="col-sm-3 col-form-label"><?php echo $this->user->lang("Currency");?></dt>
                            <dd class="col-sm-9">
                                <?php if ($this->clients_edit_payment_type) { ?>
                                    <?php echo $this->unescape($this->currencyDropdown);?>
                                    <input type="hidden" class="selected_currency" id="selected_currency" value="<?php echo $this->currencyCode;?>" />
                                <?php } else { echo $this->currencyText; } ?>
                            </dd>

                            <!-- Payment Type -->
                            <dt class="col-sm-3 col-form-label"><?php echo $this->user->lang("Payment Processor");?></dt>
                            <dd class="col-sm-9">
                                <?php if ($this->clients_edit_payment_type) { ?>
                                    <select name="paymenttype" class="form-select"
                                        onChange="CheckForCCDisplay(this.value); showHideRecurring(this.value); showHideProfileID(this.value); showHidePaymentInformation(this.value);">
                                        <?php foreach($this->plugins as $plugin) { ?>
                                            <option value="<?php echo $plugin['paymentTypeOptionValue'];?>" <?php echo $plugin['paymentTypeOptionSelected'];?>>
                                                <?php echo $plugin['paymentTypeOptionLabel'];?>
                                            </option>
                                        <?php } ?>
                                    </select>
                                <?php } else { echo $this->paymenttypeText; } ?>
                            </dd>

                            <!-- Recurring -->
                            <div id="use_recurring" style="display:<?php echo $this->displayRecurring;?>">
                                <dt class="col-sm-3 col-form-label">
                                    <span data-bs-toggle="tooltip" data-bs-placement="right" data-bs-html="true"
                                          title="<?php echo $this->user->lang("If you switch to Recurring Subscriptions don't forget<br/>to generate the pending invoices that will initiate the<br/>subscription with the 3rd Party Processor.");?>">
                                        <?php echo $this->user->lang("Use Subscription");?>
                                    </span>
                                </dt>
                                <dd class="col-sm-9">
                                    <?php if ($this->clients_edit_payment_type) { ?>
                                        <div class="form-check form-check-inline">
                                            <input class="form-check-input" type="radio" name="recurring_paypal" value="0" <?php echo $this->recurringPaypalNo;?>>
                                            <label class="form-check-label"><?php echo $this->user->lang("No");?></label>
                                        </div>
                                        <div class="form-check form-check-inline">
                                            <input class="form-check-input" type="radio" name="recurring_paypal" value="1" <?php echo $this->recurringPaypalYes;?>>
                                            <label class="form-check-label"><?php echo $this->user->lang("Yes");?></label>
                                        </div>
                                    <?php } else { echo $this->recurringPaypalText; } ?>
                                </dd>
                            </div>
                        <?php } ?>

                        <!-- Billing Profile ID -->
                        <div class="DivBillingProfileID" id="DivBillingProfileID" style="display:none">
                            <dt class="col-sm-3 col-form-label"><?php echo $this->user->lang("Billing Profile ID");?></dt>
                            <dd class="col-sm-9">
                                <?php foreach ($this->profile_id_array as $paymenttype => $profile_id) { ?>
                                    <span class="BillingProfileID <?php echo $paymenttype.'BillingProfileID';?>" id="<?php echo $paymenttype.'BillingProfileID';?>" style="display:none">
                                        <?php echo $profile_id; ?>
                                        <?php if ($this->clients_edit_credit_card && $this->selectedPlugin === $paymenttype) { ?>
                                            &nbsp;&nbsp;&nbsp;
                                            <button class="cm-btns lined-blue" id="btnUpdateProfileID">
                                                <span class="buttontext">
                                                    <?php echo ($profile_id === '')? $this->user->lang('Add') : $this->user->lang('Update'); ?>
                                                </span>
                                            </button>
                                            <?php if ($profile_id !== '') { ?>
                                                <button class="cm-btns lined-grey" id="btnDeleteProfileID">
                                                    <?php echo $this->user->lang('Delete');?>
                                                </button>
                                            <?php } ?>
                                            <input type="hidden" class="selected_paymenttype" id="selected_paymenttype" value="<?php echo $this->selectedPlugin;?>" />
                                        <?php } ?>
                                    </span>
                                <?php } ?>
                            </dd>
                        </div>

                        <!-- Credit Card Info -->
<div id="creditcardinfo" class="mb-4" style="display:<?php echo $this->displayCC;?>">

    <!-- CREDIT CARD NUMBER -->
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label fw-semibold">
            <?php echo $this->user->lang("Credit Card Number");?>
        </label>

        <div class="col-sm-9">

            <?php if ($this->showCreditCardSection) { ?>

                <?php if ($this->isPassphrased) { ?>

                    <?php if ($this->ccIsThere) { ?>

                        <?php if ($this->canEditCreditCard) { ?>

                            <?php if ($this->clients_passphrase_cc) { ?>

                                <!-- MASKED NUMBER + BUTTONS (same line) -->
                                <div class="d-flex align-items-center gap-3">
                                    <a href="javascript:" id="viewcclink" class="text-decoration-none">
                                        xxxxxxxxxxxxx <?php echo $this->lastFour;?>
                                    </a>

                                    <?php if ($this->canDeleteCC) { ?>
                                        <button class="btn btn-outline-danger btn-sm" id="btnDeletecc">
                                            <?php echo $this->user->lang("Delete CC"); ?>
                                        </button>
                                    <?php } ?>
                                </div>

                            <?php } else { ?>

                                <div class="d-flex align-items-center gap-3">
                                    xxxxxxxxxxxxx<?php echo $this->lastFour; ?>

                                    <?php if ($this->canDeleteCC) { ?>
                                        <button class="btn btn-outline-danger btn-sm" id="btnDeletecc">
                                            <?php echo $this->user->lang("Delete CC"); ?>
                                        </button>
                                    <?php } ?>
                                </div>

                            <?php } ?>

                        <?php } ?>

                        <?php echo $this->cantEditCC; ?>

                    <?php } else { ?>

                        <?php if ($this->clients_edit_credit_card) { ?>
                            <input type="text" name="ccnumber" class="form-control d-inline-block">
                            <input type="hidden" name="c_ccnumber" value="<?php echo $this->requiredMsg;?>" />
                            <input type="hidden" name="r_ccnumber" value="<?php echo $this->ccRequiredMsg;?>" />
                        <?php } else { ?>
                            <?php echo $this->user->lang("Not Available"); ?>
                        <?php } ?>

                    <?php } ?>

                <?php } else { ?>

                    <?php if ($this->hasCreditCard) { ?>

                        <?php if ($this->canEditCreditCard) { ?>

                            <?php if ($this->cardCanBePassphrased) { ?>
                                <a href="<?php echo $this->someHelpURL;?>"
                                   title="<?php echo $this->someHelpPopupText;?>"
                                   class="text-danger d-block mb-2">
                                   <?php echo $this->someHelp;?>
                                </a>
                            <?php } ?>

                            <?php if ($this->awaitingValidation) { ?>
                                <span class="text-danger d-block mb-2">
                                    <?php echo $this->user->lang("Awaiting Validation");?>
                                </span>
                            <?php } ?>

                            <!-- VALIDATE + DELETE (same line) -->
                            <div class="d-flex align-items-center gap-3">

                                <?php if ($this->clients_passphrase_cc) { ?>
                                    <button class="btn btn-outline-primary btn-sm" id="btnValidatecc">
                                        <?php echo $this->user->lang("Validate CC");?>
                                    </button>
                                <?php } ?>

                                <?php if ($this->clients_edit_credit_card) { ?>
                                    <button class="btn btn-outline-danger btn-sm" id="btnDeletecc">
                                        <?php echo $this->user->lang("Delete CC");?>
                                    </button>
                                <?php } ?>

                            </div>

                        <?php } else if ($this->awaitingValidation) { ?>

                            <span class="text-danger"><?php echo $this->user->lang("Awaiting Validation");?></span>

                        <?php } ?>

                    <?php } else { ?>

                        <?php if ($this->clients_edit_credit_card) { ?>
                            <input type="text" name="ccnumber" class="form-control d-inline-block">
                            <input type="hidden" name="c_ccnumber" value="<?php echo $this->requiredMsg;?>" />
                            <input type="hidden" name="r_ccnumber" value="<?php echo $this->ccRequiredMsg;?>" />
                        <?php } else { ?>
                            <?php echo $this->user->lang("Not Available"); ?>
                        <?php } ?>

                    <?php } ?>

                <?php } ?>

            <?php } ?>

        </div>
    </div>


    <!-- NEW CREDIT CARD FIELD -->
    <?php if ($this->showNewCreditCardTextBox) { ?>
        <div class="row mb-3">
            <label class="col-sm-3 col-form-label fw-semibold" data-bs-toggle="tooltip"
                   title="<?php echo $this->user->lang("Only enter a credit card if you want to change the one on file");?>">
                <?php echo $this->user->lang("New Credit Card Number");?>
            </label>
            <div class="col-sm-9">
                <input type="text" name="newccnumber" class="form-control w-auto d-inline-block">
                <input type="hidden" name="c_newccnumber" value="<?php echo $this->requiredMsg;?>" />
                <input type="hidden" name="r_newccnumber" value="">
            </div>
        </div>
    <?php } ?>


    <!-- EXPIRATION MONTH -->
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label fw-semibold">
            <?php echo $this->user->lang("Expiration Month");?>
        </label>
        <div class="col-sm-9">
            <?php if ($this->clients_edit_credit_card) { ?>
                <select name="ccmonth" class="form-select w-auto d-inline-block">
                    <?php foreach ($this->months as $month) { ?>
                        <option value="<?php echo $month['monthOptionsValue'];?>" <?php echo $month['monthOptionsSelected'];?>><?php echo $month['monthOptionsLabel'];?></option>
                    <?php } ?>
                </select>
            <?php } else { ?>
                <?php echo $this->ccIsThere ? $this->monthText : $this->user->lang("Not Available"); ?>
            <?php } ?>
        </div>
    </div>

    <!-- EXPIRATION YEAR -->
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label fw-semibold">
            <?php echo $this->user->lang("Expiration Year");?>
        </label>
        <div class="col-sm-9">
            <?php if ($this->clients_edit_credit_card) { ?>
                <select name="ccyear" class="form-select w-auto d-inline-block">
                    <?php foreach($this->years as $year) {
                        if ($year['yearOptionsLabel'] == 0) continue; ?>
                        <option value="<?php echo $year['yearOptionsValue'];?>" <?php echo $year['yearOptionsSelected'];?>><?php echo $year['yearOptionsLabel'];?></option>
                    <?php } ?>
                </select>
            <?php } else { ?>
                <?php echo $this->ccIsThere ? $this->yearText : $this->user->lang("Not Available"); ?>
            <?php } ?>
        </div>
    </div>

    <!-- CARD LOGOS -->
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label fw-semibold">
            <?php echo $this->user->lang("Accepting");?>
        </label>
        <div class="col-sm-9">
            <div class="d-flex align-items-center flex-wrap gap-2">
                <img src="../images/creditcards/visa.gif" id="visa_logo" style="display:<?php echo $this->visaStyle;?>;" alt="Visa">
                <img src="../images/creditcards/mc.gif" id="mastercard_logo" style="display:<?php echo $this->mastercardStyle;?>;" alt="Mastercard">
                <img src="../images/creditcards/amex1.gif" id="americanexpress_logo" style="display:<?php echo $this->americanexpressStyle;?>;" alt="AmEx">
                <img src="../images/creditcards/discover.gif" id="discover_logo" style="display:<?php echo $this->discoverStyle;?>;" alt="Discover">
                <img src="../images/creditcards/laser.gif" id="lasercard_logo" style="display:<?php echo $this->lasercardStyle;?>;" alt="Laser">
                <img src="../images/creditcards/diners.gif" id="dinersclub_logo" style="display:<?php echo $this->dinersclubStyle;?>;" alt="Diners">
                <img src="../images/creditcards/switch.gif" id="switch_logo" style="display:<?php echo $this->switchStyle;?>;" alt="Switch">
            </div>

            <input type="hidden" name="validcc"
                   value="<?php echo $this->visabit;?><?php echo $this->mcbit;?><?php echo $this->amexbit;?><?php echo $this->discbit;?><?php echo $this->laserbit;?><?php echo $this->dinersbit;?><?php echo $this->switchbit;?>" />
        </div>
    </div>

</div>


                        <!-- Gateway Iframes -->
                        <?php if ($this->clients_edit_payment_type) { ?>
                            <?php foreach ($this->gatewayIframes as $gatewayName => $gatewayIframeConfiguration) { ?>
                                <div class="PaymentInformation <?php echo $gatewayName.'PaymentInformation';?>" id="<?php echo $gatewayName.'PaymentInformation';?>" style="display:<?php echo ($gatewayName == $this->selectedPlugin)?'block':'none';?>;">
                                    <iframe class="PaymentIframe <?php echo $gatewayName.'PaymentIframe';?>" id="<?php echo $gatewayName.'PaymentIframe';?>" src="<?php echo ($gatewayName == $this->selectedPlugin)?'index.php?fuse=admin&view=ViewPluginUrl&plugintoshow='.$gatewayName:'';?>" <?php echo $gatewayIframeConfiguration;?>>
                                        <p><?php echo $this->user->lang("Your browser does not support iframes");?></p>
                                    </iframe>
                                </div>
                            <?php } ?>
                        <?php } ?>
                    </dl>

                    <div class="pt-3">
                        <?php if ($this->clients_edit_payment_type || $this->billingEditCredit || ($this->clients_edit_credit_card && ($this->showCreditCardSection || $this->showNewCreditCardTextBox))) { ?>
                            <button id="btnUpdate" class="cm-btns orange"><?php echo $this->user->lang("Save Changes");?></button>
                        <?php } ?>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>


<div class="alert alert-warning mt-3">
    <?php echo $this->user->lang("Please note that changing the currency will not convert the price amount to existing charges.");?><br/>
    <?php echo $this->user->lang("You will need to review the prices on the existing invoices and recurring fees and reconfigure the packages and addons to make sure they are using prices for the new currency.");?><br/>
</div>

<script>
    $(document).ready(function () {

        const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
        tooltipTriggerList.map(function (tooltipTriggerEl) {
            return new bootstrap.Tooltip(tooltipTriggerEl);
        });

        $('#customerdata select').select2({
            width: '100%',
            dropdownPosition: 'below',
        });

        if ($('.selected_paymenttype').length > 0 && $('.'+$('.selected_paymenttype')[0].value+'BillingProfileID').length > 0) {
            $('.'+$('.selected_paymenttype')[0].value+'BillingProfileID').show();
            $('.DivBillingProfileID').show();
        }

        $('#btnUpdate').bind('click',function(e){
            if (document.getElementsByName("currency")[0].value != $('.selected_currency')[0].value) {
                RichHTML.msgBox(clientexec.lang("<b>Are you sure you want to change the currency?</b>")+"</br></br>"+clientexec.lang("Please note that changing the currency will not convert the price amount to existing charges.")+"</br></br>"+clientexec.lang("You will need to review the prices on the existing invoices and recurring fees and reconfigure the packages and addons to make sure they are using prices for the new currency."),{type:'yesno'},function(result){
                    if(result.btn === clientexec.lang("Yes")) {
                        RichHTML.mask();
                        e.preventDefault();

                        var data = $("#customerdata").serializeArray();
                        $.post("index.php?fuse=clients&controller=userprofile&action=updateprofilebilling", data, function(xhr){
                            json = clientexec.parseResponse(xhr);
                            if (!json.error) window.location.href = "index.php?fuse=clients&controller=userprofile&view=profilebilling";
                        });
                    }
                });
            } else {
                RichHTML.mask();
                e.preventDefault();

                var data = $("#customerdata").serializeArray();
                $.post("index.php?fuse=clients&controller=userprofile&action=updateprofilebilling", data, function(xhr){
                    json = clientexec.parseResponse(xhr);
                    if (!json.error) window.location.href = "index.php?fuse=clients&controller=userprofile&view=profilebilling";
                });
            }

            return false;
        });

        if ($('#viewcclink').length > 0) {
            $('#viewcclink').click(function(){
                RichHTML.msgBox(clientexec.lang("Enter your passphrase:"),
                {type:'prompt',password:true},
                function(result){
                    if(result.btn === clientexec.lang("OK")) {

                        $.ajax({
                            type: 'POST',
                            url: 'index.php?fuse=billing&controller=creditcard&action=viewccnumber',
                            data: {
                                pp: result.elements.value
                            },
                            success: function(data) {
                                ccWindow = new RichHTML.window({
                                    title: clientexec.lang('Credit Card Number'),
                                    content: data
                                });
                                ccWindow.show();
                            }
                        });
                    }
                });
            });
        }

        if ($('#btnUpdateProfileID').length > 0) {
            $('#btnUpdateProfileID').click(function(e){
                e.preventDefault();
                updateProfileIdWindow.show();
            });
        }

        if ($('#btnDeleteProfileID').length > 0) {
            $('#btnDeleteProfileID').click(function(e){
                e.preventDefault();
                RichHTML.msgBox(clientexec.lang("Are you sure you want to delete the billing profile id on file?"),{type:'yesno'},function(result){
                    if(result.btn === clientexec.lang("Yes")) {
                        RichHTML.mask();
                        paymenttype = $('.selected_paymenttype')[0].value;
                        window.location = "index.php?fuse=clients&controller=userprofile&view=profilebilling&deleteProfileID=true&paymenttype="+paymenttype;
                    }
                });
            });
        }

        if ($('#btnDeletecc').length > 0) {
            $('#btnDeletecc').click(function(e){
                e.preventDefault();
                RichHTML.msgBox(clientexec.lang("Are you sure you want to delete the credit card on file?"),{type:'yesno'},function(result){
                    if(result.btn === clientexec.lang("Yes")) {
                        RichHTML.mask();
                        window.location = "index.php?fuse=clients&controller=userprofile&view=profilebilling&deleteCCNumber=true";
                    }
                });
            });
        }

        if ($('#btnValidatecc').length > 0) {
            $('#btnValidatecc').click(function(e){
                e.preventDefault();
                RichHTML.msgBox(clientexec.lang("Enter your passphrase:"),
                {type:'prompt',password:true},
                function(result){
                    if(result.btn === clientexec.lang("OK")) {
                        RichHTML.mask();
                        var requesturl = "index.php?fuse=billing&controller=creditcard&action=validateccnumber";
                        $.ajax({
                            type: 'POST',
                            url: requesturl,
                            success: function(xhr) {
                                json = clientexec.parseResponse(xhr);
                                if (json.success) {
                                    window.location = "index.php?fuse=clients&controller=userprofile&view=profilebilling";
                                } else {
                                    RichHTML.unMask();
                                }
                            },
                            data: {
                                passphrase: result.elements.value
                            }
                        });
                    }
                });
            });
        }

        updateProfileIdWindow = new RichHTML.window({
            showSubmit: true,
            actionUrl: 'index.php?fuse=clients&action=updatebillingprofileid&controller=userprofile',
            title: clientexec.lang("Update Billing Profile ID"),
            url: 'index.php?fuse=clients&view=billingprofileid&controller=userprofile',
            onSubmit: function(e, data) {
                response = clientexec.parseResponse(e);
                if (!response.error) {
                    window.location = 'index.php?fuse=clients&controller=userprofile&view=profilebilling';
                }

            }
        });

        addCreditWindow = new RichHTML.window({
            showSubmit: true,
            actionUrl: 'index.php?fuse=clients&action=addcredit&controller=userprofile',
            title: clientexec.lang("Add Credit"),
            url: 'index.php?fuse=clients&view=creditwindow&controller=userprofile',
            onSubmit: function(e, data) {
                response = clientexec.parseResponse(e);
                if (!response.error) {
                    window.location = 'index.php?fuse=clients&controller=userprofile&view=profilebilling';
                }

            }
        });

        removeCreditWindow = new RichHTML.window({
            // id: 'remove-credit-window',
            showSubmit: true,
            actionUrl: 'index.php?fuse=clients&action=removecredit&controller=userprofile',
            title: clientexec.lang("Remove Credit"),
            url: 'index.php?fuse=clients&view=creditwindow&controller=userprofile',
            onSubmit: function(e, data) {
                response = clientexec.parseResponse(e);
                if (!response.error) {
                    window.location = 'index.php?fuse=clients&controller=userprofile&view=profilebilling';
                }

            }
        });


        $('#credit-history-btn').on('click', function(e) {
            e.preventDefault();
            window.location = 'index.php?fuse=clients&controller=userprofile&view=credithistoryview'
        });

        $('#add-credit-btn').on('click', function(e){
            e.preventDefault();
            addCreditWindow.show();
        });

        $('#remove-credit-btn').on('click', function(e){
            e.preventDefault();
            removeCreditWindow.show();
        });

        $('#credit-history-btn').on('click', function(e){
            e.preventDefault();
        });
    });

</script>