Looks like a soduku but it's not! I have 8 sections made up with tables of 7 rows and three (sometimes four) columns. Each row is totalled up as is each column. The script below works but is going to end up very long. Is there a way to condense itthat would still work if even if more columns/rows were added to a section as long as the relevant textfields were named something like S1_R2_C1 (SECTION 1, ROW2, COLUMN 1)?
The table looks like this
SECTION 1
C1 C2 C3
---+---+---+
R1 6 1 4 = 11
---+---+---+
R2 3 1 4 = 8
---+---+---+
R3 2 7 1 = 10
---+---+---+
11 9 9 29 (COLUMN TOTALS)
SECTION 2 ETC
<script language="JavaScript">
// ROWS ARRAYS SECTION 1
A_S1_R1 = new Array(
"S1_R1_C1",
"S1_R1_C2",
"S1_R1_C3"
);
A_S1_R2 = new Array(
"S1_R2_C1",
"S1_R2_C2",
"S1_R2_C3"
);
A_S1_R3 = new Array(
"S1_R3_C1",
"S1_R3_C2",
"S1_R3_C3"
);
// COLUMNS ARRAYS
A_S1_C1 = new Array(
"S1_R1_C1",
"S1_R2_C1",
"S1_R3_C1"
);
A_S1_C2 = new Array(
"S1_R1_C2",
"S1_R2_C2",
"S1_R3_C2"
);
A_S1_C3 = new Array(
"S1_R1_C3",
"S1_R2_C3",
"S1_R3_C3"
);
// SECTION2
A_S2_R1 = new Array(
"S1_R1_C1",
"S1_R1_C2",
"S1_R1_C3"
);
A_S2_R2 = new Array(
"S1_R2_C1",
"S1_R2_C2",
"S1_R2_C3"
);
A_S2_R3 = new Array(
"S1_R3_C1",
"S1_R3_C2",
"S1_R3_C3"
);
A_S2_C1 = new Array(
"S2_R1_C1",
"S2_R2_C1",
"S2_R3_C1"
);
A_S2_C2 = new Array(
"S2_R1_C2",
"S2_R2_C2",
"S2_R3_C2"
);
A_S2_C3 = new Array(
"S2_R1_C3",
"S2_R2_C3",
"S2_R3_C3"
);
function sumSection(theForm) {
// SUM ROWS ARRAYS
var S1_R1 = sum(A_S1_R1,theForm);
var S1_R2 = sum(A_S1_R2,theForm);
var S1_R3 = sum(A_S1_R3,theForm);
// SUM COLUMNS ARRAYS
var S1_C1 = sum(A_S1_C1,theForm);
var S1_C2 = sum(A_S1_C2,theForm);
var S1_C3 = sum(A_S1_C3,theForm);
// SECTION 2
var S2_R1 = sum(A_S2_R1,theForm);
var S2_R2 = sum(A_S2_R2,theForm);
var S2_R3 = sum(A_S2_R3,theForm);
var S2_C1 = sum(A_S2_C1,theForm);
var S2_C2 = sum(A_S2_C2,theForm);
var S2_C3 = sum(A_S2_C3,theForm);
//GET ROW TOTALS
theForm.elements['S1_R1_TOTAL'].value=S1_R1;
theForm.elements['S1_R2_TOTAL'].value=S1_R2;
theForm.elements['S1_R3_TOTAL'].value=S1_R3;
//GET COLUMN TOTALS
theForm.elements['S1_C1_TOTAL'].value=S1_C1;
theForm.elements['S1_C2_TOTAL'].value=S1_C2;
theForm.elements['S1_C3_TOTAL'].value=S1_C3;
theForm.elements['S1_TOTAL'].value=S1_R1 + S1_R2 + S1_R3;
//SECTION 2
theForm.elements['S2_R1_TOTAL'].value=S2_R1;
theForm.elements['S2_R2_TOTAL'].value=S2_R2;
theForm.elements['S2_R3_TOTAL'].value=S2_R3;
theForm.elements['S2_C1_TOTAL'].value=S2_C1;
theForm.elements['S2_C2_TOTAL'].value=S2_C2;
theForm.elements['S2_C3_TOTAL'].value=S2_C3;
theForm.elements['S2_TOTAL'].value=S2_R1 + S2_R2 + S2_R3;
}
function sum(list,theForm) {
var total=0;
var val=0;
for (i=0;i<list.length;i++) {
val = theForm.elements[list].value;
if (val=='') val=0;
else val=parseInt(val);
if (isNaN(val)) {
alert('Please enter a valid number');
theForm.elements[list].focus();
return 0;
}
total += val;
}
return total;
}
</script>
Away from the actual ... everything is virtual
The table looks like this
SECTION 1
C1 C2 C3
---+---+---+
R1 6 1 4 = 11
---+---+---+
R2 3 1 4 = 8
---+---+---+
R3 2 7 1 = 10
---+---+---+
11 9 9 29 (COLUMN TOTALS)
SECTION 2 ETC
<script language="JavaScript">
// ROWS ARRAYS SECTION 1
A_S1_R1 = new Array(
"S1_R1_C1",
"S1_R1_C2",
"S1_R1_C3"
);
A_S1_R2 = new Array(
"S1_R2_C1",
"S1_R2_C2",
"S1_R2_C3"
);
A_S1_R3 = new Array(
"S1_R3_C1",
"S1_R3_C2",
"S1_R3_C3"
);
// COLUMNS ARRAYS
A_S1_C1 = new Array(
"S1_R1_C1",
"S1_R2_C1",
"S1_R3_C1"
);
A_S1_C2 = new Array(
"S1_R1_C2",
"S1_R2_C2",
"S1_R3_C2"
);
A_S1_C3 = new Array(
"S1_R1_C3",
"S1_R2_C3",
"S1_R3_C3"
);
// SECTION2
A_S2_R1 = new Array(
"S1_R1_C1",
"S1_R1_C2",
"S1_R1_C3"
);
A_S2_R2 = new Array(
"S1_R2_C1",
"S1_R2_C2",
"S1_R2_C3"
);
A_S2_R3 = new Array(
"S1_R3_C1",
"S1_R3_C2",
"S1_R3_C3"
);
A_S2_C1 = new Array(
"S2_R1_C1",
"S2_R2_C1",
"S2_R3_C1"
);
A_S2_C2 = new Array(
"S2_R1_C2",
"S2_R2_C2",
"S2_R3_C2"
);
A_S2_C3 = new Array(
"S2_R1_C3",
"S2_R2_C3",
"S2_R3_C3"
);
function sumSection(theForm) {
// SUM ROWS ARRAYS
var S1_R1 = sum(A_S1_R1,theForm);
var S1_R2 = sum(A_S1_R2,theForm);
var S1_R3 = sum(A_S1_R3,theForm);
// SUM COLUMNS ARRAYS
var S1_C1 = sum(A_S1_C1,theForm);
var S1_C2 = sum(A_S1_C2,theForm);
var S1_C3 = sum(A_S1_C3,theForm);
// SECTION 2
var S2_R1 = sum(A_S2_R1,theForm);
var S2_R2 = sum(A_S2_R2,theForm);
var S2_R3 = sum(A_S2_R3,theForm);
var S2_C1 = sum(A_S2_C1,theForm);
var S2_C2 = sum(A_S2_C2,theForm);
var S2_C3 = sum(A_S2_C3,theForm);
//GET ROW TOTALS
theForm.elements['S1_R1_TOTAL'].value=S1_R1;
theForm.elements['S1_R2_TOTAL'].value=S1_R2;
theForm.elements['S1_R3_TOTAL'].value=S1_R3;
//GET COLUMN TOTALS
theForm.elements['S1_C1_TOTAL'].value=S1_C1;
theForm.elements['S1_C2_TOTAL'].value=S1_C2;
theForm.elements['S1_C3_TOTAL'].value=S1_C3;
theForm.elements['S1_TOTAL'].value=S1_R1 + S1_R2 + S1_R3;
//SECTION 2
theForm.elements['S2_R1_TOTAL'].value=S2_R1;
theForm.elements['S2_R2_TOTAL'].value=S2_R2;
theForm.elements['S2_R3_TOTAL'].value=S2_R3;
theForm.elements['S2_C1_TOTAL'].value=S2_C1;
theForm.elements['S2_C2_TOTAL'].value=S2_C2;
theForm.elements['S2_C3_TOTAL'].value=S2_C3;
theForm.elements['S2_TOTAL'].value=S2_R1 + S2_R2 + S2_R3;
}
function sum(list,theForm) {
var total=0;
var val=0;
for (i=0;i<list.length;i++) {
val = theForm.elements[list].value;
if (val=='') val=0;
else val=parseInt(val);
if (isNaN(val)) {
alert('Please enter a valid number');
theForm.elements[list].focus();
return 0;
}
total += val;
}
return total;
}
</script>
Away from the actual ... everything is virtual