-1

My function formatMoney works when called from recalculateReservationFee() but my javascript console gives me the error "undefined function" when I try to call formatMoney from a sub function named recalculateGrandTotal() that's inside of recalculateReservationFee(). I don't understand why this is happening, how can I rescope the methods to access formatMoney from recalculateGrandTotal()?

Here's my code:

//money formatter from... http://stackoverflow.com/questions/9318674/javascript-number-currency-formatting
Number.prototype.formatMoney = function(decPlaces, thouSeparator, decSeparator) {
    var n = this,
        decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces,
        decSeparator = decSeparator == undefined ? "." : decSeparator,
        thouSeparator = thouSeparator == undefined ? "," : thouSeparator,
        sign = n < 0 ? "-" : "",
        i = parseInt(n = Math.abs(+n || 0).toFixed(decPlaces)) + "",
        j = (j = i.length) > 3 ? j % 3 : 0;
    return sign + (j ? i.substr(0, j) + thouSeparator : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thouSeparator) + (decPlaces ? decSeparator + Math.abs(n - i).toFixed(decPlaces).slice(2) : "");
};

function recalculateReservationFee() {
    var vacationRentalPrice = parseFloat( <? php echo $vacationRentalPrice; ?> );
    var TaxRate = parseFloat(document.getElementById("priceDisplayTaxRate").textContent);
    var CleaningFee = parseFloat(document.getElementById("priceDisplayCleaningFee").textContent);

    var PoolHeating = parseFloat(document.getElementById("priceDisplayPoolHeating").textContent);
    var PetFee = parseFloat(document.getElementById("priceDisplayPetFee").textContent);
    var PropertyDamageProtection = parseFloat(document.getElementById("priceDisplayPropertyDamageProtection").textContent);

    var reservationFeeTarget = vacationRentalPrice + TaxRate + CleaningFee + PoolHeating + PetFee + PropertyDamageProtection;
    var theNewReservationFee = parseFloat(( <? php echo $theReservationFeePercentage; ?> * reservationFeeTarget) / 100);

    var formattedNewReservationFee = theNewReservationFee.formatMoney(2, ',', '.'); // example "$3,543.76"
    document.getElementById("priceDisplayReservationFee").innerHTML = formattedNewReservationFee;

    //document.write('<br/>The Vars: <br/>' + vacationRentalPrice + '<br/>' + CleaningFee + '<br/>' + TaxRate + '<br/>' + PoolHeating + '<br/>' + PetFee + '<br/>' + PropertyDamageProtection + '<br/>--<br/>' + reservationFeeTarget + '<br/>' + formattedNewReservationFee);

    function recalculateGrandTotal() {
        var theGrandTotal = vacationRentalPrice + <? php echo $theRefundableDamageDepositRounded; ?> +TaxRate + CleaningFee + PoolHeating + PetFee + PropertyDamageProtection + formattedNewReservationFee;

        var formattedGrandTotal = theGrandTotal.formatMoney(2, ',', '.'); // example "$3,543.76"
        document.getElementById("priceDisplayVacationPackageTotal").innerHTML = formattedGrandTotal;
        document.getElementById("priceDisplayVacationPackageGrandTotal").innerHTML = formattedGrandTotal;
    }
    recalculateGrandTotal();
}

function clearFee(theNamedFee, theNamedClass) {
    document.getElementById(theNamedFee).innerHTML = '0.00';

    var elems = document.getElementsByClassName(theNamedClass);
    for (var i = 0; i < elems.length; i++) {
        elems[i].disabled = true;
    }
    recalculateReservationFee();
}

function showFee(theNamedFee, someFee, theNamedClass) {
    document.getElementById(theNamedFee).innerHTML = someFee;

    var elems = document.getElementsByClassName(theNamedClass);
    for (var i = 0; i < elems.length; i++) {
        elems[i].disabled = false;
    }
    recalculateReservationFee();
}
SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
Alex Williams
  • 99
  • 1
  • 3
  • 15

1 Answers1

1

The line

var theGrandTotal = vacationRentalPrice + ....  + formattedNewReservationFee; 
                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^

formattedNewReservationFee sounds like it is a string. number + "string" = "numberstring".

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Thank you epascarello! I don't code in javascript often. PHP is more forgiving when it comes to casting. Have a good evening :) – Alex Williams Oct 17 '14 at 19:19