PDA

View Full Version : Auto Currency and the Sum of All Cells


ChattanoogaRay
10-26-2003, 09:08 AM
I have included an auto curreny function into one of the cells that is involved in the total calculation for the grand total but I am now getting a NaN error where the total once was. Is it possible to still add the values of all the cells even if one of the cells contains non numeric characters? Below are my 2 scripts. The one for the calc function and one for the currency function:

"CALCULATION"
<script language="JavaScript" type="text/javascript">
function total(what,number) {
var grandTotal = 0;
for (var i=1;i<number;i++) {
if (what.elements['total_' + i].value == '')
what.elements['total_' + i].value == '0.00'; // fix for Opera.
grandTotal += (what.elements['total_' + i].value - 0);
}
what.grandTotal.value = cent(Math.round(grandTotal*Math.pow(10,2))/Math.pow(10,2));
}
</script>
"CURRENCY"
<script language="JavaScript" type="text/javascript">
function formatCurrency(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);
}
</script>

Jon Hanlon
10-26-2003, 04:22 PM
You need the parseFloat() function:

"CALCULATION"
<script language="JavaScript" type="text/javascript">
function total(what,number) {
var grandTotal = 0;
for (var i=1;i<number;i++) {
if (what.elements['total_' + i].value == '')
what.elements['total_' + i].value == '0.00'; // fix for Opera.
var pfVal = parseFloat(what.elements['total_' + i].value)
if (!isNaN(pfVal)) grandTotal += pfVal ;
}


http://www.devguru.com/Technologies/ecmascript/quickref/parsefloat.html

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsmthparsefloat.asp

ChattanoogaRay
10-26-2003, 04:52 PM
Thanks for the help. I actually just finished the updates with the solution I found. Everything works except in ns4. Go figure. Back to the drawing board I go....heh

Jon Hanlon
10-26-2003, 05:05 PM
Why on earth would you worry about the 0.03% of morons still on NS4?
Just how long are you figuring on supporting Netscape 4?
Another Ten years? Five? Two?

ChattanoogaRay
10-26-2003, 06:43 PM
Well I must not be sweating it too bad cause I haven't even went back to try and fix it yet. I am actually trying to create my first cookie ever and it isn't a pleasureable experience. Can anyone help with the following free script I found on the net. I have adjusted all the appropriate variables to coincide with my variable names but I am still getting error messages. Here is the script I have so far.

<script language="JavaScript" type="text/javascript">
<!-- Begin
// Cookie Functions //////////////////// (:)
var expDays = 100;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) { endstr = document.cookie.length; }
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}

function cookieForms() {
var mode = cookieForms.arguments[0];
for(f=1; f<cookieForms.arguments.length; f++) {
contactfm = cookieForms.arguments[f];
if(mode == 'open') {
cookieValue = GetCookie('saved_'+contactfm);
if(cookieValue != null) {
var cookieArray = cookieValue.split('#cf#');
if(cookieArray.length == document[contactfm].elements.length) {
for(i=0; i<document[contactfm].elements.length; i++) {
if(cookieArray[i].substring(0,6) == 'select') { document[contactfm].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length-1); }
else if((cookieArray[i] == 'cbtrue') || (cookieArray[i] == 'rbtrue')) { document[contactfm].elements[i].checked = true; }
else if((cookieArray[i] == 'cbfalse') || (cookieArray[i] == 'rbfalse')) { document[contactfm].elements[i].checked = false; }
else { document[contactfm].elements[i].value = (cookieArray[i]) ? cookieArray[i] : ''; }
}
}
}
}
if(mode == 'save') {
cookieValue = '';
for(i=0; i<document[contactfm].elements.length; i++) {
fieldType = document[contactfm].elements[i].type;
if(fieldType == 'password') { passValue = ''; }
else if(fieldType == 'checkbox') { passValue = 'cb'+document[contactfm].elements[i].checked; }
else if(fieldType == 'radio') { passValue = 'rb'+document[contactfm].elements[i].checked; }
else if(fieldType == 'select-one') { passValue = 'select'+document[contactfm].elements[i].options.selectedIndex; }
else { passValue = document[contactfm].elements[i].value; }
cookieValue = cookieValue + passValue + '#cf#';
}
cookieValue = cookieValue.substring(0, cookieValue.length-4); // Remove last delimiter
SetCookie('saved_'+contactfm, cookieValue, exp);
}
}
}
// End -->
</script>

<body bgcolor="#000066" onLoad="init_page(); cookieForms('open', 'contactfm')" onunload="cookieForms('save', 'contactfm')" onResize="restore()">

It is saying that cookieforms doesnt support cookieforms.arguments.length