var mainForm;

var couponMaskAppliesToDomainNameRegistration = 1;
var couponMaskAppliesToDomainNameTransfer = 2;
var couponMaskAppliesToPackage = 4;
var couponMaskAppliesToPackageSetup = 8;
var couponMaskAppliesToAddons = 16;
var couponMaskAppliesToAddonsSetup = 32;
var couponMaskAppliesToOther = 64;

// State vars
var validateCouponCount = false;
var couponArr = new Array();
var taxRate = 0;
var taxRateDisp = 0;
var useVAT = 0;
var validVATNumber = 0;
var taxName = '';
var tax2Rate = 0;
var tax2RateDisp = 0;
var useVAT2 = 0;
var tax2Name = '';
var isTax2Compound = 0;

function BackStepTwo()
{
    if (mainForm.hasDomainNameRegistration.value) {
        mainForm.step.value=1;
    } else {
        mainForm.step.value=0;             
        mainForm.clienttype.value=0;
        mainForm.package.options[mainForm.package.selectedIndex].value =0;                    
    }

    mainForm.submit();
}

function RecalculatePage()
{
    mainForm.step.value = 2;
    mainForm.submit();
}

function regenerateBill()
{
    var domainNameRegistrationFee = 0;
    var packageUnproratedFee = 0;
    var packageProratedFee = 0;
    var packageSetupFee = 0;
    var packageAddonsUnproratedFee = 0;
    var packageAddonsProratedFee = 0;
    var packageAddonsSetupFee = 0;
    var discount = 0;
    var taxes = 0;
    var taxes2 = 0;
    var taxesDiscount = 0;
    var taxes2Discount = 0;

    var packageAddonsProratedTaxFee = 0;
    var packageAddonsProratedTax2Fee = 0;
    var packageAddonsSetupTaxFee = 0;
    var packageAddonsSetupTax2Fee = 0;

    var taxesOfDomainName = 0;
    var taxes2OfDomainName = 0;
    var taxesOfPackage = 0;
    var taxes2OfPackage = 0;
    var taxesOfPackageSetup = 0;
    var taxes2OfPackageSetup = 0;
    var taxesOfAddons = 0;
    var taxes2OfAddons = 0;
    var taxesOfAddonsSetup = 0;
    var taxes2OfAddonsSetup = 0;
    var taxesOfOther = 0;
    var taxes2OfOther = 0;

    // domain name registration
    if (mainForm.hasDomainNameRegistration.value) {
        var domainData = mainForm.domainperiod.value.split('|');
        domainNameRegistrationFee = parseFloat(domainData[1]);
        var domainNameRegistrationTaxFee = 0;
        var domainNameRegistrationTax2Fee = 0;
        if (mainForm.domainTaxable.value != "0") {
            taxes += domainNameRegistrationFee * taxRate;
            taxesOfDomainName += domainNameRegistrationFee * taxRate;
            domainNameRegistrationTaxFee = domainNameRegistrationFee * taxRate;
            if(isTax2Compound && (validVATNumber == 0 || useVAT == "0")){
                taxes2 += (domainNameRegistrationFee + (domainNameRegistrationFee * taxRate)) * tax2Rate;
                taxes2OfDomainName += (domainNameRegistrationFee + (domainNameRegistrationFee * taxRate)) * tax2Rate;
                domainNameRegistrationTax2Fee = (domainNameRegistrationFee + (domainNameRegistrationFee * taxRate)) * tax2Rate;
            }else{
                taxes2 += domainNameRegistrationFee * tax2Rate;
                taxes2OfDomainName += domainNameRegistrationFee * tax2Rate;
                domainNameRegistrationTax2Fee = domainNameRegistrationFee * tax2Rate;
            }
        }

        if(domainNameRegistrationTaxFee){
            document.getElementById('checkoutDomainNameRegistrationTaxFee').innerHTML = formatAsMoney(domainNameRegistrationTaxFee, true);
        }else{
            document.getElementById('checkoutDomainNameRegistrationTaxFee').innerHTML = lang("N/A");
        }
        
        if(domainNameRegistrationTax2Fee){
            document.getElementById('checkoutDomainNameRegistrationTax2Fee').innerHTML = formatAsMoney(domainNameRegistrationTax2Fee, true);
        }else{
            document.getElementById('checkoutDomainNameRegistrationTax2Fee').innerHTML = lang("N/A");
        }

        document.getElementById('checkoutDomainNameRegistrationFee').innerHTML = formatAsMoney(domainNameRegistrationFee, true);
        var spans = document.getElementById('checkoutDomainNameRegistration').getElementsByTagName("span");
        if (domainData[0] == 0){
            spans[2].innerHTML = lang("One time payment");
        } else if (domainData[0] == 1) {
            spans[2].innerHTML = "1 " + lang('Year');
        } else {
            spans[2].innerHTML = domainData[0] + " " + lang('Years');
        }
        document.getElementById('checkoutDomainNameRegistration').style.display = "block";
    }
    // package prorated fee
    if (mainForm.prorating.value && mainForm.packageProratedFee.value) {
        packageProratedFee = parseFloat(mainForm.packageProratedFee.value);
        var packageProratedTaxFee = 0;
        var packageProratedTax2Fee = 0;
        if (mainForm.packageTaxable.value != "0") {
            taxes += packageProratedFee * taxRate;
            taxesOfPackage += packageProratedFee * taxRate;
            packageProratedTaxFee = packageProratedFee * taxRate;
            
            if(isTax2Compound && (validVATNumber == 0 || useVAT == "0")){
                taxes2 += (packageProratedFee + (packageProratedFee * taxRate)) * tax2Rate;
                taxes2OfPackage += (packageProratedFee + (packageProratedFee * taxRate)) * tax2Rate;
                packageProratedTax2Fee = (packageProratedFee + (packageProratedFee * taxRate)) * tax2Rate;
            }else{
                taxes2 += packageProratedFee * tax2Rate;
                taxes2OfPackage += packageProratedFee * tax2Rate;
                packageProratedTax2Fee = packageProratedFee * tax2Rate;
            }
        }

        if(packageProratedTaxFee){
            document.getElementById('checkoutPackageProratedTaxFee').innerHTML = formatAsMoney(packageProratedTaxFee, true);
        }else{
            document.getElementById('checkoutPackageProratedTaxFee').innerHTML = lang("N/A");
        }
        
        if(packageProratedTax2Fee){
            document.getElementById('checkoutPackageProratedTax2Fee').innerHTML = formatAsMoney(packageProratedTax2Fee, true);
        }else{
            document.getElementById('checkoutPackageProratedTax2Fee').innerHTML = lang("N/A");
        }

        document.getElementById('checkoutPackageProratedFee').innerHTML = formatAsMoney(packageProratedFee, true);
        document.getElementById('checkoutPackageProrated').style.display = "block";
    }

    // package unprorated fee
    if (mainForm.packageUnproratedFee.value != 0 || mainForm.includeFollowingPayment.value != 0) {
        packageUnproratedFee = parseFloat(mainForm.packageUnproratedFee.value);
        var packageUnproratedTaxFee = 0;
        var packageUnproratedTax2Fee = 0;
        if (mainForm.packageTaxable.value != "0") {
            taxes += packageUnproratedFee * taxRate;
            taxesOfPackage += packageUnproratedFee * taxRate;
            packageUnproratedTaxFee = packageUnproratedFee * taxRate;
            
            if(isTax2Compound && (validVATNumber == 0 || useVAT == "0")){
                taxes2 += (packageUnproratedFee + (packageUnproratedFee * taxRate)) * tax2Rate;
                taxes2OfPackage += (packageUnproratedFee + (packageUnproratedFee * taxRate)) * tax2Rate;
                packageUnproratedTax2Fee = (packageUnproratedFee + (packageUnproratedFee * taxRate)) * tax2Rate;
            }else{
                taxes2 += packageUnproratedFee * tax2Rate;
                taxes2OfPackage += packageUnproratedFee * tax2Rate;
                packageUnproratedTax2Fee = packageUnproratedFee * tax2Rate;
            }
        }

        if(packageUnproratedTaxFee){
            document.getElementById('checkoutPackageUnproratedTaxFee').innerHTML = formatAsMoney(packageUnproratedTaxFee, true);
        }else{
            document.getElementById('checkoutPackageUnproratedTaxFee').innerHTML = lang("N/A");
        }
        
        if(packageUnproratedTax2Fee){
            document.getElementById('checkoutPackageUnproratedTax2Fee').innerHTML = formatAsMoney(packageUnproratedTax2Fee, true);
        }else{
            document.getElementById('checkoutPackageUnproratedTax2Fee').innerHTML = lang("N/A");
        }

        document.getElementById('checkoutPackageUnproratedFee').innerHTML = formatAsMoney(packageUnproratedFee, true);
        document.getElementById('checkoutPackageUnprorated').style.display = "block";
    }

    // package setup fee
    if (mainForm.packageSetupFee.value > 0) {
        packageSetupFee = parseFloat(mainForm.packageSetupFee.value);
        var packageSetupTaxFee = 0;
        var packageSetupTax2Fee = 0;
        if (mainForm.packageTaxable.value != "0") {
            taxes += packageSetupFee * taxRate;
            taxesOfPackageSetup += packageSetupFee * taxRate;
            packageSetupTaxFee = packageSetupFee * taxRate;
            
            if(isTax2Compound && (validVATNumber == 0 || useVAT == "0")){
                taxes2 += (packageSetupFee + (packageSetupFee * taxRate)) * tax2Rate;
                taxes2OfPackageSetup += (packageSetupFee + (packageSetupFee * taxRate)) * tax2Rate;
                packageSetupTax2Fee = (packageSetupFee + (packageSetupFee * taxRate)) * tax2Rate;
            }else{
                taxes2 += packageSetupFee * tax2Rate;
                taxes2OfPackageSetup += packageSetupFee * tax2Rate;
                packageSetupTax2Fee = packageSetupFee * tax2Rate;
            }
        }

        if(packageSetupTaxFee){
            document.getElementById('checkoutPackageSetupTaxFee').innerHTML = formatAsMoney(packageSetupTaxFee, true);
        }else{
            document.getElementById('checkoutPackageSetupTaxFee').innerHTML = lang("N/A");
        }
        
        if(packageSetupTaxFee){
            document.getElementById('checkoutPackageSetupTax2Fee').innerHTML = formatAsMoney(packageSetupTax2Fee, true);
        }else{
            document.getElementById('checkoutPackageSetupTax2Fee').innerHTML = lang("N/A");
        }

        mainForm.setup.value = packageSetupFee;
        document.getElementById('checkoutPackageSetupFee').innerHTML = formatAsMoney(packageSetupFee, true);
        document.getElementById('checkoutPackageSetup').style.display = "block";
    }

    // package add-on fees
    if (mainForm.addonsRadioButtons.value == "1") {
        var addonHolders = document.getElementsByTagName('input');
    } else {
        var addonHolders = document.getElementsByTagName('select');
    }
    var addonChargesArr = new Array();
    var addonRecurringCharges = new Array(-1, 0, -1, 0, -1, -1, 0, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0);

    var addonRecurringTaxCharges = new Array(-1, 0, -1, 0, -1, -1, 0, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0);
    var addonRecurringTax2Charges = new Array(-1, 0, -1, 0, -1, -1, 0, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0);

    for (var i = 0; i < addonHolders.length; i++) {
        if (addonHolders[i].name.indexOf('addonSelect') == -1) {
            continue;
        }

        if (mainForm.addonsRadioButtons.value == "1" && !addonHolders[i].checked) {
            continue;
        }

        var selectedAddonArr = addonHolders[i].value.split('_');
        addonChargesArr.push(selectedAddonArr[1] + '_' + selectedAddonArr[2] + '_' + selectedAddonArr[3]);

        if (mainForm.prorating.value == "0" || mainForm.includeFollowingPayment.value) {
            var addonFee = addonRecurringPrices[selectedAddonArr[1]][selectedAddonArr[2]][selectedAddonArr[3]].fullPrice;
            addonRecurringCharges[selectedAddonArr[3]] += addonFee;
            packageAddonsUnproratedFee += addonFee;
            if (addonTaxable[selectedAddonArr[1]]) {
                taxes += addonFee * taxRate;
                taxesOfAddons += addonFee * taxRate;
                addonRecurringTaxCharges[selectedAddonArr[3]] += addonFee * taxRate;
                
                if(isTax2Compound && (validVATNumber == 0 || useVAT == "0")){
                    taxes2 += (addonFee + (addonFee * taxRate)) * tax2Rate;
                    taxes2OfAddons += (addonFee + (addonFee * taxRate)) * tax2Rate;
                    addonRecurringTax2Charges[selectedAddonArr[3]] += (addonFee + (addonFee * taxRate)) * tax2Rate;
                }else{
                    taxes2 += addonFee * tax2Rate;
                    taxes2OfAddons += addonFee * tax2Rate;
                    addonRecurringTax2Charges[selectedAddonArr[3]] += addonFee * tax2Rate;
                }
            }
        }
        if (mainForm.prorating.value) {
            packageAddonsProratedFee += addonRecurringPrices[selectedAddonArr[1]][selectedAddonArr[2]][selectedAddonArr[3]].proratedPrice;
            if (addonTaxable[selectedAddonArr[1]]) {
                taxes += addonRecurringPrices[selectedAddonArr[1]][selectedAddonArr[2]][selectedAddonArr[3]].proratedPrice * taxRate;
                taxesOfAddons += addonRecurringPrices[selectedAddonArr[1]][selectedAddonArr[2]][selectedAddonArr[3]].proratedPrice * taxRate;
                packageAddonsProratedTaxFee += addonRecurringPrices[selectedAddonArr[1]][selectedAddonArr[2]][selectedAddonArr[3]].proratedPrice * taxRate;
                
                if(isTax2Compound && (validVATNumber == 0 || useVAT == "0")){
                    taxes2 += (addonRecurringPrices[selectedAddonArr[1]][selectedAddonArr[2]][selectedAddonArr[3]].proratedPrice + (addonRecurringPrices[selectedAddonArr[1]][selectedAddonArr[2]][selectedAddonArr[3]].proratedPrice * taxRate)) * tax2Rate;
                    taxes2OfAddons += (addonRecurringPrices[selectedAddonArr[1]][selectedAddonArr[2]][selectedAddonArr[3]].proratedPrice + (addonRecurringPrices[selectedAddonArr[1]][selectedAddonArr[2]][selectedAddonArr[3]].proratedPrice * taxRate)) * tax2Rate;
                    packageAddonsProratedTax2Fee += (addonRecurringPrices[selectedAddonArr[1]][selectedAddonArr[2]][selectedAddonArr[3]].proratedPrice + (addonRecurringPrices[selectedAddonArr[1]][selectedAddonArr[2]][selectedAddonArr[3]].proratedPrice * taxRate)) * tax2Rate;
                }else{
                    taxes2 += addonRecurringPrices[selectedAddonArr[1]][selectedAddonArr[2]][selectedAddonArr[3]].proratedPrice * tax2Rate;
                    taxes2OfAddons += addonRecurringPrices[selectedAddonArr[1]][selectedAddonArr[2]][selectedAddonArr[3]].proratedPrice * tax2Rate;
                    packageAddonsProratedTax2Fee += addonRecurringPrices[selectedAddonArr[1]][selectedAddonArr[2]][selectedAddonArr[3]].proratedPrice * tax2Rate;
                }
            }
        }
        packageAddonsSetupFee += addonSetupPrices[selectedAddonArr[1]][selectedAddonArr[2]];
        if (addonTaxable[selectedAddonArr[1]]) {
            taxes += addonSetupPrices[selectedAddonArr[1]][selectedAddonArr[2]] * taxRate;
            taxesOfAddonsSetup += addonSetupPrices[selectedAddonArr[1]][selectedAddonArr[2]] * taxRate;
            packageAddonsSetupTaxFee += addonSetupPrices[selectedAddonArr[1]][selectedAddonArr[2]] * taxRate;
            
            if(isTax2Compound && (validVATNumber == 0 || useVAT == "0")){
                taxes2 += (addonSetupPrices[selectedAddonArr[1]][selectedAddonArr[2]] + (addonSetupPrices[selectedAddonArr[1]][selectedAddonArr[2]] * taxRate)) * tax2Rate;
                taxes2OfAddonsSetup += (addonSetupPrices[selectedAddonArr[1]][selectedAddonArr[2]] + (addonSetupPrices[selectedAddonArr[1]][selectedAddonArr[2]] * taxRate)) * tax2Rate;
                packageAddonsSetupTax2Fee += (addonSetupPrices[selectedAddonArr[1]][selectedAddonArr[2]] + (addonSetupPrices[selectedAddonArr[1]][selectedAddonArr[2]] * taxRate)) * tax2Rate;
            }else{
                taxes2 += addonSetupPrices[selectedAddonArr[1]][selectedAddonArr[2]] * tax2Rate;
                taxes2OfAddonsSetup += addonSetupPrices[selectedAddonArr[1]][selectedAddonArr[2]] * tax2Rate;
                packageAddonsSetupTax2Fee += addonSetupPrices[selectedAddonArr[1]][selectedAddonArr[2]] * tax2Rate;
            }
        }
    }
    mainForm.addonChargesStr.value= addonChargesArr.join('|');
    if (packageAddonsProratedFee) {
        document.getElementById('checkoutPackageAddonProratedFee').innerHTML = formatAsMoney(packageAddonsProratedFee, true);

        if (packageAddonsProratedTaxFee) {
            document.getElementById('checkoutPackageAddonProratedTaxFee').innerHTML = formatAsMoney(packageAddonsProratedTaxFee, true);
        }else{
            document.getElementById('checkoutPackageAddonProratedTaxFee').innerHTML = lang("N/A");
        }
        
        if (packageAddonsProratedTax2Fee) {
            document.getElementById('checkoutPackageAddonProratedTax2Fee').innerHTML = formatAsMoney(packageAddonsProratedTax2Fee, true);
        }else{
            document.getElementById('checkoutPackageAddonProratedTax2Fee').innerHTML = lang("N/A");
        }

        document.getElementById('checkoutPackageAddonProrated').style.display = "block";
    } else {
        document.getElementById('checkoutPackageAddonProrated').style.display = "none";
    }
    if (packageAddonsUnproratedFee) {
        for (var i = 0; i < addonRecurringCharges.length; i++) {
            if (addonRecurringCharges[i] > 0) {
                document.getElementById("checkoutPackageAddonUnproratedFee_" + i).innerHTML = formatAsMoney(addonRecurringCharges[i], true);

                if (addonRecurringTaxCharges[i] > 0) {
                    document.getElementById("checkoutPackageAddonUnproratedTaxFee_" + i).innerHTML = formatAsMoney(addonRecurringTaxCharges[i], true);
                }else{
                  document.getElementById("checkoutPackageAddonUnproratedTaxFee_" + i).innerHTML = lang("N/A");
                }
                
                if (addonRecurringTax2Charges[i] > 0) {
                    document.getElementById("checkoutPackageAddonUnproratedTax2Fee_" + i).innerHTML = formatAsMoney(addonRecurringTax2Charges[i], true);
                }else{
                  document.getElementById("checkoutPackageAddonUnproratedTax2Fee_" + i).innerHTML = lang("N/A");
                }

                document.getElementById("checkoutPackageAddonUnprorated_" + i).style.display = "block";
            } else if (addonRecurringCharges[i] == 0) {
                document.getElementById("checkoutPackageAddonUnprorated_" + i).style.display = "none";
            }
        }
    } else {
        for (var i = 0; i < addonRecurringCharges.length; i++) {
            if (addonRecurringCharges[i] == 0) {
                document.getElementById("checkoutPackageAddonUnprorated_" + i).style.display = "none";
            }
        }
    }
    if (packageAddonsSetupFee) {
        document.getElementById('checkoutPackageAddonSetupFee').innerHTML = formatAsMoney(packageAddonsSetupFee, true);
        
        if (packageAddonsSetupTaxFee) {
            document.getElementById('checkoutPackageAddonSetupTaxFee').innerHTML = formatAsMoney(packageAddonsSetupTaxFee, true);
        }else{
            document.getElementById('checkoutPackageAddonSetupTaxFee').innerHTML = lang("N/A");
        }
        
        if (packageAddonsSetupTax2Fee) {
            document.getElementById('checkoutPackageAddonSetupTax2Fee').innerHTML = formatAsMoney(packageAddonsSetupTax2Fee, true);
        }else{
            document.getElementById('checkoutPackageAddonSetupTax2Fee').innerHTML = lang("N/A");
        }

        document.getElementById('checkoutPackageAddonSetup').style.display = "block";
    } else {
        document.getElementById('checkoutPackageAddonSetup').style.display = "none";
    }

    // coupon before taxes
    document.getElementById('checkoutDiscountPreTaxes').style.display = 'none';
    if (couponArr.length > 0) {
        if (couponArr[2] == 0) {
            discount = parseFloat(couponArr[4]);
            taxesDiscount = discount * taxRate;

            if(isTax2Compound && (validVATNumber == 0 || useVAT == "0")){
                taxes2Discount = (discount + (discount * taxRate)) * tax2Rate;
            }else{
                taxes2Discount = discount * tax2Rate;
            }
        } else {
            if (mainForm.transfer.value == "0" && (couponArr[3] & couponMaskAppliesToDomainNameRegistration)) {
                discount += domainNameRegistrationFee * couponArr[4];
                taxesDiscount += taxesOfDomainName * couponArr[4];
                taxes2Discount += taxes2OfDomainName * couponArr[4];
            }
            if (mainForm.transfer.value == "1" && (couponArr[3] & couponMaskAppliesToDomainNameTransfer)) {
                discount += domainNameRegistrationFee * couponArr[4];
                taxesDiscount += taxesOfDomainName * couponArr[4];
                taxes2Discount += taxes2OfDomainName * couponArr[4];
            }
            if (couponArr[3] & couponMaskAppliesToPackage) {
                discount += (packageUnproratedFee + packageProratedFee) * couponArr[4];
                taxesDiscount += taxesOfPackage * couponArr[4];
                taxes2Discount += taxes2OfPackage * couponArr[4];
            }
            if (couponArr[3] & couponMaskAppliesToPackageSetup) {
                discount += packageSetupFee * couponArr[4];
                taxesDiscount += taxesOfPackageSetup * couponArr[4];
                taxes2Discount += taxes2OfPackageSetup * couponArr[4];
            }
            if (couponArr[3] & couponMaskAppliesToAddons) {
                discount += (packageAddonsUnproratedFee + packageAddonsProratedFee) * couponArr[4];
                taxesDiscount += taxesOfAddons * couponArr[4];
                taxes2Discount += taxes2OfAddons * couponArr[4];
            }
            if (couponArr[3] & couponMaskAppliesToAddonsSetup) {
                discount += packageAddonsSetupFee * couponArr[4];
                taxesDiscount += taxesOfAddonsSetup * couponArr[4];
                taxes2Discount += taxes2OfAddonsSetup * couponArr[4];
            }
        }

        if (discount > 0) {
            mainForm.discount.value = -discount;
        }

        if (couponArr[8] == 1) {
            taxes -= taxesDiscount;
            taxes2 -= taxes2Discount;
            if(taxes < 0){
                taxes = 0;
            }
            
            if(taxes2 < 0){
                taxes2 = 0;
            }

            document.getElementById('checkoutDiscountPreTaxesFee').innerHTML = (discount > 0? "- " : "") + formatAsMoney(discount, true);
            document.getElementById('checkoutDiscountTaxesFee').innerHTML = (taxesDiscount > 0? "- " : "") + formatAsMoney(taxesDiscount, true);
            document.getElementById('checkoutDiscountTaxes2Fee').innerHTML = (taxes2Discount > 0? "- " : "") + formatAsMoney(taxes2Discount, true);
            document.getElementById('checkoutDiscountPreTaxesName').innerHTML = couponArr[0];
            document.getElementById('checkoutDiscountPreTaxes').style.display = "block";
        }
    }

    // Subtotal
    var subtotal =  domainNameRegistrationFee
                    + packageUnproratedFee
                    + packageProratedFee
                    + packageSetupFee
                    + packageAddonsUnproratedFee
                    + packageAddonsProratedFee
                    + packageAddonsSetupFee;
    if (couponArr.length > 0 && couponArr[8] == 1) {
        subtotal -= discount;
    }
    if (subtotal < 0){
      subtotal = 0;
    }
    document.getElementById("checkoutSubtotalAmount").innerHTML = formatAsMoney(subtotal, true);

    // taxes
    var oldPriceWidth = "200px";
    var newPriceWidth = "127px";
    var newPriceWidth2 = "64px";

    var TaxColumn = new Array ('checkoutColumnNameTax', 
                               'checkoutDomainNameRegistrationTaxFee', 
                               'checkoutPackageProratedTaxFee', 
                               'checkoutPackageUnproratedTaxFee', 
                               'checkoutPackageSetupTaxFee', 
                               'checkoutPackageAddonProratedTaxFee', 
                               'checkoutPackageAddonSetupTaxFee', 
                               'checkoutDiscountTaxesFee', 
                               'checkoutTaxesAmount');

    var Tax2Column = new Array ('checkoutColumnNameTax2', 
                               'checkoutDomainNameRegistrationTax2Fee', 
                               'checkoutPackageProratedTax2Fee', 
                               'checkoutPackageUnproratedTax2Fee', 
                               'checkoutPackageSetupTax2Fee', 
                               'checkoutPackageAddonProratedTax2Fee', 
                               'checkoutPackageAddonSetupTax2Fee', 
                               'checkoutDiscountTaxes2Fee', 
                               'checkoutTaxes2Amount');

    var PriceColumn = new Array ('checkoutColumnNamePrice', 
                                 'checkoutDomainNameRegistrationFee', 
                                 'checkoutPackageProratedFee', 
                                 'checkoutPackageUnproratedFee', 
                                 'checkoutPackageSetupFee', 
                                 'checkoutPackageAddonProratedFee', 
                                 'checkoutPackageAddonSetupFee', 
                                 'checkoutDiscountPreTaxesFee', 
                                 'checkoutSubtotalAmount', 
                                 'checkoutDiscountPostTaxesFee', 
                                 'checkoutTotalFee');

    for (var i = 0; i < TaxColumn.length; i++) {
        document.getElementById(TaxColumn[i]).style.display = "none"; // reset
    }
    for (var i = 0; i < addonRecurringTaxCharges.length; i++) {
        if (addonRecurringTaxCharges[i] >= 0) {
            document.getElementById("checkoutPackageAddonUnproratedTaxFee_" + i).style.display = "none"; // reset
        }
    }
    
    for (var i = 0; i < Tax2Column.length; i++) {
        document.getElementById(Tax2Column[i]).style.display = "none"; // reset
    }
    for (var i = 0; i < addonRecurringTax2Charges.length; i++) {
        if (addonRecurringTax2Charges[i] >= 0) {
            document.getElementById("checkoutPackageAddonUnproratedTax2Fee_" + i).style.display = "none"; // reset
        }
    }
    
    for (var i = 0; i < PriceColumn.length; i++) {
        document.getElementById(PriceColumn[i]).style.width = oldPriceWidth; // reset
    }
    for (var i = 0; i < addonRecurringCharges.length; i++) {
        if (addonRecurringCharges[i] >= 0) {
            document.getElementById("checkoutPackageAddonUnproratedFee_" + i).style.width = oldPriceWidth; // reset
        }
    }

    var showedTaxColumn = false;
    if (taxes) {
        document.getElementById('checkoutTaxesRate').innerHTML = taxRateDisp;
        document.getElementById('checkoutTaxesName').innerHTML = taxName;
        document.getElementById('checkoutTaxesAmount').innerHTML = formatAsMoney(taxes, true);

        for (var i = 0; i < TaxColumn.length; i++) {
            document.getElementById(TaxColumn[i]).style.display = "";
        }
        for (var i = 0; i < addonRecurringTaxCharges.length; i++) {
            if (addonRecurringTaxCharges[i] >= 0) {
                document.getElementById("checkoutPackageAddonUnproratedTaxFee_" + i).style.display = "";
            }
        }
        
        for (var i = 0; i < PriceColumn.length; i++) {
            document.getElementById(PriceColumn[i]).style.width = newPriceWidth;
        }
        for (var i = 0; i < addonRecurringCharges.length; i++) {
            if (addonRecurringCharges[i] >= 0) {
                document.getElementById("checkoutPackageAddonUnproratedFee_" + i).style.width = newPriceWidth;
            }
        }
        
        showedTaxColumn = true;

        // Some additional logic for VAT in non default countries
        if (useVAT == "1" && vatCountry == "0" && document.getElementById('vatField').value != '' && validVATNumber == 1) {
            document.getElementById('checkoutTaxesRate').innerHTML = 0;
            document.getElementById('checkoutTaxesAmount').innerHTML = formatAsMoney(0, true);

            for (var i = 0; i < TaxColumn.length; i++) {
                document.getElementById(TaxColumn[i]).style.display = "none";
            }
            for (var i = 0; i < addonRecurringTaxCharges.length; i++) {
                if (addonRecurringTaxCharges[i] >= 0) {
                    document.getElementById("checkoutPackageAddonUnproratedTaxFee_" + i).style.display = "none";
                }
            }
            
            for (var i = 0; i < PriceColumn.length; i++) {
                document.getElementById(PriceColumn[i]).style.width = oldPriceWidth;
            }
            for (var i = 0; i < addonRecurringCharges.length; i++) {
                if (addonRecurringCharges[i] >= 0) {
                    document.getElementById("checkoutPackageAddonUnproratedFee_" + i).style.width = oldPriceWidth;
                }
            }
            
            showedTaxColumn = false;

            taxes = 0;
        }
        
    }

    if (taxes2) {
        document.getElementById('checkoutTaxes2Rate').innerHTML = tax2RateDisp;
        document.getElementById('checkoutTaxes2Name').innerHTML = tax2Name;
        document.getElementById('checkoutTaxes2Amount').innerHTML = formatAsMoney(taxes2, true);

        for (var i = 0; i < Tax2Column.length; i++) {
            document.getElementById(Tax2Column[i]).style.display = "";
        }
        for (var i = 0; i < addonRecurringTax2Charges.length; i++) {
            if (addonRecurringTax2Charges[i] >= 0) {
                document.getElementById("checkoutPackageAddonUnproratedTax2Fee_" + i).style.display = "";
            }
        }
        
        if(showedTaxColumn){
            for (var i = 0; i < PriceColumn.length; i++) {
                document.getElementById(PriceColumn[i]).style.width = newPriceWidth2;
            }
            for (var i = 0; i < addonRecurringCharges.length; i++) {
                if (addonRecurringCharges[i] >= 0) {
                    document.getElementById("checkoutPackageAddonUnproratedFee_" + i).style.width = newPriceWidth2;
                }
            }
        }else{
            for (var i = 0; i < PriceColumn.length; i++) {
                document.getElementById(PriceColumn[i]).style.width = newPriceWidth;
            }
            for (var i = 0; i < addonRecurringCharges.length; i++) {
                if (addonRecurringCharges[i] >= 0) {
                    document.getElementById("checkoutPackageAddonUnproratedFee_" + i).style.width = newPriceWidth;
                }
            }
        }

        // Some additional logic for VAT in non default countries
        if (useVAT2 == "1" && vatCountry == "0" && document.getElementById('vatField').value != '' && validVATNumber == 1) {
            document.getElementById('checkoutTaxes2Rate').innerHTML = 0;
            document.getElementById('checkoutTaxes2Amount').innerHTML = formatAsMoney(0, true);

            for (var i = 0; i < Tax2Column.length; i++) {
                document.getElementById(Tax2Column[i]).style.display = "none";
            }
            for (var i = 0; i < addonRecurringTax2Charges.length; i++) {
                if (addonRecurringTax2Charges[i] >= 0) {
                    document.getElementById("checkoutPackageAddonUnproratedTax2Fee_" + i).style.display = "none";
                }
            }
            
            if(showedTaxColumn){
                for (var i = 0; i < PriceColumn.length; i++) {
                    document.getElementById(PriceColumn[i]).style.width = newPriceWidth;
                }
                for (var i = 0; i < addonRecurringCharges.length; i++) {
                    if (addonRecurringCharges[i] >= 0) {
                        document.getElementById("checkoutPackageAddonUnproratedFee_" + i).style.width = newPriceWidth;
                    }
                }
            }else{
                for (var i = 0; i < PriceColumn.length; i++) {
                    document.getElementById(PriceColumn[i]).style.width = oldPriceWidth;
                }
                for (var i = 0; i < addonRecurringCharges.length; i++) {
                    if (addonRecurringCharges[i] >= 0) {
                        document.getElementById("checkoutPackageAddonUnproratedFee_" + i).style.width = oldPriceWidth;
                    }
                }
            }

            taxes2 = 0;
        }
        
    }
    // coupon after taxes
    document.getElementById('checkoutDiscountPostTaxes').style.display = 'none';
    if (couponArr.length > 0 && couponArr[8] == 0) {
        document.getElementById('checkoutDiscountPostTaxesFee').innerHTML = (discount > 0? "- " : "") + formatAsMoney(discount, true);
        document.getElementById('checkoutDiscountPostTaxesName').innerHTML = couponArr[0];
        document.getElementById('checkoutDiscountPostTaxes').style.display = "block";
    }

    // Total
    var total = domainNameRegistrationFee
                + packageUnproratedFee
                + packageProratedFee
                + packageSetupFee
                + packageAddonsUnproratedFee
                + packageAddonsProratedFee
                + packageAddonsSetupFee
                - discount
                + taxes
                + taxes2;
    if (total < 0){
      total = 0;
    }
    mainForm.totalBill.value = total;
    document.getElementById('checkoutTotalFee').innerHTML = formatAsMoney(total, true);
    
    if(mainForm.selectedcurrency.value.toUpperCase() != mainForm.currency.value.toUpperCase()){
        document.getElementById('divdetails').style.display = "";
    }else{
        document.getElementById('divdetails').style.display = "none";
    }
}

