PDA

View Full Version : Simple Comparison Calculation

kelley_mc
03-12-2009, 02:35 AM
I am trying to do what I think is a basic script and I have been searching the web for 2 days but I keep coming up empty with what I need to do. So I figured I would ask the experts. :) I would really like to thank everyone in advance for your help.

Here's my question, I built a basic form here: www.mynurserecruiter.com/calculator/3.htm

I would like the calculation to be the following:
Hourly Bill Rate (from form) x Contract Hours (from form)

Less:
Nurse Pay Rate (from form) x Contract Hours (from form)
Nurse Pay Rate (from form) x Contract Hours (from form) x .0765
Nurse Pay Rate (from form) x Contract Hours (from form) x .035
Nurse Pay Rate (from form) x Contract Hours (from form) x .05

Then display the net result in the "Expense of a Travel Nurse"

I am aware that the "Less" calculations can be combined but I would prefer to keep them separated in case I need to make any future changes. The amounts represent the pay rate, payroll taxes, workers comp and other benefits.

Thank you,
Kelley

jmrker
03-12-2009, 04:48 AM
Here you go... :)

<html>
<title>Nurse Pay</title>
<script type="text/javascript">

function CalculatePay() {
var basePay = document.budcalc.nursepayrate.value * document.budcalc.contracthours.value;
/*
Nurse Pay Rate (from form) x Contract Hours (from form)
Nurse Pay Rate (from form) x Contract Hours (from form) x .0765 // taxes
Nurse Pay Rate (from form) x Contract Hours (from form) x .035 // workers comp
Nurse Pay Rate (from form) x Contract Hours (from form) x .05 // other benefits
*/
NPR = basePay;
NPR -= basePay * 0.0765;
NPR -= basePay * 0.035;
NPR -= basePay * 0.05;
document.budcalc.res.value = NPR.toFixed(2);
}
</script>
<style type="text/css">
</style>
<body>
<form name="budcalc">
<table align="center" border="0" class="budcalc">
<tr>
<th colspan="2">
<h3 align="center">Travel vs Permanent</h3>
</th>
</tr>
<tr>
<th colspan="2" align="left">Contract Information</th>
</tr>
<tr>
<td align="right">Hourly Bill Rate: </td>
<td align="left">
<input type="text" name="billrate" size="10">
</td>
</tr>
<tr>
<td align="right">Contract Hours: </td>
<td align="left">
<input type="text" name="contracthours" size="10">
</td>
</tr>
<tr>
<th colspan="2" align="left">Information IF Nurse Was On Staff</th>
</tr>
<tr>
<td align="right">Nurse Pay Rate:</td>
<td align="left">
<input type="text" name="nursepayrate" size="10">
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input type="button" value="Calculate" onclick="CalculatePay()">
</td>
</tr>
<tr>
<td align="right"><b>Expense of a Travel Nurse:</b></td>
<td align="left">
</td>
</tr>
</table>
</form>
</body>
</html>

Question: What do you do with the "Hourly Pay Rate" field?
Does not appear to be used in any of your calculations.

Arbitrator
03-12-2009, 07:36 AM
I would like the calculation to be the following:
Hourly Bill Rate (from form) x Contract Hours (from form)

Less:
Nurse Pay Rate (from form) x Contract Hours (from form)
Nurse Pay Rate (from form) x Contract Hours (from form) x .0765
Nurse Pay Rate (from form) x Contract Hours (from form) x .035
Nurse Pay Rate (from form) x Contract Hours (from form) x .05Assuming that I understand you correctly, you want to take the value from "Add" and subtract the four values from "Less". Here's my take on that including fall-backs for invalid values:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">

<html lang="en-Latn">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Script-Type" content="text/ecmascript">
<style type="text/css" media="all">
* { margin: 0; padding: 0; }
html { background: white; color: black; font-family: sans-serif; line-height: 1.2; }
form { width: 20em; margin: 0 auto; padding: 1em; background: #eee; }
table { border-collapse: collapse; line-height: 1; }
caption, th[scope="rowgroup"] { font-weight: bold; }
caption { padding: 2em 0; }
col { width: 10em; }
th { padding: 0.2em; text-align: left; }
th[scope="row"] { font-weight: normal; }
tbody + tbody th[scope="rowgroup"] { padding: 1em 0.2em 0.2em; }
input, button { font: inherit; }
input { width: 9.4em; border: none; padding: 0 0.2em; text-align: right; font: inherit; }
em { font-style: normal; text-transform: uppercase; }
div { margin: 1em 0 0.8em; text-align: right; }
p.error { font-weight: bold; color: red; }
</style>
<script type="text/ecmascript" defer="defer">
var d = document;
var class_attr = "class";
function EID(ID) {
return d.getElementById(ID);
}
function calculate(default_action) {
if (d.implementation.hasFeature("Events", "2.0")) {
default_action.preventDefault();
}
else {
event.returnValue = false;
class_attr = "className";
}
var pass = 0;
function process_number(PN_type, input) {
if (PN_type === "dollar" && input.charAt(0) === "\$") {
input = input.substring(1, input.length);
}
if (input.length > 0 && !isNaN(input) && input >= 0) {
pass++;
input = parseFloat(input);
if (PN_type === "dollar") {
input = input.toFixed(2);
}
return input;
}
}
var billing_rate = process_number("dollar", EID("billing_rate").value);
var contract_hours = process_number(null, EID("contract_hours").value);
var pay_rate = process_number("dollar", EID("pay_rate").value);
var expenses = EID("expenses");
if (pass === 3) {
var calculated_expenses =
billing_rate * contract_hours
- pay_rate * contract_hours
- pay_rate * contract_hours * Math.pow(765, -2);
- pay_rate * contract_hours * Math.pow(35, -2);
- pay_rate * contract_hours * Math.pow(5, -2);
calculated_expenses = calculated_expenses.toFixed(2).toString();
if (calculated_expenses.charAt(0) === "-") {
calculated_expenses = calculated_expenses.replace(/^-/, "\u2212\$");
}
else {
calculated_expenses = "\$" + calculated_expenses;
}
expenses.firstChild.data = "Total expenses for a traveling nurse come to " + calculated_expenses + ".";
try {
if (expenses.hasAttribute("class")) {
expenses.removeAttribute("class")
}
}
catch (e) {
expenses.className = "";
}
}
else {
expenses.firstChild.data = "One or more fields have an invalid value."
expenses.setAttribute(class_attr, "error");
}
}
</script>

<body>

<form method="get" action="" onsubmit="calculate(event);">
<!-- Ideally, the default action would be a server-side fall-back for those users without ECMAScript support. -->
<table>
<caption>Traveling versus Permanent</caption>
<colgroup>
<col>
<col>
</colgroup>
<tbody>
<tr>
<th colspan="2" scope="rowgroup">Contract Information</th>
</tr>
<tr>
<th scope="row"><label for="billing_rate">Hourly Billing Rate</label></th>
<td><input id="billing_rate" type="text"></td>
</tr>
<tr>
<th scope="row"><label for="contract_hours">Contract Hours</label></th>
<td><input id="contract_hours" type="text"></td>
</tr>
</tbody>
<tbody>
<tr>
<th colspan="2" scope="rowgroup">Information <em>If</em> Nurse Were on Staff</th>
</tr>
<tr>
<th scope="row"><label for="pay_rate">Nurse Pay Rate</label></th>
<td><input id="pay_rate" type="text"></td>
</tr>
</tbody>
</table>
<div><button type="submit">Calculate</button></div>
<p id="expenses">Use the form above to calculate expenses for a traveling nurse.</p>
</form>

</body>
</html>

kelley_mc
03-12-2009, 10:39 AM
Thank you VERY much for the prompt responses.

Assuming that I understand you correctly, you want to take the value from "Add" and subtract the four values from "Less".

Yes, that is exactly correct.

Arbitrator, I tested your page here: http://www.mynurserecruiter.com/calculator/5.htm

I did a test calc and it appears to be performing the following calc:
Hourly Bill Rate (from form) x Contract Hours (from form)
Less:
Nurse Pay Rate (from form) x Contract Hours (from form)

That much is working perfectly. However, the remaining portion of the calc is not being subtracted from the balance.
Less:
Nurse Pay Rate (from form) x Contract Hours (from form) x .0765
Nurse Pay Rate (from form) x Contract Hours (from form) x .035
Nurse Pay Rate (from form) x Contract Hours (from form) x .05

I used the following entries:
Hourly Billing Rate - 60
Contract Hours - 100
Nurse Pay Rate - 30

Net Result from form = \$2999.99
Actual Calculation: (60x100)-(30x100)
Calculation Should Be: (60x100)-(30x100)-(30x100X.0765)-(30x100X.035)-(30x100x.05) = \$2,515.00

I hope that makes sense. Thank you again, I do appreciate the time. :thumbsup:

kelley_mc
03-12-2009, 10:53 AM
Here you go... :)

