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();
}
The line
var theGrandTotal = vacationRentalPrice + .... + formattedNewReservationFee;
^^^^^^^^^^^^^^^^^^^^^^^^^^
formattedNewReservationFee sounds like it is a string. number + "string" = "numberstring".
Related
i want to display TravelTimeHoursDiff and TravelTimeMinutesDiff in double digit now my time is shown as 7:0 i want to display like 07:00
if ($scope.DispatchStatus.ArrivalTime != undefined){
var today = $rootScope.getSysDate().split(" ");
var timeArrival = new Date(today[0] + ' ' + $scope.DispatchStatus.ArrivalTime);
var TravelTime = new Date(today[0] + ' ' + $scope.Route.TravelTime);
var timeArrivalHours = timeArrival.getHours();
var TravelTimeHoursDiff = timeArrivalHours - TravelTime.getHours() ;
var TravelTimeMinutesDiff = (timeArrival.getMinutes() - TravelTime.getMinutes());
if(TravelTimeHoursDiff < 0 || (TravelTimeHoursDiff <= 0 && TravelTimeMinutesDiff < 0) || (TravelTimeHoursDiff == 0 && TravelTimeMinutesDiff == 0)){
$scope.formvalidationbit = $scope.DispatchStatusAddForm[fieldName].$invalid = true;
angular.element('#' + fieldName).addClass('ng-invalid');
angular.element('#' + fieldName).removeClass('ng-valid');
$scope.DispatchStatusAddForm.$valid = false;
var errorbit = 1;
}else{
if (isNaN(TravelTimeHoursDiff)) {
TravelTimeHoursDiff = '--';
}
if (isNaN(TravelTimeMinutesDiff)) {
TravelTimeMinutesDiff = '--';
}
if(TravelTimeMinutesDiff <0){
TravelTimeMinutesDiff = TravelTimeMinutesDiff * (-1);
}
$scope.TravelTime = TravelTimeHoursDiff + ':' + TravelTimeMinutesDiff;
}
}
Just add leading 0 to values smaller then 10, something like:
let addLeadingZero(v){
return v < 10 ? ("0" + v) : v;
}
$scope.TravelTime = addLeadingZero(TravelTimeHoursDiff) + ':' + addLeadingZero(TravelTimeMinutesDiff);
I am working on a blog with custom template which includes this numbered page navigation script below. Script is working in all pages except search results for queries and labels!!! I tried some changes but as I am not a javascript expert and I couldn't make it work... So, any kind of help would be really appreciated!!!
var pageCount = 9;
var displayPageNum = 3;
var upPageWord = "<i class='fa fa-angle-left'></i>";
var downPageWord = "<i class='fa fa-angle-right'></i>";
function showpageCount(x) {
var C = home_page_url;
var E = new Array();
var y = 1;
var H = 1;
var v = 0;
var p = 0;
var G = 0;
var F = "";
var J = "";
var w = "";
for (var z = 0, A; A = x.feed.entry[z]; z++) {
var u = A.published.$t.substring(0, 19) + A.published.$t.substring(23, 29);
timestamp = encodeURIComponent(u);
var i = A.title.$t;
if (i != "") {
if (v == 0 || (v % pageCount == (pageCount - 1))) {
if (C.indexOf(timestamp) != -1) {
y = H
}
if (i != "") {
H++
}
E[E.length] = "/search?updated-max=" + timestamp + "&max-results=" + pageCount
}
}
v++
}
for (var D = 0; D < E.length; D++) {
if (D >= (y - displayPageNum - 1) && D < (y + displayPageNum)) {
if (p == 0 && D == y - 2) {
if (y == 2) {
J = '<span class="showpage">' + upPageWord + "</span>"
} else {
J = '<span class="showpage">' + upPageWord + "</span>"
}
p++
}
if (D == (y - 1)) {
F += '<span class="showpagePoint">' + y + "</span>"
} else {
if (D == 0) {
F += '<span class="showpageNum">1</span>'
} else {
F += '<span class="showpageNum">' + (D + 1) + "</span>"
}
}
if (G == 0 && D == y) {
w = '<span class="showpage"> ' + downPageWord + "</span>";
G++
}
}
}
if (y > 1) {
F = "" + J + " " + F + " "
}
F = '<div class="showpageArea">' + F;
if (y < (H - 1)) {
F += w
}
if (H == 1) {
H++
}
F += "</div>";
var I = document.getElementsByName("pageArea");
var B = document.getElementById("blog-pager");
if (H <= 2) {
F = ""
}
for (var D = 0; D < I.length; D++) {
I[D].innerHTML = F
}
if (I && I.length > 0) {
F = ""
}
if (B) {
B.innerHTML = F
}
}
function showpageCount2(A) {
var F = home_page_url;
var G = new Array();
var J = F.indexOf("/search/label/") != -1;
var M = J ? F.substr(F.indexOf("/search/label/") + 14, F.length) : "";
M = M.indexOf("?") != -1 ? M.substr(0, M.indexOf("?")) : M;
var B = 1;
var L = 1;
var y = 0;
var p = 0;
var K = 0;
var I = "";
var P = "";
var z = "";
var N = '<span class="showpageNum"><a href="/search/label/' + M + "?&max-results=" + pageCount + '">';
var F = home_page_url;
for (var C = 0, D; D = A.feed.entry[C]; C++) {
var x = D.published.$t.substring(0, 19) + D.published.$t.substring(23, 29);
timestamp = encodeURIComponent(x);
var i = D.title.$t;
if (i != "") {
if (y == 0 || (y % pageCount == (pageCount - 1))) {
if (F.indexOf(timestamp) != -1) {
B = L
}
if (i != "") {
L++
}
G[G.length] = "/search/label/" + M + "?updated-max=" + timestamp + "&max-results=" + pageCount
}
}
y++
}
for (var H = 0; H < G.length; H++) {
if (H >= (B - displayPageNum - 1) && H < (B + displayPageNum)) {
if (p == 0 && H == B - 2) {
if (B == 2) {
P = N + upPageWord + "</a></span>"
} else {
P = '<span class="showpage">' + upPageWord + "</span>"
}
p++
}
if (H == (B - 1)) {
I += '<span class="showpagePoint">' + B + "</span>"
} else {
if (H == 0) {
I = N + "1</a></span>"
} else {
I += '<span class="showpageNum">' + (H + 1) + "</span>"
}
}
if (K == 0 && H == B) {
z = '<span class="showpage"> ' + downPageWord + "</span>";
K++
}
}
}
if (B > 1) {
if (!J) {
I = "" + P + " " + I + " "
} else {
I = "" + P + " " + I + " "
}
}
I = '<div class="showpageArea">' + I;
if (B < (L - 1)) {
I += z
}
if (L == 1) {
L++
}
I += "</div>";
var O = document.getElementsByName("pageArea");
var E = document.getElementById("blog-pager");
if (L <= 2) {
I = ""
}
for (var H = 0; H < O.length; H++) {
O[H].innerHTML = I
}
if (O && O.length > 0) {
I = ""
}
if (E) {
E.innerHTML = I
}
}
var home_page_url = location.href;
var thisUrl = home_page_url;
if (thisUrl.indexOf("/search/label/") != -1) {
if (thisUrl.indexOf("?updated-max") != -1) {
var lblname1 = thisUrl.substring(thisUrl.indexOf("/search/label/") + 14, thisUrl.indexOf("?updated-max"))
} else {
var lblname1 = thisUrl.substring(thisUrl.indexOf("/search/label/") + 14, thisUrl.indexOf("?&max"))
}
}
var home_page = "/";
if (thisUrl.indexOf("?q=") == -1) {
if (thisUrl.indexOf("/search/label/") == -1) {
document.write('<script src="' + home_page + 'feeds/posts/summary?alt=json-in-script&callback=showpageCount&max-results=99999" ><\/script>')
} else {
document.write('<script src="' + home_page + "feeds/posts/full/-/" + lblname1 + '?alt=json-in-script&callback=showpageCount2&max-results=99999" ><\/script>')
}
};
We are currently facing the same problem. The pagination links are good for even pages, but not for off pages. The whole script seems quite buggy, but only for 'search keywords'. Will post results if we achieve any progress.
I want to show a div when I click on any item of the tree here
the tree is loaded in the index.html via this iframe
<IFRAME NAME="treeframe" ID="treeframe" SRC="tree.htm?150921" CLASS="fullsize" FRAMEBORDER="0" SCROLLING="no"></IFRAME>
the element ('.di')
is generated with js
var v = document.createElement("div");
v.className = "di";
var s = v.style;
s.width = sz.Tew + "px";
s.height = sh + "px";
s.left = (sx - (sz.Tew / 2)) + "px";
s.top = (sy - (sh / 2)) + "px";
if (wp) {
v.onmousedown = TCT;
v.id = i;
v.pid = e.p.i;
o.ps[i] = {
x: sx,
y: sy
};
}
var tn = e.p.hp ? (mn ? fn : (e.p.h + (sn ? (" " + sn) : ""))) : e.p.h;
v.innerHTML = "<TABLE WIDTH=\"100%\" HEIGHT=\"100%\" STYLE=\"table-layout:fixed;\">" + "<TR><TD CLASS=\"" + cc + "\" STYLE=\"font-size:" + (e.d ? sz.Tds : sz.Tfs) + "px;color:" + _4 + "\"" + " TITLE=\"" + (e.d ? "Duplicate: " : "") + EH(fn) + "\">" + (e.d ? "<I>Duplicate:</I><BR>" : "") + (e.m ? "<B>" : "") + EL(tn) + (e.m ? "</B>" : "") + "</TD></TR>" + rs + "</TABLE>";
o.appendChild(v);
I typed this code but it's not working:
jQuery(window).load( function(){
jQuery('.di').on( 'click', function ( _ev ) {
jQuery('.sidebar-wrapper').fadeIn(500);
});
jQuery('.sidebar-wrapper .sidebar-close').on( 'click', function ( _ev ) {
jQuery('.sidebar-wrapper').fadeOut(500);
});
});
jQuery(document).ready( function(){
jQuery('body').on( 'click','.dcelld', function ( _ev ) {
jQuery('.sidebar-wrapper').fadeIn(500);
});
jQuery('body').on( 'click','.sidebar-wrapper .sidebar-close', function ( _ev ) {
jQuery('.sidebar-wrapper').fadeOut(500);
});
});
I created a list of buttons from code behind, and append them on some div, how ever each button has an onclick java script function
here is how I did it:
string[] messages = CM.GetMessages(Session["USER_EMAIL"].ToString()).Split(new string[] { "$STARTCHAT$" }, StringSplitOptions.None);
string[] usersalone = CM.GetChaters(Session["USER_EMAIL"].ToString()).Split(new string[] { "$NEWUSER$" }, StringSplitOptions.None);
string[] username = CM.GetUserNames(Session["USER_EMAIL"].ToString()).Split(new string[] { "$NEWUSER$" }, StringSplitOptions.None);
for (int i = messages.Length-2; i>=0; i--)
{
Button b = new Button();
b.ID = Session["USER_EMAIL"].ToString()+username[i];
b.Text = "Chat With: " + usersalone[i] ;
b.Width = 250;
b.Height = 100;
b.OnClientClick = "return DisplayMessage('" + messages[i+1] + "','" + username[i] + "','" + Session["USER_EMAIL"].ToString() + "')";
b.Style.Add("background-color", "rgb(246, 246, 246)");
// lblChatwith.Text = username[i];
NewMsgNotArrow.Controls.Add(b);
}
and here is my java script function:
function DisplayMessage(messages, from, username) {
document.getElementById("AllMessages").innerText ="";
document.getElementById("DivDisplayMessage").style.visibility = "visible";
document.getElementById("lblChatwith").innerText = from;
var MessageForEachUser = messages.split("$SAMECHATNEWTEXT$");
for (var i = 0; i < MessageForEachUser.length; i++)
{
var ck = MessageForEachUser[i].indexOf("$" + from.toUpperCase() + "$") > -1;
if (ck == true) {
document.getElementById("AllMessages").innerText += from.toUpperCase() + ":\n";
var temp = MessageForEachUser[i].split("$" + from.toUpperCase() + "$");
MessageForEachUser[i] = temp[0];
}
if (ck == false) {
document.getElementById("AllMessages").innerText += username.toUpperCase() + ":\n";
var temp = MessageForEachUser[i].split("$" + username.toUpperCase() + "$");
MessageForEachUser[i] = temp[0];
}
document.getElementById("AllMessages").innerText += MessageForEachUser[i] + "\n______________________________________________________" + "\n";
}
return false;
}
every thing is working well but, when i want to use one of the labels like "lblchatwith" from code behind it return an empty string.
I'm using a server-side JS for a counter:
var date = new Date(2013,1,01);
var now = new Date();
var number = 0;
var increment = 3290;
var second = 1;
var secondTotal = (now.getTime()-date.getTime())/1000;
var incrementTotal = (secondTotal/1)*3290;
number = Math.round(number+incrementTotal);
function init() {
document.getElementById("kwh_fam_an").innerHTML = number;
setInterval('incrementation()',second*1000);
}
function incrementation() {
number = number+increment;
document.getElementById("kwh_fam_an").innerHTML = number;
}
I'm trying to change the format by forcing a colon and a space every 3 figures but the code below doesn't work:
function lisibilite_nombre(nbr) {
var number = '' + nbr;
var retour = '';
var count = 0;
for (var i = number.length - 1; i >= 0 ; i--) {
if (count != 0 && count % 3 == 0)
retour = number[i] + ' ' + retour ;
else
retour = number[i] + retour ;
count++;
}
alert('nb : ' + nbr + ' => ' + retour);
return retour;
}
Do you have any idea ?
You're adding the space but not the colon. Change:
retour = number[i] + ' ' + retour ;
to:
retour = number[i] + ': ' + retour;