function getSelectedCurrencyInfo()
{
    if(mainForm.currencyPrice != undefined && mainForm.currencyPrice.value != mainForm.selectedcurrency.value){
        new Ajax.Request(   'index.php?fuse=billing&action=GetCurrencyInfo&currency='+mainForm.currencyPrice.value,
                            {   onSuccess   : getSelectedCurrencyInfo_Callback}
        );
    }
}

function getSelectedCurrencyInfo_Callback(responseObj)
{
    respArr = responseObj.responseText.split("|");
    
    // Rate of the selected currency
    if (respArr[0] != "") {
        mainForm.selectedcurrencyrate.value = parseFloat(respArr[0]);
    }

    // Precision of the selected currency
    if (respArr[1] != "") {
        mainForm.selectedcurrencyprecision.value = parseInt(respArr[1]);
    }

    // Alignment of the selected currency
    if (respArr[2] != "") {
        mainForm.selectedcurrencyalignment.value = respArr[2];
    }

    // Symbol of the selected currency
    if(mainForm.currencyPrice.value == "EUR"){
        mainForm.selectedcurrencysymbol.value = mainForm.eursymbol.value;
    }else if(mainForm.currencyPrice.value == "GBP"){
        mainForm.selectedcurrencysymbol.value = mainForm.gbpsymbol.value;
    }else if(mainForm.currencyPrice.value == "JPY"){
        mainForm.selectedcurrencysymbol.value = mainForm.jpysymbol.value;
    }else if (respArr[3] != "") {
        mainForm.selectedcurrencysymbol.value = respArr[3];
    }

    mainForm.selectedcurrency.value = mainForm.currencyPrice.value;
    
    
    if(mainForm.selectedcurrency.value.toUpperCase() != mainForm.currency.value.toUpperCase()){
        document.getElementById('divdetails').style.display = "";
    }else{
        document.getElementById('divdetails').style.display = "none";
    }

    if (document.getElementById('coupon_code')) {
        if(document.forms['accountinfo'].elements['coupon_code'].value && validateCouponCount){
            validateCoupon(document.forms['accountinfo']);
        }
    }

    regenerateBill();
}