<html>
<title>Nurse Pay</title>
<script type="text/javascript">

function CalculatePay() {
var basePay = document.budcalc.nursepayrate.value * document.budcalc.contracthours.value;
/*
Nurse Pay Rate (from form) x Contract Hours (from form)
Nurse Pay Rate (from form) x Contract Hours (from form) x .0765 // taxes
Nurse Pay Rate (from form) x Contract Hours (from form) x .035 // workers comp
Nurse Pay Rate (from form) x Contract Hours (from form) x .05 // other benefits
*/
NPR = basePay;
NPR -= basePay * 0.0765;
NPR -= basePay * 0.035;
NPR -= basePay * 0.05;
document.budcalc.res.value = NPR.toFixed(2);
}
</script>
<style type="text/css">
</style>
<body>
<form name="budcalc">
<table align="center" border="0" class="budcalc">
<tr>
<th colspan="2">
<h3 align="center">Travel vs Permanent</h3>
</th>
</tr>
<tr>
<th colspan="2" align="left">Contract Information</th>
</tr>
<tr>
<td align="right">Hourly Bill Rate: </td>
<td align="left">
<input type="text" name="billrate" size="10">
</td>
</tr>
<tr>
<td align="right">Contract Hours: </td>
<td align="left">
<input type="text" name="contracthours" size="10">
</td>
</tr>
<tr>
<th colspan="2" align="left">Information IF Nurse Was On Staff</th>
</tr>
<tr>
<td align="right">Nurse Pay Rate:</td>
<td align="left">
<input type="text" name="nursepayrate" size="10">
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input type="button" value="Calculate" onclick="CalculatePay()">
</td>
</tr>
<tr>
<td align="right"><b>Expense of a Travel Nurse:</b></td>
<td align="left">
</td>
</tr>
</table>
</form>
</body>
</html>

Question: What do you do with the "Hourly Pay Rate" field?
Does not appear to be used in any of your calculations.

This works perfectly! Here is a link: http://www.mynurserecruiter.com/calculator/4.htm

I'm confused though by your response, "Question: What do you do with the "Hourly Pay Rate" field? Does not appear to be used in any of your calculations." I believe you are using that field in the calculations because the script is working, correct?

kelley_mc
03-12-2009, 11:00 AM
I don't want to be difficult but now that I look at the display, is there any way to get the calculations to show? So in my example input of:
Hourly Billing Rate - 60
Contract Hours - 100
Nurse Pay Rate - 30

Could the display look like this?
Estimated Billings - \$6,000.00
Estimated Pay - \$3,000.00
Estimated Payroll Taxes - \$229.50
Estimated Workers Comp - \$105.00
Estimated Benefits - \$150.00

Net Difference - \$2,515,50

Thank you all again, this has been SO helpful. I may be able to keep my hair. :D

jmrker
03-12-2009, 01:56 PM
This works perfectly! Here is a link: http://www.mynurserecruiter.com/calculator/4.htm

I'm confused though by your response, "Question: What do you do with the "Hourly Pay Rate" field? Does not appear to be used in any of your calculations." I believe you are using that field in the calculations because the script is working, correct?

No, none of the calculations use the "billrate" value. Hence the question.
Your original formulas left that field off the calculations.

Also, for the calculations display it is a simple change, but I won't be able to get to it until later (work still pays the bills for the nurses care :))
You might want to post how you would like to see the display (screen positions etc) until I can get back to the problem. :D

kelley_mc
03-12-2009, 03:11 PM
No, none of the calculations use the "billrate" value. Hence the question.
Your original formulas left that field off the calculations.

Sorry, I can be dumb at times. Here is the calculation from my first post:

I would like the calculation to be the following:
Hourly Bill Rate (from form) x Contract Hours (from form)

Less:
Nurse Pay Rate (from form) x Contract Hours (from form)
Nurse Pay Rate (from form) x Contract Hours (from form) x .0765
Nurse Pay Rate (from form) x Contract Hours (from form) x .035
Nurse Pay Rate (from form) x Contract Hours (from form) x .05

I would like to use the Hourly Bill Rate x the Contract Hours less the 4 items listed under "Less:"

Here is a sample of what I would like it to look like:http://www.mynurserecruiter.com/calculator/output.htm

The form would obviously be blank but I put some numbers in to show what could be entered and the results of that calculation.

Arbitrator
03-12-2009, 11:16 PM
I did a test calc and it appears to be performing the following calc:
Hourly Bill Rate (from form) x Contract Hours (from form)
Less:
Nurse Pay Rate (from form) x Contract Hours (from form)

That much is working perfectly. However, the remaining portion of the calc is not being subtracted from the balance.
Less:
Nurse Pay Rate (from form) x Contract Hours (from form) x .0765
Nurse Pay Rate (from form) x Contract Hours (from form) x .035
Nurse Pay Rate (from form) x Contract Hours (from form) x .05

I used the following entries:
Hourly Billing Rate - 60
Contract Hours - 100
Nurse Pay Rate - 30

Net Result from form = \$2999.99
Actual Calculation: (60x100)-(30x100)
Calculation Should Be: (60x100)-(30x100)-(30x100X.0765)-(30x100X.035)-(30x100x.05) = \$2,515.00Yeah, my script contained a few stray semicolons in the calculation. I also wasn’t using the pow (i.e., mathematical power) method correctly. Even after I fixed those things, 150 was being calculated as 150 plus a very small fraction. I believe that I’ve fixed all of those things in the latest script.

(By the way, the number, above, that you provided (i.e., \$2,515.00) is fifty cents off.)

Here is a sample of what I would like it to look like:http://www.mynurserecruiter.com/calculator/output.htmThe script has been further adjusted so that it generates a table showing that information.

Hopefully, there are no further problems with the script. (Feel free to point more out though.)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">

