Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JS Function help 2

Status
Not open for further replies.

theherbal

Programmer
Joined
May 31, 2006
Messages
8
Location
US
Hey, I am still dabbling to get my feet wet with js and so far I am doing pretty good but I have run into a problem that I can pin but not fix. I am not even sure if what I am doing is even possible but here it is.

I have a table with mutliple input fields and i have written a function to add up those fields( add() ). Since I have to add similar boxes all around the table I figured I would put all the variables in a separate function so I can activate it all with a button.

The following is a bit of the code and I know the statemeants in totweek() are the problem but I am unsure of how to solve this. Any help would be awesome since I am stumped!


// add() adds up all the time in a column



function add(box1, box2, dest)

{

box1 = box1.value
box2 = box2.value


dest.value = (box1 + box2)

}



// totweek() provides the variables for add()



function totweek()

{

add(from1, from2, 1reg);
add(from3, from4, 2reg);

}
 
Kinda stumped on your question, could paste the html and javascript. Seems like a variable scrop problem.

I don't know the answer but my good friend Google does.
 
OK, I am going to warn you this is rather long.

The alert boxes I got in there I am using to help me figure out where the problem is. Right now, I can not get the alert to pop up as the second statemant but it does when its the first in function totweek(). I hope this clarifys my question some.


<html>

<head>

<title>MojoSounds Timecard Calculator</title>



<body bgcolor=#FFFFFF onLoad="document.hours.from1.focus()">



<input type="button" value="Total Hours" onclick="disp_alert()"/>

<script language="Javascript">



<!--



function disp_alert()

{

alert("I am an alert box!!")

}



var colon=":";



// This here are all the functions

// calchours() does all the time math



function calchours(intime, outtime, dest)

{

inarray = intime.value.split(colon);

inhour = Number(inarray[0]);

inmin = Number(inarray[1]);



outarray = outtime.value.split(colon);

outhour = Number(outarray[0]);

outmin = Number(outarray[1]);



// Adjust for the case where no minutes are given



if (isNaN(inmin)) {

inmin = 00;

}



if (isNaN(outmin)) {

outmin = 00;

}



if (isNaN(inhour)) {

inhour = 00;

}



if (isNaN(outhour)) {

outhour = 00;

}



// Special time-math limitations



if (outmin < inmin) {

outmin = outmin + 60;

outhour = outhour - 1;

}



if (outhour < inhour) {

outhour = outhour + 12;

}



var totalhour = outhour - inhour;

var totalmin = outmin - inmin;



/*

// Take 0 minute fields out

if (totalmin == 0) {

dest.value = totalhour;

} else {

dest.value = totalhour + colon + convert(totalmin);

}

*/



dest.value = totalhour + convert(totalmin);

}





// convert() changes the minutes from 0-60 to 0-100



function convert(min)

{

if (min == 0) { return .00; }

if (min == 1) { return .02; }

if (min == 2) { return .04; }

if (min == 3) { return .05; }

if (min == 4) { return .06; }

if (min == 5) { return .08; }

if (min == 6) { return .10; }

if (min == 7) { return .12; }

if (min == 8) { return .14; }

if (min == 9) { return .15; }

if (min == 10) { return .16; }

if (min == 11) { return .18; }

if (min == 12) { return .20; }

if (min == 13) { return .22; }

if (min == 14) { return .24; }

if (min == 15) { return .25; }

if (min == 16) { return .26; }

if (min == 17) { return .28; }

if (min == 18) { return .30; }

if (min == 19) { return .32; }

if (min == 20) { return .34; }

if (min == 21) { return .35; }

if (min == 22) { return .36; }

if (min == 23) { return .38; }

if (min == 24) { return .40; }

if (min == 25) { return .42; }

if (min == 26) { return .44; }

if (min == 27) { return .45; }

if (min == 28) { return .46; }

if (min == 29) { return .48; }

if (min == 30) { return .50; }

if (min == 31) { return .52; }

if (min == 32) { return .54; }

if (min == 33) { return .55; }

if (min == 34) { return .56; }

if (min == 35) { return .58; }

if (min == 36) { return .60; }

if (min == 37) { return .62; }

if (min == 38) { return .64; }

if (min == 39) { return .65; }

if (min == 40) { return .66; }

if (min == 41) { return .68; }

if (min == 42) { return .70; }

if (min == 43) { return .72; }

if (min == 44) { return .74; }

if (min == 45) { return .75; }

if (min == 46) { return .76; }

if (min == 47) { return .78; }

if (min == 48) { return .80; }

if (min == 49) { return .82; }

if (min == 50) { return .84; }

if (min == 51) { return .85; }

if (min == 52) { return .86; }

if (min == 53) { return .88; }

if (min == 54) { return .90; }

if (min == 55) { return .92; }

if (min == 56) { return .94; }

if (min == 57) { return .95; }

if (min == 58) { return .96; }

if (min == 59) { return .98; }

}





// overtime() figures out the overtime done in a day



function overtime(rtime, dest)

{

rtime = rtime.value



if (rtime <= 8)

dest.value = 0

else

dest.value = (rtime - 8)

}





// regtime() keeps regular hours <=8



function regtime(regtime, dest)

{

regtime = regtime.value



if (regtime <= 8)

dest.value = regtime

else

dest.value = 8

}





// totot() kicks reg 40< into ot



function totot(rtime, otime, dest)

{

rtime2 = rtime.value

otime2 = otime.value



if (rtime2 <=40)

dest.value = otime2

else

dest.value = (rtime2 + otime2 - 40)

}





// totreg() keeps reg total <=40



