Can I use a global variable with getElementById?
<p>I'm new to programming and cannot figure out how to use the global variables in my code correctly. I'm trying to use the global variables as my parameters when I call certain functions. What can I do to make this work? Any suggestions or advice would be much appreciated thank you.</p>

<pre><code>&lt;!DOCTYPE HTML&gt;
&lt;html lang="en-us"&gt;
    &lt;head&gt;
        &lt;meta charset="utf-8"&gt;
        &lt;title&gt;Loan Calculator&lt;/title&gt;
        &lt;script type="text/javascript"&gt;

            /* Input: Get the principal, intrest rate, years, payments per year and number of payments to date
             * Processing:  Calculating the monthly payment of a loan and loan payoff amount
             * Output: The monthly payment and payoff amount
             */

            var inv = document.getElementById("invest").value;
            var r = document.getElementById("rate").value;
            var num = document.getElementById("numyear").value;
            var per = document.getElementById("periods").value;
            var pay = document.getElementById("pay").value;
            var invest = parseFloat(inv);
            var rate = parseFloat(r);
            var numyear = parseFloat(num);
            var periods = parseFloat(per);
            var paytodate = parseFloat(pay);

            function doPayment() {
                var result = computePayment(invest, rate, numyear, periods);
                document.getElementById("output").innerHTML = result;

            }

            function doBalance() {
                var result = computeBalance(invest, rate, numyear, periods, paytodate);
                document.getElementById("secondOut").innerHTML = result;
            }

            function computePayment(principal, annualRate, years, periodsPerYear) {
                var p = (principal * annualRate / periodsPerYear) / (1 - Math.pow((1 + annualRate / periodsPerYear), -years * periodsPerYear));
                return p.toFixed(2);

            }

            function computeBalance(principal, annualRate, years, periodsPerYear, numberOfPaymentsPaidToDate) {
                var r = computePayment(invest, rate, numyear, periods);
                var f = principal * (Math.pow(1 + annualRate / periodsPerYear, numberOfPaymentsPaidToDate)) -
                    (r * ((Math.pow(1 + annualRate / periodsPerYear, numberOfPaymentsPaidToDate)) - 1) / (annualRate / periodsPerYear));
                return f.toFixed(2);
            }

        &lt;/script&gt;
    &lt;/head&gt;

    &lt;body&gt;
        &lt;h1&gt;Loan Calculator&lt;/h1&gt;&lt;br&gt;
        Amount Borrowed &lt;input type="text" id="invest" size="5"&gt; &lt;br&gt;
        Annual Rate &lt;input type="text" id="rate" size="5"&gt;&lt;br&gt;
        Number of years &lt;input type="text" id="numyear" size="5"&gt;&lt;br&gt;
        Payments per year &lt;input type="text" id="periods" size="5"&gt;&lt;br&gt;
        Number of payments paid to date  &lt;input type="text" id="pay" size="5"&gt;&lt;br&gt;
        &lt;button type="button" onclick="doPayment()"&gt;Payment&lt;/button&gt; &lt;div id="output"&gt;&lt;/div&gt; &lt;br&gt;
        &lt;button type="button" onclick="doBalance()"&gt;Payoff amount&lt;/button&gt;&lt;div id="secondOut"&gt;&lt;/div&gt;&lt;br&gt;
        &lt;input type="button" value="Reset Form" onClick="reset"&lt;button/&gt;

    &lt;/body&gt;
&lt;/html&gt;
</code></pre>