<html lang="en-Latn">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Script-Type" content="text/ecmascript">
<style type="text/css" media="all">
* { margin: 0; padding: 0; }
html { background: white; color: black; font-family: sans-serif; line-height: 1.2; }
form { width: 20em; margin: 0 auto; padding: 1em; background: #eee; }
table { border-collapse: collapse; line-height: 1; }
caption, th[scope="rowgroup"] { font-weight: bold; }
caption { padding: 2em 0; }
col { width: 10em; }
th { padding: 0.2em; text-align: left; }
th[scope="row"] { font-weight: normal; }
tbody + tbody th[scope="rowgroup"] { padding: 1em 0.2em 0.2em; }
td { padding: 0.1em; text-align: right; }
input, button { font: inherit; }
input { width: 9.4em; border: none; padding: 0 0.2em; text-align: right; font: inherit; }
em { font-style: normal; text-transform: uppercase; }
div { margin: 1em 0 0.8em; text-align: right; }
p#expenses.error { font-weight: bold; color: red; }
p#expenses + table { margin: 1em 0 0; }
p#expenses + table col { width: 14em; }
p#expenses + table col + col { width: 6em; }
p#expenses + table td { padding: 0.2em; }
p#expenses + table tbody + tbody + tbody tr > * { padding-top: 1em; }
</style>
<script type="text/ecmascript" defer="defer">
var d = document;
var class_attr = "class";
var colspan_attr = "colspan";
function EID(ID) {
return d.getElementById(ID);
}
function E(E_name) {
return d.createElement(E_name);
}
function T(T_content) {
return d.createTextNode(T_content);
}
function calculate(default_action) {
if (d.implementation.hasFeature("Events", "2.0")) {
default_action.preventDefault();
}
else {
event.returnValue = false;
class_attr = "className";
colspan_attr = "colSpan";
}
var pass = 0;
function process_number(PN_type, input) {
if (PN_type === "dollar" && input.charAt(0) === "\$") {
input = input.substring(1, input.length);
}
if (input.length > 0 && !isNaN(input) && input >= 0) {
pass++;
input = parseFloat(input);
if (PN_type === "dollar") {
input = input.toFixed(2);
}
return input;
}
}
var billing_rate = process_number("dollar", EID("billing_rate").value);
var contract_hours = process_number(null, EID("contract_hours").value);
var pay_rate = process_number("dollar", EID("pay_rate").value);
var expenses = EID("expenses");
if (pass === 3) {
var calculated_expenses =
billing_rate * contract_hours
- pay_rate * contract_hours
- parseFloat((pay_rate * contract_hours * (765 * Math.pow(10, -4))).toFixed(4))
- parseFloat((pay_rate * contract_hours * (35 * Math.pow(10, -3))).toFixed(4))
- parseFloat((pay_rate * contract_hours * (5 * Math.pow(10, -2))).toFixed(4));
calculated_expenses = calculated_expenses.toFixed(2).toString();
if (calculated_expenses.charAt(0) === "-") {
calculated_expenses = calculated_expenses.replace(/^-/, "\u2212\$");
}
else {
calculated_expenses = "\$" + calculated_expenses;
}
expenses.firstChild.data = "Based upon the information entered, total expenses for a traveling nurse come to " + calculated_expenses + ". The breakdown is as follows:";
try {
if (expenses.hasAttribute("class")) {
expenses.removeAttribute("class")
}
}
catch (e) {
expenses.className = "";
}
var table_E = E("table");
var colgroup_E = E("colgroup");
var col_E = [E("col"), E("col")];
var tbody_E = [E("tbody"), E("tbody"), E("tbody")];
var tr_E = [E("tr"), E("tr"), E("tr"), E("tr"), E("tr"), E("tr"), E("tr")];
var th_E = [E("th"), E("th"), E("th"), E("th"), E("th"), E("th"), E("th")];
var td_E = [E("td"), E("td"), E("td"), E("td"), E("td"), E("td")];
var em_E = E("em");
table_E.appendChild(colgroup_E);
table_E.lastChild.appendChild(col_E[0]);
table_E.lastChild.appendChild(col_E[1]);
table_E.appendChild(tbody_E[0]);
table_E.lastChild.appendChild(tr_E[0]);
table_E.lastChild.lastChild.appendChild(th_E[0]);
table_E.lastChild.lastChild.appendChild(td_E[0]);
table_E.appendChild(tbody_E[1]);
table_E.lastChild.appendChild(tr_E[1]);
table_E.lastChild.lastChild.appendChild(th_E[1]);
table_E.lastChild.appendChild(tr_E[2]);
table_E.lastChild.lastChild.appendChild(th_E[2]);
table_E.lastChild.lastChild.appendChild(td_E[1]);
table_E.lastChild.appendChild(tr_E[3]);
table_E.lastChild.lastChild.appendChild(th_E[3]);
table_E.lastChild.lastChild.appendChild(td_E[2]);
table_E.lastChild.appendChild(tr_E[4]);
table_E.lastChild.lastChild.appendChild(th_E[4]);
table_E.lastChild.lastChild.appendChild(td_E[3]);
table_E.lastChild.appendChild(tr_E[5]);
table_E.lastChild.lastChild.appendChild(th_E[5]);
table_E.lastChild.lastChild.appendChild(td_E[4]);
table_E.appendChild(tbody_E[2]);
table_E.lastChild.appendChild(tr_E[6]);
table_E.lastChild.lastChild.appendChild(th_E[6]);
table_E.lastChild.lastChild.appendChild(td_E[5]);
th_E[0].setAttribute("scope", "rowgroup");
th_E[1].setAttribute(colspan_attr, "2");
th_E[1].setAttribute("scope", "rowgroup");
th_E[2].setAttribute("scope", "row");
th_E[3].setAttribute("scope", "row");
th_E[4].setAttribute("scope", "row");
th_E[5].setAttribute("scope", "row");
th_E[6].setAttribute("scope", "rowgroup");
th_E[0].appendChild(T("Bills for a Travel Nurse"));
td_E[0].appendChild(T("\$" + (billing_rate * contract_hours).toFixed(2)));
th_E[1].appendChild(T("Costs "))
th_E[1].appendChild(em_E);
th_E[1].lastChild.appendChild(T("If"));
th_E[1].appendChild(T(" Nurse Were on Staff"));
th_E[2].appendChild(T("Wages"))
td_E[1].appendChild(T("\$" + (pay_rate * contract_hours).toFixed(2)));
th_E[3].appendChild(T("Payroll Taxes"))
td_E[2].appendChild(T("\$" + (pay_rate * contract_hours * (765 * Math.pow(10, -4))).toFixed(2)));
th_E[4].appendChild(T("Workers\u2019 Compensation"))
td_E[3].appendChild(T("\$" + (pay_rate * contract_hours * (35 * Math.pow(10, -3))).toFixed(2)));
th_E[5].appendChild(T("Estimated Benefits"))
td_E[4].appendChild(T("\$" + (pay_rate * contract_hours * (5 * Math.pow(10, -2))).toFixed(2)));
th_E[6].appendChild(T("Net Difference"))
td_E[5].appendChild(T(calculated_expenses));
if (expenses.nextSibling !== null && expenses.nextSibling.nodeName.toLowerCase() === "table") {
expenses.parentNode.replaceChild(table_E, expenses.nextSibling);
}
else if (expenses.nextSibling !== null) {
expenses.parentNode.insertBefore(table_E, expenses.nextSibling);
}
else {
expenses.parentNode.appendChild(table_E);
}
}
else {
if (expenses.nextSibling !== null && expenses.nextSibling.nodeName.toLowerCase() === "table") {
expenses.parentNode.removeChild(expenses.nextSibling);
}
expenses.firstChild.data = "One or more fields have an invalid value."
expenses.setAttribute(class_attr, "error");
}
}
</script>

<body>

<form method="get" action="" onsubmit="calculate(event);">
<!-- Ideally, the default action would be a server-side fall-back for those users without ECMAScript support. -->
<table>
<caption>Traveling versus Permanent</caption>
<colgroup>
<col>
<col>
</colgroup>
<tbody>
<tr>
<th colspan="2" scope="rowgroup">Contract Information</th>
</tr>
<tr>
<th scope="row"><label for="billing_rate">Hourly Billing Rate</label></th>
<td><input id="billing_rate" type="text"></td>
</tr>
<tr>
<th scope="row"><label for="contract_hours">Contract Hours</label></th>
<td><input id="contract_hours" type="text"></td>
</tr>
</tbody>
<tbody>
<tr>
<th colspan="2" scope="rowgroup">Information <em>If</em> Nurse Were on Staff</th>
</tr>
<tr>
<th scope="row"><label for="pay_rate">Nurse Pay Rate</label></th>
<td><input id="pay_rate" type="text"></td>
</tr>
</tbody>
</table>
<div><button type="submit">Calculate</button></div>
<p id="expenses">Use the form above to calculate expenses for a traveling nurse.</p>
</form>

</body>
</html>

kelley_mc
03-12-2009, 11:48 PM
Wow, that's great.

You have this in the code" <!-- Ideally, the default action would be a server-side fall-back for those users without ECMAScript support. -->", is there a better way that you would recommend?

Thank you again!

Arbitrator
03-13-2009, 05:22 AM
You have this in the code" <!-- Ideally, the default action would be a server-side fall-back for those users without ECMAScript support. -->", is there a better way that you would recommend?The default action is what happens when you submit the form; information is sent to the server and the server does something with it. The script is currently suppressing the default action and all actions are being taken care of by said script. Ideally, you would have a fall-back that utilizes the default action for users that are using browsers with ECMAScript (i.e., JavaScript) disabled.

Essentially, you would have a second fall-back script (in, for example, the PHP language) on the server that would do exactly the same thing as the client-side script, but the process would be significantly slower since information would have to be sent to the server, the server would have to process it, and then the page would reload with the new information in the source code. All of this would have to be done each time the script was run. (Thus, the client-side script can be thought of as a convenience feature for the user since it makes the calculation faster.) Besides being a fall-back, the server-side script has the additional benefit of not being subject to browser incompatibilities (the current script has several adjustments in it to compensate for problems with the Windows Internet Explorer browser).

Currently, the form has the action attribute set to "". The empty string essentially means that the page submits to itself. However, without the server-side script, the page would simply reload (submitting onto itself with no processing of the submitted information) if ECMAScript support were not present (and, as a result, the form would not do anything useful).

I may write a server-side demo for you in a bit if I feel up to it. However, my PHP skills are weak relative to my ECMAScript skills, so I may not get around to it.

kelley_mc
03-13-2009, 07:23 PM
Thank you for everything, I really appreciate it.

Arbitrator
03-14-2009, 01:27 AM
I may write a server-side demo for you in a bit if I feel up to it. However, my PHP skills are weak relative to my ECMAScript skills, so I may not get around to it.I had to learn some new things about PHP, but I figured out how to do this.

HTML: http://www.jsgp.us/demos/CF161028.html.php?billing_rate=60&contract_hours=100&pay_rate=30
XHTML: http://www.jsgp.us/demos/CF161028.xhtml.php?billing_rate=60&contract_hours=100&pay_rate=30

I also made some improvements to the rest of the code.

HTML

<?php

\$summary = "Use the form above to calculate expenses for a traveling nurse.";
\$summary_classes = "";
\$pass_1 = 0;
\$pass_2 = 0;
\$input = array();
if (\$_GET["billing_rate"] !== null) {
\$pass_1++;
array_push(\$input, ' value="' . \$_GET["billing_rate"] . '"');
}
if (\$_GET["contract_hours"] !== null) {
\$pass_1++;
array_push(\$input, ' value="' . \$_GET["contract_hours"] . '"');
}
if (\$_GET["pay_rate"] !== null) {
\$pass_1++;
array_push(\$input, ' value="' . \$_GET["pay_rate"] . '"');
}
function process_number(\$PN_type, \$input) {
if (\$PN_type === "dollar" && substr(\$input, 0, 1) === "\$") {
\$input = substr(\$input, 1);
}
if ((bool) preg_match("/^(\d+\.\d*|\.?\d+)\$/", \$input)) {
\$GLOBALS["pass_2"]++;
\$input = (float) \$input;
if (PN_type === "dollar") {
\$input = round(\$input, 2);
}
return \$input;
}
}
function format_number(\$number) {
\$number = number_format(\$number, 2, ".", ",");
if (substr(\$number, 0, 1) === "-") {
return \$number = preg_replace("/^-/", "&#x2212;\$", \$number);
}
else {
return \$number = "\$" . \$number;
}
}
if (\$pass_1 === 3) {
\$billing_rate = process_number("dollar", \$_GET["billing_rate"]);
\$contract_hours = process_number(null, \$_GET["contract_hours"]);
\$pay_rate = process_number("dollar", \$_GET["pay_rate"]);
if (\$pass_2 === 3) {
\$bills = round(\$billing_rate * \$contract_hours, 4);
\$wages = round(\$pay_rate * \$contract_hours, 4);
\$payroll_taxes = round(\$wages * 765 * pow(10, -4), 4);
\$workers_comp = round(\$wages * 35 * pow(10, -3), 4);
\$benefits = round(\$wages * 5 * pow(10, -2), 4);
\$expenses = format_number(\$bills - \$wages - \$payroll_taxes - \$workers_comp - \$benefits);
\$bills = format_number(\$bills);
\$wages = format_number(\$wages);
\$payroll_taxes = format_number(\$payroll_taxes);
\$workers_comp = format_number(\$workers_comp);
\$benefits = format_number(\$benefits);
\$summary = "Based upon the information entered, total expenses for a traveling nurse come to " . \$expenses . ". The breakdown is as follows:";
\$T = "\t\t\t";
\$breakdown =
\$T . "<table id=\"breakdown\">\n" .
\$T . "\t<colgroup>\n" .
\$T . "\t\t<col>\n" .
\$T . "\t\t<col>\n" .
\$T . "\t</colgroup>\n" .
\$T . "\t<tbody>\n" .
\$T . "\t\t<tr>\n" .
\$T . "\t\t\t<th scope=\"rowgroup\">Bills for a Travel Nurse</th>\n" .
\$T . "\t\t\t<td>" . \$bills . "</td>\n" .
\$T . "\t\t</tr>\n" .
\$T . "\t</tbody>\n" .
\$T . "\t<tbody>\n" .
\$T . "\t\t<tr>\n" .
\$T . "\t\t\t<th colspan=\"2\" scope=\"rowgroup\">Costs <em>If</em> Nurse Were on Staff</th>\n" .
\$T . "\t\t</tr>\n" .
\$T . "\t\t<tr>\n" .
\$T . "\t\t\t<th scope=\"row\">Wages</th>\n" .
\$T . "\t\t\t<td>" . \$wages . "</td>\n" .
\$T . "\t\t</tr>\n" .
\$T . "\t\t<tr>\n" .
\$T . "\t\t\t<th scope=\"row\">Payroll Taxes</th>\n" .
\$T . "\t\t\t<td>" . \$payroll_taxes . "</td>\n" .
\$T . "\t\t</tr>\n" .
\$T . "\t\t<tr>\n" .
\$T . "\t\t\t<th scope=\"row\">Workers&#x2019; Compensation</th>\n" .
\$T . "\t\t\t<td>" . \$workers_comp . "</td>\n" .
\$T . "\t\t</tr>\n" .
\$T . "\t\t<tr>\n" .
\$T . "\t\t\t<th scope=\"row\">Estimated Benefits</th>\n" .
\$T . "\t\t\t<td>" . \$benefits . "</td>\n" .
\$T . "\t\t</tr>\n" .
\$T . "\t</tbody>\n" .
\$T . "\t<tbody>\n" .
\$T . "\t\t<tr>\n" .
\$T . "\t\t\t<th scope=\"rowgroup\">Net Difference</th>\n" .
\$T . "\t\t\t<td>" . \$expenses . "</td>\n" .
\$T . "\t\t</tr>\n" .
\$T . "\t</tbody>\n" .
\$T . "</table>\n";
}
else {
\$summary = "One or more fields have an invalid value.";
\$summary_classes = ' class="error"';
}
}
\$output =
"<p id=\"summary\"" . \$summary_classes . ">" . \$summary . "</p>\n" .
\$breakdown;

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">

<html lang="en-Latn">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Script-Type" content="text/ecmascript">
<style type="text/css" media="all">
* { margin: 0; padding: 0; }
html { background: white; color: black; font-family: sans-serif; line-height: 1.2; }
form { width: 20em; margin: 0 auto; padding: 0 1em 1em; background: #eee; }
table { border-collapse: collapse; line-height: 1; }
caption, th[scope="rowgroup"] { font-weight: bold; }
caption { padding: 2em 0; }
col { width: 10em; }
th { padding: 0.2em; text-align: left; }
th[scope="row"] { font-weight: normal; }
tbody + tbody th[scope="rowgroup"] { padding: 1em 0.2em 0.2em; }
td { padding: 0.1em; text-align: right; }
input, button { font: inherit; }
input { width: 9.4em; border: none; padding: 0 0.2em; text-align: right; font: inherit; }
em { font-style: normal; text-transform: uppercase; }
div { margin: 1em 0 0.8em; text-align: right; }
*.error { font-weight: bold; color: red; }
*#breakdown { margin: 1em 0 0; }
*#breakdown col { width: 14em; }
*#breakdown col + col { width: 6em; }
*#breakdown td { padding: 0.2em; }
*#breakdown tbody + tbody + tbody tr > * { padding-top: 1em; }
</style>
<script type="text/ecmascript" defer="defer">
var D = document;
var class_A = "class";
var colspan_A = "colspan";
function EID(ID) {
return D.getElementById(ID);
}
function E(E_name) {
return D.createElement(E_name);
}
function A(A_element, A_name, A_value) {
return A_element.setAttribute(A_name, A_value);
}
function T(T_content) {
return D.createTextNode(T_content);
}
var pass = 0;
function process_number(PN_type, input) {
if (PN_type === "dollar" && input.charAt(0) === "\$") {
input = input.substring(1, input.length);
}
if (/^(\d+\.\d*|\.?\d+)\$/.test(input)) {
pass++;
input = parseFloat(input);
if (PN_type === "dollar") {
input = parseFloat(input.toFixed(2));
}
return input;
}
}
function format_number(number) {
number = number.toString();
if (number.charAt(0) === "-") {
var prefix = "\u2212\$";
}
else {
var prefix = "\$";
}
var whole_part = number.match(/^\d*/)[0];
if (/\.\d*\$/.test(number)) {
var fractional_part = number.match(/\.\d*\$/)[0];
}
else {
var fractional_part = "";
}
var divisions = Math.floor(whole_part.length / 3);
if (whole_part.length === divisions * 3 + 1 || whole_part.length === divisions * 3 + 2) {
var new_whole_part = "";
for (var i = 0; i < divisions; i++) {
var offset_1 = whole_part.length - (i + 1) * 3;
var offset_2 = whole_part.length - i * 3;
new_whole_part = "," + whole_part.substring(offset_1, offset_2) + new_whole_part;
if (i + 1 === divisions) {
whole_part = whole_part.substring(0, offset_1) + new_whole_part;
}
}
}
switch (fractional_part.length) {
case 0:
fractional_part += ".00"
break;
case 1:
fractional_part += "00";
break;
case 2:
fractional_part += "0";
break;
default:
fractional_part = fractional_part.substring(0, 3);
}
return prefix + whole_part + fractional_part;
}
function calculate(default_action) {
if (D.implementation.hasFeature("Events", "2.0")) {
default_action.preventDefault();
}
else {
event.returnValue = false;
}
if (EID("breakdown") !== null) {
EID("breakdown").parentNode.removeChild(EID("breakdown"));
}
var billing_rate = process_number("dollar", EID("billing_rate").value);
var contract_hours = process_number(null, EID("contract_hours").value);
var pay_rate = process_number("dollar", EID("pay_rate").value);
var summary = EID("summary");
if (pass === 3) {
var bills = parseFloat((billing_rate * contract_hours).toFixed(4));
var wages = parseFloat((pay_rate * contract_hours).toFixed(4));
var payroll_taxes = parseFloat((wages * 765 * Math.pow(10, -4)).toFixed(4));
var workers_comp = parseFloat((wages * 35 * Math.pow(10, -3)).toFixed(4));
var benefits = parseFloat((wages * 5 * Math.pow(10, -2)).toFixed(4));
var expenses = format_number(bills - wages - payroll_taxes - workers_comp - benefits);
bills = format_number(bills);
wages = format_number(wages);
payroll_taxes = format_number(payroll_taxes);
workers_comp = format_number(workers_comp);
benefits = format_number(benefits);
summary.firstChild.data = "Based upon the information entered, total expenses for a traveling nurse come to " + expenses + ". The breakdown is as follows:";
if (D.implementation.hasFeature("Core", "2.0") && summary.hasAttribute("class")) {
summary.removeAttribute("class");
}
else {
summary.className = "";
}
var table_E = E("table");
var colgroup_E = E("colgroup");
var col_E = [E("col"), E("col")];
var tbody_E = [E("tbody"), E("tbody"), E("tbody")];
var tr_E = [E("tr"), E("tr"), E("tr"), E("tr"), E("tr"), E("tr"), E("tr")];
var th_E = [E("th"), E("th"), E("th"), E("th"), E("th"), E("th"), E("th")];
var td_E = [E("td"), E("td"), E("td"), E("td"), E("td"), E("td")];
var em_E = E("em");
table_E.appendChild(colgroup_E);
table_E.lastChild.appendChild(col_E[0]);
table_E.lastChild.appendChild(col_E[1]);
table_E.appendChild(tbody_E[0]);
table_E.lastChild.appendChild(tr_E[0]);
table_E.lastChild.lastChild.appendChild(th_E[0]);
table_E.lastChild.lastChild.appendChild(td_E[0]);
table_E.appendChild(tbody_E[1]);
table_E.lastChild.appendChild(tr_E[1]);
table_E.lastChild.lastChild.appendChild(th_E[1]);
table_E.lastChild.appendChild(tr_E[2]);
table_E.lastChild.lastChild.appendChild(th_E[2]);
table_E.lastChild.lastChild.appendChild(td_E[1]);
table_E.lastChild.appendChild(tr_E[3]);
table_E.lastChild.lastChild.appendChild(th_E[3]);
table_E.lastChild.lastChild.appendChild(td_E[2]);
table_E.lastChild.appendChild(tr_E[4]);
table_E.lastChild.lastChild.appendChild(th_E[4]);
table_E.lastChild.lastChild.appendChild(td_E[3]);
table_E.lastChild.appendChild(tr_E[5]);
table_E.lastChild.lastChild.appendChild(th_E[5]);
table_E.lastChild.lastChild.appendChild(td_E[4]);
table_E.appendChild(tbody_E[2]);
table_E.lastChild.appendChild(tr_E[6]);
table_E.lastChild.lastChild.appendChild(th_E[6]);
table_E.lastChild.lastChild.appendChild(td_E[5]);
th_E[0].appendChild(T("Bills for a Travel Nurse"));
td_E[0].appendChild(T(bills));
th_E[1].appendChild(T("Costs "))
th_E[1].appendChild(em_E);
th_E[1].lastChild.appendChild(T("If"));
th_E[1].appendChild(T(" Nurse Were on Staff"));
th_E[2].appendChild(T("Wages"))
td_E[1].appendChild(T(wages));
th_E[3].appendChild(T("Payroll Taxes"))
td_E[2].appendChild(T(payroll_taxes));
th_E[4].appendChild(T("Workers\u2019 Compensation"))
td_E[3].appendChild(T(workers_comp));
th_E[5].appendChild(T("Estimated Benefits"))
td_E[4].appendChild(T(benefits));
th_E[6].appendChild(T("Net Difference"))
td_E[5].appendChild(T(expenses));
A(table_E, "id", "breakdown");
A(th_E[0], "scope", "rowgroup");
A(th_E[1], colspan_A, "2");
A(th_E[1], "scope", "rowgroup");
A(th_E[2], "scope", "row");
A(th_E[3], "scope", "row");
A(th_E[4], "scope", "row");
A(th_E[5], "scope", "row");
A(th_E[6], "scope", "rowgroup");
if (summary.nextSibling !== null) {
summary.parentNode.insertBefore(table_E, summary.nextSibling);
}
else {
summary.parentNode.appendChild(table_E);
}
}
else {
summary.firstChild.data = "One or more fields have an invalid value."
A(summary, class_A, "error");
}
pass = 0;
}
function script() {
if (D.implementation.hasFeature("Events", "2.0")) {
calculate(S_event);
}, false);
}
else {
class_A = "className";
colspan_A = "colSpan";
EID("calculator").attachEvent("onsubmit", function () {
calculate();
});
}
}
</script>