function totreg(reg, dest)

{

reg = reg.value



if (reg <= 40)

dest.value = reg

else

dest.value = 40

}





// add() adds up all the time in a column



function add(box1, box2, dest)

{

box1 = box1.value
box2 = box2.value


dest.value = (box1 + box2)

}




// totweek() provides the variables for add()



function totweek()

{


add(from1, from2, 1reg);
alert("WOOT!!")

}







-->



</script>
</head>



<form name=hours>



<table border=1 cellspacing=1>



<tr bgcolor=#54C571>

<td colspan=4 align=Center><b>Week 1</b></td></tr>



<tr bgcolor=#8CC4A8>

<td><b>From:</b></td>

<td><b>To:</b></td>

<td><b>Regular:</b></td>

<td><b>Overtime:</b></td>

</tr>



<tr bgcolor=#70A8A8>

<td><input type=text size=6 name=from1></td>

<td><input type=text size=6 name=to1></td>

<td><input type=text size=6 name=total1></td>

<td><input type=text size=6 name=total1ot></td>

</tr>



<tr bgcolor=#8CC4A8>

<td><input type=text size=6 name=from2</td>

<td><input type=text size=6 name=to2></td>

<td><input type=text size=6 name=total2></td>

<td><input type=text size=6 name=total2ot></td>

</tr>



<tr bgcolor=#70A8A8>

<td><input type=text size=6 name=from3></td>

<td><input type=text size=6 name=to3></td>

<td><input type=text size=6 name=total3></td>

<td><input type=text size=6 name=total3ot></td>

</tr>



<tr bgcolor=#8CC4A8>

<td><input type=text size=6 name=from4></td>

<td><input type=text size=6 name=to4></td>

<td><input type=text size=6 name=total4></td>

<td><input type=text size=6 name=total4ot></td>

</tr>



<tr bgcolor=#70A8A8>

<td><input type=text size=6 name=from5></td>

<td><input type=text size=6 name=to5></td>

<td><input type=text size=6 name=total5></td>

<td><input type=text size=6 name=total5ot></td>

</tr>



<tr bgcolor=#8CC4A8>

<td><input type=text size=6 name=from6></td>

<td><input type=text size=6 name=to6></td>

<td><input type=text size=6 name=total6></td>

<td><input type=text size=6 name=total6ot></td>

</tr>



<tr bgcolor=#70A8A8>

<td><input type=text size=6 name=from7></td>

<td><input type=text size=6 name=to7></td>

<td><input type=text size=6 name=total7></td>

<td><input type=text size=6 name=total7ot></td>

</tr>



</table>



<p>

<p>



<table border=1 cellspacing=1>



<tr bgcolor=#54C571>

<td colspan=4 align=Center><b>Week 2</b></td></tr>



<tr bgcolor=#8CC4A8>

<td><b>From:</b></td>

<td><b>To:</b></td>

<td><b>Regular:</b></td>

<td><b>Overtime:</b></td>

</tr>



<tr bgcolor=#70A8A8>

<td><input type=text size=6 name=2from1></td>

<td><input type=text size=6 name=2to1></td>

<td><input type=text size=6 name=2total1></td>

<td><input type=text size=6 name=2total1ot></td>

</tr>



<tr bgcolor=#8CC4A8>

<td><input type=text size=6 name=2from2></td>

<td><input type=text size=6 name=2to2></td>

<td><input type=text size=6 name=2total2></td>

<td><input type=text size=6 name=2total2ot></td>

</tr>



<tr bgcolor=#70A8A8>

<td><input type=text size=6 name=2from3</td>

<td><input type=text size=6 name=2to3></td>

<td><input type=text size=6 name=2total3></td>

<td><input type=text size=6 name=2total3ot></td>

</tr>



<tr bgcolor=#8CC4A8>

<td><input type=text size=6 name=2from4</td>

<td><input type=text size=6 name=2to4></td>

<td><input type=text size=6 name=2total4></td>

<td><input type=text size=6 name=2total4ot></td>

</tr>



<tr bgcolor=#70A8A8>

<td><input type=text size=6 name=2from5></td>

<td><input type=text size=6 name=2to5></td>

<td><input type=text size=6 name=2total5></td>

<td><input type=text size=6 name=2total5ot></td>

</tr>



<tr bgcolor=#8CC4A8>

<td><input type=text size=6 name=2from6></td>

<td><input type=text size=6 name=2to6></td>

<td><input type=text size=6 name=2total6></td>

<td><input type=text size=6 name=2total6ot></td>

</tr>



<tr bgcolor=#70A8A8>

<td><input type=text size=6 name=2from7></td>

<td><input type=text size=6 name=2to7></td>

<td><input type=text size=6 name=2total7></td>

<td><input type=text size=6 name=2total7ot></td>

</tr>



</table>



<p>

<p>



<td valign=top width=500>



<table>



<tr bgcolor=#54C571>

<td><b>Totals</b></td>

<td><b>Regular:</b></td>

<td><b>Overtime:</b></td>

</tr>



<tr bgcolor=#8CC4A8>

<td><b>Week 1:</b></td>

<td><input type=text size=6 name=1reg></td>

<td><input type=text size=6 name=1ot></td>

</tr>



<tr bgcolor=#70A8A8>

<td><b>Week2:</b></td>

<td><input type=text size=6 name=2reg></td>

<td><input type=text size=6 name=2ot></td>

</tr>



<tr bgcolor=#8CC4A8>

<td><b>Total:</b></td>

<td><input type=text size=6 name=regtot></td>

<td><input type=text size=6 name=ottot></td>

</tr>



</table>



<p>

<p>



<input type="button" value="Total Hours" onclick="totweek()"/>

<input type=reset name="Clear form" onClick="document.hours.from1.focus()"/>



</form>

</body>

</html>
 
The problem is the way you're referencing your form elements in the function - it has no clue what you're passing it.

Try invoking your function like this:
Code:
add(document.forms["hours"].elements["from1"], document.forms["hours"].elements["from2"], document.forms["hours"].elements["1reg"]);

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
I would make the convert() function like this:
Code:
function convert(min)
{
var 
return (min / 60).toFixed(2);
}

and your totweek() function doesn't really refer to any form elements, just uninitialized global variables. You also can't start form element names with a number if you're going to access them in Javascript.

Code:
function add(box1, box2, dest)
{
dest.value = ((box1.value * 1) + (box2.value * 1));
}

// totweek() provides the variables for add()
function totweek()
{
var from1 = document.forms['hours'].elements['from1'];
var from2 = document.forms['hours'].elements['from2'];
var reg = document.forms['hours'].elements['reg'];

add(from1, from2, reg);
alert("WOOT!!")
}

Next time you post your code, please remove all the extra blank lines to make it easier to read. You need some for clarity, but not NEARLY as many as your posted example has.

Lee

 
Great minds think alike [smile]

But you gave the complete answer, so have a stizzar.

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Ok, thanks for the replys! I am about to sit down and try it out. I will let you know how it goes.
 
Thizzanks, kaht.

In my example for the convert() function, delete the term var from the first line inside the function. I started to write it one way and changed my mind, but forgot to delete that line.

Lee
 
OK, what exactly does the convert() function that you posted do in retro spec to the minutes that I have going to it?
 
I'll assume that you understand the math part (30 minutes divided by 60 = ...).

The toFixed() returns the number from the calculation with however many decimal places are specified in the method call (2 in this case). It does the same thing as your code, but with 1 line rather than 60.

Lee
 
Thanks for that nice line trollacious. I just popped it in and it works great.
 
OK, Your help has gotten me farther than I thought I was ever going to be going with this. I put in all the code I believe would be needed and yet their still seems to lie a couple of mistakes for it is not working. Any ideas on how to make this better or even how to run would be awesome!

Code:
<html>
<head>
<title>Herbal's Timecard Calculator</title>
<body bgcolor=#FFFFFF onLoad="document.hours.from1.focus()">

<script language="Javascript">
<!--
var colon=":";

// sumrhr() figures out all the hours worked for all days
function sumrhr()
{
calchours(document.forms["hours"].elements["from1"], document.forms["hours"].elements["to1"], document.forms["hours"].elements["total1"]);
calchours(document.forms["hours"].elements["from2"], document.forms["hours"].elements["to2"], document.forms["hours"].elements["total2"]);
calchours(document.forms["hours"].elements["from3"], document.forms["hours"].elements["to3"], document.forms["hours"].elements["total3"]);
calchours(document.forms["hours"].elements["from4"], document.forms["hours"].elements["to4"], document.forms["hours"].elements["total4"]);
calchours(document.forms["hours"].elements["from5"], document.forms["hours"].elements["to5"], document.forms["hours"].elements["total5"]);
calchours(document.forms["hours"].elements["from6"], document.forms["hours"].elements["to6"], document.forms["hours"].elements["total6"]);
calchours(document.forms["hours"].elements["from7"], document.forms["hours"].elements["to7"], document.forms["hours"].elements["total7"]);
calchours(document.forms["hours"].elements["from1b"], document.forms["hours"].elements["to1b"], document.forms["hours"].elements["total1b"]);
calchours(document.forms["hours"].elements["from2b"], document.forms["hours"].elements["to2b"], document.forms["hours"].elements["total2b"]);
calchours(document.forms["hours"].elements["from3b"], document.forms["hours"].elements["to3b"], document.forms["hours"].elements["total3b"]);
calchours(document.forms["hours"].elements["from4b"], document.forms["hours"].elements["to4b"], document.forms["hours"].elements["total4b"]);
calchours(document.forms["hours"].elements["from5b"], document.forms["hours"].elements["to5b"], document.forms["hours"].elements["total5b"]);
calchours(document.forms["hours"].elements["from6b"], document.forms["hours"].elements["to6b"], document.forms["hours"].elements["total6b"]);
calchours(document.forms["hours"].elements["from7b"], document.forms["hours"].elements["to7b"], document.forms["hours"].elements["total7b"]);
}

// calchours() does all the time math
function calchours(intime, outtime, dest)
{
inarray = intime.value.split(colon);
inhour = Number(inarray[0]);
inmin = Number(inarray[1]);

outarray = outtime.value.split(colon);
outhour = Number(outarray[0]);
outmin = Number(outarray[1]);

// Adjust for the case where no minutes are given
if (isNaN(inmin)) {
inmin = 00;
}
if (isNaN(outmin)) {
outmin = 00;
}
if (isNaN(inhour)) {
inhour = 00;
}
if (isNaN(outhour)) {
outhour = 00;
}

// Special time-math limitations
if (outmin < inmin) {
outmin = outmin + 60;
outhour = outhour - 1;
}
if (outhour < inhour) {
outhour = outhour + 12;
}  

var totalhour = outhour - inhour;
var totalmin  = outmin - inmin;

/*
// Take 0 minute fields out
if (totalmin == 0) {
dest.value = totalhour;
} else {
dest.value = totalhour + colon + convert(totalmin);
}
*/

dest.value = totalhour + convert(totalmin);
}

// convert() changes the minutes from 0-59 to 0-98
function convert(min)
{
return (min / 60).toFixed(2);
}

// sumot() figures out all overtime for all days
function sumot()
{
overtime(document.forms["hours"].elements["total1"], document.forms["hours"].elements["total1ot)"];
overtime(document.forms["hours"].elements["total2"], document.forms["hours"].elements["total2ot)"];
overtime(document.forms["hours"].elements["total3"], document.forms["hours"].elements["total3ot)"];
overtime(document.forms["hours"].elements["total4"], document.forms["hours"].elements["total4ot)"];
overtime(document.forms["hours"].elements["total5"], document.forms["hours"].elements["total5ot)"];
overtime(document.forms["hours"].elements["total6"], document.forms["hours"].elements["total6ot)"];
overtime(document.forms["hours"].elements["total7"], document.forms["hours"].elements["total7ot)"];
overtime(document.forms["hours"].elements["total1b"], document.forms["hours"].elements["total1otb)"];
overtime(document.forms["hours"].elements["total2b"], document.forms["hours"].elements["total2otb)"];
overtime(document.forms["hours"].elements["total3b"], document.forms["hours"].elements["total3otb)"];
overtime(document.forms["hours"].elements["total4b"], document.forms["hours"].elements["total4otb)"];
overtime(document.forms["hours"].elements["total5b"], document.forms["hours"].elements["total5otb)"];
overtime(document.forms["hours"].elements["total6b"], document.forms["hours"].elements["total6otb)"];
overtime(document.forms["hours"].elements["total7b"], document.forms["hours"].elements["total7otb)"];
}

// overtime() figures out the overtime done in a day
function overtime(rtime,  dest)
{
rtime = rtime.value

if (rtime <= 8) 
dest.value = 0
else 
dest.value = (rtime - 8)
}

// totalregtime() figures out all the regular hours all days
function totalregtime()
{
regtime(document.forms["hours"].elements["total1"], document.forms["hours"].elements["total1)"];
regtime(document.forms["hours"].elements["total2"], document.forms["hours"].elements["total2)"];
regtime(document.forms["hours"].elements["total3"], document.forms["hours"].elements["total3)"];
regtime(document.forms["hours"].elements["total4"], document.forms["hours"].elements["total4)"];
regtime(document.forms["hours"].elements["total5"], document.forms["hours"].elements["total5)"];
regtime(document.forms["hours"].elements["total6"], document.forms["hours"].elements["total6)"];
regtime(document.forms["hours"].elements["total7"], document.forms["hours"].elements["total7)"];
regtime(document.forms["hours"].elements["total1b"], document.forms["hours"].elements["total1b)"];
regtime(document.forms["hours"].elements["total2b"], document.forms["hours"].elements["total2b)"];
regtime(document.forms["hours"].elements["total3b"], document.forms["hours"].elements["total3b)"];
regtime(document.forms["hours"].elements["total4b"], document.forms["hours"].elements["total4b)"];
regtime(document.forms["hours"].elements["total5b"], document.forms["hours"].elements["total5b)"];
regtime(document.forms["hours"].elements["total6b"], document.forms["hours"].elements["total6b)"];
regtime(document.forms["hours"].elements["total7b"], document.forms["hours"].elements["total7b)"];
}

// regtime() keeps regular hours <=8
function regtime(regtime, dest)
{
regtime = regtime.value

if (regtime <= 8)
dest.value = regtime
else
dest.value = 8
}

// totweek() provides the variables for add()
function totweek()
{
add(document.forms["hours"].elements["total1"],
document.forms["hours"].elements["total2"],
document.forms["hours"].elements["total3"],
document.forms["hours"].elements["total4"],
document.forms["hours"].elements["total5"],
document.forms["hours"].elements["total6"],
document.forms["hours"].elements["total7"],
document.forms["hours"].elements["reg1"]);

add(document.forms["hours"].elements["total1ot"],
document.forms["hours"].elements["total2ot"],
document.forms["hours"].elements["total3ot"],
document.forms["hours"].elements["total4ot"],
document.forms["hours"].elements["total5ot"],
document.forms["hours"].elements["total6ot"],
document.forms["hours"].elements["total7ot"],
document.forms["hours"].elements["ot1"]);

add(document.forms["hours"].elements["total1b"],
document.forms["hours"].elements["total2b"],
document.forms["hours"].elements["total3b"],
document.forms["hours"].elements["total4b"],
document.forms["hours"].elements["total5b"],
document.forms["hours"].elements["total6b"],
document.forms["hours"].elements["total7b"],
document.forms["hours"].elements["reg2"]);

add(document.forms["hours"].elements["total1otb"],
document.forms["hours"].elements["total2otb"],
document.forms["hours"].elements["total3otb"],
document.forms["hours"].elements["total4otb"],
document.forms["hours"].elements["total5otb"],
document.forms["hours"].elements["total6otb"],
document.forms["hours"].elements["total7otb"],
document.forms["hours"].elements["ot2"]);
}

// add() adds up all the time in a column
function add(box1, box2, box3, box4, box5, box6, box7, dest)
{
dest.value = ((box1.value * 1) + (box2.value * 1) + (box3.value * 1) + (box4.value * 1) + (box5.value * 1) + (box6.value * 1) + (box7.value * 1))
}

// weekot() keeps all hours >40 as ot from reg 
function weekot()
{
totot(document.forms["hours"].elements["reg1"], document.forms["hours"].elements["ot1"], document.forms["hours"].elements["ot1"]);

totot(document.forms["hours"].elements["reg2"], document.forms["hours"].elements["ot2"], document.forms["hours"].elements["ot2"]);

totreg(document.forms["hours"].elements["reg1"], document.forms["hours"].elements["reg1"]);

totreg(document.forms["hours"].elements["reg2"], document.forms["hours"].elements["reg2"]);
}

// totot() kicks reg 40< into ot
function totot(rtime, otime, dest)
{
rtime2 = rtime.value
otime2 = otime.value

if (rtime2 <=40)
dest.value = otime2
else
dest.value = (rtime2 + otime2 - 40)
}

// totreg() keeps reg total <=40
function totreg(reg, dest)
{
reg = reg.value

if (reg <= 40)
dest.value = reg
else
dest.value = 40
}

// final() adds up Weeks 1 & 2
function final()
{
totalsheethr(document.forms["hours"].elements["reg1"], document.forms["hours"].elements["reg2"], document.forms["hours"].elements["regtot"]);

totalsheethr(document.forms["hours"].elements["ot1"], document.forms["hours"].elements["ot2"], document.forms["hours"].elements["ottot"]);
}

// totalsheethr() adds up weeks 1 & 2 hours
function totalsheethr(var1, var2, dest)
{
var1 = var1.value
var2 = var2.value

dest.value = (var1 + var2)
}

//total() does all the math functions in order to figure out time
function total()
{
sumrhr();
sumot();
totalregtime();
totweek();
weekot();
totreg();
final();
}

-->
</script>
</head>

<form name=hours>

<table border=1 cellspacing=1>
<tr bgcolor=#54C571>
<td colspan=4 align=Center><b>Week 1</b></td></tr>
<tr bgcolor=#8CC4A8>
<td><b>From:</b></td>
<td><b>To:</b></td>
<td><b>Regular:</b></td>
<td><b>Overtime:</b></td>
</tr>

<tr bgcolor=#70A8A8>
<td><input type=text size=6 name=from1></td>
<td><input type=text size=6 name=to1></td>
<td><input type=text size=6 name=total1></td>
<td><input type=text size=6 name=total1ot></td>
</tr>

<tr bgcolor=#8CC4A8>
<td><input type=text size=6 name=from2</td>
<td><input type=text size=6 name=to2></td>
<td><input type=text size=6 name=total2></td>
<td><input type=text size=6 name=total2ot></td>
</tr>

<tr bgcolor=#70A8A8>
<td><input type=text size=6 name=from3></td>
<td><input type=text size=6 name=to3></td>
<td><input type=text size=6 name=total3></td>
<td><input type=text size=6 name=total3ot></td>
</tr>

<tr bgcolor=#8CC4A8>
<td><input type=text size=6 name=from4></td>
<td><input type=text size=6 name=to4></td>
<td><input type=text size=6 name=total4></td>
<td><input type=text size=6 name=total4ot></td>
</tr>

<tr bgcolor=#70A8A8>
<td><input type=text size=6 name=from5></td>
<td><input type=text size=6 name=to5></td>
<td><input type=text size=6 name=total5></td>
<td><input type=text size=6 name=total5ot></td>
</tr>

<tr bgcolor=#8CC4A8>
<td><input type=text size=6 name=from6></td>
<td><input type=text size=6 name=to6></td>
<td><input type=text size=6 name=total6></td>
<td><input type=text size=6 name=total6ot></td>
</tr>

<tr bgcolor=#70A8A8>
<td><input type=text size=6 name=from7></td>
<td><input type=text size=6 name=to7></td>
<td><input type=text size=6 name=total7></td>
<td><input type=text size=6 name=total7ot></td>
</tr>

</table>
<p>
<p>
<table border=1 cellspacing=1>

<tr bgcolor=#54C571>
<td colspan=4 align=Center><b>Week 2</b></td></tr>

<tr bgcolor=#8CC4A8>
<td><b>From:</b></td>
<td><b>To:</b></td>
<td><b>Regular:</b></td>
<td><b>Overtime:</b></td>
</tr>

<tr bgcolor=#70A8A8>
<td><input type=text size=6 name=from1b></td>
<td><input type=text size=6 name=to1b></td>
<td><input type=text size=6 name=total1b></td>
<td><input type=text size=6 name=total1otb></td>
</tr>

<tr bgcolor=#8CC4A8>
<td><input type=text size=6 name=from2b></td>
<td><input type=text size=6 name=to2b></td>
<td><input type=text size=6 name=total2b></td>
<td><input type=text size=6 name=total2otb></td>
</tr>

<tr bgcolor=#70A8A8>
<td><input type=text size=6 name=from3b</td>
<td><input type=text size=6 name=to3b></td>
<td><input type=text size=6 name=total3b></td>
<td><input type=text size=6 name=total3otb></td>
</tr>

<tr bgcolor=#8CC4A8>
<td><input type=text size=6 name=from4b</td>
<td><input type=text size=6 name=to4b></td>
<td><input type=text size=6 name=total4b></td>
<td><input type=text size=6 name=total4otb></td>
</tr>

<tr bgcolor=#70A8A8>
<td><input type=text size=6 name=from5b></td>
<td><input type=text size=6 name=to5b></td>
<td><input type=text size=6 name=total5b></td>
<td><input type=text size=6 name=total5otb></td>
</tr>

<tr bgcolor=#8CC4A8>
<td><input type=text size=6 name=from6b></td>
<td><input type=text size=6 name=to6b></td>
<td><input type=text size=6 name=total6b></td>
<td><input type=text size=6 name=total6otb></td>
</tr>

<tr bgcolor=#70A8A8>
<td><input type=text size=6 name=from7b></td>
<td><input type=text size=6 name=to7b></td>
<td><input type=text size=6 name=total7b></td>
<td><input type=text size=6 name=total7otb></td>
</tr>

</table>

<p>
<p>

<td valign=top width=500>

<table>

<tr bgcolor=#54C571>
<td><b>Totals</b></td>
<td><b>Regular:</b></td>
<td><b>Overtime:</b></td>
</tr>

<tr bgcolor=#8CC4A8>
<td><b>Week 1:</b></td>
<td><input type=text size=6 name=reg1></td>
<td><input type=text size=6 name=ot1></td>
</tr>

<tr bgcolor=#70A8A8>
<td><b>Week2:</b></td>
<td><input type=text size=6 name=reg2></td>
<td><input type=text size=6 name=ot2></td>
</tr>

<tr bgcolor=#8CC4A8>
<td><b>Total:</b></td>
<td><input type=text size=6 name=regtot></td>
<td><input type=text size=6 name=ottot></td>
</tr>

</table>
<p>
<p>
<input type="button" value="Total Hours" onclick="total()"/>
<input type=reset name="Clear form" onClick="document.hours.from1.focus()"/>

</form>
</body>
</html>
 
OK, I did notice (and fixed) all the )"] mistakes ( should look like "]) )
Even after I did that I can not get any of the functions to work unless I take out all three functions;
final()
totalsheethr()
total()
I am still pretty puzzled
 
If you use Firefox for a browser, you get the advantage of their built-in Javascript console. I copied and pasted your code into a page, ran it in Firefox, and got this error:

Error: missing ) after argument list
Source File: Line: 87, Column: 98
Source Code:
overtime(document.forms["hours"].elements["total1"], document.forms["hours"].elements["total1ot)"];

You've made the same error in several places, putting a closing parenthesis in the wrong spot in the line.

I'd also write some of the repeated calls to functions in loops, and change the way the elements are referred to:

Code:
var colon=":";

// sumrhr() figures out all the hours worked for all days
function sumrhr()
{
var elements = document.forms["hours"].elements;

for (var ti=1;ti<=7;ti++)
  {
  calchours(elements["from" + ti], elements["to" + ti], elements["total" + ti]);
  calchours(elements["from" + ti + "b"], elements["to" + ti + "b"], elements["total" + ti + "b"]);
  }
}

// calchours() does all the time math
function calchours(intime, outtime, dest)
{
inarray = intime.value.split(colon);
inhour = Number(inarray[0]);
inmin = Number(inarray[1]);

outarray = outtime.value.split(colon);
outhour = Number(outarray[0]);
outmin = Number(outarray[1]);

// Adjust for the case where no minutes are given
if (isNaN(inmin)) {
inmin = 00;
}
if (isNaN(outmin)) {
outmin = 00;
}
if (isNaN(inhour)) {
inhour = 00;
}
if (isNaN(outhour)) {
outhour = 00;
}

// Special time-math limitations
if (outmin < inmin) {
outmin = outmin + 60;
outhour = outhour - 1;
}
if (outhour < inhour) {
outhour = outhour + 12;
}

var totalhour = outhour - inhour;
var totalmin  = outmin - inmin;

/*
// Take 0 minute fields out
if (totalmin == 0) {
dest.value = totalhour;
} else {
dest.value = totalhour + colon + convert(totalmin);
}
*/

dest.value = totalhour + convert(totalmin);
}

// convert() changes the minutes from 0-59 to 0-98
function convert(min)
{
return (min / 60).toFixed(2);
}

// sumot() figures out all overtime for all days
function sumot()
{

var elements = document.forms["hours"].elements;

for (var ti=1;ti<=7;ti++)
  {
  overtime(elements["total" + ti], document.forms["hours"].elements["total" + ti + "ot"]);
  overtime(elements["total" + ti + "b"], document.forms["hours"].elements["total" + ti + "otb"]);
  }
}

// overtime() figures out the overtime done in a day
function overtime(rtime,  dest)
{
rtime = rtime.value

if (rtime <= 8)
dest.value = 0
else
dest.value = (rtime - 8)
}

// totalregtime() figures out all the regular hours all days
function totalregtime()
{
var elements = document.forms["hours"].elements;

for (var ti=1;ti<=7;ti++)
  {
  regtime(elements["total" + ti], elements["total1" + ti]);
  regtime(elements["total" + ti + "b"], elements["total1" + ti + "b"]);
  }
}

// regtime() keeps regular hours <=8
function regtime(regtime, dest)
{
regtime = regtime.value

if (regtime <= 8)
dest.value = regtime
else
dest.value = 8
}

// totweek() provides the variables for add()
function totweek()
{
var elements = document.forms["hours"].elements;

add(elements["total1"], elements["total2"], elements["total3"], elements["total4"], elements["total5"], elements["total6"], elements["total7"], elements["reg1"]);

add(elements["total1ot"], elements["total2ot"], elements["total3ot"], elements["total4ot"], elements["total5ot"], elements["total6ot"], elements["total7ot"], elements["ot1"]);

add(elements["total1b"], elements["total2b"], elements["total3b"], elements["total4b"], elements["total5b"], elements["total6b"], elements["total7b"], elements["reg2"]);

add(elements["total1otb"], elements["total2otb"], elements["total3otb"], elements["total4otb"], elements["total5otb"], elements["total6otb"], elements["total7otb"], elements["ot2"]);
}

// add() adds up all the time in a column
function add(box1, box2, box3, box4, box5, box6, box7, dest)
{
dest.value = ((box1.value * 1) + (box2.value * 1) + (box3.value * 1) + (box4.value * 1) + (box5.value * 1) + (box6.value * 1) + (box7.value * 1))
}

// weekot() keeps all hours >40 as ot from reg
function weekot()
{
var elements = document.forms["hours"].elements;

totot(elements["reg1"], elements["ot1"], elements["ot1"]);

totot(elements["reg2"], elements["ot2"], elements["ot2"]);

totreg(elements["reg1"], elements["reg1"]);

totreg(elements["reg2"], elements["reg2"]);
}

// totot() kicks reg 40< into ot
function totot(rtime, otime, dest)
{
rtime2 = rtime.value
otime2 = otime.value

if (rtime2 <=40)
dest.value = otime2
else
dest.value = (rtime2 + otime2 - 40)
}

// totreg() keeps reg total <=40
function totreg(reg, dest)
{
reg = reg.value

if (reg <= 40)
dest.value = reg
else
dest.value = 40
}

// final() adds up Weeks 1 & 2
function final()
{
var elements = document.forms["hours"].elements;

totalsheethr(elements["reg1"], elements["reg2"], elements["regtot"]);

totalsheethr(elements["ot1"], elements["ot2"], elements["ottot"]);
}

// totalsheethr() adds up weeks 1 & 2 hours
function totalsheethr(var1, var2, dest)
{
var1 = var1.value
var2 = var2.value

dest.value = (var1 + var2)
}

//total() does all the math functions in order to figure out time
function total()
{
sumrhr();
sumot();
totalregtime();
totweek();
weekot();
totreg();
final();
}

Lee
 
Still a couple errors in the code I posted above. One problem is that final is a reserved word in JS.

Code:
var colon=":";

// sumrhr() figures out all the hours worked for all days
function sumrhr()
{
var elements = document.forms["hours"].elements;

for (var ti=1;ti<=7;ti++)
  {
  calchours(elements["from" + ti], elements["to" + ti], elements["total" + ti]);
  calchours(elements["from" + ti + "b"], elements["to" + ti + "b"], elements["total" + ti + "b"]);
  }
}

// calchours() does all the time math
function calchours(intime, outtime, dest)
{
inarray = intime.value.split(colon);
inhour = Number(inarray[0]);
inmin = Number(inarray[1]);

outarray = outtime.value.split(colon);
outhour = Number(outarray[0]);
outmin = Number(outarray[1]);

// Adjust for the case where no minutes are given
if (isNaN(inmin)) {
inmin = 00;
}
if (isNaN(outmin)) {
outmin = 00;
}
if (isNaN(inhour)) {
inhour = 00;
}
if (isNaN(outhour)) {
outhour = 00;
}

// Special time-math limitations
if (outmin < inmin) {
outmin = outmin + 60;
outhour = outhour - 1;
}
if (outhour < inhour) {
outhour = outhour + 12;
}

var totalhour = outhour - inhour;
var totalmin  = outmin - inmin;

/*
// Take 0 minute fields out
if (totalmin == 0) {
dest.value = totalhour;
} else {
dest.value = totalhour + colon + convert(totalmin);
}
*/

dest.value = totalhour + convert(totalmin);
}

// convert() changes the minutes from 0-59 to 0-98
function convert(min)
{
return (min / 60).toFixed(2);
}

// sumot() figures out all overtime for all days
function sumot()
{

var elements = document.forms["hours"].elements;

for (var ti=1;ti<=7;ti++)
  {
  overtime(elements["total" + ti], elements["total" + ti + "ot"]);
  overtime(elements["total" + ti + "b"], elements["total" + ti + "otb"]);
  }
}

// overtime() figures out the overtime done in a day
function overtime(rtime,  dest)
{
rtime = rtime.value

if (rtime <= 8)
dest.value = 0
else
dest.value = (rtime - 8)
}

// totalregtime() figures out all the regular hours all days
function totalregtime()
{
var elements = document.forms["hours"].elements;

for (var ti=1;ti<=7;ti++)
  {
  regtime(elements["total" + ti], elements["total1" + ti]);
  regtime(elements["total" + ti + "b"], elements["total1" + ti + "b"]);
  }
}

// regtime() keeps regular hours <=8
function regtime(regtime, dest)
{
regtime = regtime.value

if (regtime <= 8)
dest.value = regtime
else
dest.value = 8
}

// totweek() provides the variables for add()
function totweek()
{
var elements = document.forms["hours"].elements;

add(elements["total1"], elements["total2"], elements["total3"], elements["total4"], elements["total5"], elements["total6"], elements["total7"], elements["reg1"]);

add(elements["total1ot"], elements["total2ot"], elements["total3ot"], elements["total4ot"], elements["total5ot"], elements["total6ot"], elements["total7ot"], elements["ot1"]);

add(elements["total1b"], elements["total2b"], elements["total3b"], elements["total4b"], elements["total5b"], elements["total6b"], elements["total7b"], elements["reg2"]);

add(elements["total1otb"], elements["total2otb"], elements["total3otb"], elements["total4otb"], elements["total5otb"], elements["total6otb"], elements["total7otb"], elements["ot2"]);
}

// add() adds up all the time in a column
function add(box1, box2, box3, box4, box5, box6, box7, dest)
{
dest.value = ((box1.value * 1) + (box2.value * 1) + (box3.value * 1) + (box4.value * 1) + (box5.value * 1) + (box6.value * 1) + (box7.value * 1))
}

// weekot() keeps all hours >40 as ot from reg
function weekot()
{
var elements = document.forms["hours"].elements;

totot(elements["reg1"], elements["ot1"], elements["ot1"]);

totot(elements["reg2"], elements["ot2"], elements["ot2"]);

totreg(elements["reg1"], elements["reg1"]);

totreg(elements["reg2"], elements["reg2"]);
}

// totot() kicks reg 40< into ot
function totot(rtime, otime, dest)
{
rtime2 = rtime.value
otime2 = otime.value

if (rtime2 <=40)
dest.value = otime2
else
dest.value = (rtime2 + otime2 - 40)
}

// totreg() keeps reg total <=40
function totreg(reg, dest)
{
reg = reg.value;

if (reg <= 40)
  {
  dest.value = reg;
  }
else
  {
  dest.value = 40;
  }
}

// finall() adds up Weeks 1 & 2
function finall()
{
var elements = document.forms["hours"].elements;

totalsheethr(elements["reg1"], elements["reg2"], elements["regtot"]);

totalsheethr(elements["ot1"], elements["ot2"], elements["ottot"]);
}

// totalsheethr() adds up weeks 1 & 2 hours
function totalsheethr(var1, var2, dest)
{
var1 = var1.value
var2 = var2.value

dest.value = (var1 + var2)
}

//total() does all the math functions in order to figure out time
function total()
{
sumrhr();
sumot();
totalregtime();
totweek();
weekot();
totreg();
finall();
}

Lee
 
Thanks man, your help has been priceless. I use firefox on a daily basis and never knew about the debugger. I was also wondering how I could run a loop for the element referance, thanks for showing me. As this is my first script I would like little hints as to where to look and maybe a tip on what to do, I learn best by doing and besides, all the fun is gone if you do it!
With that said I have a question about this function here
Code:
// sumot() figures out all overtime for all days
function sumot()
{

var elements = document.forms["hours"].elements;

for (var ti=1;ti<=7;ti++)
  {
  overtime(elements["total" + ti], elements["total" + ti + "ot"]);
  overtime(elements["total" + ti + "b"], elements["total" + ti + "otb"]);
  }
}

// overtime() figures out the overtime done in a day
function overtime(rtime,  dest)
{
rtime = rtime.value
if (rtime <= 8)
dest.value = 0
else
dest.value = (rtime - 8)
}

The problem with it is one I have had since I made it. It will take i.e. 11.23 hours and the ot total is 3.23000000004. Is there something wrong with the code that I am not seeing? Or did I forget how to do math...

The last big rock in my way is right here;
Code:
// sumrhr() figures out all the hours worked for all days
function sumrhr()
{
var elements = document.forms["hours"].elements;

for (var ti=1;ti<=7;ti++)
  {
  calchours(elements["from" + ti], elements["to" + ti], elements["total" + ti]);
  calchours(elements["from" + ti + "b"], elements["to" + ti + "b"], elements["total" + ti + "b"]);
  }
}

// calchours() does all the time math
function calchours(intime, outtime, dest)
{
inarray = intime.value.split(colon);
var inhour = Number(inarray[0]);
var inmin = Number(inarray[1]);

outarray = outtime.value.split(colon);
var outhour = Number(outarray[0]);
var outmin = Number(outarray[1]);

// Adjust for the case where no minutes are given
if (isNaN(inmin)) {
inmin = 00;
}
if (isNaN(outmin)) {
outmin = 00;
}
if (isNaN(inhour)) {
inhour = 00;
}
if (isNaN(outhour)) {
outhour = 00;
}

// Special time-math limitations
if (outmin < inmin) {
outmin = outmin + 60;
outhour = outhour - 1;
}
if (outhour < inhour) {
outhour = outhour + 12;
}

var totalhour = outhour - inhour;
var totalmin  = outmin - inmin;
var convmin = convert(totalmin)

dest.value = totalhour + convmin;
}
It will only process the first line of both tables and then stop. I am very curious to know why it will not process the rest of the rows.
 
theHerbal, if you use Firefox daily for your web development then you DEFINITELY want to download this:


It gives you easy access to the javascript console, and will have an icon highlight to notify you when you have an error. And that's like 1% of what it can do.

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Floating point math often has slight errors like that. If you want to stick with 2 decimal places, there's a method called toFixed(numberofdecimalplaces) you can use in your overtime() function, like this:

Code:
dest.value = (rtime - 8).toFixed(2);

You asked

As this is my first script I would like little hints as to where to look and maybe a tip on what to do...

I look for patterns and repeated code, then use loops (as I did with your code) or create functions to handle the repeated areas. That helps IMMENSELY with fixing typos, since there's far fewer places for the mistakes to be made to begin with and far fewer places to correct when they do occur. It also means that any additional functionality only has to be added in one spot, and then everything calling the function or being processed inside the loop is taken care of immediately.

When I have to work with form elements, I usually set a variable that points to the closest level (often the elements array), like in the modifications to the functions in my last example above. This saves time for the code running because it doesn't have to start the search for the individual element at the document level. It also saves a lot of typing and potential errors from typos.

In this code

Code:
var inhour = Number(inarray[0]);
var inmin = Number(inarray[1]);

outarray = outtime.value.split(colon);
var outhour = Number(outarray[0]);
var outmin = Number(outarray[1]);

I would use parseInt(variable, 10) rather than Number(). If a number starts with a zero, then Javascript evaluates it as octal. If you have 08 or 09, your code won't work because octal numbers only have digits 0 - 7. I learned that one through experience working with dates.

And make sure to check the Firefox Javascript console for errors when something doesn't run correctly.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top