function formatAsMoney(mnt, format)
{
    mnt -= 0;
    
    if(mainForm.currencyPrice != undefined && mainForm.currencyPrice.value != mainForm.currency.value){
        var precision = mainForm.selectedcurrencyprecision.value;
        var alignment = mainForm.selectedcurrencyalignment.value;
        var symbol    = mainForm.selectedcurrencysymbol.value;
        
        var currencyRate = mainForm.currencyrate.value; // Rate of the base currency
        var selectedCurrencyRate = mainForm.selectedcurrencyrate.value; // Rate of the selected currency
        
        if(currencyRate != 0){
            mnt = mnt / currencyRate;
        }
        
        mnt = mnt * selectedCurrencyRate;
    }else{
        var precision = mainForm.currencyprecision.value;
        var alignment = mainForm.currencyalignment.value;
        var symbol    = mainForm.currencysymbol.value;
    }

    // prevent rounding issue
    factor = Math.pow(10, precision);
    mnt += 1 / (factor * 1000);
    mnt = Math.round(mnt * factor) / factor;
    // end rounding fix
    
    var tmnt = mnt;
    for (var i=0; i < precision; i++){
        if(intPow(mnt, 10, i) == Math.floor(intPow(mnt, 10, i))){
            if(i == 0){
                tmnt += '.';
            }
            for (var j=0; j < precision - i; j++){
                tmnt += '0';
            }
            break;
        }
    }

    if(format){
        if(alignment == 'left'){
            return symbol + tmnt;
        }else{
            return tmnt + symbol;
        }
    }else{
        return tmnt;
    }
}