<form id="calculator" method="get" action="">
<table>
<caption>Traveling versus Permanent</caption>
<colgroup>
<col>
<col>
</colgroup>
<tbody>
<tr>
<th colspan="2" scope="rowgroup">Contract Information</th>
</tr>
<tr>
<th scope="row"><label for="billing_rate">Hourly Billing Rate</label></th>
<td><input id="billing_rate" name="billing_rate" type="text"<?php echo \$input[0]; ?>></td>
</tr>
<tr>
<th scope="row"><label for="contract_hours">Contract Hours</label></th>
<td><input id="contract_hours" name="contract_hours" type="text"<?php echo \$input[1]; ?>></td>
</tr>
</tbody>
<tbody>
<tr>
<th colspan="2" scope="rowgroup">Information <em>If</em> Nurse Were on Staff</th>
</tr>
<tr>
<th scope="row"><label for="pay_rate">Nurse Pay Rate</label></th>
<td><input id="pay_rate" name="pay_rate" type="text"<?php echo \$input[2]; ?>></td>
</tr>
</tbody>
</table>
<div><button type="submit">Calculate</button></div>
<?php echo \$output; ?>
</form>

</body>
</html>

(continued due to 20000 character post limit)

Arbitrator
03-14-2009, 01:29 AM
(continued due to 20000 character post limit)

