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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Look for help i java-calculations 2

Status
Not open for further replies.

martinus1509

Technical User
Feb 5, 2012
3
DK
Hi
Perhaps I'm in the wrong forum. But nevertheless - here I go:
I'm looking for a solution on a calculation-form in which user fill in text-boxes. Then the script should first perform an addition of the values and then multiply the result with factor 1.5..

Does it make any sense?
Maybe this example will:

What I want to do is this:
(2+2+2)*1.5=9

Can this be done with one calculation or do I have to have two steps in the process?

Appearantly I'm not a programmer. But I desperately need help? So Please if anyone can answe me - and help set it up within the frame of a form produced in RSForm! Pro (from RSjoomla.com)...

Yours Sincerely

Claus Vesterager Martinus
 
A few clarifications:

1. Java is not Javascript. Since Joomla is a framework for websites, and RsForm! Pro does web forms I'll assume what you really want is a Javascript solution. In which case this is the correct forum.


2. How many textboxes? If the number is not known then I assume there would be a way of identifying the textboxes that are supposed to be added. A name or an Id maybe.


3. Why does this need to be done in Javascript? Why not in whatever processes your form server side?

4 If it does in fact need to be done client side, where is this result supposed to go to? Another textbox? Displayed as text on the page?

With all that out of the way, the basic premise would be as simple as your calculation:

Code:
function calculate(){

var result=( textbox1.value + textbox2.value + textbox3.value ... ) * 1.5;
}



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Hi 'Vacunita'

Thank you for your fast reply... As simple as I had hoped for!

As you might have figured I'm not a trained programmer. Informationarchitecture and journalism is my trade!

In order to answe your questions:
So Yes of course it's a javascripting. Sorry, will remember the right terminology next time.

Actually I'm trying to build a timetracking system for a small company. They operate with two-week periods. So for the five categories, I have a text field for each day including saturdays and sundays. On top there is the trigger-button and the total-fields.

The categories are: 1. At which customer did the employee work on each day of the period. 2. How may working hours? 3. overtime (where the number of hours should factored by factor 1.5 - hence the first question) 4. Sickness - if any how many days (7.5 hours per full day) and finally 5. Vacation - if any how many days (7.5 hours per full day)

Why does it have to be javaScripting?
Well, a good question! My starting point is to examine my client's needs. I set up a homepage using Joomla and customized template. And in order to find solutions to his needs I examine different solutions available to be running inside the Joomla framework.

Thus is my way to this point: Joomla-->RSForm! Pro-->form with different calculations. All to be delivered into the database for my customer to extract and forward to the company that actually produces the paperwork for the payment of wages and sends the actual order to the bank to pay out the correct wages for the employees...

So you see: Perhaps there really isn't any good reason for chosing this modus operandi - on the other hand it's a step by step process that hopefully will end up making my customer satisfied.

ANd by the way: The result will also be displayed on the users monitor and by printed as a pdf-document and send to him/her by email.

So once again. Thank you for your prompt reply.

Though I'm not at all finish with my work I wondered if you now or later would be interested taking a look at the form... I'm struggling with the task of setting it up so that most of the calculations can be done without having to press a trigger button...

I can make it work in a plain html-page. But not with the RSForm! Pro...

So perhaps I could persuade you to take a look and direct me in the right direction?

Yours sincerely

Claus Vesterager Martinus
 
You can use the onblur event of each textbox to execute the calculation function when the focus leaves the textbox in question.

I'm not really all that familiar with Joomla or its form maker Rs Forms.

The best I can offer is the general code to get the operation going, how you incorporate it into the Joomla form will be up to you.

Code:
<html>
<head> ...
<script type="text/Javascript">
function calculate(){
var textbox1=document.getElementById('txtbox1_ID');
var textbox2=document.getElementById('txtbox2_ID');
...

var result = (textbox1.value + textbox2.value) * 1.5;

[green]\\Output result value somewhere. [/green]
doument.getElementById("resultdisplay").innerHTML=result;
}
</script>

...

<body>


...

<input type="text"  name="textbox1" id="txtbox1_ID" onblur="calculate();">
<input type="text"  name="textbox2" id="txtbox1_ID" onblur="calculate();">

...

<span id="resultdisplay"></span>

ANd by the way: The result will also be displayed on the users monitor and by printed as a pdf-document and send to him/her by email.
Yes, I'm sure its supposed to do that, the question was more leading to how where in the page the value should be sent to.
For instance if you are submitting the form to a server side script to build the pdf and create and deliver the email, then perhaps the resulting value should be sent to a hidden form field to be processed.

Or perhaps simply sent to a div or a span or some other HTML element on screen to be displayed decoratively, or maybe use a small pop-up dialog box to show the value etc....

The code above outputs to a span element in the HTML.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Hi

Just one more thing, Phil. Some [tt]parseInt()[/tt] or [tt]parseFloat()[/tt] calls...

( ... and two typos : [tt]do[red]c[/red]ument[/tt] in the function's last line and txtbox[red]2[/red]_ID in the 2[sup]nd[/sup] [tt]input[/tt]'s [tt]id[/tt]. )

