<script language="JavaScript" type="text/javascript">
    var stateVAR = "";
    var countryVAR = "";

    // subtotalVAR = setup + package price + prorated amount
    var subtotalVAR = 0;

    // tSubtotalVAR = setup + package price + prorated amount + domain name + package addons
    var tSubTotalVAR = 0;

    var addonsRecurringChargeVAR = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); // only 1st, 3rd, 6th and 12th indexes are used (M, Q, S, A)
    var addonsRecurringChargeProratedVAR = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    var addonsSetupChargeVAR = 0;

    var discountVal = 0;
    var currencysymbolVAR = "";
    var taxableVAR = "";
    var gTaxRate = 0;
    var domainpriceVAR = 0;
    
    function RecalculatePage()
    {
        document.forms['accountinfo'].elements['step'].value = 2;
        document.forms['accountinfo'].submit();
    }

    function formatAsMoney(mnt)
    {
        mnt -= 0;
        mnt = (Math.round((mnt+0.00001)*100))/100;
        return (mnt == Math.floor(mnt)) ? mnt + '.00'
                  : ( (mnt*10 == Math.floor(mnt*10)) ?
                           mnt + '0' : mnt);
    }

    function GoBack()
    {
        document.forms['accountinfo'].elements['step'].value=0;
        document.forms['accountinfo'].elements['clienttype'].value=0;
        document.forms['accountinfo'].elements['package'].value=0;
        document.forms['accountinfo'].elements['oldpackage'].value=0;
        document.forms['accountinfo'].submit();
    }

    function GetTax()
    {
        if (taxableVAR != 1) {
            return;
        }

        var country = "";
        var state = "";
        if (document.forms['accountinfo'].elements[countryVAR] !=null){
            country = document.forms['accountinfo'].elements[countryVAR].value;         
        }
        if (document.forms['accountinfo'].elements[stateVAR] !=null){
            state = document.forms['accountinfo'].elements[stateVAR].value;
        }

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

    function AddDomainPrice(myObj)
    {
        var domainyear='';
        aOptionPairs = myObj.value.split('|');
        domainpriceVAR = aOptionPairs[1];
        if (parseInt(aOptionPairs[0])==1){
             domainyear = '1 [l]Year[/l]';
        }else if(parseInt(aOptionPairs[0])==0){
             domainyear = 'One time payment';
        }else{
             domainyear = aOptionPairs[0] + ' [l]Years[/l]';                 
        }

        $('domainprice').innerHTML = currencysymbolVAR+' '+formatAsMoney(aOptionPairs[1]);
        $('domainyear').innerHTML = domainyear;
        CalculateSubTotal();
    }

    function CalculateSubTotal()
    {
        tSubTotalVAR =   parseFloat(subtotalVAR)
                       + parseFloat(domainpriceVAR)
                       + parseFloat(addonsSetupChargeVAR);
        if ($('prorating').value == 1) {
            tSubTotalVAR +=   parseFloat(addonsRecurringChargeProratedVAR[1])
                            + parseFloat(addonsRecurringChargeProratedVAR[3])
                            + parseFloat(addonsRecurringChargeProratedVAR[6])
                            + parseFloat(addonsRecurringChargeProratedVAR[12]);
        }
        if ($('prorating').value == 0 || $('includeFollowingPayment').value == 1) {
            tSubTotalVAR +=  parseFloat(addonsRecurringChargeVAR[1])
                            + parseFloat(addonsRecurringChargeVAR[3])
                            + parseFloat(addonsRecurringChargeVAR[6])
                            + parseFloat(addonsRecurringChargeVAR[12]);
        }

        $('subtotaldiv').innerHTML = currencysymbolVAR+' '+formatAsMoney(tSubTotalVAR);
        SetTax();               
    }

    function ConfirmLogin_Callback(responseObj)
    {
        //>0 = User is valid
        //0 = User is invalid
        var userid = responseObj.responseText;
        if (userid>0){
            $('membervalid').innerHTML = '';
            country = document.forms['accountinfo'].elements['memberuserid'].value=userid;
            document.forms['accountinfo'].submit();                 
        }else{
            $('membervalid').innerHTML = '<font color=red>[l]Invalid login information, please contact support for assistance[/l]</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 SetTax(responseObj)
    {
        if (typeof responseObj != 'undefined') {
            taxrate = responseObj.responseText;
            gTaxRate = taxrate;
        }

        var total = tSubTotalVAR + parseFloat(discountVal);
        if (gTaxRate) {
            var tax = total * gTaxRate/100;
            total += tax;
            $('tax').innerHTML = '<font class=signuptotal>[l]Tax[/l] '+gTaxRate+''+'%</font>';
            $('taxparam').innerHTML = currencysymbolVAR+' '+formatAsMoney(tax);
            document.forms['accountinfo'].elements['taxrate'].value = gTaxRate;
        }
        $('totalparam').innerHTML = currencysymbolVAR+' '+formatAsMoney(total);
        $('totalBill').value = total;

        if (discountVal || gTaxRate) {
            $('subtotaldiv').style.display = 'block';
            $('subtotalLabel').style.display = 'block';
        }
    }
    
    function SubmitStepTwo()
    {
        if(document.forms['accountinfo'].elements['member'].checked){
            //check to see if their member
            //information is accurate
            ConfirmLogin();
        }else{
            check(document.forms['accountinfo'],document.forms['accountinfo'].elements.length);                 
        }
        return false;
    }
    
    function BackStepTwo()
    {
        <!-- BEGIN DYNAMIC BLOCK: allowstep1 -->
            document.forms['accountinfo'].elements['step'].value=1;
        <!-- END DYNAMIC BLOCK: allowstep1 -->
        <!-- BEGIN DYNAMIC BLOCK: notallowstep1 -->
            document.forms['accountinfo'].elements['step'].value=0;             
            document.forms['accountinfo'].elements['clienttype'].value=0;
            document.forms['accountinfo'].elements['package'].options[document.forms['accountinfo'].elements['package'].selectedIndex].value =0;                    
        <!-- END DYNAMIC BLOCK: notallowstep1 -->           
        document.forms['accountinfo'].submit();
    }

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

    function validateCoupon(form)
    {
        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 discountArr = responseObj.responseText.split('|');

        if (discountArr == '0') {
            $('invalid_code').style.visibility = 'visible';
            return;
        }

        if (discountArr[2] != '0') {
            var discount = discountArr[3]*100+'%';
            var value = formatAsMoney(tSubTotalVAR * discountArr[3]);
        } else {
            var discount = currencysymbolVAR+discountArr[3];
            var value = formatAsMoney(discountArr[3]);
        }
        discountVal = -value;
        $('discount').value = discountVal;

        $('invalid_code').style.visibility = 'hidden';
        $('coupon_name').innerHTML = discountArr[0] + ': '+discount+' '+lang('off');
        if (discountArr[4] != '0') {
            $('coupon_name').innerHTML += ' '+lang('on every periodic payment');
        }
        $('coupon_name').style.display = 'block';
        $('coupon_description').innerHTML = discountArr[1];
        $('coupon_description').style.display = 'block';

        $('discount_description').innerHTML = discountArr[0];
        $('discountLabelDiv').style.display = 'block';
        $('discountValueDiv').innerHTML = '-' + ' ' + currencysymbolVAR + ' ' + value;
        $('discountValueDiv').style.display = 'block';

        CalculateSubTotal();
    }

    function applyAddonsCharges()
    {
        // global vars
        addonsRecurringChargeVAR = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        addonsRecurringChargeProratedVAR = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        addonsSetupChargeVAR = 0;

        var addonChargesArr = new Array();
        
        
        var existStyle = $('tableAddons').getElementsByTagName('select');
        
        if(existStyle.length > 0) {
            var addonSelects = $('tableAddons').getElementsByTagName('select');
        } else {
            var addonSelects = $('tableAddons').getElementsByTagName('input');
        }
        for (var i = 0; i < addonSelects.length; i++) {
            if (existStyle.length > 0 || addonSelects[i].checked ) {
                var idsAndCycle = addonSelects[i].value.split('_');
                addonsRecurringChargeVAR[idsAndCycle[3]] += addonRecurringPrices[idsAndCycle[1]][idsAndCycle[2]][idsAndCycle[3]].fullPrice;
                addonsRecurringChargeProratedVAR[idsAndCycle[3]] += addonRecurringPrices[idsAndCycle[1]][idsAndCycle[2]][idsAndCycle[3]].proratedPrice;
                addonsSetupChargeVAR += addonSetupPrices[idsAndCycle[1]][idsAndCycle[2]];
                addonChargesArr.push(idsAndCycle[1] + '_' + idsAndCycle[2] + '_' + idsAndCycle[3]);
            }
        }

        $('addonsRecurringChargesProratedLabelDiv').style.display = 'none';
        $('addonsRecurringChargesProratedValueDiv').style.display = 'none';
        var hasProratedAddons = false;
        var totalAddonsProrating = 0;
        var cycles = new Array(1, 3, 6, 12);
        for (var i = 0; i < cycles.length; i++) {
            if (addonsRecurringChargeVAR[cycles[i]] > 0 && ($('prorating').value == 0 || $('includeFollowingPayment').value == 1)) {
                $('addonsRecurringChargesLabelDiv'+cycles[i]).style.display = 'block';
                $('addonsRecurringChargesValueDiv'+cycles[i]).innerHTML = currencysymbolVAR + ' ' + formatAsMoney(addonsRecurringChargeVAR[cycles[i]]);
                $('addonsRecurringChargesValueDiv'+cycles[i]).style.display = 'block';
            } else {
                // hide previously shown addon prices when selecting another addon
                $('addonsRecurringChargesLabelDiv'+cycles[i]).style.display = 'none';
                $('addonsRecurringChargesValueDiv'+cycles[i]).style.display = 'none';
            }
            
            if ($('prorating').value == 1 && addonsRecurringChargeProratedVAR[cycles[i]] > 0) {
                var hasProratedAddons = true;
                totalAddonsProrating += addonsRecurringChargeProratedVAR[cycles[i]];
            }
        }
        if (hasProratedAddons) {
            $('addonsRecurringChargesProratedLabelDiv').style.display = 'block';
            $('addonsRecurringChargesProratedValueDiv').innerHTML = currencysymbolVAR + ' ' + formatAsMoney(totalAddonsProrating);
            $('addonsRecurringChargesProratedValueDiv').style.display = 'block';
        }

        if (addonsSetupChargeVAR > 0) {
            $('addonsSetupChargesLabelDiv').style.display = 'block';
            $('addonsSetupChargesValueDiv').innerHTML = currencysymbolVAR + ' ' + formatAsMoney(addonsSetupChargeVAR);
            $('addonsSetupChargesValueDiv').style.display = 'block';
        } else {
            $('addonsSetupChargesLabelDiv').style.display = 'none';
            $('addonsSetupChargesValueDiv').style.display = 'none';
        }
        $('addonChargesStr').value= addonChargesArr.join('|');

        CalculateSubTotal();
    }
</script>