// use only integers exponents
function intPow(number, base, exponent)
{
    var result = number;
    if(exponent > 0){
        for(var i=0; i<exponent; i++){
            result *= base;
        }
    }else if(exponent < 0){
        for(var i=0; i>exponent; i--){
            result /= base;
        }
    }
    return result;
}

function validateCoupon(form)
{   
    validateCouponCount = true;
    if (!form.coupon_code.value) {
        return;
    }

    var couponCode = encodeURIComponent(form.coupon_code.value);
    var clientType = encodeURIComponent(form.clienttype.value);
    // 'package' must be quoted or I get an error
    var plan = encodeURIComponent(form.elements['package'].value);
    if (typeof form.paymentterm == 'undefined') {
        var billingCycle = 0;
    } else {
        var billingCycle  = encodeURIComponent(form.paymentterm.value);
    }

    new Ajax.Request('index.php?fuse=clients&view=ValidateCoupon',
                     {parameters    : 'code='+couponCode+'&clienttype='+clientType+'&plan='+plan+'&billingcycle='+billingCycle,
                      onLoading     : function(){resetStatus(lang('Validating'))},
                      onComplete    : applyDiscount});
}

function applyDiscount(responseObj)
{
    unsetStatus(false);

    var responseArr = responseObj.responseText.split('|');

    couponArr = responseArr;
    
    if (couponArr == '0') {
        document.getElementById('invalid_code').style.display = '';
        document.getElementById('coupon_name').innerHTML = '';
        regenerateBill();
        return;
    }

    if (couponArr[2] != '0') {
        var discount = couponArr[4]*100+'%';
        var langDiscount = couponArr[4]*100; // can't pass % to lang()
    } else {
        var discount = formatAsMoney(couponArr[4], true);
        var langDiscount = discount;
    }

    document.getElementById('invalid_code').style.display = 'none';
    document.getElementById('coupon_name').innerHTML = couponArr[0] + ': '+discount+' '+lang('off');

    if (couponArr[2] != 0 &&  couponArr[3] != (  couponMaskAppliesToDomainNameRegistration
                                                | couponMaskAppliesToDomainNameTransfer
                                                | couponMaskAppliesToPackage
                                                | couponMaskAppliesToPackageSetup
                                                | couponMaskAppliesToAddons
                                                | couponMaskAppliesToAddonsSetup
                                                | couponMaskAppliesToOther))
    {
        var appliesTo = new Array();
        if (couponArr[3] & couponMaskAppliesToDomainNameRegistration) {
            appliesTo.push(lang("Domain Name Registration Fees"));
        }
        if (couponArr[3] & couponMaskAppliesToDomainNameTransfer) {
            appliesTo.push(lang("Domain Name Transfer Fees"));
        }
        if (couponArr[3] & couponMaskAppliesToPackage) {
            appliesTo.push(lang("Package Fees"));
        }
        if (couponArr[3] & couponMaskAppliesToPackageSetup) {
            appliesTo.push(lang("Package Setup Fees"));
        }
        if (couponArr[3] & couponMaskAppliesToAddons) {
            appliesTo.push(lang("Package Add-on Fees"));
        }
        if (couponArr[3] & couponMaskAppliesToAddonsSetup) {
            appliesTo.push(lang("Package Add-on Setup Fees"));
        }

        document.getElementById('coupon_name').innerHTML += ' ' + lang('on %', appliesTo.join(", "));
    }

    if (couponArr[5] != '0') {
        document.getElementById('coupon_name').innerHTML += ' '+lang('on every periodic payment');
        if (couponArr[7] != '0') {
            document.getElementById('coupon_name').innerHTML += ' '+lang('during % month(s), as follows:', couponArr[7])+'<br />';
            document.getElementById('coupon_name').innerHTML += lang('* % discount(s) of %', Math.ceil(couponArr[7]/couponArr[9]), langDiscount) + (couponArr[2] != 0? '%' : "");
            document.getElementById('coupon_name').innerHTML += ', '+lang('1 discount every % month(s)', couponArr[9]);
        }
    }

    regenerateBill();
}

