PDA

View Full Version : Need help with my Math total function; getting wrong amount

Broll05
12-08-2010, 09:02 PM
I need some help on my math equation, I got the calculations and confirm.submit, and the alert messages to all work properly, but when I go to put the data into the math equation, I am coming up with some outrageous number way off from what it is supposed to be. What I am trying to accomplish is to take the calculated number of day and hours and minutes; multiply the total time in hours by the equipment cost per hour and calculate the total amount of rent. My problem I think is in the calculate total amount of rent section in the script. Please help, your expertise is greatly appreciated.

Below is the math equation in the script section:

//calculate days from date field

function checkit() {

var today = new Date();
var nowyear = today.getFullYear();

var d1 = document.forms[0].pickupDate.value.split("/");
var yr = d1[2];
var mm = d1[0]-1;
var dy = d1[1];
var OK1 = checkValidDate(yr,mm,dy);
if ((yr < nowyear || yr > nowyear +1)) {OK1 = false} // only valid for current year and next year

var d2 = document.forms[0].returnDate.value.split("/");
yr = d2[2];
mm = d2[0]-1;
dy = d2[1];
var OK2 = checkValidDate(yr,mm,dy);

if ((yr < nowyear || yr > nowyear+2)) {OK2 = false}

if ((!OK1) || (!OK2)) {
document.forms[0].pickupDate.value = "";
document.forms[0].returnDate.value = "";
return false;
}

// arbitrary or example start and end times obtained from elsewhere (select lists)
var fhrs = parseFloat(document.forms[0].pickupHours.value);
var fmins = parseFloat(document.forms[0].pickupMinutes.value);
var shrs = parseFloat(document.forms[0].returnHours.value);
var smins = parseFloat(document.forms[0].returnMinutes.value);

var firstDate = new Date(d1[2],d1[0]-1,d1[1],fhrs,fmins); // note month must be 0-11!! Also note USA date format
var secondDate = new Date(d2[2],d2[0]-1,d2[1],shrs,smins);
var difference = secondDate.getTime() - firstDate.getTime();

var secs = difference/1000;
var days = Math.floor(secs/86400);
secs %= 86400;
var hours= Math.floor(secs/3600);

if (hours<10) {hours = "0" + hours}
secs %= 3600;
var mins = Math.floor(secs/60);

if (mins<10) {mins = "0" + mins}
alert ("Time Difference is " + days + " Days " + hours + " Hours " + mins + " Minutes");

//calculate total amount of rent

var equip = parseFloat(document.forms[0].equipment.value);
var total = (((days * 24) + hours + mins) * equip);
var OK = window.confirm(" The total rental cost is \$" + total + "\n Click OK to accept, Cancel to decline");

if (OK) {return true}
else {return false}
}

function checkValidDate(yr,mm,dy) {

var nd = new Date();
nd.setFullYear(yr,mm,dy); // YYYY,MM(0-11),DD
var ndmm = nd.getMonth();

if (ndmm != mm) {
return false;
}
else {
return true;
}
}

This is the form in the body section:

<form onsubmit = "return validateForm()" action = "mailto:rogalskibf@gmail.com?subject=ABC Customer Reservation" method="post" enctype="text/plain">
<table border = "0">
<tr>
<td>
Equipment:<br/>
<select name = "equipment">
<option value="unselected">Select Equipment Type</option>
<option value = 20>Fishing Boat</option>
<option value = 15>Kayak</option>
<option value = 2>Mountain Bike</option>
<option value = 10>Scuba Gear</option>
</select>
</td>
</tr>
<tr>
<td>
Pick-up Date: <br/>
<input type = "text" id = "dateIn" name = "pickupDate" id = "date1" onchange = checkValidPDate(this.value)>
</td>
<td>
Pick-up Time: <br/>
<select name = "pickupHours">
<option value="unselected">hr</option>
<option value = 7>07</option>
<option value = 8>08</option>
<option value = 9>09</option>
<option value = 10>10</option>
<option value = 11>11</option>
<option value = 12>12</option>
<option value = 13>13</option>
<option value = 14>14</option>
<option value = 15>15</option>
<option value = 16>16</option>
<option value = 17>17</option>
</select>

<select name = "pickupMinutes">
<option value="unselected">min</option>
<option value = 0>00</option>
<option value = .5>30</option>
</select>
</td>
</tr>
<tr>
<td>
Return Date: <br/>
<input type = "text" id = "dateOut" name = "returnDate" id = "date2" onchange = checkValidRDate(this.value)>
</td>
<td>
Return Time: <br/>
<select name = "returnHours">
<option value="unselected">hr</option>
<option value = 7>07</option>
<option value = 8>08</option>
<option value = 9>09</option>
<option value = 10>10</option>
<option value = 11>11</option>
<option value = 12>12</option>
<option value = 13>13</option>
<option value = 14>14</option>
<option value = 15>15</option>
<option value = 16>16</option>
<option value = 17>17</option>
</select>

<select name = "returnMinutes">
<option value="unselected">min</option>
<option value = 0>00</option>
<option value = .5>30</option>
</select>
</td>
</tr>
<tr>
<td>
First Name: <br/>
<input type = "text" name = "firstName"/>
</td>
<td>
Last Name: <br/>
<input type = "text" name = "lastName"/>
</td>
</tr>
<tr>
<td>
Street: <br/>
<input type = "text" name = "street"/>
</td>
<td>
City: <br/>
<input type = "text" name = "city"/>
</td>
<td>
Zip:<br/>
<input type = "text" name = "zip" maxlength = "5"/>
</td>
</tr>
<tr>
<td>
Date of Birth: <br/>
<input type="text" name="date" onblur="checkAge(this.value)" />
</td>
</tr>
<tr>
<td colspan = "3" align = "center">
<input type = "submit" name = "submit" value = "Submit Reservation" onclick = "return checkit()"/>
</td>
</tr>
</table>
</form>

Broll05
12-08-2010, 10:22 PM
My math equation, is not calculating as an equation but is adding like it is a line. Any suggestions?

Broll05
12-08-2010, 10:26 PM
Thanks for all your help guys, especially you Philip M. You were such a huge help and you spelled it out in a way I could understand. Thank you so much.

Philip M
12-09-2010, 09:31 AM
My math equation, is not calculating as an equation but is adding like it is a line. Any suggestions?

Input values in JavaScript are strings unless they are convered to numbers by one of several methods.

So "2" + "2" = 22, not 4. The plus sign concatenates, not adds, strings.

So parseint("2") + parseInt("2") does = 4.

I think that I have already pointed out the extreme unreliablity of mailto: as a way of sending a form.