Ext.onReady(function(){

    if($('deleteccbtn')!=null){
        var btDeleteCC = new Ext.Button({
            id:"deletecc",
            renderTo:"deleteccbtn",
            text: "&nbsp;&nbsp;"+lang('Delete CC')+"&nbsp;&nbsp;",
            handler:function(){
                Ext.MessageBox.confirm('Confirm', 'Are you sure you want to delete the credit card on file?', DeleteCC);
            }
        });
    }

    function DeleteCC(btn){
        if(btn=="yes"){

            var success=false;
            Ext.MessageBox.show({
               title: 'Please wait',
               msg: 'Deleting Credit Card Data',
               progressText: 'Deleting ...',
               width:300,
               progress:true,
               closable:false,
               animEl: 'mb6'
             });

             var requesturl = "index.php?fuse=clients&action=deleteCCNumber";
             Ext.Ajax.request({
                   url: requesturl,
                   success: function(xhr) {
                       success = true;
                   }
             });


             // this hideous block creates the bogus progress
             var f = function(v){
                return function(){
                    if((v == 99) || ((success) && (v>6) )){
                        window.location = "index.php?fuse=clients&view=Admin_Profile_Billing";
                    }else{
                        var i = v/7;
                        Ext.MessageBox.updateProgress(i);
                    }
               };
             };
             for(var i = 1; i < 100; i++){
               setTimeout(f(i), i*500);
             }
         }
    }

    if($('validateccbtn')!=null){
        var btValidateCC = new Ext.Button({
            id:"validatecc",
            renderTo:"validateccbtn",
            text: "&nbsp;&nbsp;"+lang('Validate CC')+ "&nbsp;&nbsp;",
            handler:function(){
                Ext.MessageBox.prompt('Validate Credit Card', 'Enter your passphrase:', ValidateCC);
            }
        });
    }

    function ValidateCC(btn,text){
        if(btn=="ok"){

           var success=false;
            Ext.MessageBox.show({
               title: 'Please wait',
               msg: 'Encrypting Credit Card Data',
               progressText: 'Encrypting ...',
               width:300,
               progress:true,
               closable:false,
               animEl: 'mb6'
             });

             var requesturl = "index.php?fuse=clients&action=adminValidateCCNumber";
             Ext.Ajax.request({
                   url: requesturl,
                   success: function(xhr) {
                       success = true;
                   },
                   failure: function() {
                        Ext.Msg.alert("Fail");
                   },
                   params: {passphrase: text}
             });

             // this hideous block creates the bogus progress
             var f = function(v){
                return function(){
                    if((v == 99) || ((success) && (v>6) )){
                        window.location = "index.php?fuse=clients&view=Admin_Profile_Billing";
                    }else{
                        var i = v/7;
                        Ext.MessageBox.updateProgress(i);
                    }
               };
             };
             for(var i = 1; i < 100; i++){
               setTimeout(f(i), i*500);
             }


        }
    }

    if($('subscriptiontip')!=null){
        new Ext.ToolTip({
            target: 'subscriptiontip',
            anchor: 'left',
            width:300,
            html: lang("If you switch to Recurring Subscriptions don't forget to generate the pending invoices that will initiate the subscription with the 3rd Party Processor."),
            dismissDelay: 15000 // auto hide after 15 seconds
        });
    }

    if($('newcctip')!=null){
         new Ext.ToolTip({
            target: 'newcctip',
            anchor: 'left',
            width:170,
            html: lang("Only enter a credit card if you want to change the one on file"),
            dismissDelay: 15000 // auto hide after 15 seconds
        });
    }

    var paymenttypetip_html = "<b>"+lang("Payment Type")+"</b><br/>"+lang('You can configure your payment plugin at:')+"<br>System Setup->Plugins->Payment Processors";
    new Ext.ToolTip({
        target: 'paymenttypetip',
        anchor: 'left',
        width:250,
        html: paymenttypetip_html,
        dismissDelay: 15000 // auto hide after 15 seconds
    });

});

function OpenShowCCWindow(btn,txt){
    if(btn == "ok"){
        if (txt==""){
            alert(lang('You will need to enter your passphrase before viewing the entire credit card number.'));
            return;
        }

        var mywin = window.open('','cewindow','scrollbars=no,menubar=no,location=no,width=200,height=10,resizable=no');
        var passphrase = encodeURIComponent(txt);
        mywin.location.href = "index.php?fuse=clients&pp=" + passphrase + "&view=ViewCCNumber";
        mywin.focus();
    }
}

function ShowCC(){
    Ext.MessageBox.prompt('View Credit Card', 'Enter your passphrase:', OpenShowCCWindow);
}