Link to home
Start Free TrialLog in
Avatar of VAN
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.
Avatar of VAN
VAN

ASKER

Oh, and the 2 subtotal boxes at the bottom need to be re-calculated with each action. TtlREG should never exceed 40.
Avatar of devic
<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.match(/Worked/))
            {
                  if((Regular+Overtime+form.elements[i].value*1)>PerWeekMax)
                  {
                        form.elements[i+1].value=0;
                        form.elements[i+2].value=form.elements[i].value;                  
                  }
                  else if((form.elements[i].value*1)>PerDayMax)
                  {
                        form.elements[i+2].value=form.elements[i].value*1-PerDayMax;
                        form.elements[i+1].value=form.elements[i].value*1-form.elements[i+2].value*1;
                  }
                  else
                  {
                        form.elements[i+1].value=form.elements[i].value;
                        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=Regular+Overtime
      
}
function isOverHours(obj)
{
      var totalHours=0;
      for(var i=0;i<document.forms[0].elements.length;i++)
      if(document.forms[0].elements[i].name.substr(0,3).match(/REG|OT/i))
      totalHours+=document.forms[0].elements[i].value*1
      totalHours+=obj.value*1;
      if(totalHours>40)
      return true
      return false
}
for(var i=0;i<document.forms[0].elements.length;i++)
{
      document.forms[0].elements[i].onblur=function (){calcsum(this)}
      if(!document.forms[0].elements[i].name.match(/Worked/i))
      document.forms[0].elements[i].disabled=true;

}
</script>
Avatar of VAN

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. :(
no need to press ctrl+alt+del, just change bad value ;)
I got what do you mean, cu
Avatar of VAN

ASKER

You cant change a bad value when an alert message wont go away. ;)
Avatar of VAN

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.length;i++){
 if (form.elements[i].type=="text" && /Worked/.test(form.elements[i].name) || /Holiday/.test(form.elements[i].name)){
  sum+=1*form.elements[i].value
  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.elements[i].value-(sum-40)
 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-form.elements[i+1].value
 }
 
 if (sum <= 40 && sumsave <= 40  && form.elements[i].value!=''){
   form.elements[i+1].value=form.elements[i].value
 }

 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.

ASKER CERTIFIED SOLUTION
Avatar of Bustarooms
Bustarooms
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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].onkeyup=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...
Avatar of VAN

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!
cool, the problem solved, now I can calmly watch tv: Athens 2004
;)