I apologize in advance, because I am kinda new to this whole Javascript thing, so I'll try to explain my situation as best as possible. I am not getting any errors, but the script seems to freeze when trying to run through this function:
Here are the 2 arrays that I'm trying to pass into the above function:
And here is the calcDepart() function that gets called in the PerDiem() function (this is where the script seems to get frozen). The other functions called in the PerDiem() function are similar to this calcDepart() function:
The script seems to run fine if I don't enter any dates into the form's fields, but once I enter a date, it freezes up. I'm not sure what I'm doing wrong, but I'm sure I have a lot of syntactical errors. I'm thinking that I'm referencing the arrays incorrectly within the PerDiem() function. ANY help would be greatly appreciated.
Thanks!
Code:
//calculate individual Per Diem Days & populate fields
function perDiem(arr1, arr2) {
for (var i=1; i<=8; i++) {
var days = 0;
var dec = days.toFixed(2);
if (i == 1) {
days = calcDepart(arr1[i - 1]);
}
if (document.getElementById('aDate' + (i - 1)).value != "") {
days = days + calcBusNextDest(arr1[i]) + calcFullBus(arr1[i - 1], arr1[i]);
} else if (document.getElementById('aDate' + i).value != "" && document.getElementById('dDate' + i).value != "") {
days = days + calcBusDest(arr1[i]) + calcFullBus(arr1[i - 1], arr1[i]);
} else if (document.getElementById('aDate' + i).value != "" && document.getElementById('dDate' + i).value == "") {
days = days + calcReturn(arr2[i-1], arr1[i]);
}
//convert number value to a string
dec = dec + "";
alert("perDiem days" + i + "=" + dec);
//populate whole day fields
document.getElementById("dayWhole" + i).value = dec.substring(0, dec.indexOf('.'));
//populate quarter day fields
document.getElementById("dayQuarter" + i).selectedIndex = getQuarter(dec);
}
}
Here are the 2 arrays that I'm trying to pass into the above function:
Code:
//read and store aDate and aTime fields
function storeArrival(form) {
arrival = new Array();
for (var i=0; i<=8; i++) {
var hour = 0;
var minute = 0;
var date = document.getElementById('aDate' + i).value;
if (date != null) {
arrival[i] = new Date();
arrival[i].setMonth(date.substring(0, date.indexOf('/')) - 1);
arrival[i].setDate(date.substring(date.indexOf('/') + 1, date.lastIndexOf('/')));
arrival[i].setYear(date.substring(date.lastIndexOf('/') + 1, date.length) + 100);
var time = document.getElementById('aTime' + i).value;
hour = parseInt(time.substring(0, time.indexOf(":")), 10);
minute = parseInt(time.substring(time.indexOf(":") + 1, time.length), 10);
if (hour == 12 && document.getElementById('aAMPM' + i).selectedIndex == 0) {hour = hour - 12};
if (hour != 12 && document.getElementById('aAMPM' + i).selectedIndex == 1) {hour = hour + 12};
arrival[i].setHours(hour);
arrival[i].setMinutes(minute);
arrival[i].setSeconds(0);
arrival[i].setMilliseconds(0);
}
alert("arrival" + i + "=" + arrival[i]);
}
return arrival;
}
//read and store dDate and dTime fields
function storeDeparture() {
depart = new Array();
for (var i=0; i<=8; i++) {
var hour = 0;
var minute = 0;
var date = document.getElementById('dDate' + i).value;
if (date != null) {
depart[i] = new Date();
depart[i].setMonth(date.substring(0, date.indexOf('/')) - 1);
depart[i].setDate(date.substring(date.indexOf('/') + 1, date.lastIndexOf('/')));
depart[i].setYear(date.substring(date.lastIndexOf('/') + 1, date.length) + 100);
var time = document.getElementById('dTime' + i).value;
hour = parseInt(time.substring(0, time.indexOf(":")), 10);
minute = parseInt(time.substring(time.indexOf(":") + 1, time.length), 10);
if (hour == 12 && document.getElementById('dAMPM' + i).selectedIndex == 0) {hour = hour - 12};
if (hour != 12 && document.getElementById('dAMPM' + i).selectedIndex == 1) {hour = hour + 12};
depart[i].setHours(hour);
depart[i].setMinutes(minute);
depart[i].setSeconds(0);
depart[i].setMilliseconds(0);
}
alert("depart" + i + "=" + depart[i]);
}
return depart;
}
And here is the calcDepart() function that gets called in the PerDiem() function (this is where the script seems to get frozen). The other functions called in the PerDiem() function are similar to this calcDepart() function:
Code:
//calculate Per Diem Days allowed for departure from Home Base
function calcDepart(arr) {
var total = 0;
alert("calcDepart");
while (arr.getHours() >= 0 && arr.getHours() <= 6) {
if (arr.getHours() == 0 && arr.getMinutes() < 1) {total = 0.25;}
else if (arr.getHours() == 6 && arr.getMinutes() > 0) {total = 0.75;}
else total = 1.00;
}
while (arr.getHours() >= 7 && arr.getHours() <= 12) {
if (arr.getHours() == 12 && arr.getMinutes() > 0) {total = 0.50;}
else total = 0.75;
}
while (arr.getHours() >= 13 && arr.getHours() <= 18) {
if (arr.getHours() == 18 && arr.getMinutes() > 0) {total = 0.25;}
else total = 0.50;
}
while (arr.getHours() >= 19 && arr.getHours() <= 23) {
total = 0.25;
}alert("total=" + total.toFixed(2))
return total.toFixed(2);
}
The script seems to run fine if I don't enter any dates into the form's fields, but once I enter a date, it freezes up. I'm not sure what I'm doing wrong, but I'm sure I have a lot of syntactical errors. I'm thinking that I'm referencing the arrays incorrectly within the PerDiem() function. ANY help would be greatly appreciated.
Thanks!