Feherke.
 
Sorry about that, that's what I get for typing it straight into the reply box.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Hi 'Vacunita' and 'Feherke'
Thank you for your answers. They certainly helped me though it seems it fundamental knowledge to you guys...

I have made it work in my form. But now I have a follow-up question.

As I'm doing a time-tracking functionality on a webpage(intranet) for a small company, the calculation of course should manage overtime-payment.

The rules that I try to apply my form are these:
1.: Overtime on normal weekdays is by factor 1.5 - one hour = 1.5 hours pay

2.: Overtime on saturdays and sundays - and certain days during the year - is by factor 2.0. One hour overtime = 2 hours pay.

And now the - at least for me - difficult part:

3.: The firste four hours of overtime is to be factored by 1.5. If more than four hours (even on a normal weekday) they should factored by 2.0.

Eg. During the two-week period one employee has performed three hours of overtime. They will as the setup is done right now be factored by 1.5.

Another employee has performed 6 hours overtime. All in normal weekdays from mon-fri. Four of these hours should be factored by 1.5 whereas the last two hours should be factored by 2.0. In this example the total result is (4*1.5)+(2*2)=10 hours.

My javascript is right now like this:
function calculate2Text()
{
var op1=document.getElementById('Overarbejde');
var op2=document.getElementById('Overarbejde2');
var op3=document.getElementById('Overarbejde3');
var op4=document.getElementById('Overarbejde4');
var op5=document.getElementById('Overarbejde5');
var op6=document.getElementById('Overarbejde6');
var op7=document.getElementById('Overarbejde7');
var op8=document.getElementById('Overarbejde copy');
var op9=document.getElementById('Overarbejde2 copy');
var op10=document.getElementById('Overarbejde3 copy');
var op11=document.getElementById('Overarbejde4 copy');
var op12=document.getElementById('Overarbejde5 copy');
var op13=document.getElementById('Overarbejde6 copy');
var op14=document.getElementById('Overarbejde7 copy');

var result=document.getElementById('overarbejdstimertotal');

if(op1.value=="" || op1.value!=parseFloat(op1.value)) op1.value=0;
if(op2.value=="" || op2.value!=parseFloat(op2.value)) op2.value=0;
if(op3.value=="" || op3.value!=parseFloat(op3.value)) op3.value=0;
if(op4.value=="" || op4.value!=parseFloat(op4.value)) op4.value=0;
if(op5.value=="" || op5.value!=parseFloat(op5.value)) op5.value=0;
if(op6.value=="" || op6.value!=parseFloat(op6.value)) op6.value=0;
if(op7.value=="" || op7.value!=parseFloat(op7.value)) op7.value=0;
if(op8.value=="" || op8.value!=parseFloat(op8.value)) op8.value=0;
if(op9.value=="" || op9.value!=parseFloat(op9.value)) op9.value=0;
if(op10.value=="" || op10.value!=parseFloat(op10.value)) op10.value=0;
if(op11.value=="" || op11.value!=parseFloat(op11.value)) op11.value=0;
if(op12.value=="" || op12.value!=parseFloat(op12.value)) op12.value=0;
if(op13.value=="" || op13.value!=parseFloat(op13.value)) op13.value=0;
if(op14.value=="" || op14.value!=parseFloat(op14.value)) op14.value=0;

result.value=0;
result.value=parseFloat(result.value);
result.value=parseFloat(result.value)+(parseFloat(op1.value)*1.5)+(parseFloat(op2.value)*1.5)+(parseFloat(op3.value)*1.5)+(parseFloat(op4.value)*1.5)+(parseFloat(op5.value)*1.5)+(parseFloat(op6.value)*2)+(parseFloat(op7.value)*2)+(parseFloat(op8.value)*1.5)+(parseFloat(op9.value)*1.5)+(parseFloat(op10.value)*1.5)+(parseFloat(op11.value)*1.5)+(parseFloat(op12.value)*1.5)+(parseFloat(op13.value)*2)+(parseFloat(op14.value)*2);
}

As you can see I multiply the input for normal weekdays (op1-op5+op8-op11) by factor 1.5 while weekends (op6+op7+op13+op14) is by factor 2.0.

Is is possible to do this:
Calculate input as above and then examine normal weekdays and factor the first four hours by factor 1.5 and the rest by factor 2.0 and at last add the input - if any - for overtime in weekends?

SOmething like: (op1+op2+op3+op4+op5+op8+op9+op10+op11+op12)=x where if x is 4 hours or less just add it with the input -if any - from (op6+op7+op13+op14)

And if (op1+op2+op3+op4+op5+op8+op9+op10+op11+op12)=x where x is higher than 4 hours do this calculation: (4*1.5)+((x-4)*2)+any input for work on saturdays and sundays)...

As it is right now I do the factorering already when the user is typing his input to the textfield. So the result now is by the right factors with regard to weekdays and weekends.

Should this be altered? Is it better to perform the actual calculation in the end?

Any suggestions?

Thank you in advance...

Claus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top