function getTax()
{
    var country = "";
    var state = "";
    if (typeof countryVAR != 'undefined' && mainForm.elements[countryVAR] !=null){
        country = mainForm.elements[countryVAR].value;         
    }
    if (typeof stateVAR != 'undefined' && mainForm.elements[stateVAR] !=null){
        state = mainForm.elements[stateVAR].value;
    }

    if (country) {
        new Ajax.Request(   'index.php?fuse=clients&action=GetTax&country='+country+'&state='+state,
                            {   onSuccess   : setTax}
        );
    } else {
        regenerateBill();
    }
}

function setTax(responseObj)
{
    respArr = responseObj.responseText.split("|");
    mainForm.taxrate.value = respArr[0];
    mainForm.usevat.value = respArr[1];
    taxName = respArr[2];
    mainForm.tax2rate.value = respArr[3];
    mainForm.usevat2.value = respArr[4];
    tax2Name = respArr[5];
    mainForm.tax2compound.value = respArr[6];
    isTax2Compound = parseInt(respArr[6]);
    mainForm.vatCountry.value = respArr[7];
    
    taxRateDisp = respArr[0];
    taxRate = parseFloat(respArr[0]) / 100;
    tax2RateDisp = respArr[3];
    tax2Rate = parseFloat(respArr[3]) / 100;
    useVAT = respArr[1];
    useVAT2 = respArr[4];
    vatCountry = respArr[7];
    
    if(document.getElementById('vatField') != undefined){
        if(useVAT == "1" || useVAT2 == "1"){
            document.getElementById('vatField').disabled=false;
            
            if(document.getElementById('vatBlock') != undefined){
                document.getElementById('vatBlock').style.display='';
            }
        }else{
            document.getElementById('vatField').disabled=true;
            document.getElementById('vatField').value='';
            
            if(document.getElementById('vatBlock') != undefined){
                document.getElementById('vatBlock').style.display='none';
            }
        }
        
        checkVAT();
    }else{
        regenerateBill();
    }
}

