Hi Guys,
At the moment I am trying to convert a paper based timesheet into a web based solution. I have managed to code out the layout and feel of the timesheet & managed to grab some solutions from already answered qns in Experts Exchange. I've tried to modify some of the coding to make it suit my needs but I haven't had much luck. Basically I have several problems, which are..
1. I need to calculate the total hours worked between 2 times a start time and finish time, which I have got working but I can't get the format of totalHr1 to be 00:00 so if hours worked is 10hrs it should be 10:00.
2. Once I get the total hours worked calculated I need to make a further calculation to subtract break times from total hours which I have got working but sometimes the time format isn't correct ie when I enter 1hr for breaks and it gives 9::0 because of the convert format function. The format should be 00:00 aswell.
3. This problem is to do with flexi times, standard hours for mon - thurs is 7.25mins. I need to subtract the hrWorked by Standard and obtain the extra time worked or how much time wasn't work.
Below is the current coding for my timesheet....
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml"
>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Flexible Working Hours Timesheet</title>
<link href="/flexiTimesheet.css"
rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript" src="datetimepicker.js"></
script>
<script type="text/javascript"><!-
-converts a number into time format-->
function sayampm(t) {
if (t>= "0000"&& t<= "1159"); else
if (t>= "1200"&& t<= "2359");
}
function checkEntry(e) {
var text= e.value;
if (text.length== 2) text= "00:"+ text; else
if (text.length== 3) {
if (text.substr(0, 1)== ":") text= "00"+ text; else
text= "0"+ text.substr(0, 1)+ ":"+ text.substr(1, 2);
} else
if (text.length== 4) {
if (text.substr(1, 1)== ":") text= "0"+ text; else
text= text.substr(0, 2)+ ":"+ text.substr(2, 2);
} else if (text.length> 0) {
text= text.replace(/:/, "");
text= text.substr(text.length- 4, 2)+ ":"+ text.substr(text.length- 2, 2);
}
e.value= text;
if (text!= "") {
var timetext= text.substr(0, 2)+ text.substr(3, 2);
if (timetext>= "0000"&& timetext<= "2359"&& !isNaN(timetext)) sayampm(timetext); else {
alert('Please Enter a Valid Format in HH:MM');
e.value= "00:00";
}
}
}
function calculate(){
var start = document.getElementById("s
tartTime1"
).value.sp
lit(':');
var end = document.getElementById("f
inishTime1
").value.s
plit(':');
var total = document.getElementById("t
otalHr1");
var shour = parseInt(start[0].substrin
g(0,1)== '0'? start[0].substring(1):star
t[0]);
var smin = parseInt(start[1]);
var ehour = parseInt(end[0].substring(
0,1)== '0'? end[0].substring(1):end[0]
);
var emin = parseInt(end[1]);
if (shour>ehour)
alert("error");
else if (shour == ehour)
total.value = "00:"+(emin-smin);
else{
if (emin>=smin)
total.value=(ehour-shour)+
":"+(emin-
smin);
else
total.value=(ehour-shour-1
)+":"+(60-
smin+emin)
;
}
}
</script>
<script language="javascript">
function calculateWorked(){
var total = document.getElementById("t
otalHr1").
value.spli
t(':');
var breaks = document.getElementById("l
unch1").va
lue.split(
':');
var worked = document.getElementById("h
rWorked1")
;
var shour = parseInt(total[0].substrin
g(0,1)== '0'? total[0].substring(1):tota
l[0]);
var smin = parseInt(total[1]);
var ehour = parseInt(breaks[0].substri
ng(0,1)== '0'? breaks[0].substring(1):bre
aks[0]);
var emin = parseInt(breaks[1]);
if (shour == ehour)
worked.value = "00:"+(smin-emin);
else{
if (smin>=emin)
worked.value=(shour-ehour)
+":"+(smin
-emin);
else
worked.value=(shour-ehour-
1)+":"+(60
-emin+smin
);
}
}
</script>
<script language="javascript">
function cumulative(){
var worked = document.getElementById("h
rWorked1")
.value.spl
it(':');
var standard = document.getElementById("s
tandard1")
.value.spl
it(':');
var credit = document.getElementById("c
redit1");
var shour = parseInt(worked[0].substri
ng(0,1)== '0'? worked[0].substring(1):wor
ked[0]);
var smin = parseInt(worked[1]);
var ehour = parseInt(standard[0].subst
ring(0,1)=
= '0'? standard[0].substring(1):s
tandard[0]
);
var emin = parseInt(standard[1]);
if (shour == ehour)
credit.value = "00:"+(smin-emin);
else{
if (smin>=emin)
credit.value=(shour-ehour)
+":"+(smin
-emin);
else
credit.value=(ehour-shour-
1)+":"+(60
-emin+smin
);
}
}
</script>
</head>
<body>
<div class="containerAll">
<div class="contentMain">
<div class="container">
<form action="" method="POST" name="flexi" id="flexi">
<table bgcolor="#efefef" width="100%" border="1" bordercolor="#cccccc" cellpadding="2" cellspacing="3">
<tr>
<td>Name :<input class="textfield" type="text" id="empname" name="empname" value=""/></td>
<td>Job Title :<input class="textfield" type="text" id="jobTitle" name="jobTitle" value=""/></td>
<td>Location :<input class="textfield" type="text" id="location" name="location" value=""/></td>
<td>Period Commencing :<input class="textfield" type="text" id="period" name="period" value=""/></td>
</tr>
</table>
<blockquote></blockquote>
<table bgcolor="#efefef" width="100%" border="1" bordercolor="#cccccc" cellpadding="2" cellspacing="3">
<tr>
<td>Flex Bands :</td>
<td>08:00am - 10:00am</td>
<td>12:00pm - 2:00pm</td>
<td>4:00pm - 6:00pm</td>
<td>Core Time :</td>
<td>10:00am - 12:00pm</td>
<td>2:00 - 6.00pm</td>
</tr>
</table>
<blockquote></blockquote>
<table bgcolor="#efefef" width="100%" border="1" bordercolor="#cccccc" cellpadding="2" cellspacing="3">
<tr>
<td></td>
<td>Date :</td>
<td>Day :</td>
<td>Start Time :</td>
<td>Finish Time :</td>
<td>Total Hours for Day :</td>
<td>Lunch Break :<br />| Minimum 30mins |</td>
<td>Hours Worked :</td>
<td>Standard Hours :</td>
<td>Cumulative :<br />| Credit B/F |</td>
<td>Cumulative :<br />| Debit B/F |</td>
</tr>
<tr>
<td align="center">wk:1</td>
</tr>
<tr>
<td align="center"><a href="javascript:NewCal('t
xtDate1','
ddmmyyyy')
"><img src="cal.gif" width="20" height="20" border="0" alt="Select From Date"></a></td>
<td><input name="txtDate1" type="text" id="txtDate1" value="" size="8" /></td>
<td>Mon</td>
<td><input name="startTime1" type="text" id="startTime1" value="" size="8" onBlur="checkEntry(this);"
/></td>
<td><input name="finishTime1" type="text" id="finishTime1" value="" size="8" onBlur="checkEntry(this),c
alculate()
;"/></td>
<td><input name="totalHr1" type="text" id="totalHr1" value="" size="8"/></td>
<td><input name="lunch1" type="text" id="lunch1" value="" size="8" onBlur="checkEntry(this),c
alculateWo
rked();"/>
</td>
<td><input name="hrWorked1" type="text" id="hrWorked1" value="" size="8" onBlur="cumulative(),check
Entry(this
);"/></td>
<td><input name="standard1" type="text" id="standard1" value="07:25" size="8" readonly=""/></td>
<td><input name="credit1" type="text" id="credit1" value="" size="8" /></td>
<td><input name="debit1" type="text" id="debit2" value="" size="8" /></td>
</tr>
</table>
</div></div></div>
P.S is it possible to get the current functions to work with all the fields? Instead of having to code extra functions for each field? For example my time sheet contains 4 weeks so I have labelled the fields as mon1,mon2,mon3 etc etc.
Many Thanks for any help given.