function showSavings()
{
    if (fieldsAreValid())
    {
        // reference DOM elements on the page
        var poundsUsedTextBox = document.getElementById('poundsUsedTextBox')
        var avgCostPerPoundTextBox = document.getElementById('avgCostPerPoundTextBox')
        var yourResinCostSpan = document.getElementById('yourResinCostSpan')
        var potentialSavingsSpan = document.getElementById('potentialSavingsSpan')
        var action = document.getElementById('action')
        
        // extract values from DOM elements as floats
        var poundsUsed = parseFloat(poundsUsedTextBox.value);
        var avgCostPerPound = parseFloat(avgCostPerPoundTextBox.value);

        // calculate cost and savings
        var avgRtiClientCostSavings = 0.03;
        var yourResinCost = poundsUsed * avgCostPerPound;
        var potentialSavings = yourResinCost - (poundsUsed * (avgCostPerPound - avgRtiClientCostSavings));

        // inject cost and savings values into HTML
        yourResinCostSpan.innerHTML = '<b>' + formatCurrency(yourResinCost) + '</b>';
        potentialSavingsSpan.innerHTML = '<b>' + formatCurrency(potentialSavings) + '</b>';
        action.style.display = "block";
    }
}

function fieldsAreValid()
{
    var valid = 
        validateField('poundsUsedTextBox', 'Pounds Used') &&
        validateField('avgCostPerPoundTextBox', 'Avg. Cost Per Pound');
    return valid;
}

function validateField(fieldId, fieldName)
{
    var field;
    field = document.getElementById(fieldId);

    if (field == null)
    {
        alert('Unable to find "' + fieldId + '" in the DOM');
        return false;
    }
    
    if (!isNumeric(field.value))
    {
        alert('"' + fieldName + '" contains a non numeric value: ' + field.value);
        return false;
    }
    
    return true;
}

function removeDollarSign(value)
{
    return field.value.replace(/^\$/, '');
}

function isNumeric(x) 
{
    var rgx = /^[0-9]*\.?[0-9]+$/;
    var match = x.match(rgx);
    var isMatch = (match != null);
    return isMatch;
}

function formatFloat(floatVal)
{
    var decimalPlaces = 2;
    var m = Math.pow(10, decimalPlaces);
    var formatted = parseInt(floatVal * m, 10) / m;
    return formatted;
}

function formatCurrency(value) 
{
    // globally replace '$' and ',' in value
    //value = value.toString().replace(/\$|\,/g, '');
    
    // booleanize whether or not a sign is needed to represent a debit 
    //var sign = (value == (value = Math.abs(value)));
    
    // promote the value by a factor of 100 and seed any rounding with .50000000001
    var bigValue = Math.floor(value * 100 + 0.50000000001);
    
    // extract the sub dollar amount (cents) using modulus
    var cents = bigValue % 100;
    
    // demote the bigValue by a factor of 100 to get dollar amount only
    var dollars = Math.floor(bigValue / 100).toString();
    
    // pad cents with a zero
    var formattedCents;
    if (cents < 10)
        formattedCents = "0" + cents;
    else
        formattedCents = cents.toString();
        
    // loop through the chars in dollar and add commas as necessary
    var i = 0;
    var p = 0;
    var formattedDollars = '';

    for (var i = dollars.length - 1; i >= 0; i--)
    {
        formattedDollars = dollars.substr(i, 1) + formattedDollars;
        
        if (p == 2)
        {
            p = 0;
            
            if (i > 0) { formattedDollars = "," + formattedDollars; }
        }
        else
        {
            p++;
        }
    }

    //var formatted = (((sign)?'':'-') + '$' + value + '.' + cents);
    var formatted = '$' + formattedDollars + '.' + formattedCents;
    
    return formatted
}
