Javascript code that sometimes works, and sometimes doesn't
Hello,
I'm hoping someone can help me figure out why certain code sometimes works, and sometimes doesn't. I didn't write any of the code, I copied and pasted the parts I needed from different google searches (Thank you, internet!).
The pdf contains 12 months. Each month has 5 boxes (representing weeks) with dates in this format "mmm d, yyyy". The idea is that if I enter a date for the first box in the first week of January, all the other dates are auto populated by adding 7 days to the previous box. The reason for the 5 boxes is because sometimes you get 5 7 day periods in one month. For example, if the first box is Jan 1, then the following boxes will populate Jan 8, Jan 15, Jan 22, Jan 29. 5 Boxes. However, If, instead, the first box is Jan 4, then the other boxes fill be Jan 11, Jan 18, Jan 25. The fifth box would technically be February so the 5th box goes blank. In that case, the first box in February would be Feb 1st. SO forth and so on.
To make all this happen, there is a document level script:
function DateAddDays(cFormat, cDate, nDays) {
// convert date string to date object
var cReturn = "";
if(cDate.toString() != "") {
var oDate = util.scand(cFormat, cDate);
// add nDays
oDate.setDate(oDate.getDate() + nDays);
cReturn = util.printd(cFormat, oDate);
}
// return formatted date date stirng
return cReturn;
} // end AddDays funciton
Then, there is a script in the first box of each month (Except January), here's the one for February:
// custom calculation script for date + 7 days field
// field names for base date
var cDate='Ene7';
if (this.getField(cDate).valueAsString=="") {cDate = 'Ene6'; }
// format for date fields
var cDateFormat = "mmm d, yyyy";
// number of days to add
var nNumDays = 7;
// add seven days
event.value = DateAddDays(cDateFormat, this.getField(cDate).valueAsString, nNumDays);
There's a script in the 5th box of every month:
// custom calculation script for date + 7 days field
// field names for base date
var cDate = 'Feb6';
// format for date fields
var cDateFormat = "mmm d, yyyy";
// number of days to add
var nNumDays = 7;
// add seven days
x = DateAddDays(cDateFormat, this.getField(cDate).valueAsString, nNumDays);
if (x[0]=='F') {
event.value = x;}
else {event.value = ""; r=false}
Finally, there's a script in all the other box fields that just adds 7 days to the previous:
// custom calculation script for date + 7 days field
// field names for base date
var cDate = 'Feb3';
// format for date fields
var cDateFormat = "mmm d, yyyy";
// number of days to add
var nNumDays = 7;
// add seven days
event.value = DateAddDays(cDateFormat, this.getField(cDate).valueAsString, nNumDays);
The problem I'm having is that even though I copied and pasted the same scripts for each month and just changed the field names, it seems only January through February work consistently. IN other months, sometimes it adds 5 days instead of 7, or for some reason, April's 5th box never displays a date, even though it should. All you have to do to reproduce is change the date on January 1st, and look at the dates in April and you'll see the 5th box never shows a date, and the first box in May will be wrong.
Do any of you think you can determine why it is inconsistent? I'm attaching the form for reference, if it helps.
Thanks in advance!
Link (Couldn't figure out how to attach): http://https://www.dropbox.com/s/vm7scij2hpvkutd/S-3-S_4up%20Auto%20%281%29.pdf?dl=0