function checkVAT()
{
    var country = '';
    var vatnum = '';
    
    if(typeof countryVAR != 'undefined' && mainForm.elements[countryVAR] !=null){
        country = mainForm.elements[countryVAR].value;
    }
    
    if(document.getElementById('vat_country') != undefined){
        // Greece has to be different...
        if (country == 'GR'){
            document.getElementById('vat_country').innerHTML = 'EL';
        }else{
            document.getElementById('vat_country').innerHTML = country;
        }
    }
    
    if(document.getElementById('vatField') != 'undefined' && document.getElementById('vatField').value != ''){
        vatnum = document.getElementById('vatField').value
    }
    
    if(country){
        new Ajax.Request(   'index.php?fuse=clients&action=CheckVAT&country='+country+'&vat='+vatnum,
                            {   onSuccess   : checkVAT_Callback}
        );
    }else{
        validVATNumber = 0;
        document.getElementById('vat_valid').style.display='none';
        document.getElementById('vat_invalid').style.display='none';
        //document.getElementById('vat_error').style.display='none';
        regenerateBill();
    }
}

function checkVAT_Callback(responseObj)
{
    respArr = responseObj.responseText.split("|");
    
    if(respArr[1] != ""
      && document.getElementById('vatField') != 'undefined'
      && document.getElementById('vatField').value == ""){
        document.getElementById('vatField').value = respArr[1];
    }
    
    switch(respArr[0]){
        /*
        //  UNCOMMENT WHEN SIGNUP INCLUDES THE VAT ERROR
        case "-1":
            validVATNumber = 0;
            document.getElementById('vat_valid').style.display='none';
            document.getElementById('vat_invalid').style.display='none';
            
            if(respArr[1] != ""){
                document.getElementById('vat_error').style.display='';
            }else{
                document.getElementById('vat_error').style.display='none';
            }
            break;
        */
        case "0":
            validVATNumber = 0;
            document.getElementById('vat_valid').style.display='none';
            //document.getElementById('vat_error').style.display='none';
            
            if(respArr[1] != ""){
                document.getElementById('vat_invalid').style.display='';
            }else{
                document.getElementById('vat_invalid').style.display='none';
            }
            break;
        case "1":
            validVATNumber = 1;
            document.getElementById('vat_valid').style.display='';
            document.getElementById('vat_invalid').style.display='none';
            //document.getElementById('vat_error').style.display='none';
            
            if(useVAT == "1"){
                document.getElementById('taxrate').value = 0;
            }
            
            if(useVAT2 == "1"){
                document.getElementById('tax2rate').value = 0;
            }
            break;
    }
    
    regenerateBill();
}

