

    /* $Id: account.js 31160 2010-05-27 05:29:49Z mmohta $ */

   function makePayment(e) {
        obj = getObjectFromEvent(e);
        amount = document.getElementById('amount').value;

        // make sure we don't have any html code or 2 "$" signs in the amount text
        var re = /<\S[^>]*>/g;
        amount = amount.replace(re, '');
        if (amount.indexOf('$') == -1) {
            amount = '$' + amount;
        }

        obj.disabled = true;
        if (checkForm(obj.form) && confirm('You are about to make a payment of ' + amount + ' AUD.\n\nThis process may take up to 45 seconds.')) {
            obj.form.submit();
        } else {
            obj.disabled = false;
        }
    }

    function upgradeAccount() {
        location.href = 'upgrade.php';
    }

    function invoiceAddSubmit(id) {
        if(!id) {
            alert("Please select an account first");
            return false;
        }

        document.getElementById('account_id').value = id;
        document.forms[0].submit();
    }

    /*
 * Referral Functions
 */

    function openReferralSignUp(e, use_wysiwyg)
    {
        var url = '/em/forms/get_ref_code.php';
        var owin = openWindow(e, url, 640, 480, 'resizable=yes, scrollbars=yes');
        owin.focus();
    }


    function userPayInvoices(e, invoice_ids) {
        if (invoice_ids) {
            var url = package_root + '/payment/payment.php?ids=' + invoice_ids;
            window.location = url;
        } else {
            customInfoBubble(getObjectFromEvent(e), 'Please select at least one Invoice.');
        }
    }

    function userPrintInvoice(e, invoice_ids)
    {
        if (invoice_ids) {
            var url = package_root + '/acc_print_invoice.php?ids=' + invoice_ids;
            //var win = openWindow(e,url,screen.availWidth-5,screen.availHeight-30, 'scrollbars=yes, top=0, left=0, menubar=1');
            var win = openWindow(e,url,750,600, 'scrollbars=yes, top=0, left=0, menubar=1, resizable=yes');
            win.focus();
        } else {
            customInfoBubble(getObjectFromEvent(e), 'Please select at least one Invoice.');
        }
    }

    function userPreviewInvoice(e, invoice_id)
    {
        if(getIdCount() == 1) {
            if (invoice_id) {
                var url = package_root + '/acc_invoice_preview.php?id=' + invoice_id;
                var owin = openWindow(e,url,650,490, 'resizable=yes, scrollbars=yes');
                owin.focus();
            } else {
                alert('Please select an Invoice to preview');
            }
        } else {
            if(getIdCount() > 1) {
                customInfoBubble(getObjectFromEvent(e), 'You can only preview one Invoice at a time.  Please de-select ' + (getIdCount()-1) + (getIdCount() == 2 ? inv = ' invoice' : inv = ' invoices'));
            } else {
                customInfoBubble(getObjectFromEvent(e), 'Please select at least one Invoice.');
            }
        }
    }

    function downloadInvoice(e, invoice_id)
    {
        if (invoice_id) {
            var url = package_root + '/invoice/download.php?id=' + invoice_id + '&accept_disclaimer=true';
            var content = 'This download consists of Invoice lines and values contained in Invoices received from '+ billing_parent_company_name +'.\r\n\r\n' +
                          billing_parent_company_name + ' cannot guarantee that these values accurately reflect the System Usage for you or your Accounts,' +
                          ' and adjustments may be made to future Invoices to resolve any Billing issues experienced in the previous period.\r\n\r\n' +
                          'Any adjustments will appear on subsequent Invoices, and will be available for download from this screen.';

            delayedConfirm(e, 5, content, url, 0, 0, '', '', '', '', 350, 230);
        } else {
            customInfoBubble(getObjectFromEvent(e), 'Please select an Invoice.');
        }
    }

    function createHiddenField(field_name, field_value) {
        field_obj = parent.document.createElement('INPUT');
        field_obj.setAttribute('type', 'hidden');
        field_obj.setAttribute('name',  field_name);
        field_obj.setAttribute('value', field_value);
        return field_obj;
    }


    function viewDetailedUsage(id){
        openWindow(null, 'usage/usage_detail.php?id=' + id, 550, 550);
    }




    function submitPayment(e, btn) {
        if (btn) {
            btn.disabled = true;
            btn.value    = "Please wait...";
        }
    }

    function updateApproverDropDown(obj, default_user) {
        if(obj.value == 0) {
            setSelection(0, document.getElementById('db_force_approver_id'));
            document.getElementById('db_force_approver_id').disabled = true;
        } else {
            setSelection(default_user, document.getElementById('db_force_approver_id'));
            document.getElementById('db_force_approver_id').disabled = false;
        }
    }


    function updateRequiresApprovalDropDown(obj) {
        if(obj.value != 0) {
            setSelection(1, document.getElementById('db_require_approval'));
        }
    }


    function toggleDbSettingsFields(value) {
        // 2 = creation permissions
        if((value == 2) || (value == 3)) {
            document.getElementById('db_require_approval').disabled = false;
            document.getElementById('db_force_approver_id').disabled = false;
        } else {
            setSelection(0, document.getElementById('db_force_approver_id'));
            setSelection(0, document.getElementById('db_require_approval'));
            document.getElementById('db_require_approval').disabled = true;
            document.getElementById('db_force_approver_id').disabled = true;
        }
    }



    function viewPricing(e, url) {
        var win = openWindow(e, url, 650, 600, 'scrollbars=yes,resizable=yes');
        win.focus();
    }


    function viewPayment(e, id) {

        var payment_id;

        if (id) {
            payment_id = id;
        } else if (getRecordId()) {
            payment_id = getRecordId();
        } else {
            customInfoBubble(getObjectFromEvent(e), 'Please select a payment');
            return false;
        }

        var win = openWindow(e, 'details.php?id=' + payment_id, 500, 421, 'scrollbars=no,resizable=yes');
        win.focus();
        return true;
    }

    function viewTerms(e, t, p) {
        var oWin = openWindow(e, '/main/terms_conditions.php?type=' + t + '&popup=' + p, 600,450,'resizable=yes, scrollbars=yes');
        oWin.focus();
    }

    // Used by HTML::creditCardField()
    function toggleCcvLocation(side, fld_name) {
        var str = 'Your credit card verification number can be found on the ' + side + ' of your card';
        var ccv = document.getElementById(fld_name + '[ccv]');
        var ccvtext = document.getElementById('ccvtext');
        if(ccvtext && ccv) {
            ccvtext.innerText = str;
            if (side == 'back') {
                if (ccv.value.length > 3) {
                    ccv.value = ccv.value.substr(0, 3);
                }
                ccv.maxLength = 3;
            } else if (side == 'front') {
                ccv.maxLength = 4;
            }
        }
    }

    function editReferralCode(e, id) {
        if (id) {
            var owin = openWindow(e, package_root + '/referrals/edit_code.php?id=' + id, 250, 140);
            owin.focus();
        } else {
            customInfoBubble(getObjectFromEvent(e), 'Please select a referral code');
        }
    }

    function deleteReferralCode(e, id) {
        if (id) {
            delayedConfirm(e, 3, 'Are you certain you wish to delete the selected referral code?', package_root + '/referrals/del_code.php?id=' + getRecordId() + '&r=' + encodeURIComponent(document.location), 0, 0, '');
        } else {
            customInfoBubble(getObjectFromEvent(e), 'Please select a referral code');
        }
    }

    function newReferralCode(e) {
        var owin = openWindow(e, package_root + '/referrals/edit_code.php', 250, 140);
        owin.focus();
    }