XHTML

<?php

\$summary = "Use the form above to calculate expenses for a traveling nurse.";
\$summary_classes = "";
\$pass_1 = 0;
\$pass_2 = 0;
\$input = array();
if (\$_GET["billing_rate"] !== null) {
\$pass_1++;
array_push(\$input, ' value="' . \$_GET["billing_rate"] . '"');
}
if (\$_GET["contract_hours"] !== null) {
\$pass_1++;
array_push(\$input, ' value="' . \$_GET["contract_hours"] . '"');
}
if (\$_GET["pay_rate"] !== null) {
\$pass_1++;
array_push(\$input, ' value="' . \$_GET["pay_rate"] . '"');
}
function process_number(\$PN_type, \$input) {
if (\$PN_type === "dollar" && substr(\$input, 0, 1) === "\$") {
\$input = substr(\$input, 1);
}
if ((bool) preg_match("/^(\d+\.\d*|\.?\d+)\$/", \$input)) {
\$GLOBALS["pass_2"]++;
\$input = (float) \$input;
if (PN_type === "dollar") {
\$input = round(\$input, 2);
}
return \$input;
}
}
function format_number(\$number) {
\$number = number_format(\$number, 2, ".", ",");
if (substr(\$number, 0, 1) === "-") {
return \$number = preg_replace("/^-/", "&#x2212;\$", \$number);
}
else {
return \$number = "\$" . \$number;
}
}
if (\$pass_1 === 3) {
\$billing_rate = process_number("dollar", \$_GET["billing_rate"]);
\$contract_hours = process_number(null, \$_GET["contract_hours"]);
\$pay_rate = process_number("dollar", \$_GET["pay_rate"]);
if (\$pass_2 === 3) {
\$bills = round(\$billing_rate * \$contract_hours, 4);
\$wages = round(\$pay_rate * \$contract_hours, 4);
\$payroll_taxes = round(\$wages * 765 * pow(10, -4), 4);
\$workers_comp = round(\$wages * 35 * pow(10, -3), 4);
\$benefits = round(\$wages * 5 * pow(10, -2), 4);
\$expenses = format_number(\$bills - \$wages - \$payroll_taxes - \$workers_comp - \$benefits);
\$bills = format_number(\$bills);
\$wages = format_number(\$wages);
\$payroll_taxes = format_number(\$payroll_taxes);
\$workers_comp = format_number(\$workers_comp);
\$benefits = format_number(\$benefits);
\$summary = "Based upon the information entered, total expenses for a traveling nurse come to " . \$expenses . ". The breakdown is as follows:";
\$T = "\t\t\t";
\$breakdown =
\$T . "<table id=\"breakdown\">\n" .
\$T . "\t<colgroup>\n" .
\$T . "\t\t<col></col>\n" .
\$T . "\t\t<col></col>\n" .
\$T . "\t</colgroup>\n" .
\$T . "\t<tbody>\n" .
\$T . "\t\t<tr>\n" .
\$T . "\t\t\t<th scope=\"rowgroup\">Bills for a Travel Nurse</th>\n" .
\$T . "\t\t\t<td>" . \$bills . "</td>\n" .
\$T . "\t\t</tr>\n" .
\$T . "\t</tbody>\n" .
\$T . "\t<tbody>\n" .
\$T . "\t\t<tr>\n" .
\$T . "\t\t\t<th colspan=\"2\" scope=\"rowgroup\">Costs <em>If</em> Nurse Were on Staff</th>\n" .
\$T . "\t\t</tr>\n" .
\$T . "\t\t<tr>\n" .
\$T . "\t\t\t<th scope=\"row\">Wages</th>\n" .
\$T . "\t\t\t<td>" . \$wages . "</td>\n" .
\$T . "\t\t</tr>\n" .
\$T . "\t\t<tr>\n" .
\$T . "\t\t\t<th scope=\"row\">Payroll Taxes</th>\n" .
\$T . "\t\t\t<td>" . \$payroll_taxes . "</td>\n" .
\$T . "\t\t</tr>\n" .
\$T . "\t\t<tr>\n" .
\$T . "\t\t\t<th scope=\"row\">Workers&#x2019; Compensation</th>\n" .
\$T . "\t\t\t<td>" . \$workers_comp . "</td>\n" .
\$T . "\t\t</tr>\n" .
\$T . "\t\t<tr>\n" .
\$T . "\t\t\t<th scope=\"row\">Estimated Benefits</th>\n" .
\$T . "\t\t\t<td>" . \$benefits . "</td>\n" .
\$T . "\t\t</tr>\n" .
\$T . "\t</tbody>\n" .
\$T . "\t<tbody>\n" .
\$T . "\t\t<tr>\n" .
\$T . "\t\t\t<th scope=\"rowgroup\">Net Difference</th>\n" .
\$T . "\t\t\t<td>" . \$expenses . "</td>\n" .
\$T . "\t\t</tr>\n" .
\$T . "\t</tbody>\n" .
\$T . "</table>\n";
}
else {
\$summary = "One or more fields have an invalid value.";
\$summary_classes = ' class="error"';
}
}
\$output =
"<p id=\"summary\"" . \$summary_classes . ">" . \$summary . "</p>\n" .
\$breakdown;
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>