function ConfirmLogin_Callback(responseObj)
{
    //>0 = User is valid
    //0 = User is invalid
    var userid = responseObj.responseText;
    if (userid>0){
        document.getElementById('membervalid').innerHTML = '';
        country = document.forms['accountinfo'].elements['memberuserid'].value=userid;
        document.forms['accountinfo'].submit();                 
    }else{
        document.getElementById('membervalid').innerHTML = '<font color=red>'+lang('Invalid login information, please contact support for assistance')+'</font>';
    }
}

function ConfirmLogin()
{
    new Ajax.Request(   'index.php?fuse=clients&action=ConfirmLogin',
                        {parameters     : 'memberEmail=' + document.forms['accountinfo'].elements['member_email'].value + '&passwd=' + document.forms['accountinfo'].elements['member_password'].value,
                         onComplete     : ConfirmLogin_Callback});
}

function preConfirmLogin_Callback(responseObj)
{
    respArr = responseObj.responseText.split("|");
    //>0 = User is valid
    //0 = User is invalid

    var userid = parseInt(respArr[0]);
    if (userid>0){
        document.getElementById('membervalid').innerHTML = '<font color=green>'+lang('Valid login information')+'</font>';
        if (typeof countryVAR != 'undefined'){
            mainForm.elements[countryVAR].value = respArr[1];
        }
        if (typeof stateVAR != 'undefined'){
            mainForm.elements[stateVAR].value = respArr[2];
        }
        if(document.getElementById('vatField') != 'undefined'){
            document.getElementById('vatField').value = respArr[3];
        }
    }else{
        document.getElementById('membervalid').innerHTML = '<font color=red>'+lang('Invalid login information, please contact support for assistance')+'</font>';
    }
    getTax();
}

