function DeleteCC(){
    //calls the ajax action that returns the fields
    //then call category with edit
	Ext.Ajax.request({
		url:      "index.php?fuse=clients&action=deleteCCNumber",
		success:  function(t) { 
			var data = ce.parseAjaxResponse(t);
			if(data.error){
				return false;
			}
			window.location = "index.php?fuse=clients&view=Profile_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 paymentmethodchanged=false;
	
	var paymenttype = '';
	var ccyear = '';
	var ccmonth = '';
	var ccnumber = '';
	var old_subscription = '';
	var old_selectedplugin = '';
	var new_subscription = '';

    if(document.getElementById('paymenttype')!=null){
        paymenttype = document.getElementById('paymenttype').value;
        if(document.getElementById('paymenttype').value!=selectedplugin) paymentmethodchanged=true;
    }else{
        paymenttype = selectedplugin;
    }

    if(document.getElementById('ccyear')!=null){
       ccyear = document.getElementById('ccyear').value;
    }
    if(document.getElementById('ccmonth')!=null){
        ccmonth = document.getElementById('ccmonth').value;
    }

    if(document.getElementById('newccnumber')!=null){
        if( (document.getElementById('ccnumber')!=null) && (document.getElementById('ccnumber').value!="") ){
            ccnumber = document.getElementById('ccnumber').value;
        }else{
            ccnumber = document.getElementById('newccnumber').value;
        }
    }

    if(old_subscriptionyes){
        old_subscription = '1';
    }else{
		old_subscription = '0';
    }
	old_selectedplugin = selectedplugin;
    

    var radioGrp = document['forms']['billingform']['recurring_paypal'];
    for(i=0; i < radioGrp.length; i++){
        if (radioGrp[i].checked == true) {
            var new_subscriptionvalue = radioGrp[i].value;
        }
    }

    //only check if we are not showing cc information
    if( (new_subscriptionvalue!=null) && (document.getElementById('creditcardinfo').style.display=="none") ){
		new_subscription = new_subscriptionvalue;
        if(old_subscriptionyes!=new_subscriptionvalue){
            if(!SubscriptionConfirmation(new_subscriptionvalue,paymentmethodchanged)){
                return false;
            };
        }
    }else{
		new_subscription = '0';
        if( (old_subscriptionyes) && (paymentmethodchanged) ){
            if(!SubscriptionConfirmation(new_subscriptionvalue,paymentmethodchanged)){
                return false;
            };
        }
    }

    Ext.Ajax.request({
        url:      "index.php?fuse=clients&action=UpdateBilling",
        success:  function(t) {
            var json = ce.parseAjaxResponse(t);
            if(json.error){
                return false;
            }
            window.location = "index.php?fuse=clients&view=Profile_Billing";
        },
        params: {
            paymenttype:        paymenttype,
            ccyear:             ccyear,
            ccmonth:            ccmonth,
            ccnumber:           ccnumber,
            old_subscription:   old_subscription,
            old_selectedplugin: old_selectedplugin,
            new_subscription:   new_subscription
        }
    });
    
    pausecomp(2000);
}

function pausecomp(ms) {
    ms += new Date().getTime();
    while (new Date() < ms){}
}

 function GetCCInformation(selObj){
	Ext.Ajax.request({
		url:      "index.php?fuse=clients&action=GetPaymentPluginDetails",
		success:  function(t) {
			var json = ce.parseAjaxResponse(t);

			if(json.error){
				return false;
			}


			//show subscription options
			if(json.showsubscriptionoptions){

				document.getElementById('use_recurring').style.display = 'block';

        if(json.editsubscriptionoptions){
            document.getElementById('subscriptionno').disabled = false;
            document.getElementById('subscriptionyes').disabled = false;
        }else{
            document.getElementById('subscriptionno').disabled = true;
            document.getElementById('subscriptionyes').disabled = true;
        }

				if(json.subscriptionyes){
					document.getElementById('subscriptionyes').checked = true;
					old_subscriptionyes = true;
				}else{
					old_subscriptionyes = false;
				}
			}else{
				document.getElementById('use_recurring').style.display = 'none';
			}

			//show show edit vault payment information
			if(json.showeditvaultpaymentinformation){
				document.getElementById('vaultpaymentinformation').style.display = 'block';
				document.getElementById('vaultpaymentiframe').src = "index.php?fuse=admin&view=ViewPluginUrl&plugintoshow=quantumvault";
			}else{
				document.getElementById('vaultpaymentinformation').style.display = 'none';
				document.getElementById('vaultpaymentiframe').src = '';
			}

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


			if(json.awaitingvalidation!=""){
				document.getElementById('awaitingvalidation').innerHTML = json.awaitingvalidation;
				document.getElementById('awaitingvalidationspan').style.display = '';
				document.getElementById('ccnumberspan').style.display = 'none';
				document.getElementById('deleteccspan').style.display = 'none';
				document.getElementById('newccspan').style.display = "none";
			}else{
				if(json.last4!=""){
					document.getElementById('newccspan').style.display = "";
					document.getElementById('ccnumberspan').style.display = 'none';
					document.getElementById('lastfourspan').style.display = '';
					document.getElementById('lastfour').innerHTML = "xxxxxxxxxxxx"+json.last4;
					document.getElementById('deleteccspan').style.display = '';
				}else{
					document.getElementById('newccspan').style.display = "none";
					document.getElementById('ccnumberspan').style.display = '';
					document.getElementById('lastfourspan').style.display = 'none';
					document.getElementById('lastfour').innerHTML = "";
					document.getElementById('deleteccspan').style.display = 'none';

				}
				document.getElementById('awaitingvalidationspan').style.display = 'none';
			}

			//if cc then which cards do we show
			document.getElementById('visa_logo').style.display = json.visastyle;
			document.getElementById('mastercard_logo').style.display = json.mastercardstyle;
			document.getElementById('americanexpress_logo').style.display = json.americanexpressstyle;
			document.getElementById('discover_logo').style.display = json.discoverstyle;
			
			document.getElementById('lasercard_logo').style.display = json.lasercardstyle;
			document.getElementById('dinersclub_logo').style.display = json.dinersclubstyle;
			document.getElementById('switch_logo').style.display = json.switchstyle;
			//for card validation
			document.getElementById('validccbits').value= json.visabit+''+json.mcbit+''+json.amexbit+''+json.discbit+''+json.laserbit+''+json.dinersbit+''+json.switchbit;

		},
		params:   { plugin:  selObj }
	});
}