Solved

Need a script to calculate overtime

Posted on 2004-08-12
10
557 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:VAN
10 Comments
 
LVL 1

Author Comment

by:VAN
ID: 11786734
Oh, and the 2 subtotal boxes at the bottom need to be re-calculated with each action. TtlREG should never exceed 40.
0
 
LVL 25

Expert Comment

by:devic
ID: 11787383
<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>
0
 
LVL 1

Author Comment

by:VAN
ID: 11787525
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. :(
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 25

Expert Comment

by:devic
ID: 11789046
no need to press ctrl+alt+del, just change bad value ;)
I got what do you mean, cu
0
 
LVL 1

Author Comment

by:VAN
ID: 11792135
You cant change a bad value when an alert message wont go away. ;)
0
 
LVL 1

Author Comment

by:VAN
ID: 11795299
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.

0
 
LVL 10

Accepted Solution

by:
Bustarooms earned 500 total points
ID: 11795736
i didnt do too much testing on this, but i think its closer



<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("Too many hours")
          obj.value="";
          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=PerWeekMax-Regular;
                 form.elements[i+2].value=form.elements[i].value -

form.elements[i+1].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)
{

if(obj.value > 24) 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>
0
 
LVL 14

Expert Comment

by:ziffgone
ID: 11795986
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...
0
 
LVL 1

Author Comment

by:VAN
ID: 11796052
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!
0
 
LVL 25

Expert Comment

by:devic
ID: 11796355
cool, the problem solved, now I can calmly watch tv: Athens 2004
;)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to use embargo Date/time in php 29 38
Get the number of current month days 2 25
Javascript tutorials with real examples 5 25
how can i test my code? 1 36
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question