function preConfirmLogin()
{
    if(document.forms['accountinfo'].elements['member_email'].value != "" && document.forms['accountinfo'].elements['member_password'].value != ""){
        new Ajax.Request(   'index.php?fuse=clients&action=ConfirmLogin',
                            {parameters     : 'memberEmail=' + document.forms['accountinfo'].elements['member_email'].value + '&passwd=' + document.forms['accountinfo'].elements['member_password'].value + '&getinfo=1',
                             onComplete     : preConfirmLogin_Callback});
    }else{
        document.getElementById('membervalid').innerHTML = '';
    }
}

function SubmitStepTwo()
{
    if (document.getElementById('coupon_code')) {
        if(document.forms['accountinfo'].elements['coupon_code'].value && !validateCouponCount){
            if(confirm("You inserted a coupon code, but it has not validated. Do you want validate the coupon now?")){
                validateCoupon(document.forms['accountinfo']);
            }
        }
    }
    
    if(document.forms['accountinfo'].elements['member'].checked){
        // check if domaim credentials have to be entered
        if (document.getElementById("domainCredentials")) {
            if (document.getElementById("new_domain_username").value == "" || document.getElementById("new_domain_password").value == "") {
                alert(lang("Please fill the domain username and password"));
                return false;
            }
        }

        //check to see if their member information is accurate
        ConfirmLogin();
    }else{
        if (document.getElementById('new_password') != undefined && document.getElementById('new_password_confirm') != undefined && document.getElementById('new_password').value != document.getElementById('new_password_confirm').value) {
            alert(lang("Passwords do not match"));
            return false;
        }
        if ((useVAT == "1" || useVAT2 == "1") && !validVATNumber && document.getElementById('vatField') != undefined) {
            document.getElementById('vatField').value = '';
        }
        check(document.forms['accountinfo'],document.forms['accountinfo'].elements.length);                 
    }
    return false;
}

function SwitchRecurringVisibility(paymentGateway) 
{
    field = document.getElementById('recurringField');
    if (paymentGateway.value.indexOf('paypal') != -1 && paymentGateway.value.indexOf('paypalpro') == -1) {
        // don't allow paypal subscriptions when trial period > 90
        subscriptionsDisabled = document.getElementById('disablesubscriptions');
        if ( subscriptionsDisabled.value == '1') return;
        
        field.style.display = '';
    } else {
        field.style.display = 'none';
    }
}
