# Remote N.aN problem when calculating a form total

• 08-23-2012, 07:59 AM
flynnburge
Remote N.aN problem when calculating a form total
Hi. I'm new at Javascript.

I'm trying to generate a customised WORLDPAY (similar to Paypal) payment form.

I have this form I picked up. I basically want the javascript to take a text user-input amount and then add on a 'card fee', which is assigned by either one of two sums (user selected radio box).

Of the two radio boxes assigning the 'card fee' The first choice would be a fixed variable of 1, and the other being of percentage variable = 3% of the 'user-input'.

Then when a user presses a submit button 'Calculate Total' it should generate the sum of the two - the user input + card fee.

This coding below was provided by the WORLDPAY support site and I have tried my best to customise it. I can get the 'CALCULATE TOTAL' to work locally/client-side on my Mac/Win7 and provide me with a correct numerical result (incl. fixed fee or %).

However, when I transfer the same page/file online the 'Total' box displays the "N.aN" when you click on 'Calculate Total' - which I gather means Not A Number??? But why does it work locally and not remotely.

Can anyone help? And thanks in advance!

Code:

<script>
<!-- The next two functions round numbers to numerical formatting. They do not need to be altered when adding or removing products. -->
function roundOff(value, precision)
{
return types(value,1,precision);
}
function types(X, M, N)
{
var T, S=new String(Math.round(X*Number("1e"+N)))

while (S.length<M+N) S='0'+S
var y = S.substr(0, T=(S.length-N));
if(N>0)
{
y += '.' + S.substr(T, N);
}
return y;
}
<!-- This function checks for empty quantities. It does not need to be altered when adding or removing products. -->
function CheckNull(value)
{
if (value == "")
{
value = "0";
}

return value;
}
<!-- This function defines the card type and fee. It does not need to be altered when adding or removing products. -->
function typeOfCarriage(x,card_charge)
{
x.card_type.value = card_charge;
}
<!-- This function addeds the card fee to the original amount. -->
function calculate(x)
{
basicprice = calc(x);
var percent = (basicprice * 0.03);
if(Number(basicprice) > 0)
{
switch (x.card_type.value)
{
case "debit_card" :
x.postage_and_packaging.value = 1.00;
break
case "credit_card" :
x.postage_and_packaging.value = percent;
break
default :
x.postage_and_packaging.value = 0;
break;
}
x.amount.value = Number(basicprice) + Number(x.postage_and_packaging.value);

}
else
{
x.amount.value = "0";
}
x.amount.value = roundOff(x.amount.value,2);
}
<!-- The standard amount, exluding card fee is calculated here. It does not need to be altered. -->
function calc(x)
{
x.amount.value = 0;
var y = x.price.length;
var z = x.qty.length;
var a = Number(x.amount.value);
var b,c;
while(y > 0)
{
b = Number(CheckNull(x.price[y-1].value));
c = Number(CheckNull(x.qty[y-1].value));
a += (b * c);
y--;
}
return a;
}
</script>

<tr><td colSpan=3><h3><b>Enter Amount</b> - \$
<input name="qty" size="3" value="0">
<input name="price" type="hidden" value="1">
</h3></td></tr>
<tr><td colSpan=3><input name="price" type="hidden" value="0"></td></tr>
<tr><td><input type="hidden" name="qty" size="3" value="0"></td>
</tr>
</table>

<!-- This table is used as the total calculator and postage and packaging selector. -->
<input name=card_type type=hidden value=debit_card>
<tr><td><b>Card Fee:</b></td>
<td><input checked name="cardtype" onClick="typeOfCard(this.form,'debit_card');calculate(this.form)" type=radio value=""><B>Debit</B> (&pound;1.00)</td>
<td><input name="cardtype" onClick="typeOfCard(this.form,'credit_card');calculate(this.form)" type=radio value=""><b>Credit</b> (3%)</td></tr>
<tr>

<!-- This line generates a button that calculates the cost of the whole order. It does not need to be altered when adding or removing products. -->
<td colspan="2" align="right" style="text-align:center"><input name="calcButton" onClick="calculate(this.form)" type=button value="Calculate Total"></td>
<td colspan="4" style="text-align:center"><b>Total: &pound; </b><input name="amount" size=4 value="0"></td>
</tr>
</table>
</td>
</tr>
</table>

<!-- This generates a button that submits the information and send the user into the Worldpay payment pages. -->
<p align="center">

<input type=submit value=Checkout onClick="calculate(this.form)"></p>

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.09250 seconds
• Memory Usage 2,393KB
• Queries Executed 11 (?)
Template Usage (20):
• (1)bbcode_code_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates