VAN
asked on
Need a script to calculate overtime
I have a form that needs to calculate overtime based on 3 sets of overtime rules. 2 of them I already have working, but the final one (for use in Colorado) is giving me problems.
The Colorado overtime law is that any hours worked in a single day > 12 is overtime and any hours worked in a 7 day period > 40 is also overtime.
My form structure is as follows:
===================
<tr>
<td>Day</td>
<td>Hours Worked</td>
<td>Regular</td>
<td>Overtime</td>
</tr>
<tr>
<td>Sunday</td>
<td><input type=text name=SunWorked></td>
<td><input type=text name=REG1></td>
<td><input type=text name=OT1></td>
</tr>
<tr>
<td>Monday</td>
<td><input type=text name=MonWorked></td>
<td><input type=text name=REG2></td>
<td><input type=text name=OT2></td>
</tr>
<tr>
<td>Tuesdayday</td>
<td><input type=text name=TueWorked></td>
<td><input type=text name=REG3></td>
<td><input type=text name=OT3></td>
</tr>
<tr>
<td>Wednesday</td>
<td><input type=text name=WedWorked></td>
<td><input type=text name=REG4></td>
<td><input type=text name=OT4></td>
</tr>
<tr>
<td>Thursdayday</td>
<td><input type=text name=ThuWorked></td>
<td><input type=text name=REG5></td>
<td><input type=text name=OT5></td>
</tr>
<tr>
<td>Friday</td>
<td><input type=text name=FriWorked></td>
<td><input type=text name=REG6></td>
<td><input type=text name=OT6></td>
</tr>
<tr>
<td>Saturday</td>
<td><input type=text name=SatWorked></td>
<td><input type=text name=REG7></td>
<td><input type=text name=OT7></td>
</tr>
<tr>
<td>Total</td>
<td><input type=text name=TtlREG></td>
<td><input type=text name=TtlOT></td>
<td><input type=text name=TtlOT></td>
</tr>
========================== =
What needs to happen is this...
If I enter a value that is <= 12 in the the first box (SunWorked), then a value of 12 is placed into the second box (REG1) and the 3rd box (OT1) is left blank. If I enter a value that is > 12 in the first box, then a value of 12 goes to the second box (REG1), and the difference goes into the 3rd box (OT1).
That part is fairly simple.
Now if I enter a value of 10 in the first 5 boxes(SunWorked - ThuWorked), the second 4 boxes (REG1 - REG6) should each get a 10, while the 5th REG box (REG5) would be blank and the 5th OT box (OT5) would show a value of 10.
This is probably a lot simpler than I'm making it out to be. If you need clarification, just ask.
The Colorado overtime law is that any hours worked in a single day > 12 is overtime and any hours worked in a 7 day period > 40 is also overtime.
My form structure is as follows:
===================
<tr>
<td>Day</td>
<td>Hours Worked</td>
<td>Regular</td>
<td>Overtime</td>
</tr>
<tr>
<td>Sunday</td>
<td><input type=text name=SunWorked></td>
<td><input type=text name=REG1></td>
<td><input type=text name=OT1></td>
</tr>
<tr>
<td>Monday</td>
<td><input type=text name=MonWorked></td>
<td><input type=text name=REG2></td>
<td><input type=text name=OT2></td>
</tr>
<tr>
<td>Tuesdayday</td>
<td><input type=text name=TueWorked></td>
<td><input type=text name=REG3></td>
<td><input type=text name=OT3></td>
</tr>
<tr>
<td>Wednesday</td>
<td><input type=text name=WedWorked></td>
<td><input type=text name=REG4></td>
<td><input type=text name=OT4></td>
</tr>
<tr>
<td>Thursdayday</td>
<td><input type=text name=ThuWorked></td>
<td><input type=text name=REG5></td>
<td><input type=text name=OT5></td>
</tr>
<tr>
<td>Friday</td>
<td><input type=text name=FriWorked></td>
<td><input type=text name=REG6></td>
<td><input type=text name=OT6></td>
</tr>
<tr>
<td>Saturday</td>
<td><input type=text name=SatWorked></td>
<td><input type=text name=REG7></td>
<td><input type=text name=OT7></td>
</tr>
<tr>
<td>Total</td>
<td><input type=text name=TtlREG></td>
<td><input type=text name=TtlOT></td>
<td><input type=text name=TtlOT></td>
</tr>
==========================
What needs to happen is this...
If I enter a value that is <= 12 in the the first box (SunWorked), then a value of 12 is placed into the second box (REG1) and the 3rd box (OT1) is left blank. If I enter a value that is > 12 in the first box, then a value of 12 goes to the second box (REG1), and the difference goes into the 3rd box (OT1).
That part is fairly simple.
Now if I enter a value of 10 in the first 5 boxes(SunWorked - ThuWorked), the second 4 boxes (REG1 - REG6) should each get a 10, while the 5th REG box (REG5) would be blank and the 5th OT box (OT5) would show a value of 10.
This is probably a lot simpler than I'm making it out to be. If you need clarification, just ask.
<form>
<table>
<tr>
<td>Day</td>
<td>Hours Worked</td>
<td>Regular</td>
<td>Overtime</td>
</tr>
<tr>
<td>Sunday</td>
<td><input type=text name=SunWorked></td>
<td><input type=text name=REG1></td>
<td><input type=text name=OT1></td>
</tr>
<tr>
<td>Monday</td>
<td><input type=text name=MonWorked></td>
<td><input type=text name=REG2></td>
<td><input type=text name=OT2></td>
</tr>
<tr>
<td>Tuesdayday</td>
<td><input type=text name=TueWorked></td>
<td><input type=text name=REG3></td>
<td><input type=text name=OT3></td>
</tr>
<tr>
<td>Wednesday</td>
<td><input type=text name=WedWorked></td>
<td><input type=text name=REG4></td>
<td><input type=text name=OT4></td>
</tr>
<tr>
<td>Thursdayday</td>
<td><input type=text name=ThuWorked></td>
<td><input type=text name=REG5></td>
<td><input type=text name=OT5></td>
</tr>
<tr>
<td>Friday</td>
<td><input type=text name=FriWorked></td>
<td><input type=text name=REG6></td>
<td><input type=text name=OT6></td>
</tr>
<tr>
<td>Saturday</td>
<td><input type=text name=SatWorked></td>
<td><input type=text name=REG7></td>
<td><input type=text name=OT7></td>
</tr>
<tr>
<td>Total</td>
<td><input type=text name=TtlTOTAL></td>
<td><input type=text name=TtlREG></td>
<td><input type=text name=TtlOT></td>
</tr>
</table>
</form>
<script>
function calcsum(obj)
{
if(isOverHours(obj))
{
alert("max 40 hours")
obj.select();
obj.focus();
return;
}
var form=obj.form;
var PerDayMax=12;
var PerWeekMax=40;
var totaltimae
var Regular=0;
var Overtime=0;
for(var i=0;i<form.elements.length ;i++)
{
if(form.elements[i].name.m atch(/Work ed/))
{
if((Regular+Overtime+form. elements[i ].value*1) >PerWeekMa x)
{
form.elements[i+1].value=0 ;
form.elements[i+2].value=f orm.elemen ts[i].valu e;
}
else if((form.elements[i].value *1)>PerDay Max)
{
form.elements[i+2].value=f orm.elemen ts[i].valu e*1-PerDay Max;
form.elements[i+1].value=f orm.elemen ts[i].valu e*1-form.e lements[i+ 2].value*1 ;
}
else
{
form.elements[i+1].value=f orm.elemen ts[i].valu e;
form.elements[i+2].value=0 ;
}
Regular+=form.elements[i+1 ].value*1;
Overtime+=form.elements[i+ 2].value*1 ;
}
i=i+2;
}
form.TtlREG.value=Regular
form.TtlOT.value=Overtime
form.TtlTOTAL.value=Regula r+Overtime
}
function isOverHours(obj)
{
var totalHours=0;
for(var i=0;i<document.forms[0].el ements.len gth;i++)
if(document.forms[0].eleme nts[i].nam e.substr(0 ,3).match( /REG|OT/i) )
totalHours+=document.forms [0].elemen ts[i].valu e*1
totalHours+=obj.value*1;
if(totalHours>40)
return true
return false
}
for(var i=0;i<document.forms[0].el ements.len gth;i++)
{
document.forms[0].elements [i].onblur =function (){calcsum(this)}
if(!document.forms[0].elem ents[i].na me.match(/ Worked/i))
document.forms[0].elements [i].disabl ed=true;
}
</script>
<table>
<tr>
<td>Day</td>
<td>Hours Worked</td>
<td>Regular</td>
<td>Overtime</td>
</tr>
<tr>
<td>Sunday</td>
<td><input type=text name=SunWorked></td>
<td><input type=text name=REG1></td>
<td><input type=text name=OT1></td>
</tr>
<tr>
<td>Monday</td>
<td><input type=text name=MonWorked></td>
<td><input type=text name=REG2></td>
<td><input type=text name=OT2></td>
</tr>
<tr>
<td>Tuesdayday</td>
<td><input type=text name=TueWorked></td>
<td><input type=text name=REG3></td>
<td><input type=text name=OT3></td>
</tr>
<tr>
<td>Wednesday</td>
<td><input type=text name=WedWorked></td>
<td><input type=text name=REG4></td>
<td><input type=text name=OT4></td>
</tr>
<tr>
<td>Thursdayday</td>
<td><input type=text name=ThuWorked></td>
<td><input type=text name=REG5></td>
<td><input type=text name=OT5></td>
</tr>
<tr>
<td>Friday</td>
<td><input type=text name=FriWorked></td>
<td><input type=text name=REG6></td>
<td><input type=text name=OT6></td>
</tr>
<tr>
<td>Saturday</td>
<td><input type=text name=SatWorked></td>
<td><input type=text name=REG7></td>
<td><input type=text name=OT7></td>
</tr>
<tr>
<td>Total</td>
<td><input type=text name=TtlTOTAL></td>
<td><input type=text name=TtlREG></td>
<td><input type=text name=TtlOT></td>
</tr>
</table>
</form>
<script>
function calcsum(obj)
{
if(isOverHours(obj))
{
alert("max 40 hours")
obj.select();
obj.focus();
return;
}
var form=obj.form;
var PerDayMax=12;
var PerWeekMax=40;
var totaltimae
var Regular=0;
var Overtime=0;
for(var i=0;i<form.elements.length
{
if(form.elements[i].name.m
{
if((Regular+Overtime+form.
{
form.elements[i+1].value=0
form.elements[i+2].value=f
}
else if((form.elements[i].value
{
form.elements[i+2].value=f
form.elements[i+1].value=f
}
else
{
form.elements[i+1].value=f
form.elements[i+2].value=0
}
Regular+=form.elements[i+1
Overtime+=form.elements[i+
}
i=i+2;
}
form.TtlREG.value=Regular
form.TtlOT.value=Overtime
form.TtlTOTAL.value=Regula
}
function isOverHours(obj)
{
var totalHours=0;
for(var i=0;i<document.forms[0].el
if(document.forms[0].eleme
totalHours+=document.forms
totalHours+=obj.value*1;
if(totalHours>40)
return true
return false
}
for(var i=0;i<document.forms[0].el
{
document.forms[0].elements
if(!document.forms[0].elem
document.forms[0].elements
}
</script>
ASKER
Almost, but there is no limit to what can be worked, and your script will not allow anything over 40. A user can work more than 40, but everything over 40 is overtime. TtlREG will never exceed 40.
Using your exact code as an example, you should be able to enter a value of 10 in each of the 7 boxes in the first column. once you enter 10 in the 5th box, everything from that point and forward should go into the OT column.
Also, your script keeps looping the alert, so I had to ctrl+alt+del to get out. :(
Using your exact code as an example, you should be able to enter a value of 10 in each of the 7 boxes in the first column. once you enter 10 in the 5th box, everything from that point and forward should go into the OT column.
Also, your script keeps looping the alert, so I had to ctrl+alt+del to get out. :(
no need to press ctrl+alt+del, just change bad value ;)
I got what do you mean, cu
I got what do you mean, cu
ASKER
You cant change a bad value when an alert message wont go away. ;)
ASKER
Anyone else care to give this a try? :)
Maybe this will help spark some ideas...
Below is the code I am using to calculate pay for other states. Using this code, as soon as the total time entered hits 40, any time added after gets placed in the OT column...
============
function calcsum(form)
{sum=0
OtSum=0
regSum=0
sumsave=0
sum=0
for (i=0;i<form.elements.lengt h;i++){
if (form.elements[i].type=="t ext" && /Worked/.test(form.element s[i].name) || /Holiday/.test(form.elemen ts[i].name )){
sum+=1*form.elements[i].va lue
form.elements[i+1].value=' '
form.elements[i+2].value=' '
if (sum >= 40 && sumsave <= 40 && form.elements[i].value!='' ){
form.elements[i+1].value=1 *form.elem ents[i].va lue-(sum-4 0)
if (sum-40 == 0) {
form.elements[i+2].value=' '
} else {
form.elements[i+2].value=( sum-40)
}
}
if (sum >= 40 && sumsave >= 40 && form.elements[i].value!='' ){
form.elements[i+1].value=' '
form.elements[i+2].value= 1*form.elements[i].value-f orm.elemen ts[i+1].va lue
}
if (sum <= 40 && sumsave <= 40 && form.elements[i].value!='' ){
form.elements[i+1].value=f orm.elemen ts[i].valu e
}
if ( form.elements[i].value=='' ){
form.elements[i+2].value=' '
form.elements[i+1].value=' '
}
}
sumsave=sum
}
}
=====================
Now this is the code I am using to verify that all hours for a single day > 12 are counted as overtime:
============
function calc1() {
var SunVal = document.myForm.SunWorked. value
if (document.myForm.SunWorked .value > 12) {
document.myForm.OT1.value = SunVal - 12;
document.myForm.REG1.value = 12;
} else {
document.myForm.OT1.value = '';
document.myForm.REG1.value = SunVal;
}
================
So what I need is to combine these 2 functions, basically.
Maybe this will help spark some ideas...
Below is the code I am using to calculate pay for other states. Using this code, as soon as the total time entered hits 40, any time added after gets placed in the OT column...
============
function calcsum(form)
{sum=0
OtSum=0
regSum=0
sumsave=0
sum=0
for (i=0;i<form.elements.lengt
if (form.elements[i].type=="t
sum+=1*form.elements[i].va
form.elements[i+1].value='
form.elements[i+2].value='
if (sum >= 40 && sumsave <= 40 && form.elements[i].value!=''
form.elements[i+1].value=1
if (sum-40 == 0) {
form.elements[i+2].value='
} else {
form.elements[i+2].value=(
}
}
if (sum >= 40 && sumsave >= 40 && form.elements[i].value!=''
form.elements[i+1].value='
form.elements[i+2].value= 1*form.elements[i].value-f
}
if (sum <= 40 && sumsave <= 40 && form.elements[i].value!=''
form.elements[i+1].value=f
}
if ( form.elements[i].value==''
form.elements[i+2].value='
form.elements[i+1].value='
}
}
sumsave=sum
}
}
=====================
Now this is the code I am using to verify that all hours for a single day > 12 are counted as overtime:
============
function calc1() {
var SunVal = document.myForm.SunWorked.
if (document.myForm.SunWorked
document.myForm.OT1.value = SunVal - 12;
document.myForm.REG1.value
} else {
document.myForm.OT1.value = '';
document.myForm.REG1.value
}
================
So what I need is to combine these 2 functions, basically.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Bustarooms, if I read VAN's question correctly, then your answer seems to be BANG on.
Although I think it works more smoothly if you change this line:
document.forms[0].elements [i].onblur =function (){calcsum(this)}
To a "onkeyup" event:
document.forms[0].elements [i].onkeyu p=function (){calcsum(this)}
This allows it to be calculated on the fly and doen't require the person to click outside the for it to calculate the last value in the Saturday column.
Good work.
Regards...
Although I think it works more smoothly if you change this line:
document.forms[0].elements
To a "onkeyup" event:
document.forms[0].elements
This allows it to be calculated on the fly and doen't require the person to click outside the for it to calculate the last value in the Saturday column.
Good work.
Regards...
ASKER
Bustarooms, you're a lifesaver! Thanks so much! Although I'm not sure if you will actually get that bonus since it seems to be violating some rules. Some how, some way, I'll get you some extra points. :D
Thanks again!
Thanks again!
cool, the problem solved, now I can calmly watch tv: Athens 2004
;)
;)
ASKER