<html xml:lang="en-Latn" xmlns="http://www.w3.org/1999/xhtml">

<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8"></meta>
<meta http-equiv="Content-Script-Type" content="application/ecmascript"></meta>
<style type="text/css" media="all"><![CDATA[
*|* { margin: 0; padding: 0; }
*|html { background: white; color: black; font-family: sans-serif; line-height: 1.2; }
*|form { width: 20em; margin: 0 auto; padding: 0 1em 1em; background: #eee; }
*|table { border-collapse: collapse; line-height: 1; }
*|caption, *|th[*|scope="rowgroup"] { font-weight: bold; }
*|caption { padding: 2em 0; }
*|col { width: 10em; }
*|th { padding: 0.2em; text-align: left; }
*|th[*|scope="row"] { font-weight: normal; }
*|tbody + *|tbody *|th[*|scope="rowgroup"] { padding: 1em 0.2em 0.2em; }
*|td { padding: 0.1em; text-align: right; }
*|input, *|button { font: inherit; }
*|input { width: 9.4em; border: none; padding: 0 0.2em; text-align: right; font: inherit; }
*|em { font-style: normal; text-transform: uppercase; }
*|div { margin: 1em 0 0.8em; text-align: right; }
*|*.error { font-weight: bold; color: red; }
*|*#breakdown { margin: 1em 0 0; }
*|*#breakdown *|col { width: 14em; }
*|*#breakdown *|col + *|col { width: 6em; }
*|*#breakdown *|td { padding: 0.2em; }
*|*#breakdown *|tbody + *|tbody + *|tbody *|tr > *|* { padding-top: 1em; }
]]></style>
<script type="application/ecmascript" defer="defer"><![CDATA[
var D = document;
var XHTML_NS = "http://www.w3.org/1999/xhtml";
function EID(ID) {
return D.getElementById(ID);
}
function E(E_name) {
return D.createElementNS(XHTML_NS, E_name);
}
function A(A_element, A_name, A_value) {
return A_element.setAttributeNS(null, A_name, A_value);
}
function T(T_content) {
return D.createTextNode(T_content);
}
var pass = 0;
function process_number(PN_type, input) {
if (PN_type === "dollar" && input.charAt(0) === "\$") {
input = input.substring(1, input.length);
}
if (/^(\d+\.\d*|\.?\d+)\$/.test(input)) {
pass++;
input = parseFloat(input);
if (PN_type === "dollar") {
input = parseFloat(input.toFixed(2));
}
return input;
}
}
function format_number(number) {
number = number.toString();
if (number.charAt(0) === "-") {
var prefix = "\u2212\$";
}
else {
var prefix = "\$";
}
var whole_part = number.match(/^\d*/)[0];
if (/\.\d*\$/.test(number)) {
var fractional_part = number.match(/\.\d*\$/)[0];
}
else {
var fractional_part = "";
}
var divisions = Math.floor(whole_part.length / 3);
if (whole_part.length === divisions * 3 + 1 || whole_part.length === divisions * 3 + 2) {
var new_whole_part = "";
for (var i = 0; i < divisions; i++) {
var offset_1 = whole_part.length - (i + 1) * 3;
var offset_2 = whole_part.length - i * 3;
new_whole_part = "," + whole_part.substring(offset_1, offset_2) + new_whole_part;
if (i + 1 === divisions) {
whole_part = whole_part.substring(0, offset_1) + new_whole_part;
}
}
}
switch (fractional_part.length) {
case 0:
fractional_part += ".00"
break;
case 1:
fractional_part += "00";
break;
case 2:
fractional_part += "0";
break;
default:
fractional_part = fractional_part.substring(0, 3);
}
return prefix + whole_part + fractional_part;
}
function calculate(default_action) {
default_action.preventDefault();
if (EID("breakdown") !== null) {
EID("breakdown").parentNode.removeChild(EID("breakdown"));
}
var billing_rate = process_number("dollar", EID("billing_rate").value);
var contract_hours = process_number(null, EID("contract_hours").value);
var pay_rate = process_number("dollar", EID("pay_rate").value);
var summary = EID("summary");
if (pass === 3) {
var bills = parseFloat((billing_rate * contract_hours).toFixed(4));
var wages = parseFloat((pay_rate * contract_hours).toFixed(4));
var payroll_taxes = parseFloat((wages * 765 * Math.pow(10, -4)).toFixed(4));
var workers_comp = parseFloat((wages * 35 * Math.pow(10, -3)).toFixed(4));
var benefits = parseFloat((wages * 5 * Math.pow(10, -2)).toFixed(4));
var expenses = format_number(bills - wages - payroll_taxes - workers_comp - benefits);
bills = format_number(bills);
wages = format_number(wages);
payroll_taxes = format_number(payroll_taxes);
workers_comp = format_number(workers_comp);
benefits = format_number(benefits);
summary.firstChild.data = "Based upon the information entered, total expenses for a traveling nurse come to " + expenses + ". The breakdown is as follows:";
if (summary.hasAttributeNS(null, "class")) {
summary.removeAttributeNS(null, "class");
}
var table_E = E("table");
var colgroup_E = E("colgroup");
var col_E = [E("col"), E("col")];
var tbody_E = [E("tbody"), E("tbody"), E("tbody")];
var tr_E = [E("tr"), E("tr"), E("tr"), E("tr"), E("tr"), E("tr"), E("tr")];
var th_E = [E("th"), E("th"), E("th"), E("th"), E("th"), E("th"), E("th")];
var td_E = [E("td"), E("td"), E("td"), E("td"), E("td"), E("td")];
var em_E = E("em");
table_E.appendChild(colgroup_E);
table_E.lastChild.appendChild(col_E[0]);
table_E.lastChild.appendChild(col_E[1]);
table_E.appendChild(tbody_E[0]);
table_E.lastChild.appendChild(tr_E[0]);
table_E.lastChild.lastChild.appendChild(th_E[0]);
table_E.lastChild.lastChild.appendChild(td_E[0]);
table_E.appendChild(tbody_E[1]);
table_E.lastChild.appendChild(tr_E[1]);
table_E.lastChild.lastChild.appendChild(th_E[1]);
table_E.lastChild.appendChild(tr_E[2]);
table_E.lastChild.lastChild.appendChild(th_E[2]);
table_E.lastChild.lastChild.appendChild(td_E[1]);
table_E.lastChild.appendChild(tr_E[3]);
table_E.lastChild.lastChild.appendChild(th_E[3]);
table_E.lastChild.lastChild.appendChild(td_E[2]);
table_E.lastChild.appendChild(tr_E[4]);
table_E.lastChild.lastChild.appendChild(th_E[4]);
table_E.lastChild.lastChild.appendChild(td_E[3]);
table_E.lastChild.appendChild(tr_E[5]);
table_E.lastChild.lastChild.appendChild(th_E[5]);
table_E.lastChild.lastChild.appendChild(td_E[4]);
table_E.appendChild(tbody_E[2]);
table_E.lastChild.appendChild(tr_E[6]);
table_E.lastChild.lastChild.appendChild(th_E[6]);
table_E.lastChild.lastChild.appendChild(td_E[5]);
th_E[0].appendChild(T("Bills for a Travel Nurse"));
td_E[0].appendChild(T(bills));
th_E[1].appendChild(T("Costs "))
th_E[1].appendChild(em_E);
th_E[1].lastChild.appendChild(T("If"));
th_E[1].appendChild(T(" Nurse Were on Staff"));
th_E[2].appendChild(T("Wages"))
td_E[1].appendChild(T(wages));
th_E[3].appendChild(T("Payroll Taxes"))
td_E[2].appendChild(T(payroll_taxes));
th_E[4].appendChild(T("Workers\u2019 Compensation"))
td_E[3].appendChild(T(workers_comp));
th_E[5].appendChild(T("Estimated Benefits"))
td_E[4].appendChild(T(benefits));
th_E[6].appendChild(T("Net Difference"))
td_E[5].appendChild(T(expenses));
A(table_E, "id", "breakdown");
A(th_E[0], "scope", "rowgroup");
A(th_E[1], "colspan", "2");
A(th_E[1], "scope", "rowgroup");
A(th_E[2], "scope", "row");
A(th_E[3], "scope", "row");
A(th_E[4], "scope", "row");
A(th_E[5], "scope", "row");
A(th_E[6], "scope", "rowgroup");
if (summary.nextSibling !== null) {
summary.parentNode.insertBefore(table_E, summary.nextSibling);
}
else {
summary.parentNode.appendChild(table_E);
}
}
else {
summary.firstChild.data = "One or more fields have an invalid value."
A(summary, "class", "error");
}
pass = 0;
}
function script() {
calculate(S_event);
}, false);
}
]]></script>

