Click to See Complete Forum and Search --> : Sorting script: slight modification?


sallyoz
04-19-2004, 05:41 PM
I hope!

At the moment this code relies on the sequence q_0, q_1, q_2, q_3

I would like to change it so it is q_00, q_01, q_02, q_03 to allow me to sort.

As a novice I have tried a few things but no luck. I would love some assistance from you gurus

orm, cur_field, cur_field.value)) {
return;
}
}
// check if a quantity entered
if (form.sub_tot.value==0) {
alert("You haven't ordered anything.");
return false;
}
}

function setFocus(fld) {
fld.focus();
fld.select();
}

function isPosInt(frm,fld,val) {
var re = /^\d+$/
if (!re.test(val)) {
alert("Please enter whole numbers only." + val);
if (document.forms[frm.name]) { // protect ns4 in case of nesting
setTimeout("setFocus(document.forms['"+frm.name+"'].elements['"+fld.name+"'])",100);
} else {
fld.focus(); fld.select();
}
return false;
} else return true;
}



// generic positive number decimal formatting function
// from JS Bible by Danny Goodman
function formatDec(expr,decplaces) {
var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces));
while (str.length <= decplaces) {
str = "0" + str;
}
var decpoint = str.length - decplaces;
return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}

function validate() {
if(document.entry_form.name.value.length <1) {
alert('please enter your full name');
}
}

//-->
</script>

Exuro
04-19-2004, 06:57 PM
I'm going to make the assumption that q_0, q_1, ect are elements in your form, probably to hold the "quantity" of items. In that case, why don't you just them in an array instead?

sallyoz
04-19-2004, 07:05 PM
Sorry it didn't paste in full.

Array up the top of the script which needs to correspond with form fields. The array is then the q_0, q_1. At the moment because it is dynamically generated it's putting them in the wrong order.

Thanks for your assitance


<script type="text/javascript">
<!--
/*
This code is from Dynamic Web Coding
at http://www.dyn-web.com/
Copyright 2002 by Sharon Paine
See Terms of Use at http://www.dyn-web.com/bus/terms.html
Permission granted to use this code
as long as this entire notice is included.
*/

// unit prices here
// number of items in this array needs to match
// number of quantity fields (named q_0, q_1, q_2, ...)
var cur_prices = new Array(
66.00, // product category 1
27.95, // product category 2
27.95, // product category 3
6934.00 // product category 4
)

//var cur_sales_tax=.075; // state sales tax

function getProductTotal(field,form) {
if (field.value=="") field.value=0;
if (!isPosInt(form,field,field.value)) { // valid entry?
return;
} else {
// which product?
var product_num = field.name.substr(field.name.indexOf("_")+1);
var amt = field.value*cur_prices[product_num];
var total_field = eval("form.tot_"+product_num);
// format
total_field.value= formatDec(amt,2);
doTotals(form);
}
}

function doTotals(form) {
var sub_tot_amt=0;
for (var i=0; i<cur_prices.length; i++) {
var cur_field = eval("form.q_"+i);
if (!isPosInt(form, cur_field, cur_field.value)) { // check again
return;
} else sub_tot_amt+= parseFloat(cur_field.value)*cur_prices[i];
}
form.sub_tot.value = formatDec(sub_tot_amt,2);


}

function inspectOptions(btn,field,form) {
field.value = formatDec(btn.value,2);
if (form.sub_tot.value>0) doTotals(form);
}

function doSalesTax(field,form) {
if (field.checked)
form.tax_amt.value = formatDec(cur_sales_tax*form.sub_tot.value,2);
else form.tax_amt.value = 0;
if (form.sub_tot.value>0) doTotals(form);
}

function finalCheck(form) {
// final check of quantity entries' validity
for (var i=0; i<cur_prices.length; i++) {
var cur_field = eval("form.q_"+i);
if (!isPosInt(form, cur_field, cur_field.value)) {
return;
}
}
// check if a quantity entered
if (form.sub_tot.value==0) {
alert("You haven't ordered anything.");
return false;
}
}

function setFocus(fld) {
fld.focus();
fld.select();
}

function isPosInt(frm,fld,val) {
var re = /^\d+$/
if (!re.test(val)) {
alert("Please enter whole numbers only." + val);
if (document.forms[frm.name]) { // protect ns4 in case of nesting
setTimeout("setFocus(document.forms['"+frm.name+"'].elements['"+fld.name+"'])",100);
} else {
fld.focus(); fld.select();
}
return false;
} else return true;
}



// generic positive number decimal formatting function
// from JS Bible by Danny Goodman
function formatDec(expr,decplaces) {
var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces));
while (str.length <= decplaces) {
str = "0" + str;
}
var decpoint = str.length - decplaces;
return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}

function validate() {
if(document.entry_form.name.value.length <1) {
alert('please enter your full name');
}
}

//-->
</script>

Exuro
04-20-2004, 08:30 PM
What exactly isn't working? My best guess is that you need to call doTotals() after you generate your form...