...

View Full Version : Resolved Javascript not performing automatic calculations on webpage



tonyascw
05-14-2009, 04:35 AM
Hello, I need help with the code below. I am not a java expert but this code is suppose to calculate totals when users enter data. The URL to see the form page is http://tonyascw.com/order%20forms/mothers%20day%20orderform.html
Please help:

var oF = {
taxBOOL: false,
smallBOOL: false,
largeBOOL: false,
persBOOL: false,

smallARR: new Array("Purple Expressions " ,"Purple Rose Bouquet " ,"Flower In Stripped Kettle "),

designARR: new Array(),

qty: 0,
currentSubTotal: 0,
currentTotal: 0,

singlePers: 0.25,
dozenPers: 2.00,

singleSmallPrice: 2.50,
singleLargePrice: 2.75,

dozenSmallPrice: 10.50,
dozenLargePrice: 12.50,
dozenMixedPrice: 12.50,


stateID: 'state',
designID: 'designtext',
signatureID: 'signature',
shippingID: 'shippingtext',
qtyID: 'quantitytext',
couponID: 'couponCode',
shippingRequiredID: 'shippingRequired',

couponValue: 'Mother',

totalID: 'subtotaltext',
subTotalID: 'ordersubtotaltext',
taxID: 'taxtext',

totalViewID: 'subtotalview',
subTotalViewID: 'ordersubtotalview',
taxViewID: 'taxview',

totalOBJ: '',
subtotalOBJ: '',

init: function () {
//qty
addEvent(document.getElementById(oF.qtyID),'change',oF.updateTotals,false);

//state
addEvent(document.getElementById(oF.stateID),'change',oF.updateTotals,false);

//design
addEvent(document.getElementById(oF.designID),'click',oF.updateTotals,false);

//signature
addEvent(document.getElementById(oF.signatureID),'blur',oF.updateTotals,false);

//coupon code
addEvent(document.getElementById(oF.couponID),'blur',oF.updateTotals,false);

//required shipping
addEvent(document.getElementById(oF.shippingRequiredID),'change',oF.updateTotals,false);

},

currencyFormatted: function(num)
{
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
},

checkFilled: function(value) {
if(value == 'none' || value == 'None' || value == '') {
return false;
}
else {
return true;
}
},

findArray: function(arr,element) {

for(j = 0; j < arr.length; j++) {
if(arr[j] == element) {
return true;
break;
}
}

return false;
},

setBOOL: function(variable,trueValue) {
if(variable == trueValue) {
return true;
} else {
return false;
}
},

getValues: function() {
var tempSigBOOL;

oF.qty = document.getElementById(oF.qtyID).value;

//if bible verse or signature is filled in, set personalization boolean to TRUE to charge them
tempSigBOOL = oF.checkFilled(document.getElementById(oF.signatureID).value);

oF.persBOOL = oF.checkFilled(document.getElementById(oF.signatureID).value);

//alert('Bible: ' + tempBibleBOOL + ', Signature: ' + tempSigBOOL);

oF.taxBOOL = oF.setBOOL(document.getElementById(oF.stateID).value,'New Jersey');

if(document.getElementById(oF.stateID).value == 'New Jersey') {
oF.taxBOOL = true;
} else {
oF.taxBOOL = false;
}

for(i = 0; i < document.getElementById(oF.designID).options.length; i++) {
currentOpt = document.getElementById(oF.designID).options[i];

//if selected, add to array
if(currentOpt.selected) {
if(oF.findArray(oF.smallARR,currentOpt.value) == true) {
oF.smallBOOL = true;
} else {
oF.largeBOOL = true;
}

}

oF.designARR[oF.designARR.length] = currentOpt.value;
}
}
};

checkCoupon: function() {

return document.getElementById(oF.couponID).value == oF.couponValue;

},