<form id="calculator" method="get" action="">
<table>
<caption>Traveling versus Permanent</caption>
<colgroup>
<col></col>
<col></col>
</colgroup>
<tbody>
<tr>
<th colspan="2" scope="rowgroup">Contract Information</th>
</tr>
<tr>
<th scope="row"><label for="billing_rate">Hourly Billing Rate</label></th>
<td><input id="billing_rate" name="billing_rate" type="text"<?php echo \$input[0]; ?>></input></td>
</tr>
<tr>
<th scope="row"><label for="contract_hours">Contract Hours</label></th>
<td><input id="contract_hours" name="contract_hours" type="text"<?php echo \$input[1]; ?>></input></td>
</tr>
</tbody>
<tbody>
<tr>
<th colspan="2" scope="rowgroup">Information <em>If</em> Nurse Were on Staff</th>
</tr>
<tr>
<th scope="row"><label for="pay_rate">Nurse Pay Rate</label></th>
<td><input id="pay_rate" name="pay_rate" type="text"<?php echo \$input[2]; ?>></input></td>
</tr>
</tbody>
</table>
<div><button type="submit">Calculate</button></div>
<?php echo \$output; ?>
</form>

</body>
</html>

jmrker
03-14-2009, 04:50 AM
Alternative JS and HTML (no PHP code) solution ... :thumbsup:

<html>
<title>Nurse Pay</title>
<script type="text/javascript">

function CalculatePay() {
/*
Nurse Pay Rate (from form) x Contract Hours (from form)
Nurse Pay Rate (from form) x Contract Hours (from form) x .0765 // taxes
Nurse Pay Rate (from form) x Contract Hours (from form) x .035 // workers comp
Nurse Pay Rate (from form) x Contract Hours (from form) x .05 // other benefits
*/
var basePay = document.getElementById('nursepayrate').value * document.getElementById('contracthours').value;
var movePay = document.getElementById('billrate').value * document.getElementById('contracthours').value
NPR = basePay;
Taxes = basePay * 0.0765;
WorkComp = basePay * 0.035;
Benefits = basePay * 0.05;
var res = (NPR-Taxes-WorkComp-Benefits).toFixed(2);

document.getElementById('res').value = '\$'+res;
document.getElementById('Wages').innerHTML = '\$'+basePay.toFixed(2);
document.getElementById('Taxes').innerHTML = '\$'+Taxes.toFixed(2);
document.getElementById('WorkComp').innerHTML = '\$'+WorkComp.toFixed(2);
document.getElementById('Benefits').innerHTML = '\$'+Benefits.toFixed(2);
document.getElementById('travelPay').innerHTML = '\$'+movePay.toFixed(2);
document.getElementById('Expense').innerHTML = res;
document.getElementById('NetDiff').innerHTML = '\$'+(movePay-NPR-Taxes-WorkComp-Benefits).toFixed(2);

}
</script>
<style type="text/css">
</style>
<body>
<form name="budcalc">
<table align="center" border="0" class="budcalc">
<tr>
<th colspan="2">
<h3 align="center">Travel vs Permanent</h3>
</th>
</tr>
<tr>
<th colspan="2" align="left">Contract Information</th>
</tr>
<tr>
<td align="right">Hourly Bill Rate: </td>
<td align="left">
<input type="text" id="billrate" name="billrate" size="10" align="right">
</td>
</tr>
<tr>
<td align="right">Contract Hours: </td>
<td align="left">
<input type="text" id="contracthours" name="contracthours" size="10" align="right">
</td>
</tr>
<tr>
<th colspan="2" align="left">Information IF Nurse Was On Staff</th>
</tr>
<tr>
<td align="right">Nurse Pay Rate:</td>
<td align="left">
<input type="text" id="nursepayrate" name="nursepayrate" size="10" align="right">
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input type="button" value="Calculate" onclick="CalculatePay()">
</td>
</tr>
<tr>
<td align="right"><b>Expense of a Travel Nurse:</b></td>
<td align="left">
</td>
</tr>

<tr>
<td colspan="2">
<p><b>Based on the information entered, the expense of a travel nurse would be
\$<span id="Expense" name="Expense"></span>
The breakdown is as follows:</b> </p><br>
</td>
</tr>
<tr>
<td width="310" valign="top" align="right">
<div align="left"> <b>Total Billings for a Travel Nurse</b></div>
</td>
<td width="82" valign="top">
<div id="travelPay" name="travelPay" align="right"></div>
</td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td>
<td valign="top">&nbsp;</td>
</tr>
<tr>
<td valign="top" align="right">
<div align="left"><b>Costs if the Nurse Was On Staff</b></div>
</td>
<td valign="top">&nbsp;</td>
</tr>
<tr>
<td valign="top" align="right"><b>Wages</b></td>
<td valign="top">
<div id="Wages" name="Wages" align="right"></div>
</td>
</tr>
<tr>
<td valign="top" align="right"><b>Payroll Taxes</b></td>
<td valign="top">
<div id="Taxes" name="Taxes" align="right"></div>
</td>
</tr>
<tr>
<td valign="top" align="right"><b>Workers' Compensation</b></td>
<td valign="top">
<div id="WorkComp" name="WorkComp" align="right"></div>
</td>
</tr>
<tr>
<td width="310" valign="top" align="right"><b>Estimated Benefits</b></td>
<td valign="top">
<div id="Benefits" name="Benefits" align="right"></div>
</td>
</tr>
<tr>
<td width="310" valign="top" align="right">&nbsp;</td>
<td valign="top">&nbsp;</td>
</tr>
<tr>
<td width="310" valign="top" align="right"><b>Net Difference</b></td>
<td valign="top">
<div id="NetDiff" name="NetDiff" align="right"></div>
</td>
</tr>

</table>
</form>
</body>
</html>

kelley_mc
03-14-2009, 01:39 PM
Thank you both! It's wonderful that you take your own time to help others.

Thanks again!