updateTotal: function() {
var tempTotal = 0;

//check < 12
if(oF.qty < 12) {
if(oF.persBOOL == true) {
tempTotal = 0.25;
}

if(oF.smallBOOL == true) {
tempTotal += oF.qty * oF.singleSmallPrice;
}

} else {
tempTotal += oF.qty * oF.singleLargePrice;
}

}

} else {
if(oF.persBOOL == true) {
tempTotal = 2;
}

//mixed dozen small and large
if(oF.smallBOOL == true && oF.largeBOOL == true) {
tempTotal += ((oF.qty/12) * oF.dozenMixedPrice);
}
} else if(oF.smallBOOL == true) {
tempTotal += (oF.qty/12) * oF.dozenSmallPrice;
}

} else {
//all large cards
if(oF.largeBOOL == true) {
tempTotal += (oF.qty/12) * oF.dozenLargePrice;
}
}
}

//coupon discount
if(oF.checkCoupon() == true) {
tempTotal = tempTotal * .95;
}

oF.currentTotal = tempTotal;
oF.currentSubTotal = tempTotal;

document.getElementById(oF.totalID).value = oF.currencyFormatted(oF.currentTotal);

document.getElementById(oF.totalViewID).firstChild.nodeValue = oF.currencyFormatted(oF.currentTotal);


},

updateSubTotal: function() {

var tempTotal = 0;

//calc tax
if(oF.taxBOOL == true) {
taxAmt = oF.currentSubTotal * .07;
oF.currentSubTotal = oF.currentSubTotal * 1.07;

document.getElementById(oF.taxID).value = oF.currencyFormatted(taxAmt);
document.getElementById(oF.taxViewID).firstChild.nodeValue = oF.currencyFormatted(taxAmt);
}

//calc shipping
oF.currentSubTotal += oF.calculateShipping();

//update subtotal field
document.getElementById(oF.subTotalID).value = oF.currencyFormatted(oF.currentSubTotal);

document.getElementById(oF.subTotalViewID).firstChild.nodeValue = oF.currencyFormatted(oF.currentSubTotal);
},

calculateShipping: function() {
var shipCost = 0;

if(oF.checkShipping() == true) {
if(oF.qty > 0 && oF.qty <= 2) {
shipCost = 1.25;
} else if(oF.qty >= 3 && oF.qty <= 4) {
shipCost = 2.50;
} else if(oF.qty >= 5 && oF.qty <= 7) {
shipCost = 4.0;
} else if(oF.qty >= 8 && oF.qty <= 11) {
shipCost = 5.25;
} else if(oF.qty >= 12 && oF.qty <= 24) {
shipCost = 6.50;
} else {
shipCost = 8.0;
}
}

return shipCost;
},

//used to see if person requires shipping for cost calculation
checkShipping: function() {
var shipping = false;

for(i = 0; i < document.getElementById(oF.shippingRequiredID).options.length; i++) {
currentOpt = document.getElementById(oF.shippingRequiredID).options[i];

if(currentOpt.selected == true && currentOpt.value == 'yes') {
shipping = true;
}
}

return shipping;

},

updateTotals: function() {
//reset array
oF.designARR = new Array();

oF.smallBOOL = false;
oF.largeBOOL = false;

oF.getValues();

oF.updateTotal();

oF.updateSubTotal();
}

}

addEvent(window,'load',oF.init,false);

Fou-Lu
05-14-2009, 05:09 AM
Javascript is not the same as Java.
Moving to Javascript forum.

Philip M
05-14-2009, 08:23 AM
Please follow the posting guidelines and wrap your code in [code] tags. This means use the octothorpe or # button on the toolbar which will insert opening [code] and closing [/codx] - change the X to E - tags . You can edit your previous post.

You will need to post your HTML if we have any hope of deciphering this code.

tonyascw
05-14-2009, 03:54 PM
Philip,thank you very much for your help. I am new to this forum so I hope I made the changes you suggested correctly. Please advise

tonyascw
05-14-2009, 03:55 PM
Fou-Lu thank you for moving this to the correct forum



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum